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,7 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
51 if which btrfs
>/dev
/null
2>&1 && \
52 btrfs subvolume list
"$1" >/dev
/null
2>&1; then
60 if which chcon
>/dev
/null
2>&1; then
61 selinuxenabled
>/dev
/null
2>&1
67 # fix up the container_rootfs
68 container_rootfs_patch
()
70 echo "Patching container rootfs $container_rootfs for Oracle Linux $container_release_major.$container_release_minor"
72 # copy ourself into the container to be used to --patch the rootfs when
73 # yum update on certain packages is done. we do this here instead of in
74 # container_rootfs_configure() in case the patching done in this function
75 # is updated in the future, we can inject the updated version of ourself
76 # into older containers.
77 if [ $container_rootfs != "/" ]; then
78 cp -f `readlink -f $0` $container_rootfs/usr
/bin
/lxc-patch
79 if [ $container_release_major -lt "6" ]; then
80 mkdir
-p $container_rootfs/usr
/lib
/yum-plugins
81 cp @DATADIR@
/lxc
/lxc-patch.py
$container_rootfs/usr
/lib
/yum-plugins
83 if [ $container_release_major -ge "6" ]; then
84 mkdir
-p $container_rootfs/usr
/share
/yum-plugins
85 cp @DATADIR@
/lxc
/lxc-patch.py
$container_rootfs/usr
/share
/yum-plugins
87 mkdir
-p $container_rootfs/etc
/yum
/pluginconf.d
88 cat <<EOF > $container_rootfs/etc/yum/pluginconf.d/lxc-patch.conf
91 packages=dbus,initscripts,iptables,openssh-server,setup,selinux-policy,readahead,udev,util-linux,util-linux-ng
95 if [ $container_release_major = "4" ]; then
96 # yum plugin type of TYPE_INTERFACE works in all releases but gives a
97 # deprecation warning on major > 4, so we default to TYPE_INTERACTIVE
99 sed -i 's|TYPE_INTERACTIVE|TYPE_INTERFACE|' $container_rootfs/usr
/lib
/yum-plugins
/lxc-patch.py
100 if [ -f $container_rootfs/etc
/yum.repos.d
/ULN-Base.repo
]; then
101 mv $container_rootfs/etc
/yum.repos.d
/ULN-Base.repo \
102 $container_rootfs/etc
/yum.repos.d
/ULN-Base.repo.lxc-disabled
104 echo "plugins = 1" >>$container_rootfs/etc
/yum.conf
107 # "disable" selinux in the guest. The policy in the container isn't
108 # likely to match the hosts (unless host == guest exactly) and the
109 # kernel can only be enforcing one policy.
111 # The OL 5 init honors /etc/selinux/config, but note that
112 # this doesnt actually disable it if it's enabled in the host, since
113 # libselinux::is_selinux_enabled() in the guest will check
114 # /proc/filesystems and see selinuxfs, thus reporting that it is on
115 # (ie. check the output of sestatus in the guest). We also replace
116 # /usr/sbin/selinuxenabled with a symlink to /bin/false so that init
117 # scripts (ie. mcstransd) that call that think selinux is disabled.
118 mkdir
-p $container_rootfs/selinux
119 echo 0 > $container_rootfs/selinux
/enforce
120 if [ -e $container_rootfs/etc
/selinux
/config
]; then
121 sed -i 's|SELINUX=enforcing|SELINUX=disabled|' $container_rootfs/etc
/selinux
/config
123 mkdir
-p $container_rootfs/etc
/selinux
124 echo "SELINUX=disabled" >$container_rootfs/etc
/selinux
/config
126 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*close|#session required pam_selinux.so close|' $container_rootfs/etc
/pam.d
/login
127 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*open|#session required pam_selinux.so open|' $container_rootfs/etc
/pam.d
/login
128 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*close|#session required pam_selinux.so close|' $container_rootfs/etc
/pam.d
/sshd
129 sed -i 's|session[ \t]*required[ \t]*pam_selinux.so[ \t]*open|#session required pam_selinux.so open|' $container_rootfs/etc
/pam.d
/sshd
131 # setting /proc/$$/loginuid doesn't work under user namespace, which
132 # prevents logins from working
133 sed -i 's|session[ \t]*required[ \t]*pam_loginuid.so|#session required pam_loginuid.so|' $container_rootfs/etc
/pam.d
/sshd
134 sed -i 's|session[ \t]*required[ \t]*pam_loginuid.so|#session required pam_loginuid.so|' $container_rootfs/etc
/pam.d
/login
136 if [ -f $container_rootfs/usr
/sbin
/selinuxenabled
]; then
137 mv $container_rootfs/usr
/sbin
/selinuxenabled
$container_rootfs/usr
/sbin
/selinuxenabled.lxcorig
138 ln -s /bin
/false
$container_rootfs/usr
/sbin
/selinuxenabled
141 # ensure /dev/ptmx refers to the newinstance devpts of the container, or
142 # pty's will get crossed up with the hosts (https://lkml.org/lkml/2012/1/23/512)
143 rm -f $container_rootfs/dev
/ptmx
144 ln -s pts
/ptmx
$container_rootfs/dev
/ptmx
146 # OL7 has systemd, no rc.sysinit
147 if [ $container_release_major = "7" ]; then
148 # with newer systemd (OL7.2), getty service include container-getty.service
149 # let that be the one who manage the getty service instead
150 if [ ! -f $container_rootfs/usr
/lib
/systemd
/system
/container-getty@.service
]; then
151 # from mhw in the fedora template: We do need to disable the
152 # "ConditionalPathExists=/dev/tty0" line or no gettys are started on
153 # the ttys in the container. Lets do it in an override copy of the
154 # service so it can still pass rpm verifies and not be automatically
155 # updated by a new systemd version.
156 sed -e 's/^ConditionPathExists=/#LXC ConditionPathExists=/' \
157 < $container_rootfs/usr
/lib
/systemd
/system
/getty\@.service \
158 > $container_rootfs/etc
/systemd
/system
/getty\@.service
159 # Setup getty service on the 4 ttys we are going to allow in the
160 # default config. Number should match lxc.tty
161 ( cd $container_rootfs/etc
/systemd
/system
/getty.target.wants
162 for i
in 1 2 3 4 ; do ln -sf ..
/getty\@.service getty@tty
${i}.service
; done )
163 # We only want to spawn a getty on /dev/console in lxc, libvirt-lxc
164 # symlinks /dev/console to /dev/tty1
165 sed -i '/Before=getty.target/a ConditionVirtualization=lxc' $container_rootfs/usr
/lib
/systemd
/system
/console-getty.service
168 # disable some systemd services, set default boot, sigpwr target
169 rm -f $container_rootfs/usr
/lib
/systemd
/system
/sysinit.target.wants
/kmod-static-nodes.service
170 chroot
$container_rootfs systemctl
-q disable graphical.target
171 chroot
$container_rootfs systemctl
-q enable multi-user.target
173 # systemd in userns won't be able to set /proc/self/oom_score_adj which
174 # prevents the dbus service from starting
175 sed -i 's|^OOMScoreAdjust|#LXC OOMScoreAdjust|' $container_rootfs/usr
/lib
/systemd
/system
/dbus.service
179 # silence error in checking for selinux
180 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc
/rc.sysinit
181 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc
/rc.d
/rc.sysinit
183 # on ol4 pam_limits prevents logins when using user namespaces
184 if [ $container_release_major = "4" ]; then
185 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
188 # avoid error in ol5 attempting to copy non-existent resolv.conf
189 if [ $container_release_major = "5" ]; then
190 sed -i 's|resolv.conf.predhclient|resolv.conf.predhclient 2>/dev/null|' $container_rootfs/sbin
/dhclient-script
193 # disable interactive ovmd asking questions
194 if [ -f $container_rootfs/etc
/sysconfig
/ovmd
]; then
195 sed -i 's|INITIAL_CONFIG=yes|INITIAL_CONFIG=no|' $container_rootfs/etc
/sysconfig
/ovmd
198 # disable disabling of ipv4 forwarding and defrag on shutdown since
199 # we mount /proc/sys ro
200 if [ $container_release_major = "5" ]; then
201 sed -i 's|-f /proc/sys/net/ipv4/ip_forward|-w /proc/sys/net/ipv4/ip_forward|' $container_rootfs/etc
/rc.d
/init.d
/network
202 sed -i 's|-f /proc/sys/net/ipv4/ip_always_defrag|-w /proc/sys/net/ipv4/ip_always_defrag|' $container_rootfs/etc
/rc.d
/init.d
/network
205 # disable ipv6 on ol6
206 rm -f $container_rootfs/etc
/sysconfig
/network-scripts
/init.ipv6-global
208 # remove module stuff for iptables it just shows errors that are not
209 # relevant in a container
210 if [ -f "$container_rootfs/etc/sysconfig/iptables-config" ]; then
211 sed -i 's|IPTABLES_MODULES=".*|IPTABLES_MODULES=""|' $container_rootfs/etc
/sysconfig
/iptables-config
212 sed -i 's|IPTABLES_MODULES_UNLOAD=".*|IPTABLES_MODULES_UNLOAD="no"|' $container_rootfs/etc
/sysconfig
/iptables-config
215 # disable readahead in the container
216 if [ $container_release_major = "6" -a -e $container_rootfs/etc
/sysconfig
/readahead
]; then
217 rm -f $container_rootfs/etc
/init
/readahead-collector.conf
218 rm -f $container_rootfs/etc
/init
/readahead-disable-services.conf
219 sed -i 's|READAHEAD="yes"|READAHEAD="no"|' $container_rootfs/etc
/sysconfig
/readahead
222 if [ $container_release_major = "4" ]; then
223 # enable fastboot always
224 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc
/rc.sysinit
225 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc
/rc.d
/rc.sysinit
227 # dont attempt to set kernel parameters
228 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc
/rc.sysinit
229 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc
/rc.d
/rc.sysinit
230 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc
/init.d
/network
2>/dev
/null
231 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc
/init.d
/NetworkManager
2>/dev
/null
234 # no need to attempt to mount /
235 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc
/rc.sysinit
236 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc
/rc.d
/rc.sysinit
237 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc
/rc.sysinit
238 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc
/rc.d
/rc.sysinit
240 # disable udev in the container
241 if [ $container_release_major = "4" ]; then
242 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc
/rc.sysinit
243 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc
/rc.d
/rc.sysinit
245 sed -i 's|.sbin.start_udev||' $container_rootfs/etc
/rc.sysinit
246 sed -i 's|.sbin.start_udev||' $container_rootfs/etc
/rc.d
/rc.sysinit
249 # disable nash raidautorun in the container since no /dev/md*
250 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
251 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc
/rc.sysinit
252 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc
/rc.d
/rc.sysinit
255 # prevent rc.sysinit from attempting to loadkeys
256 if [ \
( $container_release_major = "4" -o $container_release_major = "5" \
) -a -e $container_rootfs/etc
/sysconfig
/keyboard
]; then
257 rm $container_rootfs/etc
/sysconfig
/keyboard
260 # dont use the hwclock, it messes up the host's time
261 if [ $container_release_major = "4" ]; then
262 sed -i 's|runcmd $"Syncing hardware clock|# LXC no hwclock runcmd $"Syncing hardware clock|' $container_rootfs/etc
/rc.d
/init.d
/halt
264 sed -i 's|\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.d
/init.d
/halt
266 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.sysinit
267 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc
/rc.d
/rc.sysinit
268 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc
/rc.sysinit
269 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc
/rc.d
/rc.sysinit
272 if [ $container_release_major -lt "6" -a -f $container_rootfs/sbin
/lvm.static
]; then
273 mv $container_rootfs/sbin
/lvm.static
$container_rootfs/sbin
/lvm.static.lxc-disabled
275 if [ $container_release_major = "6" ]; then
276 touch $container_rootfs/.nolvm
279 # fix assumptions that plymouth is available
280 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc
/rc.sysinit
281 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc
/rc.d
/rc.sysinit
282 rm -f $container_rootfs/etc
/init
/plymouth-shutdown.conf
283 rm -f $container_rootfs/etc
/init
/quit-plymouth.conf
284 rm -f $container_rootfs/etc
/init
/splash-manager.conf
286 # dont try to unmount /dev/lxc devices
287 sed -i 's|&& $1 !~ /^\\/dev\\/ram/|\&\& $2 !~ /^\\/dev\\/lxc/ \&\& $1 !~ /^\\/dev\\/ram/|' $container_rootfs/etc
/init.d
/halt
289 # don't try to unmount swap
290 sed -i 's|\[ -f /proc/swaps \]|# LXC [ -f /proc/swaps ]|' $container_rootfs/etc
/init.d
/halt
292 # sem_open(3) checks that /dev/shm is SHMFS_SUPER_MAGIC, so make sure to
293 # mount /dev/shm (normally done by dracut initrd) as tmpfs
294 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
295 grep -q "mount -t tmpfs tmpfs /dev/shm" $container_rootfs/etc
/rc.sysinit
296 if [ $?
-eq 1 ]; then
297 echo "mkdir -p /dev/shm && mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc
/rc.sysinit
298 echo "mkdir -p /dev/shm && mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc
/rc.d
/rc.sysinit
301 if [ $container_release_major = "6" ]; then
302 sed -i 's|mount -n -o remount /dev/shm >/dev/null 2>&1$|mkdir -p /dev/shm \&\& mount -t tmpfs tmpfs /dev/shm # LXC|' $container_rootfs/etc
/rc.sysinit
303 sed -i 's|mount -n -o remount /dev/shm >/dev/null 2>&1$|mkdir -p /dev/shm \&\& mount -t tmpfs tmpfs /dev/shm # LXC|' $container_rootfs/etc
/rc.d
/rc.sysinit
306 # there might be other services that are useless but the below set is a good start
307 # some of these might not exist in the image, so we silence chkconfig complaining
308 # about the service file not being found
310 acpid apmd auditd autofs cpuspeed dund gpm haldaemon hidd \
311 ip6tables irqbalance iscsi iscsid isdn kdump kudzu \
312 lm_sensors lvm2-monitor mdmonitor microcode_ctl \
313 ntpd pcmcia postfix sendmail udev-post xfs
;
315 chroot
$container_rootfs chkconfig
2>/dev
/null
$service off
318 for service
in rsyslog
;
320 chroot
$container_rootfs chkconfig
2>/dev
/null
$service on
324 container_rootfs_configure
()
326 container_rootfs_patch
327 echo "Configuring container for Oracle Linux $container_release_major.$container_release_minor"
329 # configure the network to use dhcp. we set DHCP_HOSTNAME so the guest
330 # will report its name and be resolv'able by the hosts dnsmasq
331 cat <<EOF > $container_rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
336 DHCP_HOSTNAME=\`hostname\`
342 if [ $container_release_major -ge "7" ]; then
343 # systemd honors /etc/hostname
344 echo "$name" >$container_rootfs/etc
/hostname
346 cat <<EOF > $container_rootfs/etc/sysconfig/network
353 echo "127.0.0.1 localhost $name" > $container_rootfs/etc
/hosts
355 # this file has to exist for libvirt/Virtual machine monitor to boot the container
356 touch $container_rootfs/etc
/mtab
358 # setup console and tty[1-4] for login. note that /dev/console and
359 # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
360 # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
361 # lxc will maintain these links and bind mount ptys over /dev/lxc/*
362 # since lxc.devttydir is specified in the config.
364 # allow root login on console, tty[1-4], and pts/0 for libvirt
365 echo "# LXC (Linux Containers)" >>$container_rootfs/etc
/securetty
366 echo "lxc/console" >>$container_rootfs/etc
/securetty
368 echo "lxc/tty$i" >>$container_rootfs/etc
/securetty
370 echo "# For libvirt/Virtual Machine Monitor" >>$container_rootfs/etc
/securetty
371 for i
in 0 1 2 3 4; do
372 echo "pts/$i" >>$container_rootfs/etc
/securetty
375 # prevent mingetty from calling vhangup(2) since it fails with userns
376 if [ -f $container_rootfs/etc
/init
/tty.conf
]; then
377 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc
/init
/tty.conf
380 # create maygetty which only spawns a getty on the console when running
381 # under lxc, not libvirt-lxc which symlinks /dev/console to the same pty
383 cat <<EOF >$container_rootfs/sbin/maygetty
385 if [ "\$container" = "lxc" ]; then
386 exec /sbin/mingetty \$@
390 chmod 755 $container_rootfs/sbin
/maygetty
392 # start a getty on /dev/console, /dev/tty[1-4]
393 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
394 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc
/inittab
395 sed -i '/1:2345:respawn/i cns:2345:respawn:/sbin/maygetty --nohangup --noclear console' $container_rootfs/etc
/inittab
396 sed -i '/5:2345:respawn/d' $container_rootfs/etc
/inittab
397 sed -i '/6:2345:respawn/d' $container_rootfs/etc
/inittab
400 if [ $container_release_major = "6" ]; then
401 cat <<EOF > $container_rootfs/etc/init/console.conf
404 # This service maintains a getty on the console from the point the system is
405 # started until it is shut down again.
407 start on stopped rc RUNLEVEL=[2345]
408 stop on runlevel [!2345]
412 exec /sbin/maygetty --nohangup --noclear /dev/console
416 # lxc-shutdown sends SIGPWR to init, OL4 and OL5 have SysVInit, just
417 # make it do shutdown now instead of delaying 2 minutes. OL6 uses
418 # upstart, so we create an upstart job to handle SIGPWR to shut down
419 # cleanly. We use "init 0" instead of shutdown -h now to avoid SELinux
420 # permission denied when upstart's shutdown tries to connect to the
421 # /com/ubuntu/upstart socket.
422 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
423 sed -i 's|pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; |pf::powerfail:/sbin/shutdown -f -h now "|' $container_rootfs/etc
/inittab
424 elif [ $container_release_major = "6" ]; then
425 cat <<EOF > $container_rootfs/etc/init/power-status-changed.conf
426 # power-status-changed - used to cleanly shut down the container
428 # This task is run whenever init receives SIGPWR
429 # Used to shut down the machine.
431 start on power-status-changed
437 # start with a clean /var/log/messages
438 rm -f $container_rootfs/var
/log
/messages
440 # set initial timezone as on host
441 if [ -f /etc
/sysconfig
/clock ]; then
442 .
/etc
/sysconfig
/clock
443 if [ $container_release_major = "5" -o $container_release_major = "6" ]; then
444 echo ZONE
=$ZONE > $container_rootfs/etc
/sysconfig
/clock
445 chroot
$container_rootfs tzdata-update
448 chroot
$container_rootfs ln -sf ..
/usr
/share
/zoneinfo
/$ZONE /etc
/localtime
451 ZONE
=`readlink /etc/localtime | sed -s "s/\.\.\/usr\/share\/zoneinfo\///g"`
453 if [ $container_release_major = "5" -o $container_release_major = "6" ]; then
454 echo ZONE
=$ZONE > $container_rootfs/etc
/sysconfig
/clock
455 chroot
$container_rootfs tzdata-update
457 # if /etc/localtime is a symlink, this should preserve it.
458 cp -a /etc
/localtime
$container_rootfs/etc
/localtime
461 echo "Timezone in container is not configured. Adjust it manually."
466 chroot
$container_rootfs useradd
-m -s /bin
/bash oracle
467 printf "Added container user:\033[1moracle\033[0m\n"
468 printf "Added container user:\033[1mroot\033[0m\n"
471 # create the container's lxc config file
472 container_config_create
()
474 echo "Create configuration file $cfg_dir/config"
475 mkdir
-p $cfg_dir || die
"unable to create config dir $cfg_dir"
477 echo "# Common configuration" >> $cfg_dir/config
478 if [ -e "@LXCTEMPLATECONFIG@/oracle.common.conf" ]; then
479 echo "lxc.include = @LXCTEMPLATECONFIG@/oracle.common.conf" >> $cfg_dir/config
482 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
483 # Container configuration for Oracle Linux $container_release_major.$container_release_minor
487 grep -q "^lxc.rootfs" $cfg_dir/config
2>/dev
/null ||
echo "lxc.rootfs = $container_rootfs" >> $cfg_dir/config
489 if [ $container_release_major != "4" ]; then
490 echo "lxc.cap.drop = sys_resource" >>$cfg_dir/config
493 # systemd services like logind and journald need these
494 if [ $container_release_major != "7" ]; then
495 echo "lxc.cap.drop = setfcap setpcap" >>$cfg_dir/config
498 echo "# Networking" >>$cfg_dir/config
499 # see if the default network settings were already specified
500 lxc_network_type
=`grep '^lxc.net.0.type' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
501 if [ -z "$lxc_network_type" ]; then
502 echo "lxc.net.0.type = veth" >>$cfg_dir/config
503 lxc_network_type
=veth
506 lxc_network_link
=`grep '^lxc.net.0.link' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
507 if [ -z "$lxc_network_link" ]; then
508 echo "lxc.net.0.link = lxcbr0" >>$cfg_dir/config
509 lxc_network_link
=lxcbr0
512 lxc_network_hwaddr
=`grep '^lxc.net.0.hwaddr' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
513 if [ -z "$lxc_network_hwaddr" ]; then
514 # generate a hwaddr for the container
515 # see http://sourceforge.net/tracker/?func=detail&aid=3411497&group_id=163076&atid=826303
516 local hwaddr
="00:16:3e:`dd if=/dev/urandom bs=8 count=1 2>/dev/null |od -t x8 | \
517 head -n1 | awk '{print $2}' | cut -c1-6 | \
518 sed 's/\(..\)/\1:/g; s/.$//'`"
519 echo "lxc.net.0.hwaddr = $hwaddr" >>$cfg_dir/config
522 lxc_network_flags
=`grep '^lxc.net.0.flags' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
523 if [ -z "$lxc_network_flags" ]; then
524 echo "lxc.net.0.flags = up" >>$cfg_dir/config
527 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
528 lxc.net.0.name = eth0
533 container_rootfs_clone
()
535 if is_btrfs_subvolume
$template_rootfs; then
536 # lxc-create already made $container_rootfs a btrfs subvolume, but
537 # in this case we want to snapshot the original subvolume so we we
538 # have to delete the one that lxc-create made
539 btrfs subvolume delete
$container_rootfs
540 btrfs subvolume snapshot
$template_rootfs $container_rootfs || die
"btrfs clone template"
542 echo "Copying rootfs ..."
543 cp -axT $template_rootfs $container_rootfs || die
"copy template"
547 container_rootfs_repo_create
()
549 echo "# LXC generated .repo file" >$1
551 echo "name=Oracle Linux $container_release_major.$container_release_minor ($basearch)" >>$1
552 echo "baseurl=$3/" >>$1
553 echo "enabled=1" >>$1
554 echo "skip_if_unavailable=1" >>$1
556 if [ "$4" != "" ]; then
557 echo "gpgkey=$yum_url/RPM-GPG-KEY-oracle-ol$container_release_major" >>$1
558 echo "gpgcheck=1" >>$1
560 echo "gpgcheck=0" >>$1
564 container_rootfs_dev_create
()
566 # create required devices. note that /dev/console will be created by lxc
567 # or libvirt itself to be a symlink to the right pty.
568 # take care to not nuke /dev in case $container_rootfs isn't set
569 dev_path
="$container_rootfs/dev"
570 if [ $container_rootfs != "/" -a -d $dev_path ]; then
575 # ensure symlinks created in /dev have the right context
576 chcon
-t device_t
$dev_path
578 mknod
-m 666 $dev_path/null c
1 3
579 mknod
-m 666 $dev_path/zero c
1 5
580 mknod
-m 666 $dev_path/random c
1 8
581 mknod
-m 666 $dev_path/urandom c
1 9
582 mkdir
-m 755 $dev_path/pts
583 mkdir
-m 1777 $dev_path/shm
584 mknod
-m 666 $dev_path/tty c
5 0
585 mknod
-m 666 $dev_path/tty1 c
4 1
586 mknod
-m 666 $dev_path/tty2 c
4 2
587 mknod
-m 666 $dev_path/tty3 c
4 3
588 mknod
-m 666 $dev_path/tty4 c
4 4
589 mknod
-m 666 $dev_path/full c
1 7
590 mknod
-m 600 $dev_path/initctl p
592 # set selinux labels same as host
594 for node
in null zero random urandom pts shm \
595 tty tty0 tty1 tty2 tty3 tty4 full
;
597 chcon
--reference /dev
/$node $dev_path/$node 2>/dev
/null
602 container_rootfs_create
()
605 chcon
--reference / $container_rootfs 2>/dev
/null
609 if [ $container_release_major -lt "6" ]; then
610 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
611 db_dump_cmd
="db5.1_dump"
613 if [ $host_distribution = "OracleServer" -o \
614 $host_distribution = "Fedora" ]; then
615 db_dump_cmd
="db_dump"
618 cmds
="$cmds $db_dump_cmd file"
621 which $cmd >/dev
/null
2>&1
622 if [ $?
-ne 0 ]; then
623 die
"The $cmd command is required, please install it"
627 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
630 if [ $?
-ne 0 ]; then
631 die
"The template is busy."
634 echo "Yum installing release $container_release_major.$container_release_minor for $basearch"
636 if [ -n "$repourl" ]; then
639 yum_url
=http
://public-yum.oracle.com
641 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
649 if [ -n "$baseurl" ]; then
650 # create .repo pointing at baseurl
652 mkdir
-p $container_rootfs/etc
/yum.repos.d
653 container_rootfs_repo_create \
654 $container_rootfs/etc
/yum.repos.d
/lxc-install.repo
$repo $baseurl
656 # get public-yum repo file
657 if [ $container_release_major = "4" ]; then
658 repofile
=public-yum-el4.repo
659 elif [ $container_release_major = "5" ]; then
660 repofile
=public-yum-el5.repo
661 elif [ $container_release_major = "6" ]; then
662 repofile
=public-yum-ol6.repo
663 elif [ $container_release_major = "7" ]; then
664 repofile
=public-yum-ol7.repo
666 die
"Unsupported release $container_release_major"
669 mkdir
-p $container_rootfs/etc
/yum.repos.d
670 wget
-q $yum_url/$repofile -O $container_rootfs/etc
/yum.repos.d
/$repofile
671 if [ $?
-ne 0 ]; then
672 die
"Unable to download repo file $yum_url/$repofile, release unavailable"
675 # yum will take $basearch from host, so force the arch we want
676 sed -i "s|\$basearch|$basearch|" $container_rootfs/etc
/yum.repos.d
/$repofile
678 # replace url if they specified one
679 if [ -n "$repourl" ]; then
680 sed -i "s|baseurl=http://public-yum.oracle.com/repo|baseurl=$repourl/repo|" $container_rootfs/etc
/yum.repos.d
/$repofile
681 sed -i "s|gpgkey=http://public-yum.oracle.com|gpgkey=$repourl|" $container_rootfs/etc
/yum.repos.d
/$repofile
684 # disable all repos, then enable the repo for the version we are installing.
685 if [ $container_release_minor = "latest" ]; then
686 repo
=$latest_L""$container_release_major"_"$container_release_minor
687 elif [ $container_release_major = "7" ]; then
688 repo
="ol"$container_release_major"_u"$container_release_minor"_base"
689 elif [ $container_release_major = "6" ]; then
690 if [ $container_release_minor = "0" ]; then
691 repo
="ol"$container_release_major"_ga_base"
693 repo
="ol"$container_release_major"_u"$container_release_minor"_base"
695 elif [ $container_release_major = "5" ]; then
696 if [ $container_release_minor = "0" ]; then
697 repo
="el"$container_release_major"_ga_base"
698 elif [ $container_release_minor -lt "6" ]; then
699 repo
="el"$container_release_major"_u"$container_release_minor"_base"
701 repo
="ol"$container_release_major"_u"$container_release_minor"_base"
703 elif [ $container_release_major = "4" -a $container_release_minor -gt "5" ]; then
704 repo
="el"$container_release_major"_u"$container_release_minor"_base"
706 die
"Unsupported release $container_release_major.$container_release_minor"
708 sed -i "s|enabled=1|enabled=0|" $container_rootfs/etc
/yum.repos.d
/$repofile
709 sed -i "/\[$repo\]/,/\[/ s/enabled=0/enabled=1/" $container_rootfs/etc
/yum.repos.d
/$repofile
712 container_rootfs_dev_create
714 # don't put devpts,proc, nor sysfs in here, it will already be mounted for us by lxc/libvirt
715 echo "" >$container_rootfs/etc
/fstab
717 # create rpm db, download and yum install minimal packages
718 mkdir
-p $container_rootfs/var
/lib
/rpm
719 rpm
--root $container_rootfs --initdb
720 yum_args
="--installroot $container_rootfs --disablerepo=* --enablerepo=$repo -y --nogpgcheck"
721 min_pkgs
="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils oraclelinux-release"
722 if [ $container_release_major -lt "6" ]; then
723 min_pkgs
="$min_pkgs db4-utils"
726 # we unshare the mount namespace because yum installing the ol4
727 # packages causes $rootfs/proc to be mounted on
728 lxc-unshare
-s MOUNT yum
-- $yum_args install $min_pkgs $user_pkgs
729 if [ $?
-ne 0 ]; then
730 die
"Failed to download and install the rootfs, aborting."
733 # rsyslog and pam depend on coreutils for some common commands in
734 # their POSTIN scriptlets, but coreutils wasn't installed yet. now
735 # that coreutils is installed, reinstall the packages so their POSTIN
736 # runs right. similarly, libutempter depends on libselinux.so.1 when
737 # it runs /usr/sbin/groupadd, so reinstall it too
739 if [ $container_release_major = "5" ]; then
740 if [ $container_release_minor = "latest" ]; then
741 redo_pkgs
="pam rsyslog libutempter"
742 elif [ $container_release_minor -lt 2 ]; then
744 elif [ $container_release_minor -lt 6 ]; then
745 redo_pkgs
="pam rsyslog"
746 elif [ $container_release_minor -gt 5 ]; then
747 redo_pkgs
="pam rsyslog libutempter"
750 # shadow utils fails on ol4 and ol6.1
751 if [ $container_release_major = "4" -o \
752 $container_release_major = "6" -a $container_release_minor = "1" ]; then
753 redo_pkgs
="shadow-utils"
755 if [ x
"$redo_pkgs" != x
]; then
756 rpm
--root $container_rootfs --nodeps -e $redo_pkgs
757 lxc-unshare
-s MOUNT yum
-- $yum_args install $redo_pkgs
758 if [ $?
-ne 0 ]; then
759 die
"Unable to reinstall packages"
763 # if installing from a baseurl, create a .repo that the container
764 # can use to update to _latest from http://public-yum.oracle.com
765 if [ -n "$baseurl" ]; then
766 container_rootfs_repo_create \
767 "$container_rootfs/etc/yum.repos.d/public-yum-"$latestL""$container_release_major".repo" \
768 $latest_L""$container_release_major"_latest" \
769 $yum_url"/repo/OracleLinux/"$latest_U""$container_release_major"/latest/$basearch" gpg
772 # these distributions put the rpm database in a place the guest is
773 # not expecting it, so move it
774 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
775 mv $container_rootfs/$HOME/.rpmdb
/* $container_rootfs/var
/lib
/rpm
778 # if the native rpm created the db with Hash version 9, we need to
779 # downgrade it to Hash version 8 for use with OL5.x
780 db_version
=`file $container_rootfs/var/lib/rpm/Packages | \
781 grep -o 'version [0-9]*' |awk '{print $2}'`
782 if [ $container_release_major -lt "6" -a $db_version != "8" ]; then
783 echo "Fixing (downgrading) rpm database from version $db_version"
784 rm -f $container_rootfs/var
/lib
/rpm
/__db
*
785 for db
in $container_rootfs/var
/lib
/rpm
/* ; do
786 $db_dump_cmd $db |chroot
$container_rootfs db_load
/var
/lib
/rpm
/`basename $db`.new
791 # the host rpm may not be the same as the guest, rebuild the db with
792 # the guest rpm version
793 echo "Rebuilding rpm database"
794 rm -f $container_rootfs/var
/lib
/rpm
/__db
*
795 chroot
$container_rootfs rpm
--rebuilddb >/dev
/null
2>&1
797 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-oracle-
$name
798 if [ $?
-ne 0 ]; then
803 container_release_get
()
805 if [ -f $1/etc
/oracle-release
]; then
806 container_release_version
=`cat $1/etc/oracle-release |awk '/^Oracle/ {print $5}'`
807 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
808 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
809 elif grep -q "Enterprise Linux AS" $1/etc
/redhat-release
; then
810 container_release_major
=`cat $1/etc/redhat-release |awk '{print $7}'`
811 container_release_minor
=`cat $1/etc/redhat-release |awk '{print $10}' |tr -d ")"`
812 container_release_version
="$container_release_major.$container_release_minor"
813 elif grep -q "Enterprise Linux Server" $1/etc
/redhat-release
; then
814 container_release_version
=`cat $1/etc/redhat-release |awk '{print $7}'`
815 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
816 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
818 echo "Unable to determine container release version"
826 -a|--arch=<arch> architecture (ie. i386, x86_64)
827 -R|--release=<release> release to download for the new container
828 --rootfs=<path> rootfs path
829 -r|--rpms=<rpm name> additional rpms to install into container
830 -u|--url=<url> replace yum repo url (ie. Oracle public-yum mirror)
831 --baseurl=<url> use package repository (ie. file:///mnt)
832 arch and release must also be specified
833 -t|--templatefs=<path> copy/clone rootfs at path instead of downloading
834 -P|--patch=<path> only patch the rootfs at path for use as a container
837 Release is of the format "major.minor", for example "5.8", "6.3", or "6.latest"
838 This template supports Oracle Linux releases 4.6 - 7.0
843 options
=$
(getopt
-o hp
:n
:a
:R
:r
:u
:t
: -l help,rootfs
:,path
:,name
:,arch
:,release
:,rpms
:,url
:,templatefs
:,patch:,baseurl
: -- "$@")
844 if [ $?
-ne 0 ]; then
849 eval set -- "$options"
853 -h|
--help) usage
$0 && exit 0;;
854 -p|
--path) cfg_dir
=$2; shift 2;;
855 --rootfs) container_rootfs
=$2; shift 2;;
856 -n|
--name) name
=$2; shift 2;;
857 -a|
--arch) arch
=$2; shift 2;;
858 -R|
--release) container_release_version
=$2; shift 2;;
859 -r|
--rpms) user_pkgs
=$2; shift 2;;
860 -u|
--url) repourl
=$2; shift 2;;
861 -t|
--templatefs) template_rootfs
=$2; shift 2;;
862 --patch) patch_rootfs
=$2; shift 2;;
863 --baseurl) baseurl
=$2; shift 2;;
864 --) shift 1; break ;;
869 # make sure mandatory args are given and valid
870 if [ "$(id -u)" != "0" ]; then
871 echo "This script should be run as 'root'"
875 if [ -n "$baseurl" ]; then
876 if [ "$arch" = "" -o "$container_release_version" = "" ]; then
877 echo "The --arch and --release must be specified when using --baseurl"
883 if [ "$arch" = "" ]; then
887 if [ -n "$patch_rootfs" ]; then
888 container_rootfs
="$patch_rootfs"
889 container_release_get
$container_rootfs
890 container_rootfs_patch
894 if [ -z $name ]; then
895 echo "Container name must be given"
900 if [ -z $cfg_dir ]; then
901 echo "Configuration directory must be given, check lxc-create"
907 if [ "$arch" = "i686" ]; then
911 if [ "$arch" != "i386" -a "$arch" != "x86_64" ]; then
912 echo "Bad architecture given, check lxc-create"
917 if which lsb_release
>/dev
/null
2>&1; then
918 host_distribution
=`lsb_release --id |awk '{print $3}'`
919 host_release_version
=`lsb_release --release |awk '{print $2}'`
920 host_release_major
=`echo $host_release_version |awk -F '.' '{print $1}'`
921 host_release_minor
=`echo $host_release_version |awk -F '.' '{print $2}'`
923 if [ -f /etc
/fedora-release
]; then
924 host_distribution
="Fedora"
925 host_release_version
=`cat /etc/fedora-release |awk '{print $3}'`
926 host_release_major
=$host_release_version
928 elif [ -f /etc
/oracle-release
]; then
929 host_distribution
="OracleServer"
930 host_release_version
=`cat /etc/oracle-release |awk '{print $5}'`
931 host_release_major
=`echo $host_release_version |awk -F '.' '{print $1}'`
932 host_release_minor
=`echo $host_release_version |awk -F '.' '{print $2}'`
934 echo "Unable to determine host distribution, ensure lsb_release is installed"
938 echo "Host is $host_distribution $host_release_version"
940 if [ -z "$container_rootfs" ]; then
941 container_rootfs
="$cfg_dir/rootfs"
944 if [ -n "$template_rootfs" ]; then
945 container_release_get
$template_rootfs
947 if [ -z "$container_release_version" ]; then
948 if [ $host_distribution = "OracleServer" ]; then
949 container_release_version
=$host_release_version
951 echo "No release specified with -R, defaulting to 6.5"
952 container_release_version
="6.5"
955 container_release_major
=`echo $container_release_version |awk -F '.' '{print $1}'`
956 container_release_minor
=`echo $container_release_version |awk -F '.' '{print $2}'`
959 container_config_create
960 if [ -n "$template_rootfs" ]; then
961 container_rootfs_clone
963 container_rootfs_create
966 container_release_get
$container_rootfs
968 container_rootfs_configure
970 echo "Container : $container_rootfs"
971 echo "Config : $cfg_dir/config"
972 echo "Network : eth0 ($lxc_network_type) on $lxc_network_link"