Last active
October 29, 2018 23:16
-
-
Save 0xabe-io/0b9f1e9ddf6155ec122c2bb47a8b7e12 to your computer and use it in GitHub Desktop.
Spin an Ubuntu VM from a cloud image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# quick and dirty script to spin a ubuntu VM based on a cloud image | |
NAME='' | |
DISK_SIZE='' | |
RAM='1024' | |
CPU='1' | |
LOCATION='TBD' | |
QEMU_IMG='/usr/bin/qemu-img' | |
VIRT_INSTALL='/usr/bin/virt-install' | |
BACKING_IMAGE='TBD' # path to the original ubuntu cloud image | |
SEED='TBD' # path to the seed cd image created with cloud-localds | |
usage() { | |
echo "$1" >&2 | |
echo "usage: $0 NAME DISK_SIZE [RAM] [CPU] [LOCATION]" >&2 | |
exit 1 | |
} | |
if [[ $# -lt 2 || $# -gt 5 ]]; then | |
usage "Bad number of arguments!" | |
fi | |
NAME="$1" | |
DISK_SIZE="$2" | |
if [[ $# -gt 2 ]]; then | |
RAM="$3" | |
fi | |
if [[ $# -gt 3 ]]; then | |
CPU="$4" | |
fi | |
if [[ $# -gt 4 ]]; then | |
LOCATION="$5" | |
fi | |
if [[ ! -x "$QEMU_IMG" ]]; then | |
usage "$QEMU_IMG not found!" | |
fi | |
if [[ ! -x "$VIRT_INSTALL" ]]; then | |
usage "$VIRT_INSTALL not found!" | |
fi | |
if [[ ! -f "$BACKING_IMAGE" ]]; then | |
usage "$BACKING_IMAGE not found!" | |
fi | |
if [[ ! -f "$SEED" ]]; then | |
usage "$SEED not found!" | |
fi | |
DISK="${LOCATION}/${NAME}.qcow2" | |
sudo $QEMU_IMG create -f qcow2 -b "$BACKING_IMAGE" "$DISK" "$DISK_SIZE" | |
if [[ $? -ne 0 ]]; then | |
echo "Failed to create $DISK" >&2 | |
rm -f "$DISK" 2>/dev/null | |
exit 1 | |
fi | |
sudo chown libvirt-qemu:kvm "$DISK" | |
$VIRT_INSTALL --connect "qemu:///system" \ | |
-n "$NAME" \ | |
-r "$RAM" \ | |
--os-type "linux" \ | |
--os-variant "ubuntu16.04" \ | |
--disk "$DISK,bus=virtio" \ | |
--network network="default" \ | |
--graphics none \ | |
--cdrom "$SEED" | |
if [[ $? -ne 0 ]]; then | |
rm -f "$DISK" 2>/dev/null | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment