Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kvm automation #18

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions kvm/add-drive.sh
Original file line number Diff line number Diff line change
@@ -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
125 changes: 125 additions & 0 deletions kvm/config/rockylinux.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<!--
rockylinux template
Author: Michael Valdron
Created on: 2022-06-07
-->
<domain type='kvm'>
<name>rockylinux-temp</name>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://rockylinux.org/rocky/8-unknown"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-q35-6.1'>hvm</type>
<loader readonly='yes' type='pflash'>/usr/share/edk2/ovmf/OVMF_CODE.fd</loader>
<nvram>/var/lib/libvirt/qemu/nvram/rockylinux_VARS.fd</nvram>
</os>
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='host-model' check='partial'>
<topology sockets='1' dies='1' cores='2' threads='1'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<controller type='usb' index='0' model='qemu-xhci' ports='15'>
<address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
</controller>
<controller type='sata' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x1f' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pcie-root'/>
<controller type='pci' index='1' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='1' port='0x10'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0' multifunction='on'/>
</controller>
<controller type='pci' index='2' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='2' port='0x11'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x1'/>
</controller>
<controller type='pci' index='3' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='3' port='0x12'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x2'/>
</controller>
<controller type='pci' index='4' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='4' port='0x13'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x3'/>
</controller>
<controller type='pci' index='5' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='5' port='0x14'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x4'/>
</controller>
<controller type='pci' index='6' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='6' port='0x15'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x5'/>
</controller>
<controller type='pci' index='7' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='7' port='0x16'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x6'/>
</controller>
<controller type='pci' index='8' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='8' port='0x17'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x7'/>
</controller>
<controller type='pci' index='9' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='9' port='0x18'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0' multifunction='on'/>
</controller>
<controller type='pci' index='10' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='10' port='0x19'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
</controller>
<controller type='scsi' index='0' model='virtio-scsi'>
<address type='pci' domain='0x0000' bus='0x08' slot='0x00' function='0x0'/>
</controller>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
</rng>
</devices>
</domain>

25 changes: 25 additions & 0 deletions kvm/create.sh
Original file line number Diff line number Diff line change
@@ -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

# Variables
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