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