lbuild-initvm .sh: add option -i image - to specify the location of the rootfs
[build.git] / lbuild-initvm.sh
index 199cacf..b9c0109 100755 (executable)
@@ -499,6 +499,11 @@ function debian_install () {
     #virsh -c lxc:/// lxc-enter-namespace $lxc /bin/bash -c "apt-get -y install $DEBIAN_PREINSTALLED"
     chroot $lxc_root apt-get update
     chroot $lxc_root apt-get -y install $DEBIAN_PREINSTALLED
+    # configure hostname
+    cat <<EOF > ${lxc_root}/etc/hostname
+$GUEST_HOSTNAME
+EOF
+    
 }
 
 function debian_configure () {
@@ -539,11 +544,15 @@ function setup_lxc() {
     pkg_method=$(package_method $fcdistro)
     case $pkg_method in
        yum)
-           fedora_install || { echo "failed to install fedora root image"; exit 1 ; }
+            if [ -z "$IMAGE" ]; then
+                fedora_install ||  { echo "failed to install fedora root image"; exit 1 ; }
+            fi
            fedora_configure || { echo "failed to configure fedora for a container"; exit 1 ; }
            ;;
        debootstrap)
-           debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
+            if [ -z "$IMAGE" ]; then
+               debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
+            fi
            debian_configure || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; }
            ;;
        *)
@@ -561,7 +570,7 @@ function setup_lxc() {
     [ -f $lxc_root/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $lxc_root/etc/hosts
     
     # grant ssh access from host to guest
-    mkdir $lxc_root/root/.ssh
+    mkdir -p $lxc_root/root/.ssh
     cat /root/.ssh/id_rsa.pub >> $lxc_root/root/.ssh/authorized_keys
     chmod 700 $lxc_root/root/.ssh
     chmod 600 $lxc_root/root/.ssh/authorized_keys
@@ -719,6 +728,8 @@ function devel_or_vtest_tools () {
                ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e s,main,universe, > sources.list.d/universe.list )
                # also adding a link to updates sounds about right
                ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e 's, main,-updates main,' > sources.list.d/updates.list )
+               # tell apt about the changes
+               chroot /vservers/$lxc apt-get update
            fi
            for package in $packages ; do
                # container not started yet
@@ -853,6 +864,7 @@ function usage () {
     echo " -r repo-url - used to populate yum.repos.d - required in test mode"
     echo " -P pkgs_file - defines a set of extra packages to install in guest"
     echo "    by default we use devel.pkgs (build mode) or runtime.pkgs (test mode)"
+    echo " -i image - the location of the rootfs"
     echo " -v be verbose"
     exit 1
 }
@@ -868,7 +880,7 @@ function main () {
           exit 1
     fi
 
-    while getopts "n:f:d:p:r:P:v" opt ; do
+    while getopts "n:f:d:p:r:P:i:v" opt ; do
        case $opt in
            n) GUEST_HOSTNAME=$OPTARG;;
            f) fcdistro=$OPTARG;;
@@ -876,6 +888,7 @@ function main () {
            p) personality=$OPTARG;;
            r) REPO_URL=$OPTARG;;
            P) PREINSTALLED=$OPTARG;;
+            i) IMAGE=$OPTARG;;
            v) VERBOSE=true; set -x;;
            *) usage ;;
        esac
@@ -887,6 +900,7 @@ function main () {
     [[ -z "$@" ]] && usage
     lxc=$1 ; shift
     lxc_root=/vservers/$lxc
+
     # rainchecks
     almost_empty $lxc_root || \
        { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
@@ -894,6 +908,14 @@ function main () {
        { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
     mkdir -p $lxc_root
 
+    # if IMAGE, copy the provided rootfs to lxc_root
+    if [ -n "$IMAGE" ] ; then
+        [ ! -d "$IMAGE" ] && \
+        { echo "$IMAGE rootfs folder does not exist - exiting" ; exit 1 ; }
+        cp -r $IMAGE/* $lxc_root/
+    fi
+
+
     # check we've exhausted the arguments
     [[ -n "$@" ]] && usage
 
@@ -916,7 +938,7 @@ function main () {
 
     if [ -n "$BUILD_MODE" ] ; then
        # we can now set GUEST_HOSTNAME safely
-        [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$lxc
+        [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$(echo $lxc | sed -e 's,\.,-,g')
     else
        # as this command can be used in other contexts, not specifying
        # a repo is considered a warning
@@ -963,6 +985,8 @@ function main () {
     post_install $lxc $personality
     
     echo $COMMAND Done
+
+    exit 0
 }
 
 main "$@"