summaryrefslogtreecommitdiff
path: root/create-guest-with-cloudinit
diff options
context:
space:
mode:
Diffstat (limited to 'create-guest-with-cloudinit')
-rwxr-xr-xcreate-guest-with-cloudinit92
1 files changed, 92 insertions, 0 deletions
diff --git a/create-guest-with-cloudinit b/create-guest-with-cloudinit
new file mode 100755
index 0000000..56eec5f
--- /dev/null
+++ b/create-guest-with-cloudinit
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+VG='vg01'
+ETC='/etc/cloudinit'
+VARDIR='/var/lib/libvirt/cloudinit'
+
+fail() { [ $# -eq 0 ] || echo "$@"; exit 1; }
+bad_usage() { usage 1>&2; [ $# -eq 0 ] || fail "$@"; exit 1; }
+
+usage() {
+ cat <<EOF
+
+Usage: ${0##*/} [ $options ] guest_name
+
+ Creates a new guest VM using cloud-init to utilize nocloud.
+
+ options:
+ -h show usage
+ -s size disk size in GB
+
+EOF
+}
+
+
+short_opts="hs:"
+getopt_out=$(getopt --name "${0##*/}" \
+ --options "${short_opts}" -- "$@") &&
+ eval set -- "${getopt_out}" ||
+ fail "Error !"
+
+
+while [ $# -ne 0 ]; do
+ cur=${1}; next=${2};
+ case "$cur" in
+ -h) Usage ; exit 0;;
+ -s) size=$next; shift;;
+ --) shift; break;;
+ esac
+ shift;
+done
+
+
+[ $# -lt 1 ] && bad_usage "must provide guest name"
+[ $# -gt 1 ] && bad_usage "too many arguments"
+
+vmname=$1
+userdata="$ETC/$vmname-user-data"
+
+
+[ -e /dev/$VG/$vmname ] && fail "/dev/$VG/$vmname exists - please delete it or choose another guest name"
+
+[ -e $userdata ] || fail "please provide userdata in $userdata"
+
+echo
+echo "Creating guest VM $vmname with size: $size GB, using $userdata for clound-init"
+echo
+
+
+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
+virt-clone -o leap-baseimage-wheezy -n $vmname -f /dev/$VG/$vmname --force
+
+# resize second (root) partition
+echo ",+," | sfdisk -N2 /dev/$VG/$vmname > /dev/null
+
+libvirt-make-seed-disk $VARDIR/$vmname-user-data.img $userdata
+libvirt-write-disk-attach-xml $VARDIR/$vmname-user-data.img > $VARDIR/$vmname-disk-attach.xml
+
+echo
+echo "Attached userdata-disk is at $VARDIR/$vmname-user-data.img"
+echo
+echo "Finished creating guest $vmname. Enjoy."
+echo
+echo Press <return> to start the new guest.
+read bogus
+
+echo
+echo 'Starting new guest, attaching to console. Exit with Ctrl+]'
+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
+
+