From bd261a0979b0738b2719bedc90975b4ad75feca6 Mon Sep 17 00:00:00 2001 From: varac Date: Thu, 10 Jan 2013 15:02:01 +0100 Subject: use virsh attach-disk instead of attach-device so we don't need the libvirt-write-disk-attach-xml script --- libvirt-write-disk-attach-xml | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100755 libvirt-write-disk-attach-xml diff --git a/libvirt-write-disk-attach-xml b/libvirt-write-disk-attach-xml deleted file mode 100755 index e813452..0000000 --- a/libvirt-write-disk-attach-xml +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh -f - -disks="$1" && [ -n "$disks" ] || - { echo "must give disk image"; exit 1; } -name=${2:-my-example-dom} -devs=bcdefghij -n=0 -for disk in "$@"; do - disk=$(readlink -f "$disk") || { echo "failed to get path to $disk" 1>&2; exit 1; } - t=${devs#?} - dev=vd${devs%${t}} - devs=${devs#?} - n=$(($n+1)) - cat < - - - - - -EOF -done - -- cgit v1.2.3 From 03da7596348d7e3ee4dea9300d20115bf6110d11 Mon Sep 17 00:00:00 2001 From: varac Date: Thu, 10 Jan 2013 15:03:18 +0100 Subject: Create seperate root and swap disks so resizing becomes much easier --- create-guest-with-cloudinit | 132 +++++++++++++++++++++++++++----------------- 1 file 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 < -s -n guest_name Creates a new guest VM using cloud-init to utilize nocloud. Uses $ETC/.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 ? to abort, 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 -- cgit v1.2.3