]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oracle.in
Improve on the case where default networking config is incomplete
[mirror_lxc.git] / templates / lxc-oracle.in
1 #!/bin/sh
2 #
3 # Template script for generating Oracle Enterprise Linux container for LXC
4 # based on lxc-fedora, lxc-ubuntu
5 #
6 # Copyright © 2011 Wim Coekaerts <wim.coekaerts@oracle.com>
7 # Copyright © 2012 Dwight Engen <dwight.engen@oracle.com>
8 #
9 # Modified for Oracle Linux 5
10 # Wim Coekaerts <wim.coekaerts@oracle.com>
11 #
12 # Modified for Oracle Linux 6,7 combined OL4,5,6 into one template script
13 # Dwight Engen <dwight.engen@oracle.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #
29
30 # Detect use under userns (unsupported)
31 for arg in "$@"; do
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
36 exit 1
37 fi
38 done
39
40 # Make sure the usual locations are in PATH
41 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
42
43 die()
44 {
45 echo "failed: $1"
46 exit 1
47 }
48
49 is_btrfs_subvolume()
50 {
51 if which btrfs >/dev/null 2>&1 && \
52 btrfs subvolume list "$1" >/dev/null 2>&1; then
53 return 0
54 fi
55 return 1
56 }
57
58 can_chcon()
59 {
60 if which chcon >/dev/null 2>&1; then
61 selinuxenabled >/dev/null 2>&1
62 return $?
63 fi
64 return 1
65 }
66
67 # fix up the container_rootfs
68 container_rootfs_patch()
69 {
70 echo "Patching container rootfs $container_rootfs for Oracle Linux $container_release_major.$container_release_minor"
71
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
82 fi
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
86 fi
87 mkdir -p $container_rootfs/etc/yum/pluginconf.d
88 cat <<EOF > $container_rootfs/etc/yum/pluginconf.d/lxc-patch.conf
89 [main]
90 enabled=1
91 packages=dbus,initscripts,iptables,openssh-server,setup,selinux-policy,readahead,udev,util-linux,util-linux-ng
92 EOF
93 fi
94
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
98 # and fix it up here
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
103 fi
104 echo "plugins = 1" >>$container_rootfs/etc/yum.conf
105 fi
106
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.
110 #
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
122 else
123 mkdir -p $container_rootfs/etc/selinux
124 echo "SELINUX=disabled" >$container_rootfs/etc/selinux/config
125 fi
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
130
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
135
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
139 fi
140
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
145
146 # OL7 has systemd, no rc.sysinit
147 if [ $container_release_major = "7" ]; then
148 # from mhw in the fedora template: We do need to disable the
149 # "ConditionalPathExists=/dev/tty0" line or no gettys are started on
150 # the ttys in the container. Lets do it in an override copy of the
151 # service so it can still pass rpm verifies and not be automatically
152 # updated by a new systemd version.
153 sed -e 's/^ConditionPathExists=/#LXC ConditionPathExists=/' \
154 < $container_rootfs/usr/lib/systemd/system/getty\@.service \
155 > $container_rootfs/etc/systemd/system/getty\@.service
156 # Setup getty service on the 4 ttys we are going to allow in the
157 # default config. Number should match lxc.tty
158 ( cd $container_rootfs/etc/systemd/system/getty.target.wants
159 for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
160 # We only want to spawn a getty on /dev/console in lxc, libvirt-lxc
161 # symlinks /dev/console to /dev/tty1
162 sed -i '/Before=getty.target/a ConditionVirtualization=lxc' $container_rootfs/usr/lib/systemd/system/console-getty.service
163
164 # disable some systemd services, set default boot, sigpwr target
165 rm -f $container_rootfs/usr/lib/systemd/system/sysinit.target.wants/kmod-static-nodes.service
166 chroot $container_rootfs systemctl -q disable graphical.target
167 chroot $container_rootfs systemctl -q enable multi-user.target
168 if [ ! -e $container_rootfs/etc/systemd/system/sigpwr.target ]; then
169 chroot $container_rootfs ln -s /usr/lib/systemd/system/halt.target /etc/systemd/system/sigpwr.target
170 fi
171
172 # systemd in userns won't be able to set /proc/self/oom_score_adj which
173 # prevents the dbus service from starting
174 sed -i 's|^OOMScoreAdjust|#LXC OOMScoreAdjust|' $container_rootfs/usr/lib/systemd/system/dbus.service
175 return
176 fi
177
178 # silence error in checking for selinux
179 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc/rc.sysinit
180 sed -i 's|cat /proc/self/attr/current|cat /proc/self/attr/current 2>/dev/null|' $container_rootfs/etc/rc.d/rc.sysinit
181
182 # on ol4 pam_limits prevents logins when using user namespaces
183 if [ $container_release_major = "4" ]; then
184 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
185 fi
186
187 # avoid error in ol5 attempting to copy non-existent resolv.conf
188 if [ $container_release_major = "5" ]; then
189 sed -i 's|resolv.conf.predhclient|resolv.conf.predhclient 2>/dev/null|' $container_rootfs/sbin/dhclient-script
190 fi
191
192 # disable interactive ovmd asking questions
193 if [ -f $container_rootfs/etc/sysconfig/ovmd ]; then
194 sed -i 's|INITIAL_CONFIG=yes|INITIAL_CONFIG=no|' $container_rootfs/etc/sysconfig/ovmd
195 fi
196
197 # disable disabling of ipv4 forwarding and defrag on shutdown since
198 # we mount /proc/sys ro
199 if [ $container_release_major = "5" ]; then
200 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
201 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
202 fi
203
204 # disable ipv6 on ol6
205 rm -f $container_rootfs/etc/sysconfig/network-scripts/init.ipv6-global
206
207 # remove module stuff for iptables it just shows errors that are not
208 # relevant in a container
209 if [ -f "$container_rootfs/etc/sysconfig/iptables-config" ]; then
210 sed -i 's|IPTABLES_MODULES=".*|IPTABLES_MODULES=""|' $container_rootfs/etc/sysconfig/iptables-config
211 sed -i 's|IPTABLES_MODULES_UNLOAD=".*|IPTABLES_MODULES_UNLOAD="no"|' $container_rootfs/etc/sysconfig/iptables-config
212 fi
213
214 # disable readahead in the container
215 if [ $container_release_major = "6" -a -e $container_rootfs/etc/sysconfig/readahead ]; then
216 rm -f $container_rootfs/etc/init/readahead-collector.conf
217 rm -f $container_rootfs/etc/init/readahead-disable-services.conf
218 sed -i 's|READAHEAD="yes"|READAHEAD="no"|' $container_rootfs/etc/sysconfig/readahead
219 fi
220
221 if [ $container_release_major = "4" ]; then
222 # enable fastboot always
223 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc/rc.sysinit
224 sed -i 's|\[ -f /fastboot \]|/bin/true|' $container_rootfs/etc/rc.d/rc.sysinit
225
226 # dont attempt to set kernel parameters
227 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc/rc.sysinit
228 sed -i 's|action $"Configuring kernel parameters|# LXC action $"Configuring kernel parameters|' $container_rootfs/etc/rc.d/rc.sysinit
229 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc/init.d/network 2>/dev/null
230 sed -i 's|action $"Setting network parameters|# LXC action $"Setting network parameters|' $container_rootfs/etc/init.d/NetworkManager 2>/dev/null
231 fi
232
233 # no need to attempt to mount /
234 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc/rc.sysinit
235 sed -i 's|mount -f /$|# LXC mount -f /|' $container_rootfs/etc/rc.d/rc.sysinit
236 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc/rc.sysinit
237 sed -i 's|action \$"Remounting root filesystem|/bin/true # LXC action $"Remounting root filesystem|' $container_rootfs/etc/rc.d/rc.sysinit
238
239 # disable udev in the container
240 if [ $container_release_major = "4" ]; then
241 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc/rc.sysinit
242 sed -i 's|\[ -x /sbin/start_udev \]|# LXC no udev|' $container_rootfs/etc/rc.d/rc.sysinit
243 else
244 sed -i 's|.sbin.start_udev||' $container_rootfs/etc/rc.sysinit
245 sed -i 's|.sbin.start_udev||' $container_rootfs/etc/rc.d/rc.sysinit
246 fi
247
248 # disable nash raidautorun in the container since no /dev/md*
249 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
250 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc/rc.sysinit
251 sed -i 's|echo "raidautorun /dev/md0"|echo ""|' $container_rootfs/etc/rc.d/rc.sysinit
252 fi
253
254 # prevent rc.sysinit from attempting to loadkeys
255 if [ \( $container_release_major = "4" -o $container_release_major = "5" \) -a -e $container_rootfs/etc/sysconfig/keyboard ]; then
256 rm $container_rootfs/etc/sysconfig/keyboard
257 fi
258
259 # dont use the hwclock, it messes up the host's time
260 if [ $container_release_major = "4" ]; then
261 sed -i 's|runcmd $"Syncing hardware clock|# LXC no hwclock runcmd $"Syncing hardware clock|' $container_rootfs/etc/rc.d/init.d/halt
262 else
263 sed -i 's|\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc/rc.d/init.d/halt
264 fi
265 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc/rc.sysinit
266 sed -i 's|^\[ -x /sbin/hwclock|\[ 0 -eq 1|' $container_rootfs/etc/rc.d/rc.sysinit
267 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc/rc.sysinit
268 sed -i 's|^/sbin/hwclock|# LXC /sbin/nohwclock|' $container_rootfs/etc/rc.d/rc.sysinit
269
270 # dont start lvm
271 if [ $container_release_major -lt "6" -a -f $container_rootfs/sbin/lvm.static ]; then
272 mv $container_rootfs/sbin/lvm.static $container_rootfs/sbin/lvm.static.lxc-disabled
273 fi
274 if [ $container_release_major = "6" ]; then
275 touch $container_rootfs/.nolvm
276 fi
277
278 # fix assumptions that plymouth is available
279 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc/rc.sysinit
280 sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc/rc.d/rc.sysinit
281 rm -f $container_rootfs/etc/init/plymouth-shutdown.conf
282 rm -f $container_rootfs/etc/init/quit-plymouth.conf
283 rm -f $container_rootfs/etc/init/splash-manager.conf
284
285 # dont try to unmount /dev/lxc devices
286 sed -i 's|&& $1 !~ /^\\/dev\\/ram/|\&\& $2 !~ /^\\/dev\\/lxc/ \&\& $1 !~ /^\\/dev\\/ram/|' $container_rootfs/etc/init.d/halt
287
288 # don't try to unmount swap
289 sed -i 's|\[ -f /proc/swaps \]|# LXC [ -f /proc/swaps ]|' $container_rootfs/etc/init.d/halt
290
291 # sem_open(3) checks that /dev/shm is SHMFS_SUPER_MAGIC, so make sure to
292 # mount /dev/shm (normally done by dracut initrd) as tmpfs
293 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
294 grep -q "mount -t tmpfs tmpfs /dev/shm" $container_rootfs/etc/rc.sysinit
295 if [ $? -eq 1 ]; then
296 echo "mkdir -p /dev/shm && mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc/rc.sysinit
297 echo "mkdir -p /dev/shm && mount -t tmpfs tmpfs /dev/shm" >>$container_rootfs/etc/rc.d/rc.sysinit
298 fi
299 fi
300 if [ $container_release_major = "6" ]; then
301 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
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.d/rc.sysinit
303 fi
304
305 # there might be other services that are useless but the below set is a good start
306 # some of these might not exist in the image, so we silence chkconfig complaining
307 # about the service file not being found
308 for service in \
309 acpid apmd auditd autofs cpuspeed dund gpm haldaemon hidd \
310 ip6tables irqbalance iscsi iscsid isdn kdump kudzu \
311 lm_sensors lvm2-monitor mdmonitor microcode_ctl \
312 ntpd pcmcia postfix sendmail udev-post xfs ;
313 do
314 chroot $container_rootfs chkconfig 2>/dev/null $service off
315 done
316
317 for service in rsyslog ;
318 do
319 chroot $container_rootfs chkconfig 2>/dev/null $service on
320 done
321 }
322
323 container_rootfs_configure()
324 {
325 container_rootfs_patch
326 echo "Configuring container for Oracle Linux $container_release_major.$container_release_minor"
327
328 # configure the network to use dhcp. we set DHCP_HOSTNAME so the guest
329 # will report its name and be resolv'able by the hosts dnsmasq
330 cat <<EOF > $container_rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
331 DEVICE=eth0
332 BOOTPROTO=dhcp
333 ONBOOT=yes
334 HOSTNAME=$name
335 DHCP_HOSTNAME=\`hostname\`
336 NM_CONTROLLED=no
337 TYPE=Ethernet
338 EOF
339
340 # set the hostname
341 if [ $container_release_major -ge "7" ]; then
342 # systemd honors /etc/hostname
343 echo "$name" >$container_rootfs/etc/hostname
344 fi
345 cat <<EOF > $container_rootfs/etc/sysconfig/network
346 NETWORKING=yes
347 NETWORKING_IPV6=no
348 HOSTNAME=$name
349 EOF
350
351 # set minimal hosts
352 echo "127.0.0.1 localhost $name" > $container_rootfs/etc/hosts
353
354 # this file has to exist for libvirt/Virtual machine monitor to boot the container
355 touch $container_rootfs/etc/mtab
356
357 # setup console and tty[1-4] for login. note that /dev/console and
358 # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
359 # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
360 # lxc will maintain these links and bind mount ptys over /dev/lxc/*
361 # since lxc.devttydir is specified in the config.
362
363 # allow root login on console, tty[1-4], and pts/0 for libvirt
364 echo "# LXC (Linux Containers)" >>$container_rootfs/etc/securetty
365 echo "lxc/console" >>$container_rootfs/etc/securetty
366 for i in 1 2 3 4; do
367 echo "lxc/tty$i" >>$container_rootfs/etc/securetty
368 done
369 echo "# For libvirt/Virtual Machine Monitor" >>$container_rootfs/etc/securetty
370 for i in 0 1 2 3 4; do
371 echo "pts/$i" >>$container_rootfs/etc/securetty
372 done
373
374 # prevent mingetty from calling vhangup(2) since it fails with userns
375 if [ -f $container_rootfs/etc/init/tty.conf ]; then
376 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc/init/tty.conf
377 fi
378
379 # create maygetty which only spawns a getty on the console when running
380 # under lxc, not libvirt-lxc which symlinks /dev/console to the same pty
381 # as /dev/tty1
382 cat <<EOF >$container_rootfs/sbin/maygetty
383 #!/bin/sh
384 if [ "\$container" = "lxc" ]; then
385 exec /sbin/mingetty \$@
386 fi
387 exec sleep infinity
388 EOF
389 chmod 755 $container_rootfs/sbin/maygetty
390
391 # start a getty on /dev/console, /dev/tty[1-4]
392 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
393 sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc/inittab
394 sed -i '/1:2345:respawn/i cns:2345:respawn:/sbin/maygetty --nohangup --noclear console' $container_rootfs/etc/inittab
395 sed -i '/5:2345:respawn/d' $container_rootfs/etc/inittab
396 sed -i '/6:2345:respawn/d' $container_rootfs/etc/inittab
397 fi
398
399 if [ $container_release_major = "6" ]; then
400 cat <<EOF > $container_rootfs/etc/init/console.conf
401 # console - getty
402 #
403 # This service maintains a getty on the console from the point the system is
404 # started until it is shut down again.
405
406 start on stopped rc RUNLEVEL=[2345]
407 stop on runlevel [!2345]
408 env container
409
410 respawn
411 exec /sbin/maygetty --nohangup --noclear /dev/console
412 EOF
413 fi
414
415 # lxc-shutdown sends SIGPWR to init, OL4 and OL5 have SysVInit, just
416 # make it do shutdown now instead of delaying 2 minutes. OL6 uses
417 # upstart, so we create an upstart job to handle SIGPWR to shut down
418 # cleanly. We use "init 0" instead of shutdown -h now to avoid SELinux
419 # permission denied when upstart's shutdown tries to connect to the
420 # /com/ubuntu/upstart socket.
421 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
422 sed -i 's|pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; |pf::powerfail:/sbin/shutdown -f -h now "|' $container_rootfs/etc/inittab
423 elif [ $container_release_major = "6" ]; then
424 cat <<EOF > $container_rootfs/etc/init/power-status-changed.conf
425 # power-status-changed - used to cleanly shut down the container
426 #
427 # This task is run whenever init receives SIGPWR
428 # Used to shut down the machine.
429
430 start on power-status-changed
431
432 exec init 0
433 EOF
434 fi
435
436 # start with a clean /var/log/messages
437 rm -f $container_rootfs/var/log/messages
438
439 # set initial timezone as on host
440 if [ -f /etc/sysconfig/clock ]; then
441 . /etc/sysconfig/clock
442 if [ $container_release_major = "5" -o $container_release_major = "6" ]; then
443 echo ZONE=$ZONE > $container_rootfs/etc/sysconfig/clock
444 chroot $container_rootfs tzdata-update
445 else
446 ZONE="${ZONE// /_}"
447 chroot $container_rootfs ln -sf ../usr/share/zoneinfo/$ZONE /etc/localtime
448 fi
449 else
450 ZONE=`readlink /etc/localtime | sed -s "s/\.\.\/usr\/share\/zoneinfo\///g"`
451 if [ "$ZONE" ]; then
452 if [ $container_release_major = "5" -o $container_release_major = "6" ]; then
453 echo ZONE=$ZONE > $container_rootfs/etc/sysconfig/clock
454 chroot $container_rootfs tzdata-update
455 else
456 # if /etc/localtime is a symlink, this should preserve it.
457 cp -a /etc/localtime $container_rootfs/etc/localtime
458 fi
459 else
460 echo "Timezone in container is not configured. Adjust it manually."
461 fi
462 fi
463
464 # add oracle user, set root password
465 chroot $container_rootfs useradd -m -s /bin/bash oracle
466 echo "oracle:oracle" | chroot $container_rootfs chpasswd
467 echo "root:root" | chroot $container_rootfs chpasswd
468 printf "Added container user:\033[1moracle\033[0m password:\033[1moracle\033[0m\n"
469 printf "Added container user:\033[1mroot\033[0m password:\033[1mroot\033[0m\n"
470 }
471
472 # create the container's lxc config file
473 container_config_create()
474 {
475 echo "Create configuration file $cfg_dir/config"
476 mkdir -p $cfg_dir || die "unable to create config dir $cfg_dir"
477
478 echo "# Common configuration" >> $cfg_dir/config
479 if [ -e "@LXCTEMPLATECONFIG@/oracle.common.conf" ]; then
480 echo "lxc.include = @LXCTEMPLATECONFIG@/oracle.common.conf" >> $cfg_dir/config
481 fi
482
483 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
484 # Container configuration for Oracle Linux $container_release_major.$container_release_minor
485 lxc.arch = $arch
486 lxc.utsname = $name
487 EOF
488 grep -q "^lxc.rootfs" $cfg_dir/config 2>/dev/null || echo "lxc.rootfs = $container_rootfs" >> $cfg_dir/config
489
490 if [ $container_release_major != "4" ]; then
491 echo "lxc.cap.drop = sys_resource" >>$cfg_dir/config
492 fi
493
494 # systemd services like logind and journald need these
495 if [ $container_release_major != "7" ]; then
496 echo "lxc.cap.drop = setfcap setpcap" >>$cfg_dir/config
497 fi
498
499 echo "# Networking" >>$cfg_dir/config
500 # see if the default network settings were already specified
501 lxc_network_type=`grep '^lxc.network.type' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
502 if [ -z "$lxc_network_type" ]; then
503 echo "lxc.network.type = veth" >>$cfg_dir/config
504 lxc_network_type=veth
505 fi
506
507 lxc_network_link=`grep '^lxc.network.link' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
508 if [ -z "$lxc_network_link" ]; then
509 echo "lxc.network.link = lxcbr0" >>$cfg_dir/config
510 lxc_network_link=lxcbr0
511 fi
512
513 lxc_network_hwaddr=`grep '^lxc.network.hwaddr' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
514 if [ -z "$lxc_network_hwaddr" ]; then
515 # generate a hwaddr for the container
516 # see http://sourceforge.net/tracker/?func=detail&aid=3411497&group_id=163076&atid=826303
517 local hwaddr="00:16:3e:`dd if=/dev/urandom bs=8 count=1 2>/dev/null |od -t x8 | \
518 head -n1 | awk '{print $2}' | cut -c1-6 | \
519 sed 's/\(..\)/\1:/g; s/.$//'`"
520 echo "lxc.network.hwaddr = $hwaddr" >>$cfg_dir/config
521 fi
522
523 lxc_network_flags=`grep '^lxc.network.flags' $cfg_dir/config | awk -F'[= \t]+' '{ print $2 }'`
524 if [ -z "$lxc_network_flags" ]; then
525 echo "lxc.network.flags = up" >>$cfg_dir/config
526 fi
527
528 cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
529 lxc.network.name = eth0
530 lxc.network.mtu = 1500
531 EOF
532 }
533
534 container_rootfs_clone()
535 {
536 if is_btrfs_subvolume $template_rootfs; then
537 # lxc-create already made $container_rootfs a btrfs subvolume, but
538 # in this case we want to snapshot the original subvolume so we we
539 # have to delete the one that lxc-create made
540 btrfs subvolume delete $container_rootfs
541 btrfs subvolume snapshot $template_rootfs $container_rootfs || die "btrfs clone template"
542 else
543 echo "Copying rootfs ..."
544 cp -axT $template_rootfs $container_rootfs || die "copy template"
545 fi
546 }
547
548 container_rootfs_repo_create()
549 {
550 echo "# LXC generated .repo file" >$1
551 echo "[$2]" >>$1
552 echo "name=Oracle Linux $container_release_major.$container_release_minor ($basearch)" >>$1
553 echo "baseurl=$3/" >>$1
554 echo "enabled=1" >>$1
555 echo "skip_if_unavailable=1" >>$1
556
557 if [ "$4" != "" ]; then
558 echo "gpgkey=$yum_url/RPM-GPG-KEY-oracle-ol$container_release_major" >>$1
559 echo "gpgcheck=1" >>$1
560 else
561 echo "gpgcheck=0" >>$1
562 fi
563 }
564
565 container_rootfs_dev_create()
566 {
567 # create required devices. note that /dev/console will be created by lxc
568 # or libvirt itself to be a symlink to the right pty.
569 # take care to not nuke /dev in case $container_rootfs isn't set
570 dev_path="$container_rootfs/dev"
571 if [ $container_rootfs != "/" -a -d $dev_path ]; then
572 rm -rf $dev_path
573 fi
574 mkdir -p $dev_path
575 if can_chcon; then
576 # ensure symlinks created in /dev have the right context
577 chcon -t device_t $dev_path
578 fi
579 mknod -m 666 $dev_path/null c 1 3
580 mknod -m 666 $dev_path/zero c 1 5
581 mknod -m 666 $dev_path/random c 1 8
582 mknod -m 666 $dev_path/urandom c 1 9
583 mkdir -m 755 $dev_path/pts
584 mkdir -m 1777 $dev_path/shm
585 mknod -m 666 $dev_path/tty c 5 0
586 mknod -m 666 $dev_path/tty1 c 4 1
587 mknod -m 666 $dev_path/tty2 c 4 2
588 mknod -m 666 $dev_path/tty3 c 4 3
589 mknod -m 666 $dev_path/tty4 c 4 4
590 mknod -m 666 $dev_path/full c 1 7
591 mknod -m 600 $dev_path/initctl p
592
593 # set selinux labels same as host
594 if can_chcon; then
595 for node in null zero random urandom pts shm \
596 tty tty0 tty1 tty2 tty3 tty4 full ;
597 do
598 chcon --reference /dev/$node $dev_path/$node 2>/dev/null
599 done
600 fi
601 }
602
603 container_rootfs_create()
604 {
605 if can_chcon; then
606 chcon --reference / $container_rootfs 2>/dev/null
607 fi
608
609 cmds="rpm wget yum"
610 if [ $container_release_major -lt "6" ]; then
611 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
612 db_dump_cmd="db5.1_dump"
613 fi
614 if [ $host_distribution = "OracleServer" -o \
615 $host_distribution = "Fedora" ]; then
616 db_dump_cmd="db_dump"
617 fi
618
619 cmds="$cmds $db_dump_cmd file"
620 fi
621 for cmd in $cmds; do
622 which $cmd >/dev/null 2>&1
623 if [ $? -ne 0 ]; then
624 die "The $cmd command is required, please install it"
625 fi
626 done
627
628 mkdir -p @LOCALSTATEDIR@/lock/subsys
629 (
630 flock -x 9
631 if [ $? -ne 0 ]; then
632 die "The template is busy."
633 fi
634
635 echo "Yum installing release $container_release_major.$container_release_minor for $basearch"
636
637 if [ -n "$repourl" ]; then
638 yum_url=$repourl
639 else
640 yum_url=http://public-yum.oracle.com
641 fi
642 if [ $container_release_major = "4" -o $container_release_major = "5" ]; then
643 latest_L="el"
644 latest_U="EL"
645 else
646 latest_L="ol"
647 latest_U="OL"
648 fi
649
650 if [ -n "$baseurl" ]; then
651 # create .repo pointing at baseurl
652 repo="lxc-install"
653 mkdir -p $container_rootfs/etc/yum.repos.d
654 container_rootfs_repo_create \
655 $container_rootfs/etc/yum.repos.d/lxc-install.repo $repo $baseurl
656 else
657 # get public-yum repo file
658 if [ $container_release_major = "4" ]; then
659 repofile=public-yum-el4.repo
660 elif [ $container_release_major = "5" ]; then
661 repofile=public-yum-el5.repo
662 elif [ $container_release_major = "6" ]; then
663 repofile=public-yum-ol6.repo
664 elif [ $container_release_major = "7" ]; then
665 repofile=public-yum-ol7.repo
666 else
667 die "Unsupported release $container_release_major"
668 fi
669
670 mkdir -p $container_rootfs/etc/yum.repos.d
671 wget -q $yum_url/$repofile -O $container_rootfs/etc/yum.repos.d/$repofile
672 if [ $? -ne 0 ]; then
673 die "Unable to download repo file $yum_url/$repofile, release unavailable"
674 fi
675
676 # yum will take $basearch from host, so force the arch we want
677 sed -i "s|\$basearch|$basearch|" $container_rootfs/etc/yum.repos.d/$repofile
678
679 # replace url if they specified one
680 if [ -n "$repourl" ]; then
681 sed -i "s|baseurl=http://public-yum.oracle.com/repo|baseurl=$repourl/repo|" $container_rootfs/etc/yum.repos.d/$repofile
682 sed -i "s|gpgkey=http://public-yum.oracle.com|gpgkey=$repourl|" $container_rootfs/etc/yum.repos.d/$repofile
683 fi
684
685 # disable all repos, then enable the repo for the version we are installing.
686 if [ $container_release_minor = "latest" ]; then
687 repo=$latest_L""$container_release_major"_"$container_release_minor
688 elif [ $container_release_major = "7" ]; then
689 repo="ol"$container_release_major"_u"$container_release_minor"_base"
690 elif [ $container_release_major = "6" ]; then
691 if [ $container_release_minor = "0" ]; then
692 repo="ol"$container_release_major"_ga_base"
693 else
694 repo="ol"$container_release_major"_u"$container_release_minor"_base"
695 fi
696 elif [ $container_release_major = "5" ]; then
697 if [ $container_release_minor = "0" ]; then
698 repo="el"$container_release_major"_ga_base"
699 elif [ $container_release_minor -lt "6" ]; then
700 repo="el"$container_release_major"_u"$container_release_minor"_base"
701 else
702 repo="ol"$container_release_major"_u"$container_release_minor"_base"
703 fi
704 elif [ $container_release_major = "4" -a $container_release_minor -gt "5" ]; then
705 repo="el"$container_release_major"_u"$container_release_minor"_base"
706 else
707 die "Unsupported release $container_release_major.$container_release_minor"
708 fi
709 sed -i "s|enabled=1|enabled=0|" $container_rootfs/etc/yum.repos.d/$repofile
710 sed -i "/\[$repo\]/,/\[/ s/enabled=0/enabled=1/" $container_rootfs/etc/yum.repos.d/$repofile
711 fi
712
713 container_rootfs_dev_create
714
715 # don't put devpts,proc, nor sysfs in here, it will already be mounted for us by lxc/libvirt
716 echo "" >$container_rootfs/etc/fstab
717
718 # create rpm db, download and yum install minimal packages
719 mkdir -p $container_rootfs/var/lib/rpm
720 rpm --root $container_rootfs --initdb
721 yum_args="--installroot $container_rootfs --disablerepo=* --enablerepo=$repo -y --nogpgcheck"
722 min_pkgs="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils oraclelinux-release"
723 if [ $container_release_major -lt "6" ]; then
724 min_pkgs="$min_pkgs db4-utils"
725 fi
726
727 # we unshare the mount namespace because yum installing the ol4
728 # packages causes $rootfs/proc to be mounted on
729 lxc-unshare -s MOUNT yum -- $yum_args install $min_pkgs $user_pkgs
730 if [ $? -ne 0 ]; then
731 die "Failed to download and install the rootfs, aborting."
732 fi
733
734 # rsyslog and pam depend on coreutils for some common commands in
735 # their POSTIN scriptlets, but coreutils wasn't installed yet. now
736 # that coreutils is installed, reinstall the packages so their POSTIN
737 # runs right. similarly, libutempter depends on libselinux.so.1 when
738 # it runs /usr/sbin/groupadd, so reinstall it too
739 redo_pkgs=""
740 if [ $container_release_major = "5" ]; then
741 if [ $container_release_minor = "latest" ]; then
742 redo_pkgs="pam rsyslog libutempter"
743 elif [ $container_release_minor -lt 2 ]; then
744 redo_pkgs="pam"
745 elif [ $container_release_minor -lt 6 ]; then
746 redo_pkgs="pam rsyslog"
747 elif [ $container_release_minor -gt 5 ]; then
748 redo_pkgs="pam rsyslog libutempter"
749 fi
750 fi
751 # shadow utils fails on ol4 and ol6.1
752 if [ $container_release_major = "4" -o \
753 $container_release_major = "6" -a $container_release_minor = "1" ]; then
754 redo_pkgs="shadow-utils"
755 fi
756 if [ x"$redo_pkgs" != x ]; then
757 rpm --root $container_rootfs --nodeps -e $redo_pkgs
758 lxc-unshare -s MOUNT yum -- $yum_args install $redo_pkgs
759 if [ $? -ne 0 ]; then
760 die "Unable to reinstall packages"
761 fi
762 fi
763
764 # if installing from a baseurl, create a .repo that the container
765 # can use to update to _latest from http://public-yum.oracle.com
766 if [ -n "$baseurl" ]; then
767 container_rootfs_repo_create \
768 "$container_rootfs/etc/yum.repos.d/public-yum-"$latestL""$container_release_major".repo" \
769 $latest_L""$container_release_major"_latest" \
770 $yum_url"/repo/OracleLinux/"$latest_U""$container_release_major"/latest/$basearch" gpg
771 fi
772
773 # these distributions put the rpm database in a place the guest is
774 # not expecting it, so move it
775 if [ $host_distribution = "Ubuntu" -o $host_distribution = "Debian" ]; then
776 mv $container_rootfs/$HOME/.rpmdb/* $container_rootfs/var/lib/rpm
777 fi
778
779 # if the native rpm created the db with Hash version 9, we need to
780 # downgrade it to Hash version 8 for use with OL5.x
781 db_version=`file $container_rootfs/var/lib/rpm/Packages | \
782 grep -o 'version [0-9]*' |awk '{print $2}'`
783 if [ $container_release_major -lt "6" -a $db_version != "8" ]; then
784 echo "Fixing (downgrading) rpm database from version $db_version"
785 rm -f $container_rootfs/var/lib/rpm/__db*
786 for db in $container_rootfs/var/lib/rpm/* ; do
787 $db_dump_cmd $db |chroot $container_rootfs db_load /var/lib/rpm/`basename $db`.new
788 mv $db.new $db
789 done
790 fi
791
792 # the host rpm may not be the same as the guest, rebuild the db with
793 # the guest rpm version
794 echo "Rebuilding rpm database"
795 rm -f $container_rootfs/var/lib/rpm/__db*
796 chroot $container_rootfs rpm --rebuilddb >/dev/null 2>&1
797
798 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-oracle-$name
799 if [ $? -ne 0 ]; then
800 exit 1
801 fi
802 }
803
804 container_release_get()
805 {
806 if [ -f $1/etc/oracle-release ]; then
807 container_release_version=`cat $1/etc/oracle-release |awk '/^Oracle/ {print $5}'`
808 container_release_major=`echo $container_release_version |awk -F '.' '{print $1}'`
809 container_release_minor=`echo $container_release_version |awk -F '.' '{print $2}'`
810 elif grep -q "Enterprise Linux AS" $1/etc/redhat-release; then
811 container_release_major=`cat $1/etc/redhat-release |awk '{print $7}'`
812 container_release_minor=`cat $1/etc/redhat-release |awk '{print $10}' |tr -d ")"`
813 container_release_version="$container_release_major.$container_release_minor"
814 elif grep -q "Enterprise Linux Server" $1/etc/redhat-release; then
815 container_release_version=`cat $1/etc/redhat-release |awk '{print $7}'`
816 container_release_major=`echo $container_release_version |awk -F '.' '{print $1}'`
817 container_release_minor=`echo $container_release_version |awk -F '.' '{print $2}'`
818 else
819 echo "Unable to determine container release version"
820 exit 1
821 fi
822 }
823
824 usage()
825 {
826 cat <<EOF
827 -a|--arch=<arch> architecture (ie. i386, x86_64)
828 -R|--release=<release> release to download for the new container
829 --rootfs=<path> rootfs path
830 -r|--rpms=<rpm name> additional rpms to install into container
831 -u|--url=<url> replace yum repo url (ie. Oracle public-yum mirror)
832 --baseurl=<url> use package repository (ie. file:///mnt)
833 arch and release must also be specified
834 -t|--templatefs=<path> copy/clone rootfs at path instead of downloading
835 -P|--patch=<path> only patch the rootfs at path for use as a container
836 -h|--help
837
838 Release is of the format "major.minor", for example "5.8", "6.3", or "6.latest"
839 This template supports Oracle Linux releases 4.6 - 7.0
840 EOF
841 return 0
842 }
843
844 options=$(getopt -o hp:n:a:R:r:u:t: -l help,rootfs:,path:,name:,arch:,release:,rpms:,url:,templatefs:,patch:,baseurl: -- "$@")
845 if [ $? -ne 0 ]; then
846 usage $(basename $0)
847 exit 1
848 fi
849
850 eval set -- "$options"
851 while true
852 do
853 case "$1" in
854 -h|--help) usage $0 && exit 0;;
855 -p|--path) cfg_dir=$2; shift 2;;
856 --rootfs) container_rootfs=$2; shift 2;;
857 -n|--name) name=$2; shift 2;;
858 -a|--arch) arch=$2; shift 2;;
859 -R|--release) container_release_version=$2; shift 2;;
860 -r|--rpms) user_pkgs=$2; shift 2;;
861 -u|--url) repourl=$2; shift 2;;
862 -t|--templatefs) template_rootfs=$2; shift 2;;
863 --patch) patch_rootfs=$2; shift 2;;
864 --baseurl) baseurl=$2; shift 2;;
865 --) shift 1; break ;;
866 *) break ;;
867 esac
868 done
869
870 # make sure mandatory args are given and valid
871 if [ "$(id -u)" != "0" ]; then
872 echo "This script should be run as 'root'"
873 exit 1
874 fi
875
876 if [ -n "$baseurl" ]; then
877 if [ "$arch" = "" -o "$container_release_version" = "" ]; then
878 echo "The --arch and --release must be specified when using --baseurl"
879 usage
880 exit 1
881 fi
882 fi
883
884 if [ "$arch" = "" ]; then
885 arch=$(uname -m)
886 fi
887
888 if [ -n "$patch_rootfs" ]; then
889 container_rootfs="$patch_rootfs"
890 container_release_get $container_rootfs
891 container_rootfs_patch
892 exit 0
893 fi
894
895 if [ -z $name ]; then
896 echo "Container name must be given"
897 usage
898 exit 1
899 fi
900
901 if [ -z $cfg_dir ]; then
902 echo "Configuration directory must be given, check lxc-create"
903 usage
904 exit 1
905 fi
906
907 basearch=$arch
908 if [ "$arch" = "i686" ]; then
909 basearch="i386"
910 fi
911
912 if [ "$arch" != "i386" -a "$arch" != "x86_64" ]; then
913 echo "Bad architecture given, check lxc-create"
914 usage
915 exit 1
916 fi
917
918 if which lsb_release >/dev/null 2>&1; then
919 host_distribution=`lsb_release --id |awk '{print $3}'`
920 host_release_version=`lsb_release --release |awk '{print $2}'`
921 host_release_major=`echo $host_release_version |awk -F '.' '{print $1}'`
922 host_release_minor=`echo $host_release_version |awk -F '.' '{print $2}'`
923 else
924 if [ -f /etc/fedora-release ]; then
925 host_distribution="Fedora"
926 host_release_version=`cat /etc/fedora-release |awk '{print $3}'`
927 host_release_major=$host_release_version
928 host_release_minor=0
929 elif [ -f /etc/oracle-release ]; then
930 host_distribution="OracleServer"
931 host_release_version=`cat /etc/oracle-release |awk '{print $5}'`
932 host_release_major=`echo $host_release_version |awk -F '.' '{print $1}'`
933 host_release_minor=`echo $host_release_version |awk -F '.' '{print $2}'`
934 else
935 echo "Unable to determine host distribution, ensure lsb_release is installed"
936 exit 1
937 fi
938 fi
939 echo "Host is $host_distribution $host_release_version"
940
941 if [ -z "$container_rootfs" ]; then
942 container_rootfs="$cfg_dir/rootfs"
943 fi
944
945 if [ -n "$template_rootfs" ]; then
946 container_release_get $template_rootfs
947 else
948 if [ -z "$container_release_version" ]; then
949 if [ $host_distribution = "OracleServer" ]; then
950 container_release_version=$host_release_version
951 else
952 echo "No release specified with -R, defaulting to 6.5"
953 container_release_version="6.5"
954 fi
955 fi
956 container_release_major=`echo $container_release_version |awk -F '.' '{print $1}'`
957 container_release_minor=`echo $container_release_version |awk -F '.' '{print $2}'`
958 fi
959
960 container_config_create
961 if [ -n "$template_rootfs" ]; then
962 container_rootfs_clone
963 else
964 container_rootfs_create
965 fi
966
967 container_release_get $container_rootfs
968
969 container_rootfs_configure
970
971 echo "Container : $container_rootfs"
972 echo "Config : $cfg_dir/config"
973 echo "Network : eth0 ($lxc_network_type) on $lxc_network_link"