try out libvirt-1.0.4
[build.git] / vbuild-init-lxc.sh
index bab5818..bc1b4d7 100755 (executable)
@@ -5,19 +5,21 @@
 
 COMMAND=$(basename $0)
 DIRNAME=$(dirname $0)
+BUILD_DIR=$(pwd)
 
 # pkgs parsing utilities
 PATH=$(dirname $0):$PATH export PATH
 . build.common
 
-DEFAULT_FCDISTRO=f8
+DEFAULT_FCDISTRO=f16
 DEFAULT_PLDISTRO=planetlab
-DEFAULT_PERSONALITY=linux32
+DEFAULT_PERSONALITY=linux64
 DEFAULT_IFNAME=eth0
 
 COMMAND_VBUILD="vbuild-init-lxc.sh"
 COMMAND_MYPLC="vtest-init-lxc.sh"
 
+libvirt_version="1.0.3"
 function bridge_init () {
 
     # turn on verbosity
@@ -48,9 +50,6 @@ function bridge_init () {
     # take extra arg for ifname, if provided
     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
 
-    ### Checking
-    type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
-
     #if we have already configured the same host_box no need to do it again
     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
@@ -82,7 +81,8 @@ broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head
     sleep 2
     echo "Setting bridge address=$address broadcast=$broadcast"
     # static
-    /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
+    #/sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
+    dhclient $INTERFACE_BRIDGE
     sleep 1
 
     #Reconfigure the routing table
@@ -127,53 +127,50 @@ echo $cidr
 
 }
 
-function prepare_host() {
-        
-       #Bridge init
-       isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
-       if [ "$isInstalled" != "br0" ] ; then
-          bridge_init
-           sleep5
-        fi
-
-       #install development tools
-        isInstalled=$(yum grouplist "Development Tools" | grep Installed)
-        if [ -z "$isInstalled" ] ; then
-                echo "Installing Development Tools ..."
-                yum -y groupinstall "Development Tools"
-       fi
-
-        #install libcap-devel, libvirt
-        isInstalled=$(rpm -qa | grep libcap-devel)
-        if [ -z "$isInstalled" ] ; then
-                echo "Installing libcap-devel ..."
-                yum -y install libcap-devel
-        fi
+function check_yum_installed () {
+    package=$1; shift
+    rpm -q $package >& /dev/null || yum -y install $package
+}
 
-        isInstalled=$(rpm -qa | grep libvirt)
-        if [ -z "$isInstalled" ] ; then
-                echo "Installing libvirt ..."
-                yum -y install libvirt
-        fi
+function check_yumgroup_installed () {
+    group="$1"; shift
+    yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
+}
 
-        #retreive and install lxc from sources 
-        isInstalled=$(lxc-version | cut -d: -f2 | grep "0.8.0-rc1")
-        if [ -z "$isInstalled" ] ; then
-                echo "Installing lxc ..."
-                cd /root
-                git clone git://lxc.git.sourceforge.net/gitroot/lxc/lxc 
-                cd lxc
-                ./autogen.sh
-                ./configure
-                make
-                make install
-        fi
-        
-        #create a symlink (just a hack to make lxc works)
-        [ ! -d "/usr/local/var/lib" ] && mkdir -p /usr/local/var/lib
-        #[ ! -f "/usr/local/var/lib/lxc" ] && ln -s /var/lib/lxc /usr/local/var/lib/lxc
+function prepare_host() {
+   
+    host_fcdistro="$(cat /etc/fedora-release | cut -d' ' -f3)"    
+    ## check if libvirt 1.0.2-1 is installed
+    virsh -v | grep -e "1.0.3" || { echo "Libvirt 1.0.3 needs to be installed!!!" ; exit 1 ; }
+
+#    host_fcdistro="$(cat /etc/fedora-release | cut -d' ' -f3)"
+#    if [ ! -f /etc/yum.repos.d/libvirt.repo ] ; then
+#       touch /etc/yum.repos.d/libvirt.repo
+#       cat <<EOF > /etc/yum.repos.d/libvirt.repo
+#[libvirt]
+#name=libvirt-1.0.2-1
+#baseurl=http://build.onelab.eu/lxc/2013.02.25--lxc$host_fcdistro/RPMS/
+#enabled=1
+#gpgcheck=0
+#EOF
+#
+#       yum --assumeno update
+#       check_yumgroup_installed "Development Tools"
+#       check_yum_installed libcap-devel
+#       check_yum_installed libvirt
+#       systemctl start libvirtd
+#    fi
+
+    #################### bride initialization
+    check_yum_installed bridge-utils
+    #Bridge init
+    isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
+    if [ "$isInstalled" != "br0" ] ; then
+       bridge_init
+        sleep 5
+    fi
 
-       return 0
+    return 0
 }
 
 
@@ -200,15 +197,20 @@ MTU=1500
 EOF
 
 # set the hostname
+if [[ "$fcdistro" == "f18" ]] ; then
+    cat <<EOF > ${rootfs_path}/etc/hostname
+$HOSTNAME
+EOF
+else
     cat <<EOF > ${rootfs_path}/etc/sysconfig/network
 NETWORKING=yes
 HOSTNAME=$HOSTNAME
 EOF
-
     # set minimal hosts
-#    cat <<EOF > $rootfs_path/etc/hosts
-#127.0.0.1 localhost $HOSTNAME
-#EOF
+    cat <<EOF > $rootfs_path/etc/hosts
+127.0.0.1 localhost $HOSTNAME
+EOF
+fi
 
     dev_path="${rootfs_path}/dev"
     rm -rf $dev_path
@@ -241,20 +243,27 @@ function configure_fedora_init() {
 
     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
-    chroot ${rootfs_path} chkconfig udev-post off
-    chroot ${rootfs_path} chkconfig network on
+    chroot ${rootfs_path} /sbin/chkconfig udev-post off
+    chroot ${rootfs_path} /sbin/chkconfig network on
 }
 
 
 function configure_fedora_systemd() {
-
     unlink ${rootfs_path}/etc/systemd/system/default.target
+    ln -s /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
     touch ${rootfs_path}/etc/fstab
-    chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
-    chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
-    #dependency on a device unit fails it specially that we disabled udev
-    sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
-    chroot ${rootfs_path} chkconfig network on
+    ln -s /dev/null ${rootfs_path}/etc/systemd/system/udev.service
+# Thierry - Feb 2013
+# this was intended for f16 initially, in order to enable getty that otherwise would not start
+# having a getty running is helpful only if ssh won't start though, and we see a correlation between
+# VM's that refuse to lxc-stop and VM's that run crazy getty's
+# so, turning getty off for now instead
+#   #dependency on a device unit fails it specially that we disabled udev
+#    sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
+    ln -s /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
+    rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
+# can't seem to handle this one with systemctl
+    chroot ${rootfs_path} /sbin/chkconfig network on
 }
 
 function download_fedora() {
@@ -275,9 +284,21 @@ set -x
   
     
     MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
-    RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
-    echo "Fetching from $RELEASE_URL"
-    curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
+    RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
+    # with fedora18 the rpms are scattered by first name
+    RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
+    RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
+    found=""
+    for attempt in $RELEASE_URL1 $RELEASE_URL2; do
+       if curl -f $attempt -o $RELEASE_TARGET ; then
+           echo "Retrieved $attempt"
+           found=true
+           break
+       else
+           echo "Failed attempt $attempt"
+       fi
+    done
+    [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
     
     mkdir -p $INSTALL_ROOT/var/lib/rpm
     rpm --root $INSTALL_ROOT  --initdb
@@ -315,7 +336,7 @@ set -x
 
 
 function install_fedora() {
-set -x
+    set -x
 
     mkdir -p /var/lock/subsys/
     (
@@ -374,6 +395,7 @@ lxc.network.link = $lxc_network_link
 lxc.network.name = $IFNAME
 lxc.network.mtu = 1500
 lxc.network.ipv4 = $IP/$CIDR
+lxc.network.veth.pair = $veth_pair
 #cgroups
 #lxc.cgroup.devices.deny = a
 # /dev/null and zero
@@ -510,8 +532,6 @@ function setup_lxc() {
     pldistro=$1; shift
     personality=$1; shift
 
-
-
     # create lxc container 
     copy_configuration
     if [ $? -ne 0 ]; then
@@ -538,7 +558,6 @@ function setup_lxc() {
         configure_fedora_systemd
     fi
 
-
     # Enable cgroup
     mkdir $rootfs_path/cgroup
     
@@ -551,13 +570,50 @@ function setup_lxc() {
     mkdir $rootfs_path/root/.ssh
     cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
     
-    lxc-start -d -n $lxc
-
-    sleep 20
+    # copy libvirt xml template
+    veth_pair="i$(echo $HOSTNAME | cut -d. -f1)" 
+    tmpl_name="$lxc.xml"
+    cat > $config_path/$tmpl_name<<EOF
+<domain type='lxc'>
+  <name>$lxc</name>
+  <memory>524288</memory>
+  <os>
+    <type>exe</type>
+    <init>/sbin/init</init>
+  </os>
+  <features>
+    <acpi/>
+  </features>
+  <vcpu>1</vcpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/libexec/libvirt_lxc</emulator>
+    <filesystem type='mount'>
+      <source dir='$rootfs_path'/>
+      <target dir='/'/>
+    </filesystem>
+    <interface type="bridge">
+      <source bridge="br0"/>
+      <target dev='$veth_pair'/>
+    </interface>
+    <console type='pty' />
+  </devices>
+  <network>
+    <name>host-bridge</name>
+    <forward mode="bridge"/>
+    <bridge name="br0"/>
+  </network>
+</domain>
+EOF
+   
+    # define lxc container for libvirt
+    virsh -c lxc:// define $config_path/$tmpl_name
 
     # rpm --rebuilddb
-    chroot $rootfs_path rpm --rebuilddb
-    #ssh -o "StrictHostKeyChecking no" $IP "rpm --rebuilddb"
+    chroot $rootfs_path /bin/rpm --rebuilddb
 
     configure_yum_in_lxc $lxc $fcdistro $pldistro
 
@@ -588,27 +644,27 @@ function devel_or_vtest_tools () {
     ### install individual packages, then groups
     # get target arch - use uname -i here (we want either x86_64 or i386)
    
-    lxc_arch=$(chroot $rootfs_path uname -i)
+    lxc_arch=$(chroot $rootfs_path /bin/uname -i)
     # on debian systems we get arch through the 'arch' command
-    [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path arch)
+    [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
 
     packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
     groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
 
     case "$pkg_method" in
        yum)
-           [ -n "$packages" ] && chroot $rootfs_path yum -y install $packages
+           [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
            for group_plus in $groups; do
                group=$(echo $group_plus | sed -e "s,+++, ,g")
-               chroot $rootfs_path yum -y groupinstall "$group"
+               chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
            done
            # store current rpm list in /init-lxc.rpms in case we need to check the contents
-           chroot $rootfs_path rpm -aq > $rootfs_path/init-lxc.rpms
+           chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
            ;;
        debootstrap)
-           chroot $rootfs_path apt-get update
+           chroot $rootfs_path /usr/bin/apt-get update
            for package in $packages ; do 
-               chroot $rootfs_path  apt-get install -y $package 
+               chroot $rootfs_path  /usr/bin/apt-get install -y $package 
            done
            ### xxx todo install groups with apt..
            ;;
@@ -642,29 +698,29 @@ function post_install_vbuild () {
 
 ### From myplc-devel-native.spec
 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
-    cat << EOF | chroot $rootfs_path bash -x
+    cat << EOF | chroot $rootfs_path /bin/bash -x
     # set up /dev/loop* in lxc
     for i in \$(seq 0 255) ; do
-       mknod -m 640 /dev/loop\$i b 7 \$i
+       /bin/mknod -m 640 /dev/loop\$i b 7 \$i
     done
     
     # create symlink for /dev/fd
-    [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
+    [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
 
     # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
-    sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
+    /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
     if [ -h "/sbin/new-kernel-pkg" ] ; then
-       filename=\$(readlink -f /sbin/new-kernel-pkg)
+       filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
        if [ "\$filename" == "/sbin/true" ] ; then
-               echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
-               echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
-               echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
+               /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
+               /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
+               /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
                exit 1
        fi
     fi
     
     # customize root's prompt
-    cat << PROFILE > /root/.profile
+    /bin/cat << PROFILE > /root/.profile
 export PS1="[$lxc] \\w # "
 PROFILE
 
@@ -700,28 +756,60 @@ function post_install_myplc  () {
     personality=$1; shift
 
 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
-    cat << EOF | chroot $rootfs_path bash -x
+    cat << EOF | chroot $rootfs_path /bin/bash -x
 
     # create /etc/sysconfig/network if missing
-    [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
+    [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
 
     # create symlink for /dev/fd
-    [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
+    [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
 
     # turn off regular crond, as plc invokes plc_crond
-    chkconfig crond off
+    /sbin/chkconfig crond off
 
     # take care of loginuid in /etc/pam.d 
-    sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
+    /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
 
     # customize root's prompt
-    cat << PROFILE > /root/.profile
+    /bin/cat << PROFILE > /root/.profile
 export PS1="[$lxc] \\w # "
 PROFILE
 
 EOF
 }
 
+function start_lxc() {
+
+    set -x
+    set -e
+    #trap failure ERR INT
+
+    lxc=$1; shift
+  
+    virsh -c lxc:// start $lxc
+  
+    echo $IP is up, waiting for ssh...
+
+    #wait max 5 min for sshd to start 
+    ssh_up=""
+    stop_time=$(($(date +%s) + 300))
+    current_time=$(date +%s)
+    
+    counter=1
+    while [ "$current_time" -lt "$stop_time" ] ; do
+         echo "$counter-th attempt to reach sshd in container $lxc ..."
+         ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
+         sleep 10
+         current_time=$(($current_time + 10))
+         counter=$(($counter+1))
+    done
+
+    # Thierry: this is fatal, let's just exit with a failure here
+    [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; } 
+
+    return 0
+}
+
 function usage () {
     set +x 
     echo "Usage: $COMMAND_VBUILD [options] lxc-name"
@@ -825,7 +913,10 @@ function main () {
         echo "Unknown personality: $personality"
     fi
 
-    
+    # need lxc installed before we can run lxc-ls
+    # need bridge installed
+    prepare_host    
+
     if [ -n "$VBUILD_MODE" ] ; then
 
        # Bridge IP affectation
@@ -839,15 +930,18 @@ function main () {
        
         lxc_network_type=veth
         lxc_network_link=virbr0
+       veth_pair="veth$z"
         echo "the IP address of container $lxc is $IP "
     else
         [[ -z "$REPO_URL" ]] && usage
         [[ -z "$IP" ]] && usage
-        NETMASK=$(ifconfig br0 | grep 'inet addr' | awk '{print $4}' | sed -e 's/.*://')
+       
+        NETMASK=$(ifconfig br0 | grep 'inet ' | awk '{print $4}' | sed -e 's/.*://')
         GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
         [[ -z "$HOSTNAME" ]] && usage
         lxc_network_type=veth
         lxc_network_link=br0
+        veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
     fi
 
     CIDR=$(cidr_notation $NETMASK)
@@ -858,29 +952,24 @@ function main () {
           exit 1
     fi
 
-    if [ ! -z "$(lxc-ls | grep $lxc)" ];then
-        echo "container $lxc exists"
-        exit 1
-    fi
-
-    
     path=/var/lib/lxc
     rootfs_path=$path/$lxc/rootfs
     config_path=$path/$lxc
     cache_base=/var/cache/lxc/fedora/$arch
     cache=$cache_base/$release
     root_password=root
-
-
-    prepare_host
+    
+    # check whether the rootfs directory is created to know if the container exists
+    # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
+    [ -d $rootfs_path ] && { echo "container $lxc already exists - exiting" ; exit 1 ; }
 
     setup_lxc $lxc $fcdistro $pldistro $personality 
 
     devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
 
     post_install $lxc $personality
-
     
+    start_lxc $lxc
 
     echo $COMMAND Done
 }