diff options
| -rwxr-xr-x | create-guest-with-cloudinit | 132 | 
1 files changed, 82 insertions, 50 deletions
| diff --git a/create-guest-with-cloudinit b/create-guest-with-cloudinit index 5b5d5b8..c8936e9 100755 --- a/create-guest-with-cloudinit +++ b/create-guest-with-cloudinit @@ -1,9 +1,26 @@ -#!/bin/sh  +#!/bin/bash   # depends on following packages: -# apt-get install virtinst libvirt-bin dosfstools mtools kvm kvm-qemu +# apt-get install virtinst libvirt-bin dosfstools mtools kvm + +# For resizing the root partition we tried two ways:  + +# sfdisk  +# ------ +# For sfdisk we need util-linux >= 2.17.2-3 (debian wheezy, ubuntu oneiric),  +# because of the sfdisk-feature which lets us script the growing of the root partition. +# see https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/686124  # apt-get install -t wheezy util-linux +# parted +# ------ +# seems to be more robust than sfdisk, so we use that approach +# unfortunatly, there's no way to resize a partition with parted >= 2.4 +# deleting and recreating is the only way, which leaves us with an  +# dirty workaround, giving the beginning of the root partition in sectors +# with the variable $ROOT_PARTITION_BEGINNING + +  # defaults, configurable in config  VG='vg01'  ETC='/etc/libvirt/local/cloudinit' @@ -12,16 +29,27 @@ VARDIR='/var/lib/libvirt/cloudinit'  SCRIPTDIR='/usr/local/bin/leap_cloudadmin'  KVM_URI='qemu:///system'  SEED='cloudinit_seed.img' -[ -z "$LC_ALL" ] && LC_ALL="en_US.UTF-8" + +BASEIMAGE='leap-baseimage' +SWAPUUID='3a79d708-2a74-4344-a953-15e955dbdae7' + +# Defaults for leap-baseimage, change if you  +# have a different disk layout  +ROOT_PARTITION_NR='2' +ROOT_PARTITION_BEGINNING='4096s' + +# unconfigurable variables +MIN_ROOTSIZE=2 +DEFAULT_SWAPSIZE=512 +rootsize=$MIN_ROOTSIZE +swapsize=$DEFAULT_SWAPSIZE +  # overwrite defaults  [ -e $CONFIG ] && . $CONFIG -# unconfigurable variables -# swapsize is swapsize of the base image -SWAPSIZE=4 -MIN_DISKSIZE=10 -size=$MIN_DISKSIZE +[ -z "$LC_ALL" ] && LC_ALL="en_US.UTF-8" +  fail() { [ $# -eq 0 ] || echo "$@"; exit 1; }  bad_usage() { usage 1>&2; [ $# -eq 0 ] || fail "$@"; exit 1; } @@ -29,56 +57,57 @@ bad_usage() { usage 1>&2; [ $# -eq 0 ] || fail "$@"; exit 1; }  usage() {         cat <<EOF -Usage: ${0##*/} -s ..  guest_name +Usage: ${0##*/} -r <gb> -s <mb> -n guest_name    Creates a new guest VM using cloud-init to utilize nocloud.    Uses $ETC/<guest_name>.cfg as meta- und user-data file      options:      -h             show usage -    -s size        disk size in GB, including $SWAPSIZE GB swap. Defaults to minimal value of $MIN_DISKSIZE GB. - +    -r size        root size in GB. Defaults to minimal value of $MIN_ROOTSIZE GB. +    -s size        swap size in MB. Defaults to $DEFAULT_SWAPSIZE MB. +    -n name	   guestname  EOF  } -short_opts="hs:" -getopt_out=$(getopt --name "${0##*/}" \ -       --options "${short_opts}" -- "$@") && -       eval set -- "${getopt_out}" || -       fail "Error !"  - +# Parse cmdline options -while [ $# -ne 0 ]; do -       cur=${1}; next=${2}; -       case "$cur" in -               -h) Usage ; exit 0;; -               -s) size=$next; shift;; -               --) shift; break;; -       esac -       shift; +while getopts "hr:s:n:" OPTION +do +  #echo $OPTION $OPTARG +  case $OPTION in +     h) Usage +        exit 0;; +     r) rootsize=$OPTARG;;  +     s) swapsize=$OPTARG;; +     n) vmname=$OPTARG;;  +   esac  done -[ $# -lt 1 ] && bad_usage "must provide guest name" -[ $# -gt 1 ] && bad_usage "too many arguments" +[ -z $vmname  ] && bad_usage "must provide guest name" +[ $# -gt 6 ] && bad_usage "too many arguments" +[ $rootsize -lt $MIN_ROOTSIZE ] && bad_usage "Minimal root size is $MIN_ROOTSIZE GB !" -[ $size -lt $MIN_DISKSIZE ] && bad_usage "Minimal disk size is 10 GB !" +cfg="$ETC/$vmname.cfg" +rootdev="/dev/$VG/${vmname}-root" +swapdev="/dev/$VG/${vmname}-swap"  # requirements  echo "mtools_skip_check=1">~/.mtoolsrc  [ -e $VARDIR ] || ( mkdir $VARDIR ; chown libvirt-qemu $VARDIR ) - -vmname=$1 -cfg="$ETC/$vmname.cfg" - -[ -e /dev/$VG/$vmname ] && fail "/dev/$VG/$vmname exists - please delete it or choose another guest name" - +[ -e $rootdev ] && fail "$rootdev exists - please delete it or choose another guest name" +[ -e $swapdev ] && fail "$swapdev exists - please delete it or choose another guest name"  [ -e $cfg ] ||  fail "please provide config file in $cfg"  echo -echo "Creating guest VM $vmname with size: $size GB, using $cfg for cloud-init" +echo "Creating guest VM $vmname:" +echo "Root size: $rootsize GB, using $rootdev" +echo "Swap size: $swapsize MB, using $swapdev" +echo "using $cfg for cloud-init" +echo  echo "OK ? <Ctrl-C> to abort, <enter> to resume."  read enter @@ -86,28 +115,30 @@ read enter  virsh dominfo $vmname > /dev/null 2>&1  if [ $? -eq 0 ]; then fail "Domain $vmname is defined in libvirt. Please undefine first."; fi - -lvcreate -L ${size}g -n $vmname $VG +# create root and swap disk +lvcreate -L ${rootsize}g -n ${vmname}-root $VG +lvcreate -L ${swapsize}m -n ${vmname}-swap $VG +mkswap  --uuid $SWAPUUID $swapdev  sleep 4  +virt-clone --connect $KVM_URI -o $BASEIMAGE -n $vmname -f $rootdev  --force -virt-clone --connect $KVM_URI -o leap-baseimage-wheezy -n $vmname -f /dev/$VG/$vmname  --force +# resize root partition -# resize second (root) partition -echo ",+," | sfdisk -q  -N2  /dev/$VG/$vmname > /dev/null 2>&1 - - -# create cloudinit seed disk +# echo ",+," | sfdisk -q -N2  /dev/$VG/$vmname > /dev/null 2>&1 +# sfdisk is unreliable for us and doesn't work on a new kvm host +parted $rootdev --script -- rm $ROOT_PARTITION_NR +parted $rootdev --script -- mkpart primary $ROOT_PARTITION_BEGINNING -1s +# create cloudinit seed disk   csplit -q --prefix "$VARDIR/$vmname" $cfg '/#cloud-config/'  metadata="$VARDIR/${vmname}00"  userdata="$VARDIR/${vmname}01" -  $SCRIPTDIR/libvirt-make-seed-disk $VARDIR/$vmname-$SEED $userdata $metadata -$SCRIPTDIR/libvirt-write-disk-attach-xml $VARDIR/$vmname-$SEED > $VARDIR/$vmname-disk-attach.xml +  echo -echo "Attached cloudinit-seeddisk is at $VARDIR/$vmname-$SEED" +echo "Cloudinit-seeddisk is at $VARDIR/$vmname-$SEED, attaching after booting"  echo  echo "Finished creating guest $vmname. Enjoy."  echo @@ -120,10 +151,11 @@ echo  virsh start $vmname   sleep 2  -virsh attach-device $vmname $VARDIR/$vmname-disk-attach.xml -virsh console $vmname  -#do we really need to detach the cloud-init disk ? -#virsh detach-device $vmname $VARDIR/$vmname-disk-attach.xml +# create seed- and swapdisk definition and attach them +virsh attach-disk $vmname --source $swapdev --target vdb  --config +virsh attach-disk $vmname --source $VARDIR/$vmname-$SEED --target vdc --sourcetype file --type disk --driver qemu --subdriver raw --config + +virsh console $vmname  | 
