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 # This is where sliceimage(s) store their reference images
14 sliceimage_dir=/vservers/.vref
15 sliceimage_stubs=/vservers/.vstub
16 lxc_dir=/vservers/.lvref
17 tmp_dir=/vservers/.ltmp
19 # Check if we are in the build environment
20 function check_node () {
21 mount | grep -q 'planetlab-vservers' || exit 0
22 [ -d $sliceimage_dir ] || { echo "No sliceimage installed" ; exit 1; }
31 slicefamilies=$(cd $sliceimage_dir ; ls )
33 for slicefamily in $slicefamilies; do
34 # initialize if needed
35 [ -d $lxc_dir/$slicefamily ] || btrfs subvolume create $lxc_dir/$slicefamily
37 #btrfs subvolume create $lxc_dir/lxc-squeeze-x86_64
38 # copy the slice image into the btrfs ?
39 rsync -a --delete $sliceimage_dir/$slicefamily/ $lxc_dir/$slicefamily/
40 # tmp -- should very probably be done at build time
41 patch_lvref $lxc_dir/$slicefamily
44 # create ref images from stubs
49 echo -n "Checking node .. "
52 echo "From installed sliceimage variants"
54 echo "Exported to lxc"
58 # inspired from https://gist.github.com/1142202
59 ETC=/etc/systemd/system
60 LIB=/lib/systemd/system
61 function patch_lvref () {
62 # this applies to systemd only
63 [ -d $rootfs/$LIB ] || return
65 # sliceimage comes with graphical.target as default
66 chroot $rootfs ln -sf $LIB/multi-user.target $ETC/default.target
67 # sysinit.target seems to stick on boot, so disable it. However, we need
68 # systemd-tmpfiles-setup.service that was started by the dependency of
69 # sysinit.target to boot up correctly, so start it instead.
70 chroot $rootfs cp $LIB/basic.target $ETC/basic.target
71 chroot $rootfs sed -i 's/sysinit.target/systemd-tmpfiles-setup.service/' $ETC/basic.target
72 # Stop starting sysinit.target. Symlinking one to /dev/null is a standard way
73 # to disable a target (or a service and others).
74 chroot $rootfs ln -s /dev/null $ETC/sysinit.target
75 # It also a cause of stuck on boot
76 chroot $rootfs ln -s /dev/null $ETC/udev-settle.service
77 # It prevents systemd-tmpfiles-setup.service from starting
78 chroot $rootfs ln -s /dev/null $ETC/fedora-readonly.service
79 # Libvirt lxc provides only tty1
80 chroot $rootfs rm -f $ETC/getty.target.wants/getty\@tty{2,3,4,5,6}.service
81 # It launches sulogin on console(tty1) but it interferes getty@tty1
82 chroot $rootfs ln -s /dev/null $ETC/console-shell.service
83 # Workarounds for libvirt 0.9.4. Without this, getty@tty1 doen't launch
84 # because a trigger event on tty1 doesn't happen.
85 chroot $rootfs cp $LIB/getty\@.service $ETC/getty\@.service
86 chroot $rootfs sed -i 's/^BindTo/\#&/' $ETC/getty\@.service
87 chroot $rootfs ln -sf $ETC/getty\@.service $ETC/getty.target.wants/getty\@tty1.service
88 # Allow a user who logins via ssh to sudo
89 chroot $rootfs sed -i 's/^Defaults\ *requiretty/\#&/' /etc/sudoers
90 # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd
91 # which cannot run inside a container.
92 chroot $rootfs sed -i 's/^.*loginuid.so.*$/\#&/' /etc/pam.d/login
93 # Enable eth0 on bootup
94 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
99 # Tweak sshd configuration
100 chroot $rootfs sed -i 's/^UsePAM\ *yes/\#&/' /etc/ssh/sshd_config
101 # commenting out the defaults in the file is not enough, need to explicitly set these to no
102 chroot $rootfs sed -i 's/^GSSAPIAuthentication.*$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
103 chroot $rootfs sed -i 's/^PasswordAuthentication.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
104 # Allow root to login at virsh console
105 echo "pts/0" >> $rootfs/etc/securetty
106 # our image does not have NetworkManager, only network, and it is off by default
107 chroot $rootfs chkconfig network on
110 function unfold_system_slices () {
111 for clonedstamp in $sliceimage_stubs/*/*.cloned; do
112 unfold_system_slice_from_cloned $clonedstamp
116 function unfold_system_slice_from_cloned () {
117 clonedstamp=$1; shift
118 # e.g. NAME=planetflow
119 NAME=$(basename $clonedstamp .cloned)
120 DIR=$(dirname $clonedstamp)
121 # e.g. SLICEFAMILY=planetlab-f8-i386
122 SLICEFAMILY=$(cat $clonedstamp)
123 # deduce the actual name used in .vref by replacing the first part of slice-family
124 # (pldistro) with the slice name
125 # e.g. VREFNAME=planetflow-f8-i386
126 VREFNAME=$(echo $SLICEFAMILY | sed -e "s,^[^-]*-,$NAME-,")
128 VREFPATH="$lxc_dir/$VREFNAME"
130 # do not redo existing vref's unless force is mentioned
131 [ -z "$FORCE" -a -d "$VREFPATH" ] && continue
132 [ -z "$FORCE" ] && message=Building
133 [ -n "$FORCE" ] && message=Force-building
135 # Copy base reference image
136 echo -n $"$message slice image for $NAME in $VREFNAME: "
138 # build the systemslice from the one it was originally cloned from
139 FAMILYREF="$lxc_dir/$SLICEFAMILY"
140 if [ ! -d "$FAMILYREF" ] ; then
141 echo system slice from $clonedstamp - could not find reference $FAMILYREF - skipped
147 # cleanup reference image if already exists
148 [ -d "$VREFPATH" ] && btrfs subvolume delete "$VREFPATH"
150 [ -d "$VREFPATH" ] && rm -rf "$VREFPATH"
153 btrfs subvolume snapshot $FAMILYREF $VREFPATH
155 # merge the stub with the reference to get the system slice
156 (cd "$DIR/$NAME"/ && find . | cpio -m -d -u -p "$VREFPATH"/) || \
157 { echo "Could not apply stub $DIR/$NAME - skipping $clonedstamp" ; btrfs subvolume delete "$VREFPATH" ; return ; }
160 rm -f "$VREFPATH/var/lib/rpm/__db"*
166 start|restart|reload) start ; exit 0 ;;
167 status) status ; exit 0 ;;
169 *) echo $"Usage: $0 {start|stop|status}"