cleanup the libvirt section, use 1.2.1 on all fedoras
[build.git] / lbuild-initvm.sh
index b346fe2..f32d5c3 100755 (executable)
@@ -22,6 +22,7 @@ export PATH=$PATH:/bin:/sbin
 DEFAULT_FCDISTRO=f20
 DEFAULT_PLDISTRO=lxc
 DEFAULT_PERSONALITY=linux64
+DEFAULT_MEMORY=512
 
 ##########
 # constant
@@ -375,7 +376,7 @@ EOF
     guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
     ( [ -n "$BUILD_MODE" ] && write_guest_ifcfg_build || write_guest_ifcfg_test ) > $guest_ifcfg
 
-    fedora_configure_yum $lxc $fcdistro $pldistro
+    [ -z "$IMAGE" ] && fedora_configure_yum $lxc $fcdistro $pldistro
 
     return 0
 }
@@ -544,11 +545,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 ; }
            ;;
        *)
@@ -566,7 +571,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
@@ -586,7 +591,7 @@ function write_lxc_xml_test () {
     cat <<EOF
 <domain type='lxc'>
   <name>$lxc</name>
-  <memory>524288</memory>
+  <memory>$MEMORY</memory>
   <os>
     <type arch='$arch2'>exe</type>
     <init>/sbin/init</init>
@@ -625,7 +630,7 @@ function write_lxc_xml_build () {
     cat <<EOF
 <domain type='lxc'>
   <name>$lxc</name>
-  <memory>524288</memory>
+  <memory>$MEMORY</memory>
   <os>
     <type arch='$arch2'>exe</type>
     <init>/sbin/init</init>
@@ -860,6 +865,8 @@ 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 " -m memory - the amount of allocated memory in MB - defaults to $DEFAULT_MEMORY MB"
     echo " -v be verbose"
     exit 1
 }
@@ -875,7 +882,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:m:v" opt ; do
        case $opt in
            n) GUEST_HOSTNAME=$OPTARG;;
            f) fcdistro=$OPTARG;;
@@ -883,6 +890,8 @@ function main () {
            p) personality=$OPTARG;;
            r) REPO_URL=$OPTARG;;
            P) PREINSTALLED=$OPTARG;;
+            i) IMAGE=$OPTARG;;
+            m) MEMORY=$OPTARG;;
            v) VERBOSE=true; set -x;;
            *) usage ;;
        esac
@@ -894,6 +903,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 ; }
@@ -901,6 +911,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 ; }
+        rsync -a $IMAGE/ $lxc_root/
+    fi
+
+
     # check we've exhausted the arguments
     [[ -n "$@" ]] && usage
 
@@ -911,6 +929,10 @@ function main () {
     [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
     [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
     [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
+    [ -z "$MEMORY" ] && MEMORY=$DEFAULT_MEMORY
+    
+    # set memory in KB
+    MEMORY=$(($MEMORY * 1024))
     
     # the set of preinstalled packages - depends on mode
     if [ -z "$PREINSTALLED"] ; then
@@ -970,6 +992,8 @@ function main () {
     post_install $lxc $personality
     
     echo $COMMAND Done
+
+    exit 0
 }
 
 main "$@"