From c542d275072234d5adde31f536cb8e02c7828c40 Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Wed, 8 Jun 2022 00:21:47 -0400 Subject: [PATCH 1/4] create vm script added. Signed-off-by: Michael Valdron --- kvm/create.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 kvm/create.sh diff --git a/kvm/create.sh b/kvm/create.sh new file mode 100644 index 0000000..8c78340 --- /dev/null +++ b/kvm/create.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# Check if hypervisor is installed +if [ -z "$(which virsh)" ] +then + echo "libvirt is not installed." + exit 1 +# Check if running as root or privileged user +elif [ -z "$(groups | tr ' ' '\n' | grep libvirt)" ] && [ "$EUID" -ne 0 ] +then + echo "Please run as root or user under 'libvirt' group." + exit 2 +fi + +# Variable +base_dir=$(dirname $0) +template_name=$1 +vm_name=$2 + +# Create VM from template +virsh define $base_dir/config/${template_name}.xml 1> /dev/null + +# Label VM +virsh domrename ${template_name}-temp ${vm_name} 1> /dev/null + From 8eff9e94983630b43318a09879e7195a0cac876f Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Wed, 8 Jun 2022 00:22:17 -0400 Subject: [PATCH 2/4] rockylinux vm template added. Signed-off-by: Michael Valdron --- kvm/config/rockylinux.xml | 125 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 kvm/config/rockylinux.xml diff --git a/kvm/config/rockylinux.xml b/kvm/config/rockylinux.xml new file mode 100644 index 0000000..3758b26 --- /dev/null +++ b/kvm/config/rockylinux.xml @@ -0,0 +1,125 @@ + + + rockylinux-temp + + + + + + 2097152 + 2097152 + 2 + + hvm + /usr/share/edk2/ovmf/OVMF_CODE.fd + /var/lib/libvirt/qemu/nvram/rockylinux_VARS.fd + + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ + + + + + + + + + + + +
+ + + /dev/urandom +
+ + + + From 15e769d08c0639d10eed92e7ab69f71d242cbc2e Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Thu, 9 Jun 2022 03:04:48 -0400 Subject: [PATCH 3/4] fix typo Signed-off-by: Michael Valdron --- kvm/create.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kvm/create.sh b/kvm/create.sh index 8c78340..c219e7e 100644 --- a/kvm/create.sh +++ b/kvm/create.sh @@ -12,7 +12,7 @@ then exit 2 fi -# Variable +# Variables base_dir=$(dirname $0) template_name=$1 vm_name=$2 From f755678348a4708e203237db0706b4d8d34b7833 Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Thu, 9 Jun 2022 03:05:56 -0400 Subject: [PATCH 4/4] kvm add drive script added. Signed-off-by: Michael Valdron --- kvm/add-drive.sh | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 kvm/add-drive.sh diff --git a/kvm/add-drive.sh b/kvm/add-drive.sh new file mode 100644 index 0000000..d31c1d8 --- /dev/null +++ b/kvm/add-drive.sh @@ -0,0 +1,98 @@ +#!/bin/sh + +# Check if hypervisor is installed +if [ -z "$(which virsh)" ] +then + echo "libvirt is not installed." + exit 1 +# Check if running as root or privileged user +elif [ -z "$(groups | tr ' ' '\n' | grep libvirt)" ] && [ "$EUID" -ne 0 ] +then + echo "Please run as root or user under 'libvirt' group." + exit 2 +fi + +# Set pool to default path +if [ -z "${IMAGE_POOL}" ] +then + if [ "$EUID" -ne 0 ] + then + echo "Please run as root." + exit 2 + fi + IMAGE_POOL=$(virsh pool-dumpxml default | awk -F'[<>]' '/path/ {print $3}') +fi + +# Variables +base_dir=$(dirname $0) +vm_name=$1 +image_uri=$2 +new_dev=$3 + +# Read in array of current VMs +read -r -a vms <<< "$(virsh list --name --all | tr '\n' ' ')" + +# Find specified VM +vm_found=0 +for vm in ${vms[@]} +do + if [ "${vm}" == "${vm_name}" ] + then + vm_found=1 + break + fi +done + +# Throw error if specified VM is not found +if [[ $vm_found -ne 1 ]] +then + echo "'${vm_name}' does not exist." + exit 3 +fi + +# Select target bus based on target device name, error if +# device name prefix does not match a compatible bus. +if [[ $new_dev = vd* ]] +then + targetbus=virtio +elif [[ $new_dev = sd* ]] +then + targetbus=sata +else + echo "dev prefix '${new_dev:0:2}' is not valid." + exit 4 +fi + +# If uri to disk image is a local path append the file:// to uri (if not already there) +if [[ ! $image_uri = http://* ]] && [[ ! $image_uri = https://* ]] && [[ ! $image_uri = file://* ]] +then + image_uri=$([[ $image_uri = /* ]] && echo "file://${image_uri}" || echo "file://${PWD}/${image_uri}") +fi + +# Read all devices with same prefix as specified name for the new device +read -r -a existing_dev <<< "$(virsh domblklist ${vm_name} | awk '{print $1}' | grep -e ${new_dev:0:2} | tr '\n' ' ')" + +# Check if specified device name match any existing ones, if so throw error to user +for dev in ${existing_dev[@]} +do + if [ "${dev}" == "${new_dev}" ] + then + echo "'${new_dev}' already exists, please specific disk target labeled past '${existing_dev[-1]}'." + exit 5 + fi +done + +# Make image path, /path/to/pool/{vm-name}_{random-hash-string}.qcow2 +disk_path=${IMAGE_POOL}/${vm_name}_$(echo ${RANDOM} | md5sum | head -c 15; echo;).qcow2 + +# If disk path exists, regenerate a new until it does not exist +while [ -f $disk_path ] +do + disk_path=${IMAGE_POOL}/${vm_name}_$(echo ${RANDOM} | md5sum | head -c 15; echo;).qcow2 +done + +# Download / Copy template disk image from uri to disk path +curl -L $image_uri -o $disk_path + +# Attach disk at disk path to the vm specified +virsh attach-disk $vm_name $disk_path $new_dev --persistent --driver=qemu --subdriver=qcow2 --targetbus=$targetbus