| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# template script for generating Anolis container for LXC |
| 5 |
|
| 6 |
# |
| 7 |
# lxc: linux Container library |
| 8 |
|
| 9 |
# Authors: |
| 10 |
# zhaixiaojuan <[email protected]> |
| 11 |
|
| 12 |
# This library is free software; you can redistribute it and/or |
| 13 |
# modify it under the terms of the GNU Lesser General Public |
| 14 |
# License as published by the Free Software Foundation; either |
| 15 |
# version 2.1 of the License, or (at your option) any later version. |
| 16 |
|
| 17 |
# This library is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# Lesser General Public License for more details. |
| 21 |
|
| 22 |
# You should have received a copy of the GNU Lesser General Public |
| 23 |
# License along with this library; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
|
| 26 |
#Configurations |
| 27 |
default_path=@LXCPATH@ |
| 28 |
|
| 29 |
# Some combinations of the tuning knobs below do not exactly make sense. |
| 30 |
# but that's ok. |
| 31 |
# |
| 32 |
# If the "root_password" is non-blank, use it, else set a default. |
| 33 |
# This can be passed to the script as an environment variable and is |
| 34 |
# set by a shell conditional assignment. Looks weird but it is what it is. |
| 35 |
# |
| 36 |
# If the root password contains a ding ($) then try to expand it. |
| 37 |
# That will pick up things like ${name} and ${RANDOM}. |
| 38 |
# If the root password contains more than 3 consecutive X's, pass it as |
| 39 |
# a template to mktemp and take the result. |
| 40 |
# |
| 41 |
# If root_display_password = yes, display the temporary root password at exit. |
| 42 |
# If root_store_password = yes, store it in the configuration directory |
| 43 |
# If root_prompt_password = yes, invoke "passwd" to force the user to change |
| 44 |
# the root password after the container is created. |
| 45 |
# If root_expire_password = yes, you will be prompted to change the root |
| 46 |
# password at the first login. |
| 47 |
# |
| 48 |
# These are conditional assignments... The can be overridden from the |
| 49 |
# preexisting environment variables... |
| 50 |
# |
| 51 |
# Make sure this is in single quotes to defer expansion to later! |
| 52 |
# :{root_password='Root-${name}-${RANDOM}'} |
| 53 |
: ${root_password='Root-${name}-XXXXXX'} |
| 54 |
|
| 55 |
# Now, it doesn't make much sense to display, store, and force change |
| 56 |
# together. But, we gotta test, right??? |
| 57 |
: ${root_display_password='no'} |
| 58 |
: ${root_store_password='yes'} |
| 59 |
# Prompting for something interactive has potential for mayhem |
| 60 |
# with users running under the API... Don't default to "yes" |
| 61 |
: ${root_prompt_password='no'} |
| 62 |
|
| 63 |
# Expire root password? Default to yes, but can be overridden from |
| 64 |
# the environment variable |
| 65 |
: ${root_expire_password='yes'} |
| 66 |
|
| 67 |
# These are only going into comments in the resulting config... |
| 68 |
lxc_network_type=veth |
| 69 |
lxc_network_link=lxcbr0 |
| 70 |
|
| 71 |
# is this Anolis? |
| 72 |
# Alow for weird remixes like the Raspberry Pi |
| 73 |
# |
| 74 |
# Use the Mitre standard CPE identifier for the release ID if possible... |
| 75 |
# This may be in /etc/os-release or /etc/system-release-cpe. We |
| 76 |
# should be able to use EITHER. Give preference to /etc/os-release for now. |
| 77 |
|
| 78 |
# Detect use under userns (unsupported) |
| 79 |
for arg in "$@"; do |
| 80 |
[ "$arg" = "--" ] && break |
| 81 |
if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then |
| 82 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 83 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 84 |
exit 1 |
| 85 |
fi |
| 86 |
done |
| 87 |
|
| 88 |
# Make sure the usual locations are in PATH |
| 89 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 90 |
|
| 91 |
if [ -e /etc/os-release ] |
| 92 |
then |
| 93 |
# This is a shell friendly configuration file. We can just source it. |
| 94 |
# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME |
| 95 |
. /etc/os-release |
| 96 |
fi |
| 97 |
|
| 98 |
if [ "${ID}" = "anolis" ] && [ -n "${VERSION_ID}" ] |
| 99 |
then |
| 100 |
anolis_host_ver=${VERSION_ID} |
| 101 |
is_anolis=true |
| 102 |
elif [ -e /etc/anolis-release ] |
| 103 |
then |
| 104 |
# Only if all other methods fail, try to parse the anolis-release file. |
| 105 |
anolis_host_ver=$( sed -e '/^Anolis OS /!d' -e 's/Anolis OS.*\srelease\s*\([0-9][0-9.]*\)\s.*/\1/' < /etc/anolis-release ) |
| 106 |
if [ "$anolis_host_ver" != "" ] |
| 107 |
then |
| 108 |
is_anolis=true |
| 109 |
fi |
| 110 |
fi |
| 111 |
|
| 112 |
force_mknod() |
| 113 |
{ |
| 114 |
# delete a device node if exists, and create a new one |
| 115 |
rm -f $2 && mknod -m $1 $2 $3 $4 $5 |
| 116 |
} |
| 117 |
|
| 118 |
configure_anolis() |
| 119 |
{ |
| 120 |
|
| 121 |
# disable selinux in anolis |
| 122 |
mkdir -p $rootfs_path/selinux |
| 123 |
echo 0 > $rootfs_path/selinux/enforce |
| 124 |
|
| 125 |
# Also kill it in the /etc/selinux/config file if it's there... |
| 126 |
if [ -f $rootfs_path/etc/selinux/config ] |
| 127 |
then |
| 128 |
sed -i '/^SELINUX=/s/.*/SELINUX=disabled/' $rootfs_path/etc/selinux/config |
| 129 |
fi |
| 130 |
|
| 131 |
# Nice catch from Dwight Engen in the Oracle template. |
| 132 |
# Wantonly plagerized here with much appreciation. |
| 133 |
if [ -f $rootfs_path/usr/sbin/selinuxenabled ]; then |
| 134 |
mv $rootfs_path/usr/sbin/selinuxenabled $rootfs_path/usr/sbin/selinuxenabled.lxcorig |
| 135 |
ln -s /bin/false $rootfs_path/usr/sbin/selinuxenabled |
| 136 |
fi |
| 137 |
|
| 138 |
if [ -f ${rootfs_path}/etc/pam.d/crond ] |
| 139 |
then |
| 140 |
sed -i '/^session.*pam_loginuid.so/s/^session/# session/' ${rootfs_path}/etc/pam.d/crond |
| 141 |
fi |
| 142 |
|
| 143 |
# In addition to disabling pam_loginuid in the above config files |
| 144 |
# we'll also disable it by linking it to pam_permit to catch any |
| 145 |
# we missed or any that get installed after the container is built. |
| 146 |
# |
| 147 |
# Catch either or both 32 and 64 bit archs. |
| 148 |
if [ -f ${rootfs_path}/lib/security/pam_loginuid.so ] |
| 149 |
then |
| 150 |
( cd ${rootfs_path}/lib/security/ |
| 151 |
mv pam_loginuid.so pam_loginuid.so.disabled |
| 152 |
ln -s pam_permit.so pam_loginuid.so |
| 153 |
) |
| 154 |
fi |
| 155 |
|
| 156 |
if [ -f ${rootfs_path}/lib64/security/pam_loginuid.so ] |
| 157 |
then |
| 158 |
( cd ${rootfs_path}/lib64/security/ |
| 159 |
mv pam_loginuid.so pam_loginuid.so.disabled |
| 160 |
ln -s pam_permit.so pam_loginuid.so |
| 161 |
) |
| 162 |
fi |
| 163 |
|
| 164 |
# Set default localtime to the host localtime if not set... |
| 165 |
if [ -e /etc/localtime -a ! -e ${rootfs_path}/etc/localtime ] |
| 166 |
then |
| 167 |
# if /etc/localtime is a symlink, this should preserve it. |
| 168 |
cp -a /etc/localtime ${rootfs_path}/etc/localtime |
| 169 |
fi |
| 170 |
|
| 171 |
# configure the network using the dhcp |
| 172 |
cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0 |
| 173 |
DEVICE=eth0 |
| 174 |
BOOTPROTO=dhcp |
| 175 |
ONBOOT=yes |
| 176 |
HOSTNAME=${utsname} |
| 177 |
NM_CONTROLLED=no |
| 178 |
TYPE=Ethernet |
| 179 |
MTU=${MTU} |
| 180 |
DHCP_HOSTNAME=\`hostname\` |
| 181 |
EOF |
| 182 |
|
| 183 |
# set the hostname |
| 184 |
cat <<EOF > ${rootfs_path}/etc/sysconfig/network |
| 185 |
NETWORKING=yes |
| 186 |
HOSTNAME=${utsname} |
| 187 |
EOF |
| 188 |
|
| 189 |
# set minimal hosts |
| 190 |
cat <<EOF > $rootfs_path/etc/hosts |
| 191 |
127.0.0.1 localhost $name |
| 192 |
EOF |
| 193 |
|
| 194 |
# set minimal fstab |
| 195 |
cat <<EOF > $rootfs_path/etc/fstab |
| 196 |
/dev/root / rootfs defaults 0 0 |
| 197 |
EOF |
| 198 |
#set dev nodes |
| 199 |
dev_path="${rootfs_path}/dev" |
| 200 |
rm -rf $dev_path |
| 201 |
mkdir -p $dev_path |
| 202 |
mknod -m 666 ${dev_path}/null c 1 3 |
| 203 |
mknod -m 666 ${dev_path}/zero c 1 5 |
| 204 |
mknod -m 666 ${dev_path}/random c 1 8 |
| 205 |
mknod -m 666 ${dev_path}/urandom c 1 9 |
| 206 |
mkdir -m 755 ${dev_path}/pts |
| 207 |
mkdir -m 1777 ${dev_path}/shm |
| 208 |
mknod -m 666 ${dev_path}/tty c 5 0 |
| 209 |
mknod -m 666 ${dev_path}/tty0 c 4 0 |
| 210 |
mknod -m 666 ${dev_path}/tty1 c 4 1 |
| 211 |
mknod -m 666 ${dev_path}/tty2 c 4 2 |
| 212 |
mknod -m 666 ${dev_path}/tty3 c 4 3 |
| 213 |
mknod -m 666 ${dev_path}/tty4 c 4 4 |
| 214 |
mknod -m 600 ${dev_path}/console c 5 1 |
| 215 |
mknod -m 666 ${dev_path}/full c 1 7 |
| 216 |
mknod -m 600 ${dev_path}/initctl p |
| 217 |
mknod -m 666 ${dev_path}/ptmx c 5 2 |
| 218 |
|
| 219 |
# setup console and tty[1-4] for login. note that /dev/console and |
| 220 |
# /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and |
| 221 |
# /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks. |
| 222 |
# lxc will maintain these links and bind mount ptys over /dev/lxc/* |
| 223 |
# since lxc.tty.dir is specified in the config. |
| 224 |
|
| 225 |
# allow root login on console, tty[1-4], and pts/0 for libvirt |
| 226 |
echo "# LXC (Linux Containers)" >>${rootfs_path}/etc/securetty |
| 227 |
echo "lxc/console" >>${rootfs_path}/etc/securetty |
| 228 |
echo "lxc/tty1" >>${rootfs_path}/etc/securetty |
| 229 |
echo "lxc/tty2" >>${rootfs_path}/etc/securetty |
| 230 |
echo "lxc/tty3" >>${rootfs_path}/etc/securetty |
| 231 |
echo "lxc/tty4" >>${rootfs_path}/etc/securetty |
| 232 |
echo "# For libvirt/Virtual Machine Monitor" >>${rootfs_path}/etc/securetty |
| 233 |
echo "pts/0" >>${rootfs_path}/etc/securetty |
| 234 |
|
| 235 |
|
| 236 |
if [ ${root_display_password} = "yes" ] |
| 237 |
then |
| 238 |
echo "Setting root password to '$root_password'" |
| 239 |
fi |
| 240 |
if [ ${root_store_password} = "yes" ] |
| 241 |
then |
| 242 |
touch ${config_path}/tmp_root_pass |
| 243 |
chmod 600 ${config_path}/tmp_root_pass |
| 244 |
echo ${root_password} > ${config_path}/tmp_root_pass |
| 245 |
echo "Storing root password in '${config_path}/tmp_root_pass'" |
| 246 |
fi |
| 247 |
|
| 248 |
echo "root:$root_password" | chroot $rootfs_path chpasswd |
| 249 |
|
| 250 |
if [ ${root_expire_password} = "yes" ] |
| 251 |
then |
| 252 |
# Also set this password as expired to force the user to change it! |
| 253 |
chroot $rootfs_path passwd -e root |
| 254 |
fi |
| 255 |
|
| 256 |
return 0 |
| 257 |
} |
| 258 |
|
| 259 |
arch=$(uname -m) |
| 260 |
download_anolis() |
| 261 |
{ |
| 262 |
|
| 263 |
# check the mini AnolisOS was not already downloaded |
| 264 |
INSTALL_ROOT=$cache/partial |
| 265 |
mkdir -p $INSTALL_ROOT |
| 266 |
if [ $? -ne 0 ]; then |
| 267 |
echo "Failed to create '$INSTALL_ROOT' directory" |
| 268 |
return 1 |
| 269 |
fi |
| 270 |
|
| 271 |
# download a mini AnolisOS into a cache |
| 272 |
echo "Downloading AnolisOS minimal ..." |
| 273 |
YUM0="yum --installroot $INSTALL_ROOT -y --nogpgcheck" |
| 274 |
|
| 275 |
if yum -h | grep -q 'releasever=RELEASEVER'; then |
| 276 |
YUM="$YUM0 --releasever=$release" |
| 277 |
else |
| 278 |
YUM="$YUM0" |
| 279 |
fi |
| 280 |
PKG_LIST="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils cronie network-scripts coreutils net-tools iputils" |
| 281 |
|
| 282 |
# use temporary repository definition |
| 283 |
# always prefer the repo given by the user |
| 284 |
REPO_FILE=$INSTALL_ROOT/etc/yum.repos.d/lxc-AnolisOS-temp.repo |
| 285 |
mkdir -p $(dirname $REPO_FILE) |
| 286 |
if [ -n "$repo" ]; then |
| 287 |
cat <<EOF > $REPO_FILE |
| 288 |
[base] |
| 289 |
name=local repository |
| 290 |
baseurl="$repo" |
| 291 |
EOF |
| 292 |
else |
| 293 |
cat <<EOF > $REPO_FILE |
| 294 |
[BaseOS] |
| 295 |
name=BaseOS |
| 296 |
baseurl=http://mirrors.openanolis.cn/anolis/$VERSION/BaseOS/$arch/os |
| 297 |
enabled=1 |
| 298 |
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ANOLIS |
| 299 |
gpigcheck=0 |
| 300 |
|
| 301 |
[AppStream] |
| 302 |
name=AppStream |
| 303 |
baseurl=http://mirrors.openanolis.cn/anolis/$VERSION/AppStream/$arch/os |
| 304 |
enabled=1 |
| 305 |
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ANOLIS |
| 306 |
gpgcheck=0 |
| 307 |
EOF |
| 308 |
fi |
| 309 |
|
| 310 |
# create minimal device nodes, needed for "yum install" and "yum update" process |
| 311 |
mkdir -p $INSTALL_ROOT/dev |
| 312 |
force_mknod 666 $INSTALL_ROOT/dev/null c 1 3 |
| 313 |
force_mknod 666 $INSTALL_ROOT/dev/urandom c 1 9 |
| 314 |
|
| 315 |
$YUM install $PKG_LIST |
| 316 |
|
| 317 |
if [ $? -ne 0 ]; then |
| 318 |
echo "Failed to download the rootfs, aborting." |
| 319 |
return 1 |
| 320 |
fi |
| 321 |
|
| 322 |
# use same nameservers as hosts, needed for "yum update later" |
| 323 |
cp /etc/resolv.conf $INSTALL_ROOT/etc/ |
| 324 |
|
| 325 |
# check whether rpmdb is under $HOME |
| 326 |
if [ ! -e $INSTALL_ROOT/var/lib/rpm/Packages -a -e $INSTALL_ROOT/$HOME/.rpmdb/Packages ]; then |
| 327 |
echo "Fixing rpmdb location ..." |
| 328 |
mv $INSTALL_ROOT/$HOME/.rpmdb/[A-Z]* $INSTALL_ROOT/var/lib/rpm/ |
| 329 |
rm -rf $INSTALL_ROOT/$HOME/.rpmdb |
| 330 |
chroot $INSTALL_ROOT rpm --rebuilddb 2>/dev/null |
| 331 |
fi |
| 332 |
|
| 333 |
# check whether rpmdb version is correct |
| 334 |
chroot $INSTALL_ROOT rpm --quiet -q yum 2>/dev/null |
| 335 |
ret=$? |
| 336 |
|
| 337 |
# if "rpm -q" doesn't work due to rpmdb version difference, |
| 338 |
# then we need to redo the process using the newly-installed yum |
| 339 |
if [ $ret -gt 0 ]; then |
| 340 |
echo "Reinstalling packages ..." |
| 341 |
mv $REPO_FILE $REPO_FILE.tmp |
| 342 |
mkdir $INSTALL_ROOT/etc/yum.repos.disabled |
| 343 |
mv $INSTALL_ROOT/etc/yum.repos.d/*.repo $INSTALL_ROOT/etc/yum.repos.disabled/ |
| 344 |
mv $REPO_FILE.tmp $REPO_FILE |
| 345 |
mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/etc |
| 346 |
cp /etc/resolv.conf $INSTALL_ROOT/$INSTALL_ROOT/etc/ |
| 347 |
mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/dev |
| 348 |
mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/null c 1 3 |
| 349 |
mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/urandom c 1 9 |
| 350 |
mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/var/cache/yum |
| 351 |
cp -al $INSTALL_ROOT/var/cache/yum/* $INSTALL_ROOT/$INSTALL_ROOT/var/cache/yum/ |
| 352 |
chroot $INSTALL_ROOT $YUM0 install $PKG_LIST |
| 353 |
if [ $? -ne 0 ]; then |
| 354 |
echo "Failed to download the rootfs, aborting." |
| 355 |
return 1 |
| 356 |
fi |
| 357 |
mv $INSTALL_ROOT/$INSTALL_ROOT $INSTALL_ROOT.tmp |
| 358 |
rm -rf $INSTALL_ROOT |
| 359 |
mv $INSTALL_ROOT.tmp $INSTALL_ROOT |
| 360 |
fi |
| 361 |
|
| 362 |
rm -f $REPO_FILE |
| 363 |
rm -rf $INSTALL_ROOT/var/cache/yum/* |
| 364 |
|
| 365 |
mv "$INSTALL_ROOT" "$cache/rootfs" |
| 366 |
echo "Download complete." |
| 367 |
|
| 368 |
return 0 |
| 369 |
} |
| 370 |
|
| 371 |
copy_anolis() |
| 372 |
{ |
| 373 |
|
| 374 |
# make a local copy of the mini AnolisOS |
| 375 |
echo -n "Copying rootfs to $rootfs_path ..." |
| 376 |
#cp -a $cache/rootfs-$arch $rootfs_path || return 1 |
| 377 |
# i prefer rsync (no reason really) |
| 378 |
mkdir -p $rootfs_path |
| 379 |
rsync -SHaAX $cache/rootfs/ $rootfs_path/ |
| 380 |
echo |
| 381 |
return 0 |
| 382 |
} |
| 383 |
|
| 384 |
update_anolis() |
| 385 |
{ |
| 386 |
YUM="chroot $cache/rootfs yum -y --nogpgcheck" |
| 387 |
$YUM update |
| 388 |
if [ $? -ne 0 ]; then |
| 389 |
return 1 |
| 390 |
fi |
| 391 |
$YUM clean packages |
| 392 |
} |
| 393 |
|
| 394 |
install_anolis() |
| 395 |
{ |
| 396 |
mkdir -p /var/lock/subsys/ |
| 397 |
( |
| 398 |
flock -x 9 |
| 399 |
if [ $? -ne 0 ]; then |
| 400 |
echo "Cache repository is busy." |
| 401 |
return 1 |
| 402 |
fi |
| 403 |
|
| 404 |
echo "Checking cache download in $cache/rootfs ... " |
| 405 |
if [ ! -e "$cache/rootfs" ]; then |
| 406 |
download_anolis |
| 407 |
if [ $? -ne 0 ]; then |
| 408 |
echo "Failed to download 'AnolisOS base'" |
| 409 |
return 1 |
| 410 |
fi |
| 411 |
else |
| 412 |
echo "Cache found. Updating..." |
| 413 |
update_anolis |
| 414 |
if [ $? -ne 0 ]; then |
| 415 |
echo "Failed to update 'AnolisOS base', continuing with last known good cache" |
| 416 |
else |
| 417 |
echo "Update finished" |
| 418 |
fi |
| 419 |
fi |
| 420 |
|
| 421 |
echo "Copy $cache/rootfs to $rootfs_path ... " |
| 422 |
copy_anolis |
| 423 |
if [ $? -ne 0 ]; then |
| 424 |
echo "Failed to copy rootfs" |
| 425 |
return 1 |
| 426 |
fi |
| 427 |
|
| 428 |
return 0 |
| 429 |
|
| 430 |
) 9>/var/lock/subsys/lxc-anolis |
| 431 |
|
| 432 |
return $? |
| 433 |
} |
| 434 |
|
| 435 |
create_hwaddr() |
| 436 |
{ |
| 437 |
openssl rand -hex 5 | sed -e 's/\(..\)/:\1/g; s/^/fe/' |
| 438 |
} |
| 439 |
|
| 440 |
copy_configuration() |
| 441 |
{ |
| 442 |
mkdir -p $config_path |
| 443 |
|
| 444 |
grep -q "^lxc.rootfs.path" $config_path/config 2>/dev/null || echo " |
| 445 |
lxc.rootfs.path = $rootfs_path |
| 446 |
" >> $config_path/config |
| 447 |
|
| 448 |
# The following code is to create static MAC addresses for each |
| 449 |
# interface in the container. This code will work for multiple |
| 450 |
# interfaces in the default config. |
| 451 |
mv $config_path/config $config_path/config.def |
| 452 |
while read LINE |
| 453 |
do |
| 454 |
# This should catch variable expansions from the default config... |
| 455 |
if expr "${LINE}" : '.*\$' > /dev/null 2>&1 |
| 456 |
then |
| 457 |
LINE=$(eval "echo \"${LINE}\"") |
| 458 |
fi |
| 459 |
|
| 460 |
# There is a tab and a space in the regex bracket below! |
| 461 |
# Seems that \s doesn't work in brackets. |
| 462 |
KEY=$(expr "${LINE}" : '\s*\([^ ]*\)\s*=') |
| 463 |
|
| 464 |
if [[ "${KEY}" != "lxc.net.0.hwaddr" ]] |
| 465 |
then |
| 466 |
echo ${LINE} >> $config_path/config |
| 467 |
|
| 468 |
if [[ "${KEY}" == "lxc.net.0.link" ]] |
| 469 |
then |
| 470 |
echo "lxc.net.0.hwaddr = $(create_hwaddr)" >> $config_path/config |
| 471 |
fi |
| 472 |
fi |
| 473 |
done < $config_path/config.def |
| 474 |
|
| 475 |
rm -f $config_path/config.def |
| 476 |
|
| 477 |
if [ -e "@LXCTEMPLATECONFIG@/anolis.common.conf" ]; then |
| 478 |
echo " |
| 479 |
# Include common configuration |
| 480 |
lxc.include = @LXCTEMPLATECONFIG@/anolis.common.conf |
| 481 |
" >> $config_path/config |
| 482 |
fi |
| 483 |
|
| 484 |
# Append things which require expansion here... |
| 485 |
cat <<EOF >> $config_path/config |
| 486 |
lxc.arch = $arch |
| 487 |
lxc.uts.name = $utsname |
| 488 |
|
| 489 |
# When using LXC with apparmor, uncomment the next line to run unconfined: |
| 490 |
#lxc.apparmor.profile = unconfined |
| 491 |
|
| 492 |
# example simple networking setup, uncomment to enable |
| 493 |
#lxc.net.0.type = $lxc_network_type |
| 494 |
#lxc.net.0.flags = up |
| 495 |
#lxc.net.0.link = $lxc_network_link |
| 496 |
#lxc.net.0.name = eth0 |
| 497 |
# Additional example for veth network type |
| 498 |
# static MAC address, |
| 499 |
#lxc.net.0.hwaddr = 00:16:3e:77:52:20 |
| 500 |
# persistent veth device name on host side |
| 501 |
# Note: This may potentially collide with other containers of same name! |
| 502 |
#lxc.net.0.veth.pair = v-$name-e0 |
| 503 |
|
| 504 |
EOF |
| 505 |
|
| 506 |
if [ $? -ne 0 ]; then |
| 507 |
echo "Failed to add configuration" |
| 508 |
return 1 |
| 509 |
fi |
| 510 |
|
| 511 |
return 0 |
| 512 |
} |
| 513 |
|
| 514 |
clean() |
| 515 |
{ |
| 516 |
|
| 517 |
if [ ! -e $cache ]; then |
| 518 |
exit 0 |
| 519 |
fi |
| 520 |
|
| 521 |
# lock, so we won't purge while someone is creating a repository |
| 522 |
( |
| 523 |
flock -x 9 |
| 524 |
if [ $? != 0 ]; then |
| 525 |
echo "Cache repository is busy." |
| 526 |
exit 1 |
| 527 |
fi |
| 528 |
|
| 529 |
echo -n "Purging the download cache for AnolisOS-$release..." |
| 530 |
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1 |
| 531 |
exit 0 |
| 532 |
|
| 533 |
) 9>@LOCALSTATEDIR@/lock/subsys/lxc-anolis |
| 534 |
} |
| 535 |
|
| 536 |
usage() |
| 537 |
{ |
| 538 |
cat <<EOF |
| 539 |
usage: |
| 540 |
$1 -n|--name=<container_name> |
| 541 |
[-p|--path=<path>] [-c|--clean] [-R|--release=<AnolisOS_release>] [-a|--arch=<arch of the container>] |
| 542 |
[-h|--help] |
| 543 |
Mandatory args: |
| 544 |
-n,--name container name, used to as an identifier for that container from now on |
| 545 |
Optional args: |
| 546 |
-p,--path path to where the container rootfs will be created, defaults to /var/lib/lxc/name. |
| 547 |
-c,--clean clean the cache |
| 548 |
-R,--release AnolisOS release for the new container. If the host is AnolisOS, then it will default to the host's release. |
| 549 |
--fqdn fully qualified domain name (FQDN) for DNS and system naming |
| 550 |
--repo repository to use (url) |
| 551 |
-a,--arch Define what arch the container will be [i686,x86_64] |
| 552 |
-h,--help print this help |
| 553 |
EOF |
| 554 |
return 0 |
| 555 |
} |
| 556 |
|
| 557 |
options=$(getopt -o a:hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,repo:,arch:,fqdn: -- "$@") |
| 558 |
if [ $? -ne 0 ]; then |
| 559 |
usage $(basename $0) |
| 560 |
exit 1 |
| 561 |
fi |
| 562 |
|
| 563 |
arch=$(uname -m) |
| 564 |
eval set -- "$options" |
| 565 |
while true |
| 566 |
do |
| 567 |
case "$1" in |
| 568 |
-h|--help) usage $0 && exit 0;; |
| 569 |
-p|--path) path=$2; shift 2;; |
| 570 |
--rootfs) rootfs_path=$2; shift 2;; |
| 571 |
-n|--name) name=$2; shift 2;; |
| 572 |
-c|--clean) clean=1; shift 1;; |
| 573 |
-R|--release) release=$2; shift 2;; |
| 574 |
--repo) repo="$2"; shift 2;; |
| 575 |
-a|--arch) newarch=$2; shift 2;; |
| 576 |
--fqdn) utsname=$2; shift 2;; |
| 577 |
--) shift 1; break ;; |
| 578 |
*) break ;; |
| 579 |
esac |
| 580 |
done |
| 581 |
|
| 582 |
if [ ! -z "$clean" -a -z "$path" ]; then |
| 583 |
clean || exit 1 |
| 584 |
exit 0 |
| 585 |
fi |
| 586 |
|
| 587 |
basearch=${arch} |
| 588 |
|
| 589 |
# Allow the cache base to be set by environment variable |
| 590 |
cache_base=${LXC_CACHE_PATH:-"@LOCALSTATEDIR@/cache/lxc"}/anolis/$basearch |
| 591 |
|
| 592 |
# Let's do something better for the initial root password. |
| 593 |
# It's not perfect but it will defeat common scanning brute force |
| 594 |
# attacks in the case where ssh is exposed. It will also be set to |
| 595 |
# expired, forcing the user to change it at first login. |
| 596 |
if [ "${root_password}" = "" ] |
| 597 |
then |
| 598 |
root_password=Root-${name}-${RANDOM} |
| 599 |
else |
| 600 |
# If it's got a ding in it, try and expand it! |
| 601 |
if [ $(expr "${root_password}" : '.*$.') != 0 ] |
| 602 |
then |
| 603 |
root_password=$(eval echo "${root_password}") |
| 604 |
fi |
| 605 |
|
| 606 |
# If it has more than 3 consecutive X's in it, feed it |
| 607 |
# through mktemp as a template. |
| 608 |
if [ $(expr "${root_password}" : '.*XXXX') != 0 ] |
| 609 |
then |
| 610 |
root_password=$(mktemp -u ${root_password}) |
| 611 |
fi |
| 612 |
fi |
| 613 |
|
| 614 |
if [ -z "${utsname}" ]; then |
| 615 |
utsname=${name} |
| 616 |
fi |
| 617 |
|
| 618 |
# This follows a standard "resolver" convention that an FQDN must have |
| 619 |
# at least two dots or it is considered a local relative host name. |
| 620 |
# If it doesn't, append the dns domain name of the host system. |
| 621 |
# |
| 622 |
# This changes one significant behavior when running |
| 623 |
# "lxc_create -n Container_Name" without using the |
| 624 |
# --fqdn option. |
| 625 |
# |
| 626 |
# Old behavior: |
| 627 |
# utsname and hostname = Container_Name |
| 628 |
# New behavior: |
| 629 |
# utsname and hostname = Container_Name.Domain_Name |
| 630 |
|
| 631 |
if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then |
| 632 |
if [[ "$(dnsdomainname)" != "" && "$(dnsdomainname)" != "localdomain" ]]; then |
| 633 |
utsname=${utsname}.$(dnsdomainname) |
| 634 |
fi |
| 635 |
fi |
| 636 |
|
| 637 |
type yum >/dev/null 2>&1 |
| 638 |
if [ $? -ne 0 ]; then |
| 639 |
echo "'yum' command is missing" |
| 640 |
exit 1 |
| 641 |
fi |
| 642 |
|
| 643 |
if [ -z "$path" ]; then |
| 644 |
path=$default_path/$name |
| 645 |
fi |
| 646 |
|
| 647 |
if [ -z "$release" ]; then |
| 648 |
release=$anolis_host_ver |
| 649 |
fi |
| 650 |
|
| 651 |
if [ "$(id -u)" != "0" ]; then |
| 652 |
echo "This script should be run as 'root'" |
| 653 |
exit 1 |
| 654 |
fi |
| 655 |
|
| 656 |
if [ -z "$rootfs_path" ]; then |
| 657 |
rootfs_path=$path/rootfs |
| 658 |
# check for 'lxc.rootfs.path' passed in through default config by lxc-create |
| 659 |
if grep -q '^lxc.rootfs.path' $path/config 2>/dev/null ; then |
| 660 |
rootfs_path=$(sed -e '/^lxc.rootfs.path\s*=/!d' -e 's/\s*#.*//' \ |
| 661 |
-e 's/^lxc.rootfs.path\s*=\s*//' -e q $path/config) |
| 662 |
fi |
| 663 |
fi |
| 664 |
config_path=$path |
| 665 |
cache=$cache_base/$release |
| 666 |
|
| 667 |
revert() |
| 668 |
{ |
| 669 |
echo "Interrupted, so cleaning up" |
| 670 |
lxc-destroy -n $name |
| 671 |
# maybe was interrupted before copy config |
| 672 |
rm -rf $path |
| 673 |
echo "exiting..." |
| 674 |
exit 1 |
| 675 |
} |
| 676 |
|
| 677 |
trap revert SIGHUP SIGINT SIGTERM |
| 678 |
|
| 679 |
copy_configuration |
| 680 |
if [ $? -ne 0 ]; then |
| 681 |
echo "failed write configuration file" |
| 682 |
exit 1 |
| 683 |
fi |
| 684 |
|
| 685 |
install_anolis |
| 686 |
if [ $? -ne 0 ]; then |
| 687 |
echo "failed to install AnolisOS" |
| 688 |
exit 1 |
| 689 |
fi |
| 690 |
|
| 691 |
configure_anolis |
| 692 |
if [ $? -ne 0 ]; then |
| 693 |
echo "failed to configure AnolisOS for a container" |
| 694 |
exit 1 |
| 695 |
fi |
| 696 |
|
| 697 |
if [ ! -z "$clean" ]; then |
| 698 |
clean || exit 1 |
| 699 |
exit 0 |
| 700 |
fi |
| 701 |
echo " |
| 702 |
Container rootfs and config have been created. |
| 703 |
Edit the config file to check/enable networking setup. |
| 704 |
" |
| 705 |
|
| 706 |
if [ ${root_display_password} = "yes" ] |
| 707 |
then |
| 708 |
echo "The temporary password for root is: '$root_password' |
| 709 |
|
| 710 |
You may want to note that password down before starting the container. |
| 711 |
" |
| 712 |
fi |
| 713 |
|
| 714 |
if [ ${root_store_password} = "yes" ] |
| 715 |
then |
| 716 |
echo "The temporary root password is stored in: |
| 717 |
|
| 718 |
'${config_path}/tmp_root_pass' |
| 719 |
" |
| 720 |
fi |
| 721 |
|
| 722 |
if [ ${root_prompt_password} = "yes" ] |
| 723 |
then |
| 724 |
echo "Invoking the passwd command in the container to set the root password. |
| 725 |
|
| 726 |
chroot ${rootfs_path} passwd |
| 727 |
" |
| 728 |
chroot ${rootfs_path} passwd |
| 729 |
else |
| 730 |
if [ ${root_expire_password} = "yes" ] |
| 731 |
then |
| 732 |
if ( mountpoint -q -- "${rootfs_path}" ) |
| 733 |
then |
| 734 |
echo "To reset the root password, you can do: |
| 735 |
|
| 736 |
lxc-start -n ${name} |
| 737 |
lxc-attach -n ${name} -- passwd |
| 738 |
lxc-stop -n ${name} |
| 739 |
" |
| 740 |
else |
| 741 |
echo " |
| 742 |
The root password is set up as "expired" and will require it to be changed |
| 743 |
at first login, which you should do as soon as possible. If you lose the |
| 744 |
root password or wish to change it without starting the container, you |
| 745 |
can change it from the host by running the following command (which will |
| 746 |
also reset the expired flag): |
| 747 |
|
| 748 |
chroot ${rootfs_path} passwd |
| 749 |
" |
| 750 |
fi |
| 751 |
fi |
| 752 |
fi |