Debian Codegraph @

templates/lxc-openeuler.in

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