9 # pkgs parsing utilities
10 PATH=$(dirname $0):$PATH export PATH
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux64
18 COMMAND_VBUILD="vbuild-init-lxc.sh"
19 COMMAND_MYPLC="vtest-init-lxc.sh"
21 lxc_version="0.8.0-rc2"
23 function bridge_init () {
31 # Default Value for INTERFACE_LAN
32 INTERFACE_LAN=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
35 echo "========== $COMMAND: entering start - beg"
40 echo "========== $COMMAND: entering start - end"
42 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
44 # another option would be to accept the all forward packages for
45 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
46 sysctl net.bridge.bridge-nf-call-iptables=0
47 sysctl net.bridge.bridge-nf-call-ip6tables=0
48 sysctl net.bridge.bridge-nf-call-arptables=0
50 # take extra arg for ifname, if provided
51 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
53 #if we have already configured the same host_box no need to do it again
54 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
55 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
58 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
59 echo "Cannot use interface $INTERFACE_LAN - exiting"
64 #Getting host IP/masklen
65 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
66 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
68 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
69 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
71 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
72 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
75 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
76 brctl addbr $INTERFACE_BRIDGE
77 #brctl stp $INTERFACE_BRIDGE yes
78 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
79 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
80 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
82 echo "Setting bridge address=$address broadcast=$broadcast"
84 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
87 #Reconfigure the routing table
88 echo "Configuring gateway=$gateway"
89 route add default gw $gateway
91 echo "========== $COMMAND: exiting start - beg"
94 echo "========== $COMMAND: exiting start - end"
102 function failure () {
103 echo "$COMMAND : Bailing out"
107 function cidr_notation () {
111 for i in $(seq 1 4) ; do
112 part=$(echo $netmask | cut -d. -f $i)
114 "255") cidr=$((cidr + 8));;
115 "254") cidr=$((cidr + 7));;
116 "252") cidr=$((cidr + 6));;
117 "248") cidr=$((cidr + 5));;
118 "240") cidr=$((cidr + 4));;
119 "224") cidr=$((cidr + 3));;
120 "192") cidr=$((cidr + 2));;
121 "128") cidr=$((cidr + 1));;
122 "0") cidr=$((cidr + 0));;
129 function check_yum_installed () {
131 rpm -q $package >& /dev/null || yum -y install $package
134 function check_yumgroup_installed () {
136 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
139 function prepare_host() {
141 #################### lxc-tools : rebuild as current fedora release has flaws
142 #install development tools
143 check_yumgroup_installed "Development Tools"
144 #install libcap-devel, libvirt
145 check_yum_installed libcap-devel
146 check_yum_installed libvirt
148 #retrieve and install lxc from sources
149 raw_version=$(lxc-version ||: )
150 lxc_installed_version=$(echo $raw_version | sed -e 's,.*: ,,')
151 if [ "$lxc_installed_version" != "$lxc_version" ] ; then
152 echo "Expecting version" '['$lxc_version']'
153 echo "Found version" '['$lxc_installed_version']'
154 echo "Installing lxc ..."
156 git clone git://lxc.git.sourceforge.net/gitroot/lxc/lxc
158 git checkout $lxc_version
160 ./configure --prefix=/usr
165 # #create a placeholder (just a hack to make lxc works)
166 # [ -d "/usr/local/var/lib" ] || mkdir -p /usr/local/var/lib
168 #################### bride initialization
169 check_yum_installed bridge-utils
171 isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
172 if [ "$isInstalled" != "br0" ] ; then
182 function configure_fedora() {
184 # disable selinux in fedora
185 mkdir -p $rootfs_path/selinux
186 echo 0 > $rootfs_path/selinux/enforce
188 # configure the network
190 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
204 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
210 # cat <<EOF > $rootfs_path/etc/hosts
211 #127.0.0.1 localhost $HOSTNAME
214 dev_path="${rootfs_path}/dev"
217 mknod -m 666 ${dev_path}/null c 1 3
218 mknod -m 666 ${dev_path}/zero c 1 5
219 mknod -m 666 ${dev_path}/random c 1 8
220 mknod -m 666 ${dev_path}/urandom c 1 9
221 mkdir -m 755 ${dev_path}/pts
222 mkdir -m 1777 ${dev_path}/shm
223 mknod -m 666 ${dev_path}/tty c 5 0
224 mknod -m 666 ${dev_path}/tty0 c 4 0
225 mknod -m 666 ${dev_path}/tty1 c 4 1
226 mknod -m 666 ${dev_path}/tty2 c 4 2
227 mknod -m 666 ${dev_path}/tty3 c 4 3
228 mknod -m 666 ${dev_path}/tty4 c 4 4
229 mknod -m 600 ${dev_path}/console c 5 1
230 mknod -m 666 ${dev_path}/full c 1 7
231 mknod -m 600 ${dev_path}/initctl p
232 mknod -m 666 ${dev_path}/ptmx c 5 2
234 #echo "setting root passwd to $root_password"
235 #echo "root:$root_password" | chroot $rootfs_path chpasswd
241 function configure_fedora_init() {
243 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
244 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
245 chroot ${rootfs_path} chkconfig udev-post off
246 chroot ${rootfs_path} chkconfig network on
250 function configure_fedora_systemd() {
252 unlink ${rootfs_path}/etc/systemd/system/default.target
253 touch ${rootfs_path}/etc/fstab
254 chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
255 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
256 #dependency on a device unit fails it specially that we disabled udev
257 sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
258 chroot ${rootfs_path} chkconfig network on
261 function download_fedora() {
263 # check the mini fedora was not already downloaded
264 INSTALL_ROOT=$cache/partial
266 mkdir -p $INSTALL_ROOT
267 if [ $? -ne 0 ]; then
268 echo "Failed to create '$INSTALL_ROOT' directory"
272 # download a mini fedora into a cache
273 echo "Downloading fedora minimal ..."
274 YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck --releasever=$release"
275 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
278 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
279 RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
280 echo "Fetching from $RELEASE_URL"
281 curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
283 mkdir -p $INSTALL_ROOT/var/lib/rpm
284 rpm --root $INSTALL_ROOT --initdb
285 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
286 echo "$YUM install $PKG_LIST"
287 $YUM install $PKG_LIST
289 if [ $? -ne 0 ]; then
290 echo "Failed to download the rootfs, aborting."
294 mv "$INSTALL_ROOT" "$cache/rootfs"
295 echo "Download complete."
301 function copy_fedora() {
303 # make a local copy of the minifedora
304 echo -n "Copying rootfs to $rootfs_path ..."
305 mkdir -p $rootfs_path
306 rsync -a $cache/rootfs/ $rootfs_path/
311 function update_fedora() {
313 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
318 function install_fedora() {
321 mkdir -p /var/lock/subsys/
324 if [ $? -ne 0 ]; then
325 echo "Cache repository is busy."
329 echo "Checking cache download in $cache/rootfs ... "
330 if [ ! -e "$cache/rootfs" ]; then
332 if [ $? -ne 0 ]; then
333 echo "Failed to download 'fedora base'"
337 echo "Cache found. Updating..."
339 if [ $? -ne 0 ]; then
340 echo "Failed to update 'fedora base', continuing with last known good cache"
342 echo "Update finished"
346 echo "Copy $cache/rootfs to $rootfs_path ... "
348 if [ $? -ne 0 ]; then
349 echo "Failed to copy rootfs"
355 ) 200>/var/lock/subsys/lxc
361 function copy_configuration() {
363 mkdir -p $config_path
364 cat <<EOF >> $config_path/config
369 lxc.rootfs = $rootfs_path
370 lxc.mount = $config_path/fstab
372 lxc.network.type = $lxc_network_type
373 lxc.network.flags = up
374 lxc.network.link = $lxc_network_link
375 lxc.network.name = $IFNAME
376 lxc.network.mtu = 1500
377 lxc.network.ipv4 = $IP/$CIDR
379 #lxc.cgroup.devices.deny = a
381 lxc.cgroup.devices.allow = c 1:3 rwm
382 lxc.cgroup.devices.allow = c 1:5 rwm
384 lxc.cgroup.devices.allow = c 5:1 rwm
385 lxc.cgroup.devices.allow = c 5:0 rwm
386 lxc.cgroup.devices.allow = c 4:0 rwm
387 lxc.cgroup.devices.allow = c 4:1 rwm
389 lxc.cgroup.devices.allow = c 1:9 rwm
390 lxc.cgroup.devices.allow = c 1:8 rwm
391 lxc.cgroup.devices.allow = c 136:* rwm
392 lxc.cgroup.devices.allow = c 5:2 rwm
394 lxc.cgroup.devices.allow = c 254:0 rwm
395 lxc.cgroup.devices.allow = b 255:0 rwm
400 cat <<EOF > $config_path/fstab
401 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
402 devpts $rootfs_path/dev/pts devpts defaults 0 0
403 sysfs $rootfs_path/sys sysfs defaults 0 0
405 if [ $? -ne 0 ]; then
406 echo "Failed to add configuration"
416 # overwrite lxc's internal yum config
417 function configure_yum_in_lxc () {
426 echo "Initializing yum.repos.d in $lxc"
427 rm -f $rootfs_path/etc/yum.repos.d/*
429 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
431 name=Fedora $release - \$basearch
432 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/\$basearch/os/
436 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
439 name=Fedora $release - \$basearch - Updates
440 baseurl=http://mirror.onelab.eu/fedora/updates/$release/\$basearch/
444 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
447 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
448 # just mention 'none' as the repo url
449 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
450 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
451 echo "WARNING : cannot create myplc repo"
453 # exclude kernel from fedora repos
454 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
455 for repo in $rootfs_path/etc/yum.repos.d/* ; do
456 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
458 # the build repo is not signed at this stage
459 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
470 # return yum or debootstrap
471 function package_method () {
474 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
475 lenny|etch) echo debootstrap ;;
476 *) echo Unknown distro $fcdistro ;;
480 # return arch from debian distro and personality
481 function canonical_arch () {
482 personality=$1; shift
484 case $(package_method $fcdistro) in
486 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
488 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
490 echo Unknown-arch-3 ;;
494 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
495 function almost_empty () {
497 # non existing is fine
498 [ ! -d $dir ] && return 0;
499 # need to have at most one file
500 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
503 function setup_lxc() {
507 #trap failure ERR INT
512 personality=$1; shift
514 # create lxc container
516 if [ $? -ne 0 ]; then
517 echo "failed write configuration file"
522 if [ $? -ne 0 ]; then
523 echo "failed to install fedora"
528 if [ $? -ne 0 ]; then
529 echo "failed to configure fedora for a container"
533 type /bin/systemd >/dev/null 2>&1
534 if [ $? -ne 0 ]; then
535 configure_fedora_init
537 configure_fedora_systemd
541 mkdir $rootfs_path/cgroup
544 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
545 # and /etc/hosts for at least localhost
546 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
549 mkdir $rootfs_path/root/.ssh
550 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
557 chroot $rootfs_path rpm --rebuilddb
558 #ssh -o "StrictHostKeyChecking no" $IP "rpm --rebuilddb"
560 configure_yum_in_lxc $lxc $fcdistro $pldistro
565 function devel_or_vtest_tools () {
574 personality=$1; shift
576 pkg_method=$(package_method $fcdistro)
578 # check for .pkgs file based on pldistro
579 if [ -n "$VBUILD_MODE" ] ; then
584 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
586 ### install individual packages, then groups
587 # get target arch - use uname -i here (we want either x86_64 or i386)
589 lxc_arch=$(chroot $rootfs_path uname -i)
590 # on debian systems we get arch through the 'arch' command
591 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path arch)
593 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
594 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
596 case "$pkg_method" in
598 [ -n "$packages" ] && chroot $rootfs_path yum -y install $packages
599 for group_plus in $groups; do
600 group=$(echo $group_plus | sed -e "s,+++, ,g")
601 chroot $rootfs_path yum -y groupinstall "$group"
603 # store current rpm list in /init-lxc.rpms in case we need to check the contents
604 chroot $rootfs_path rpm -aq > $rootfs_path/init-lxc.rpms
607 chroot $rootfs_path apt-get update
608 for package in $packages ; do
609 chroot $rootfs_path apt-get install -y $package
611 ### xxx todo install groups with apt..
614 echo "unknown pkg_method $pkg_method"
621 function post_install () {
622 if [ -n "$VBUILD_MODE" ] ; then
623 post_install_vbuild "$@"
625 post_install_myplc "$@"
627 # setup localtime from the host
629 cp /etc/localtime $rootfs_path/etc/localtime
632 function post_install_vbuild () {
639 personality=$1; shift
641 ### From myplc-devel-native.spec
642 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
643 cat << EOF | chroot $rootfs_path bash -x
644 # set up /dev/loop* in lxc
645 for i in \$(seq 0 255) ; do
646 mknod -m 640 /dev/loop\$i b 7 \$i
649 # create symlink for /dev/fd
650 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
652 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
653 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
654 if [ -h "/sbin/new-kernel-pkg" ] ; then
655 filename=\$(readlink -f /sbin/new-kernel-pkg)
656 if [ "\$filename" == "/sbin/true" ] ; then
657 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
658 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
659 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
664 # customize root's prompt
665 cat << PROFILE > /root/.profile
666 export PS1="[$lxc] \\w # "
672 # add a "build" user to the system
673 builduser=\$(grep "^build:" /etc/passwd | wc -l)
674 if [ \$builduser -eq 0 ] ; then
675 groupadd -o -g \$gid build;
676 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
679 # Allow build user to build certain RPMs as root
680 if [ -f /etc/sudoers ] ; then
681 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
682 if [ \$buildsudo -eq 0 ] ; then
683 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
685 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
692 function post_install_myplc () {
698 personality=$1; shift
700 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
701 cat << EOF | chroot $rootfs_path bash -x
703 # create /etc/sysconfig/network if missing
704 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
706 # create symlink for /dev/fd
707 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
709 # turn off regular crond, as plc invokes plc_crond
712 # take care of loginuid in /etc/pam.d
713 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
715 # customize root's prompt
716 cat << PROFILE > /root/.profile
717 export PS1="[$lxc] \\w # "
725 echo "Usage: $COMMAND_VBUILD [options] lxc-name"
726 echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
728 echo " This command creates a fresh lxc instance, for building, or running, myplc"
729 echo "Supported options"
730 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
731 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
732 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
733 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
734 echo "-- lxc-options"
735 echo " --netdev : interface to be defined inside lxc"
736 echo " --interface : IP to be defined for the lxc"
737 echo " --hostname : Hostname to be defined for the lxc"
738 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
739 echo " myplc.repo does not get created"
747 #trap failure ERR INT
762 while getopts "f:d:p:i:" opt ; do
764 f) fcdistro=$OPTARG;;
765 d) pldistro=$OPTARG;;
766 p) personality=$OPTARG;;
772 shift $(($OPTIND - 1))
774 # parse fixed arguments
775 [[ -z "$@" ]] && usage
777 if [ -n "$MYPLC_MODE" ] ; then
778 [[ -z "$@" ]] && usage
782 # parse vserver options
783 if [[ -n "$@" ]] ; then
784 if [ "$1" == "--" ] ; then
792 eval set -- "$LXC_OPTIONS"
797 --netdev) IFNAME=$2; shift 2;;
798 --interface) IP=$2; shift 2;;
799 --hostname) HOSTNAME=$2; shift 2;;
805 if [ -n "$VBUILD_MODE" ] ; then
806 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
807 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
810 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
811 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
812 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
814 release=$(echo $fcdistro | cut -df -f2)
816 if [ "$personality" == "linux32" ]; then
819 elif [ "$personality" == "linux64" ]; then
823 echo "Unknown personality: $personality"
827 if [ -n "$VBUILD_MODE" ] ; then
829 # Bridge IP affectation
830 x=$(echo $personality | cut -dx -f2)
831 y=$(echo $fcdistro | cut -df -f2)
835 NETMASK="255.255.255.0"
836 GATEWAY="192.168.122.1"
838 lxc_network_type=veth
839 lxc_network_link=virbr0
840 echo "the IP address of container $lxc is $IP "
842 [[ -z "$REPO_URL" ]] && usage
843 [[ -z "$IP" ]] && usage
844 NETMASK=$(ifconfig br0 | grep 'inet addr' | awk '{print $4}' | sed -e 's/.*://')
845 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
846 [[ -z "$HOSTNAME" ]] && usage
847 lxc_network_type=veth
851 CIDR=$(cidr_notation $NETMASK)
854 if [ "$(id -u)" != "0" ]; then
855 echo "This script should be run as 'root'"
859 # need lxc installed before we can run lxc-ls
862 if [ ! -z "$(lxc-ls | grep $lxc)" ];then
863 echo "container $lxc exists"
869 rootfs_path=$path/$lxc/rootfs
870 config_path=$path/$lxc
871 cache_base=/var/cache/lxc/fedora/$arch
872 cache=$cache_base/$release
876 setup_lxc $lxc $fcdistro $pldistro $personality
878 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
880 post_install $lxc $personality