4 # description: Create BTRFS subvolumes for LXC reference images.
6 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
7 # Copyright (C) 2012 INRIA
10 # not needed -- Source function library
11 #. /etc/init.d/functions
13 # a f>=18 root context won't have /bin nor /sbin in its path, which is where
14 # chkconfig is located on f<=14 images
15 export PATH=$PATH:/bin:/sbin
17 # This is where sliceimage(s) store their reference images
18 sliceimage_dir=/vservers/.vref
19 sliceimage_stubs=/vservers/.vstub
20 lxc_dir=/vservers/.lvref
21 tmp_dir=/vservers/.ltmp
23 # Check if we are in the build environment
24 function check_node () {
25 mount | grep -q 'planetlab-vservers' || exit 0
26 [ -d $sliceimage_dir ] || { echo "No sliceimage installed" ; exit 1; }
35 slicefamilies=$(cd $sliceimage_dir ; ls )
37 for slicefamily in $slicefamilies; do
38 # initialize if needed
39 [ -d $lxc_dir/$slicefamily ] || btrfs subvolume create $lxc_dir/$slicefamily
41 #btrfs subvolume create $lxc_dir/lxc-squeeze-x86_64
42 # copy the slice image into the btrfs ?
43 rsync -a --delete $sliceimage_dir/$slicefamily/ $lxc_dir/$slicefamily/
44 # tmp -- should very probably be done at build time
45 patch_lvref $lxc_dir/$slicefamily
48 # create ref images from stubs
53 echo -n "Checking node .. "
56 echo "From installed sliceimage variants"
58 echo "Exported to lxc"
62 # inspired from https://gist.github.com/1142202
63 ETC=/etc/systemd/system
64 LIB=/lib/systemd/system
65 function patch_lvref () {
66 # this applies to systemd only
67 [ -d $rootfs/$LIB ] || return
69 # sliceimage comes with graphical.target as default
70 chroot $rootfs ln -sf $LIB/multi-user.target $ETC/default.target
71 # sysinit.target seems to stick on boot, so disable it. However, we need
72 # systemd-tmpfiles-setup.service that was started by the dependency of
73 # sysinit.target to boot up correctly, so start it instead.
74 chroot $rootfs cp $LIB/basic.target $ETC/basic.target
75 chroot $rootfs sed -i 's/sysinit.target/systemd-tmpfiles-setup.service/' $ETC/basic.target
76 # Stop starting sysinit.target. Symlinking one to /dev/null is a standard way
77 # to disable a target (or a service and others).
78 chroot $rootfs ln -s /dev/null $ETC/sysinit.target
79 # It also a cause of stuck on boot
80 chroot $rootfs ln -s /dev/null $ETC/udev-settle.service
81 # It prevents systemd-tmpfiles-setup.service from starting
82 chroot $rootfs ln -s /dev/null $ETC/fedora-readonly.service
83 # Libvirt lxc provides only tty1
84 chroot $rootfs rm -f $ETC/getty.target.wants/getty\@tty{2,3,4,5,6}.service
85 # It launches sulogin on console(tty1) but it interferes getty@tty1
86 chroot $rootfs ln -s /dev/null $ETC/console-shell.service
87 # Workarounds for libvirt 0.9.4. Without this, getty@tty1 doen't launch
88 # because a trigger event on tty1 doesn't happen.
89 chroot $rootfs cp $LIB/getty\@.service $ETC/getty\@.service
90 chroot $rootfs sed -i 's/^BindTo/\#&/' $ETC/getty\@.service
91 chroot $rootfs ln -sf $ETC/getty\@.service $ETC/getty.target.wants/getty\@tty1.service
92 # Allow a user who logins via ssh to sudo
93 chroot $rootfs sed -i 's/^Defaults\ *requiretty/\#&/' /etc/sudoers
94 # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd
95 # which cannot run inside a container.
96 chroot $rootfs sed -i 's/^.*loginuid.so.*$/\#&/' /etc/pam.d/login
97 # Enable eth0 on bootup
98 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
103 # Tweak sshd configuration
104 chroot $rootfs sed -i 's/^UsePAM\ *yes/\#&/' /etc/ssh/sshd_config
105 # commenting out the defaults in the file is not enough, need to explicitly set these to no
106 chroot $rootfs sed -i 's/^GSSAPIAuthentication.*$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
107 chroot $rootfs sed -i 's/^PasswordAuthentication.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
108 # Allow root to login at virsh console
109 echo "pts/0" >> $rootfs/etc/securetty
110 # our image does not have NetworkManager, only network, and it is off by default
111 chroot $rootfs chkconfig network on
114 function unfold_system_slices () {
115 for clonedstamp in $sliceimage_stubs/*/*.cloned; do
116 unfold_system_slice_from_cloned $clonedstamp
120 function unfold_system_slice_from_cloned () {
121 clonedstamp=$1; shift
122 # e.g. NAME=planetflow
123 NAME=$(basename $clonedstamp .cloned)
124 DIR=$(dirname $clonedstamp)
125 # e.g. SLICEFAMILY=planetlab-f8-i386
126 SLICEFAMILY=$(cat $clonedstamp)
127 # deduce the actual name used in .vref by replacing the first part of slice-family
128 # (pldistro) with the slice name
129 # e.g. VREFNAME=planetflow-f8-i386
130 VREFNAME=$(echo $SLICEFAMILY | sed -e "s,^[^-]*-,$NAME-,")
132 VREFPATH="$lxc_dir/$VREFNAME"
134 # do not redo existing vref's unless force is mentioned
135 [ -z "$FORCE" -a -d "$VREFPATH" ] && continue
136 [ -z "$FORCE" ] && message=Building
137 [ -n "$FORCE" ] && message=Force-building
139 # Copy base reference image
140 echo -n $"$message slice image for $NAME in $VREFNAME: "
142 # build the systemslice from the one it was originally cloned from
143 FAMILYREF="$lxc_dir/$SLICEFAMILY"
144 if [ ! -d "$FAMILYREF" ] ; then
145 echo system slice from $clonedstamp - could not find reference $FAMILYREF - skipped
151 # cleanup reference image if already exists
152 [ -d "$VREFPATH" ] && btrfs subvolume delete "$VREFPATH"
154 [ -d "$VREFPATH" ] && rm -rf "$VREFPATH"
157 btrfs subvolume snapshot $FAMILYREF $VREFPATH
159 # merge the stub with the reference to get the system slice
160 (cd "$DIR/$NAME"/ && find . | cpio -m -d -u -p "$VREFPATH"/) || \
161 { echo "Could not apply stub $DIR/$NAME - skipping $clonedstamp" ; btrfs subvolume delete "$VREFPATH" ; return ; }
164 rm -f "$VREFPATH/var/lib/rpm/__db"*
170 start|restart|reload) start ; exit 0 ;;
171 status) status ; exit 0 ;;
173 *) echo $"Usage: $0 {start|stop|status}"