3 # Template script for generating Oracle Enterprise Linux container for LXC
4 # based on lxc-fedora, lxc-ubuntu
6 # Copyright © 2011 Wim Coekaerts <wim.coekaerts@oracle.com>
7 # Copyright © 2012 Dwight Engen <dwight.engen@oracle.com>
9 # Modified for Oracle Linux 5
10 # Wim Coekaerts <wim.coekaerts@oracle.com>
12 # Modified for Oracle Linux 6, combined OL4,5,6 into one template script
13 # Dwight Engen <dwight.engen@oracle.com>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 # Detect use under userns (unsupported)
32 [ "$arg" == "--" ] && break
33 if [ "$arg" == "--mapped-uid" -o "$arg" == "--mapped-gid" ]; then
34 echo "This template can't be used for unprivileged containers." 1>&2
35 echo "You may want to try the \"download\" template instead." 1>&2
40 # Make sure the usual locations are in PATH
41 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
43 # use virbr0 that is setup by default by libvirtd
45 lxc_network_link
=virbr0
55 if which btrfs
>/dev
/null
2>&1 && \
56 btrfs subvolume list
"$1" >/dev
/null
2>&1; then
64 if which chcon
>/dev
/null
2>&1; then
65 selinuxenabled
>/dev
/null
2>&1
71 # fix up the container_rootfs
72 container_rootfs_configure
()
74 echo "Configuring container for Oracle Linux $container_release_major.$container_release_minor"
76 # "disable" selinux in the guest. The policy in the container isn't
77 # likely to match the hosts (unless host == guest exactly) and the
78 # kernel can only be enforcing one policy.
80 # The OL 5 init honors /etc/selinux/config, but note that
81 # this doesnt actually disable it if it's enabled in the host, since
82 # libselinux::is_selinux_enabled() in the guest will check
83 # /proc/filesystems and see selinuxfs, thus reporting that it is on
84 # (ie. check the output of sestatus in the guest). We also replace
85 # /usr/sbin/selinuxenabled with a symlink to /bin/false so that init
86 # scripts (ie. mcstransd) that call that think selinux is disabled.
87 mkdir
-p $container_rootfs/selinux
88 echo 0 > $container_rootfs/selinux
/enforce
89 if [ -e $container_rootfs/etc
/selinux
/config
]; then
90 sed -i 's|SELINUX=enforcing|SELINUX=disabled|' $container_rootfs/etc
/selinux
/config
92 mkdir
-p $container_rootfs/etc
/selinux
93 echo "SELINUX=disabled" >$container_rootfs/etc
/selinux
/config
95 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*close|#session required pam_selinux.so close|' $container_rootfs/etc
/pam.d
/login
96 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*open|#session required pam_selinux.so open|' $container_rootfs/etc
/pam.d
/login
98 # setting /proc/$$/loginuid doesn't work under user namespace, which
99 # prevents logins from working
100 sed -i 's|session[ \t]*required[ \t]*pam_loginuid.so|#session required pam_loginuid.so|' $container_rootfs/etc
/pam.d
/sshd
101 sed -i 's|session[ \t]*required[ \t]*pam_loginuid.so|#session required pam_loginuid.so|' $container_rootfs/etc
/pam.d
/login
103 if [ -f $container_rootfs/usr
/sbin
/selinuxenabled
]; then
104 mv $container_rootfs/usr
/sbin
/selinuxenabled
$container_rootfs/usr
/sbin
/selinuxenabled.lxcorig
105 ln -s /bin
/false
$container_rootfs/usr
/sbin
/selinuxenabled
108 # silence error in checking for selinux
109 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc
/rc.sysinit
110 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc
/rc.d
/rc.sysinit
112 # on ol4 pam_limits prevents logins when using user namespaces
113 if [ $container_release_major = "4" ]; then
114 sed -i 's|session[ \t]*required[ \t]*/lib/security/\$ISA/pam_limits.so|#session required /lib/security/$ISA/pam_limits.so|' $container_rootfs/etc
/pam.d
/system-auth
117 # configure the network to use dhcp. we set DHCP_HOSTNAME so the guest
118 # will report its name and be resolv'able by the hosts dnsmasq
119 cat <<EOF > $container_rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
129 # avoid error in ol5 attempting to copy non-existent resolv.conf
130 if [ $container_release_major = "5" ]; then
131 sed -i 's|resolv.conf.predhclient|resolv.conf.predhclient 2>/dev/null|' $container_rootfs/sbin
/dhclient-script
135 cat <<EOF > $container_rootfs/etc/sysconfig/network
141 # disable interactive ovmd asking questions
142 if [ -f $container_rootfs/etc
/sysconfig
/ovmd
]; then
143 sed -i 's|INITIAL_CONFIG=yes|INITIAL_CONFIG=no|' $container_rootfs/etc
/sysconfig
/ovmd
147 echo "127.0.0.1 localhost $name" > $container_rootfs/etc
/hosts
149 # disable ipv6 on ol6
150 rm -f $container_rootfs/etc
/sysconfig
/network-scripts
/init.ipv6-global
152 # this file has to exist for libvirt/Virtual machine monitor to boot the container
153 touch $container_rootfs/etc
/mtab
155 # don't put devpts,proc, nor sysfs in here, it will already be mounted for us by lxc/libvirt
156 cat <<EOF > $container_rootfs/etc/fstab
159 # remove module stuff for iptables it just shows errors that are not
160 # relevant in a container
161 if [ -f "$container_rootfs/etc/sysconfig/iptables-config" ]; then
162 sed -i 's|IPTABLES_MODULES=".*|IPTABLES_MODULES=""|' $container_rootfs/etc
/sysconfig
/iptables-config
163 sed -i 's|IPTABLES_MODULES_UNLOAD=".*|IPTABLES_MODULES_UNLOAD="no"|' $container_rootfs/etc
/sysconfig
/iptables-config
166 # disable readahead in the container
167 if [ $container_release_major = "6" -a -e $container_rootfs/etc
/sysconfig
/readahead
]; then
168 rm -f $container_rootfs/etc
/init
/readahead-collector.conf
169 rm -f $container_rootfs/etc
/init
/readahead-disable-services.conf
170 sed -i 's|READAHEAD="yes"|READAHEAD="no"|' $container_rootfs/etc
/sysconfig
/readahead
173 if [ $container_release_major = "4" ]; then
174 # enable fastboot always
175 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc
/rc.sysinit
176 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc
/rc.d
/rc.sysinit
178 # dont attempt to set kernel parameters
179 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc
/rc.sysinit
180 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc
/rc.d
/rc.sysinit
181 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc
/init.d
/network
2>/dev
/null
182 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc
/init.d
/NetworkManager
2>/dev
/null
185 # sem_open(3) checks that /dev/shm is SHMFS_SUPER_MAGIC, so make sure to mount /dev/shm (normally done by dracut initrd) as tmpfs
186 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
187 echo "mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc
/rc.sysinit
188 echo "mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc
/rc.d
/rc.sysinit
191 if [ $container_release_major = "6" ]; then
192 sed -i 's|mount -n -o remount /dev/shm >/dev/null 2>&1$|mount -t tmpfs tmpfs /dev/shm # LXC|' $container_rootfs/etc
/rc.sysinit
193 sed -i 's|mount -n -o remount /dev/shm >/dev/null 2>&1$|mount -t tmpfs tmpfs /dev/shm # LXC|' $container_rootfs/etc
/rc.d
/rc.sysinit
196 # no need to attempt to mount /
197 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc
/rc.sysinit
198 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc
/rc.d
/rc.sysinit
199 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc
/rc.sysinit
200 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc
/rc.d
/rc.sysinit
202 # disable udev in the container
203 if [ $container_release_major = "4" ]; then
204 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc
/rc.sysinit
205 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc
/rc.d
/rc.sysinit
207 sed -i 's|.sbin.start_udev||' $container_rootfs/etc
/rc.sysinit
208 sed -i 's|.sbin.start_udev||' $container_rootfs/etc
/rc.d
/rc.sysinit
211 # disable nash raidautorun in the container since no /dev/md*
212 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
213 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc
/rc.sysinit
214 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc
/rc.d
/rc.sysinit
217 # prevent rc.sysinit from attempting to loadkeys
218 if [ \
( $container_release_major = "4" -o $container_release_major = "5" \
) -a -e $container_rootfs/etc
/sysconfig
/keyboard
]; then
219 rm $container_rootfs/etc
/sysconfig
/keyboard
222 # dont use the hwclock, it messes up the host's time
223 if [ $container_release_major = "4" ]; then
224 sed -i 's|runcmd $"Syncing hardware clock|# LXC no hwclock runcmd $"Syncing hardware clock|' $container_rootfs/etc
/rc.d
/init.d
/halt
226 sed -i 's|\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.d
/init.d
/halt
228 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.sysinit
229 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.d
/rc.sysinit
230 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc
/rc.sysinit
231 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc
/rc.d
/rc.sysinit
234 sed -i 's|action $"Setting up Logical Volume Management:"|#action $"Setting up Logical Volume Management:"|' $container_rootfs/etc
/rc.sysinit
235 sed -i 's|action $"Setting up Logical Volume Management:"|/bin/true #action $"Setting up Logical Volume Management:"|' $container_rootfs/etc
/rc.d
/rc.sysinit
237 # fix assumptions that plymouth is available
238 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc
/rc.sysinit
239 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc
/rc.d
/rc.sysinit
240 rm -f $container_rootfs/etc
/init
/plymouth-shutdown.conf
241 rm -f $container_rootfs/etc
/init
/quit-plymouth.conf
242 rm -f $container_rootfs/etc
/init
/splash-manager.conf
244 # setup console and tty[1-4] for login. note that /dev/console and
245 # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
246 # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
247 # lxc will maintain these links and bind mount ptys over /dev/lxc/*
248 # since lxc.devttydir is specified in the config.
250 # allow root login on console, tty[1-4], and pts/0 for libvirt
251 echo "# LXC (Linux Containers)" >>$container_rootfs/etc
/securetty
252 echo "lxc/console" >>$container_rootfs/etc
/securetty
253 echo "lxc/tty1" >>$container_rootfs/etc
/securetty
254 echo "lxc/tty2" >>$container_rootfs/etc
/securetty
255 echo "lxc/tty3" >>$container_rootfs/etc
/securetty
256 echo "lxc/tty4" >>$container_rootfs/etc
/securetty
257 echo "# For libvirt/Virtual Machine Monitor" >>$container_rootfs/etc
/securetty
258 echo "pts/0" >>$container_rootfs/etc
/securetty
260 # prevent mingetty from calling vhangup(2) since it fails with userns
261 if [ -f $container_rootfs/etc
/init
/tty.conf
]; then
262 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc
/init
/tty.conf
265 # dont try to unmount /dev/lxc devices
266 sed -i 's|&& $1 !~ /^\\/dev\\/ram/|\&\& $2 !~ /^\\/dev\\/lxc/ \&\& $1 !~ /^\\/dev\\/ram/|' $container_rootfs/etc
/init.d
/halt
268 # don't try to unmount swap
269 sed -i 's|\[ -f /proc/swaps \]|# LXC [ -f /proc/swaps ]|' $container_rootfs/etc
/init.d
/halt
271 # start a getty on /dev/console, /dev/tty[1-4]
272 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
273 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc
/inittab
274 sed -i '/1:2345:respawn/i cns:2345:respawn:/sbin/mingetty --nohangup --noclear console' $container_rootfs/etc
/inittab
275 sed -i '/5:2345:respawn/d' $container_rootfs/etc
/inittab
276 sed -i '/6:2345:respawn/d' $container_rootfs/etc
/inittab
279 if [ $container_release_major = "6" ]; then
280 cat <<EOF > $container_rootfs/etc/init/console.conf
283 # This service maintains a getty on the console from the point the system is
284 # started until it is shut down again.
286 start on stopped rc RUNLEVEL=[2345]
287 stop on runlevel [!2345]
290 exec /sbin/mingetty --nohangup --noclear /dev/console
294 # lxc-shutdown sends SIGPWR to init, OL4 and OL5 have SysVInit, just
295 # make it do shutdown now instead of delaying 2 minutes. OL6 uses
296 # upstart, so we create an upstart job to handle SIGPWR to shut down
297 # cleanly. We use "init 0" instead of shutdown -h now to avoid SELinux
298 # permission denied when upstart's shutdown tries to connect to the
299 # /com/ubuntu/upstart socket.
300 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
301 sed -i 's|pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; |pf::powerfail:/sbin/shutdown -f -h now "|' $container_rootfs/etc
/inittab
303 cat <<EOF > $container_rootfs/etc/init/power-status-changed.conf
304 # power-status-changed - used to cleanly shut down the container
306 # This task is run whenever init receives SIGPWR
307 # Used to shut down the machine.
309 start on power-status-changed
315 # there might be other services that are useless but the below set is a good start
316 # some of these might not exist in the image, so we silence chkconfig complaining
317 # about the service file not being found
319 acpid apmd auditd autofs cpuspeed dund gpm haldaemon hidd \
320 ip6tables irqbalance iscsi iscsid isdn kdump kudzu \
321 lm_sensors lvm2-monitor mdmonitor microcode_ctl \
322 ntpd pcmcia postfix sendmail udev-post xfs
;
324 chroot
$container_rootfs chkconfig
2>/dev
/null
$service off
327 for service
in rsyslog
;
329 chroot
$container_rootfs chkconfig
2>/dev
/null
$service on
332 # create required devices. note that /dev/console will be created by lxc
333 # or libvirt itself to be a symlink to the right pty.
334 # take care to not nuke /dev in case $container_rootfs isn't set
335 dev_path
="$container_rootfs/dev"
336 if [ $container_rootfs != "/" -a -d $dev_path ]; then
340 # ensure symlinks created in /dev have the right context
341 chcon
-t device_t
$dev_path
344 mknod
-m 666 $dev_path/null c
1 3
345 mknod
-m 666 $dev_path/zero c
1 5
346 mknod
-m 666 $dev_path/random c
1 8
347 mknod
-m 666 $dev_path/urandom c
1 9
348 mkdir
-m 755 $dev_path/pts
349 mkdir
-m 1777 $dev_path/shm
350 mknod
-m 666 $dev_path/tty c
5 0
351 mknod
-m 666 $dev_path/tty0 c
4 0
352 mknod
-m 666 $dev_path/tty1 c
4 1
353 mknod
-m 666 $dev_path/tty2 c
4 2
354 mknod
-m 666 $dev_path/tty3 c
4 3
355 mknod
-m 666 $dev_path/tty4 c
4 4
356 mknod
-m 666 $dev_path/full c
1 7
357 mknod
-m 600 $dev_path/initctl p
359 # set selinux labels same as host
361 for node
in null zero random urandom pts shm \
362 tty tty0 tty1 tty2 tty3 tty4 full
;
364 chcon
--reference /dev
/$node $dev_path/$node 2>/dev
/null
368 # ensure /dev/ptmx refers to the newinstance devpts of the container, or
369 # pty's will get crossed up with the hosts (https://lkml.org/lkml/2012/1/23/512)
370 rm -f $container_rootfs/dev
/ptmx
371 ln -s pts
/ptmx
$container_rootfs/dev
/ptmx
373 # start with a clean /var/log/messages
374 rm -f $container_rootfs/var
/log
/messages
376 # add oracle user, set root password
377 chroot
$container_rootfs useradd
-m -s /bin
/bash oracle
378 echo "oracle:oracle" | chroot
$container_rootfs chpasswd
379 echo "root:root" | chroot
$container_rootfs chpasswd
380 printf "Added container user:\033[1moracle\033[0m password:\033[1moracle\033[0m\n"
381 printf "Added container user:\033[1mroot\033[0m password:\033[1mroot\033[0m\n"
384 # create the container's lxc config file
385 container_config_create
()
387 echo "Create configuration file $cfg_dir/config"
388 mkdir
-p $cfg_dir || die
"unable to create config dir $cfg_dir"
390 echo "# Common configuration" >> $cfg_dir/config
391 if [ -e "@LXCTEMPLATECONFIG@/oracle.common.conf" ]; then
392 echo "lxc.include = @LXCTEMPLATECONFIG@/oracle.common.conf" >> $cfg_dir/config
395 # generate a hwaddr for the container with a high mac address
396 # see http://sourceforge.net/tracker/?func=detail&aid=3411497&group_id=163076&atid=826303
397 local hwaddr
="fe:`dd if=/dev/urandom bs=8 count=1 2>/dev/null |od -t x8 | \
398 head -n 1 |awk '{print $2}' | cut -c1-10 |\
399 sed 's/\(..\)/\1:/g; s/.$//'`"
400 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
401 # Container configuration for Oracle Linux $container_release_major.$container_release_minor
405 grep -q "^lxc.rootfs" $cfg_dir/config
2>/dev
/null ||
echo "lxc.rootfs = $container_rootfs" >> $cfg_dir/config
407 if [ $container_release_major != "4" ]; then
408 echo "lxc.cap.drop = sys_resource" >>$cfg_dir/config
411 echo "# Networking" >>$cfg_dir/config
412 # see if the network settings were already specified
413 lxc_network_type
=`grep '^lxc.network.type' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
414 if [ -z "$lxc_network_type" -a \
415 \
( $host_distribution = "OracleServer" -o \
416 $host_distribution = "Fedora" \
) ]; then
417 echo "lxc.network.type = veth" >>$cfg_dir/config
418 echo "lxc.network.flags = up" >>$cfg_dir/config
419 echo "lxc.network.link = virbr0" >>$cfg_dir/config
422 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
423 lxc.network.name = eth0
424 lxc.network.mtu = 1500
425 lxc.network.hwaddr = $hwaddr
429 container_rootfs_clone
()
431 if is_btrfs_subvolume
$template_rootfs; then
432 # lxc-create already made $container_rootfs a btrfs subvolume, but
433 # in this case we want to snapshot the original subvolume so we we
434 # have to delete the one that lxc-create made
435 btrfs subvolume delete
$container_rootfs
436 btrfs subvolume snapshot
$template_rootfs $container_rootfs || die
"btrfs clone template"
438 echo "Copying rootfs ..."
439 cp -axT $template_rootfs $container_rootfs || die
"copy template"
443 container_rootfs_create
()
446 if [ $container_release_major -lt "6" ]; then
447 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
448 db_dump_cmd
="db5.1_dump"
449 db_load_cmd
="db4.3_load"
451 if [ $host_distribution = "OracleServer" -o \
452 $host_distribution = "Fedora" ]; then
453 db_dump_cmd
="db_dump"
454 db_load_cmd
="db43_load"
457 cmds
="$cmds $db_dump_cmd $db_load_cmd file"
460 which $cmd >/dev
/null
2>&1
461 if [ $?
-ne 0 ]; then
462 die
"The $cmd command is required, please install it"
466 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
/lxc
469 if [ $?
-ne 0 ]; then
470 die
"The template is busy."
473 echo "Downloading release $container_release_major.$container_release_minor for $basearch"
476 if [ -n "$repourl" ]; then
479 yum_url
=http
://public-yum.oracle.com
481 if [ $container_release_major = "4" ]; then
482 repofile
=public-yum-el4.repo
483 elif [ $container_release_major = "5" ]; then
484 repofile
=public-yum-el5.repo
485 elif [ $container_release_major = "6" ]; then
486 repofile
=public-yum-ol6.repo
488 die
"Unsupported release $container_release_major"
490 mkdir
-p $container_rootfs/etc
/yum.repos.d
491 wget
-q $yum_url/$repofile -O $container_rootfs/etc
/yum.repos.d
/$repofile
492 if [ $?
-ne 0 ]; then
493 die
"Failed to download repo file $yum_url/$repofile"
496 # yum will take $basearch from host, so force the arch we want
497 sed -i "s|\$basearch|$basearch|" $container_rootfs/etc
/yum.repos.d
/$repofile
499 # replace url if they specified one
500 if [ -n "$repourl" ]; then
501 sed -i "s|baseurl=http://public-yum.oracle.com/repo|baseurl=$repourl/repo|" $container_rootfs/etc
/yum.repos.d
/$repofile
502 sed -i "s|gpgkey=http://public-yum.oracle.com|gpgkey=$repourl|" $container_rootfs/etc
/yum.repos.d
/$repofile
505 # disable all repos, then enable the repo for the version we are installing.
506 if [ $container_release_minor = "latest" ]; then
507 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
508 repo
="el"$container_release_major"_"$container_release_minor
510 repo
="ol"$container_release_major"_"$container_release_minor
512 elif [ $container_release_major = "6" ]; then
513 if [ $container_release_minor = "0" ]; then
514 repo
="ol"$container_release_major"_ga_base"
516 repo
="ol"$container_release_major"_u"$container_release_minor"_base"
518 elif [ $container_release_major = "5" ]; then
519 if [ $container_release_minor = "0" ]; then
520 repo
="el"$container_release_major"_ga_base"
521 elif [ $container_release_minor -lt "6" ]; then
522 repo
="el"$container_release_major"_u"$container_release_minor"_base"
524 repo
="ol"$container_release_major"_u"$container_release_minor"_base"
526 elif [ $container_release_major = "4" -a $container_release_minor -gt "5" ]; then
527 repo
="el"$container_release_major"_u"$container_release_minor"_base"
529 die
"Unsupported release $container_release_major.$container_release_minor"
531 sed -i "s|enabled=1|enabled=0|" $container_rootfs/etc
/yum.repos.d
/$repofile
532 sed -i "/\[$repo\]/,/\[/ s/enabled=0/enabled=1/" $container_rootfs/etc
/yum.repos.d
/$repofile
534 # create rpm db, download and yum install minimal packages
535 mkdir
-p $container_rootfs/var
/lib
/rpm
536 rpm
--root $container_rootfs --initdb
537 yum_args
="--installroot $container_rootfs --disablerepo=* --enablerepo=$repo -y --nogpgcheck"
538 min_pkgs
="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils oraclelinux-release"
540 # we unshare the mount namespace because yum installing the ol4
541 # packages causes $rootfs/proc to be mounted on
542 lxc-unshare
-s MOUNT yum
-- $yum_args install $min_pkgs $user_pkgs
543 if [ $?
-ne 0 ]; then
544 die
"Failed to download and install the rootfs, aborting."
547 # rsyslog and pam depend on coreutils for some common commands in
548 # their POSTIN scriptlets, but coreutils wasn't installed yet. now
549 # that coreutils is installed, reinstall the packages so their POSTIN
550 # runs right. similarly, libutempter depends on libselinux.so.1 when
551 # it runs /usr/sbin/groupadd, so reinstall it too
553 if [ $container_release_major = "5" ]; then
554 if [ $container_release_minor = "latest" ]; then
555 redo_pkgs
="pam rsyslog libutempter"
556 elif [ $container_release_minor -lt 2 ]; then
558 elif [ $container_release_minor -lt 6 ]; then
559 redo_pkgs
="pam rsyslog"
560 elif [ $container_release_minor -gt 5 ]; then
561 redo_pkgs
="pam rsyslog libutempter"
564 # shadow utils fails on ol4 and ol6.1
565 if [ $container_release_major = "4" -o \
566 $container_release_major = "6" -a $container_release_minor = "1" ]; then
567 redo_pkgs
="shadow-utils"
569 if [ x
"$redo_pkgs" != x
]; then
570 rpm
--root $container_rootfs --nodeps -e $redo_pkgs
571 yum
$yum_args install $redo_pkgs
572 if [ $?
-ne 0 ]; then
573 die
"Unable to reinstall packages"
577 # these distributions put the rpm database in a place the guest is
578 # not expecting it, so move it
579 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
580 mv $container_rootfs/$HOME/.rpmdb
/* $container_rootfs/var
/lib
/rpm
583 # if the native rpm created the db with Hash version 9, we need to
584 # downgrade it to Hash version 8 for use with OL5.x
585 db_version
=`file $container_rootfs/var/lib/rpm/Packages | \
586 grep -o 'version [0-9]*' |awk '{print $2}'`
587 if [ $container_release_major -lt "6" -a $db_version != "8" ]; then
588 echo "Fixing (downgrading) rpm database from version $db_version"
589 rm -f $container_rootfs/var
/lib
/rpm
/__db
*
590 for db
in $container_rootfs/var
/lib
/rpm
/* ; do
591 $db_dump_cmd $db |
$db_load_cmd $db.new
596 # the host rpm may not be the same as the guest, rebuild the db with
597 # the guest rpm version
598 echo "Rebuilding rpm database"
599 rm -f $container_rootfs/var
/lib
/rpm
/__db
*
600 chroot
$container_rootfs rpm
--rebuilddb >/dev
/null
2>&1
602 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-oracle-
$name
605 container_release_get
()
607 if [ -f $1/etc
/oracle-release
]; then
608 container_release_version
=`cat $1/etc/oracle-release |awk '/^Oracle/ {print $5}'`
609 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
610 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
611 elif grep -q "Enterprise Linux AS" $1/etc
/redhat-release
; then
612 container_release_major
=`cat $1/etc/redhat-release |awk '{print $7}'`
613 container_release_minor
=`cat $1/etc/redhat-release |awk '{print $10}' |tr -d ")"`
614 container_release_version
="$container_release_major.$container_release_minor"
615 elif grep -q "Enterprise Linux Server" $1/etc
/redhat-release
; then
616 container_release_version
=`cat $1/etc/redhat-release |awk '{print $7}'`
617 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
618 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
620 echo "Unable to determine container release version"
628 -a|--arch=<arch> architecture (ie. i386, x86_64)
629 -R|--release=<release> release to download for the new container
630 --rootfs=<path> rootfs path
631 -r|--rpms=<rpm name> additional rpms to install into container
632 -u|--url=<url> replace yum repo url (ie. local yum mirror)
633 -t|--templatefs=<path> copy/clone rootfs at path instead of downloading
636 Release is of the format "major.minor", for example "5.8", "6.3", or "6.latest"
641 options
=$
(getopt
-o hp
:n
:a
:R
:r
:u
:t
: -l help,rootfs
:,path
:,name
:,arch
:,release
:,rpms
:,url
:,templatefs
: -- "$@")
642 if [ $?
-ne 0 ]; then
648 eval set -- "$options"
652 -h|
--help) usage
$0 && exit 0;;
653 -p|
--path) cfg_dir
=$2; shift 2;;
654 --rootfs) container_rootfs
=$2; shift 2;;
655 -n|
--name) name
=$2; shift 2;;
656 -a|
--arch) arch
=$2; shift 2;;
657 -R|
--release) container_release_version
=$2; shift 2;;
658 -r|
--rpms) user_pkgs
=$2; shift 2;;
659 -u|
--url) repourl
=$2; shift 2;;
660 -t|
--templatefs) template_rootfs
=$2; shift 2;;
661 --) shift 1; break ;;
666 # make sure mandatory args are given and valid
667 if [ "$(id -u)" != "0" ]; then
668 echo "This script should be run as 'root'"
672 if [ -z $name ]; then
673 echo "Container name must be given"
678 if [ -z $cfg_dir ]; then
679 echo "Configuration directory must be given, check lxc-create"
685 if [ "$arch" = "i686" ]; then
689 if [ "$arch" != "i386" -a "$arch" != "x86_64" ]; then
690 echo "Bad architecture given, check lxc-create"
695 if which lsb_release
>/dev
/null
2>&1; then
696 host_distribution
=`lsb_release --id |awk '{print $3}'`
697 host_release_version
=`lsb_release --release |awk '{print $2}'`
698 host_release_major
=`echo $host_release_version |awk -F '.' '{print $1}'`
699 host_release_minor
=`echo $host_release_version |awk -F '.' '{print $2}'`
701 if [ -f /etc
/fedora-release
]; then
702 host_distribution
="Fedora"
703 host_release_version
=`cat /etc/fedora-release |awk '{print $3}'`
704 host_release_major
=$host_release_version
706 elif [ -f /etc
/oracle-release
]; then
707 host_distribution
="OracleServer"
708 host_release_version
=`cat /etc/oracle-release |awk '{print $5}'`
709 host_release_major
=`echo $host_release_version |awk -F '.' '{print $1}'`
710 host_release_minor
=`echo $host_release_version |awk -F '.' '{print $2}'`
712 echo "Unable to determine host distribution, ensure lsb_release is installed"
716 echo "Host is $host_distribution $host_release_version"
718 if [ -z "$container_rootfs" ]; then
719 container_rootfs
="$cfg_dir/rootfs"
722 if [ -n "$template_rootfs" ]; then
723 container_release_get
$template_rootfs
725 if [ -z "$container_release_version" ]; then
726 if [ $host_distribution = "OracleServer" ]; then
727 container_release_version
=$host_release_version
729 echo "No release specified with -R, defaulting to 6.5"
730 container_release_version
="6.5"
733 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
734 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
737 container_config_create
738 if [ -n "$template_rootfs" ]; then
739 container_rootfs_clone
741 container_rootfs_create
744 container_release_get
$container_rootfs
746 container_rootfs_configure
748 echo "Container : $container_rootfs"
749 echo "Config : $cfg_dir/config"
750 echo "Network : eth0 ($lxc_network_type) on $lxc_network_link"