first try at creating VirtualizationInterface

This commit is contained in:
2020-05-10 18:59:30 -05:00
parent 7fe0d9a9c5
commit 426fad7b10
12 changed files with 395 additions and 22 deletions

View File

@ -0,0 +1,79 @@
#!/bin/sh -e
#
# create VMs for the capsul service
# developed by Cyberia Heavy Industries
# POSIX or die
vmname="$1"
template_file="/tank/img/$2"
vcpus="$3"
memory="$4"
pubkeys="$5"
root_voume_size="25G"
if echo "$vmname" | grep -vqE '^capsul-[a-z0-9]{10}$'; then
echo "vmname $vmname must match "'"^capsul-[a-z0-9]{10}$"'
exit 1
fi
if [ ! -f "$template_file" ]; then
echo "template $template_file not found"
exit 1
fi
if echo "$vcpus" | grep -vqE "^[0-9]+$"; then
echo "vcpus \"$vcpus\" must be an integer"
exit 1
fi
if echo "$memory" | grep -vqE "^[0-9]+$"; then
echo "memory \"$memory\" must be an integer"
exit 1
fi
echo "$pubkeys" | while IFS= read -r line; do
if echo "$line" | grep -vqE "^(ssh|ecdsa)-[0-9A-Za-z+/_=@ -]+$"; then
echo "pubkey \"$line\" must match "'"^(ssh|ecdsa)-[0-9A-Za-z+/_=@ -]+$"'
exit 1
fi
done
disk="/tank/vm/$vmname.qcow2"
cdrom="/tank/vm/$vmname.iso"
xml="/tank/vm/$vmname.xml"
if [ -f /tank/vm/$vmname.qcow2 ]; then
echo "Randomly generated name matched an existing VM! Odds are like one in a billion. Buy a lotto ticket."
exit 1
fi
qemu-img create -f qcow2 -b "$template_file" "$disk"
cp /tank/config/cyberia-cloudinit.yml /tmp/cloudinit.yml
echo "$pubkeys" | while IFS= read -r line; do
echo " - $line" >> /tmp/cloudinit.yml
done
cloud-localds "$cdrom" /tmp/cloudinit.yml
qemu-img resize "$disk" "$root_voume_size"
virt-install \
--memory "$memory" \
--vcpus "$vcpus" \
--name "$vmname" \
--disk "$disk",bus=virtio \
--disk "$cdrom",device=cdrom \
--os-type Linux \
--os-variant generic \
--virt-type kvm \
--graphics vnc,listen=127.0.0.1 \
--network network=public1,filterref=clean-traffic,model=virtio \
--import \
--print-xml > "$xml"
# --network network=public6,filterref=clean-traffic,model=virtio
chmod 0600 "$xml" "$disk" "$cdrom"
virsh define "$xml"
virsh start "$vmname"
echo "success"

View File

@ -0,0 +1,48 @@
#!/bin/sh
three_dots() {
printf '.'
sleep 0.01
printf '.'
sleep 0.01
printf '.\n'
}
vmname="$1"
if echo "$vmname" | grep -vqE '^capsul-[a-z0-9]{10}$'; then
echo "vmname $vmname must match "'"^capsul-[a-z0-9]{10}$"'
exit 1
fi
echo "deleting $vmname:"
count=$(ls /tank/vm/$vmname* 2> /dev/null | wc -l)
if [ "$count" -gt 3 ]; then
echo "too many files found, exiting cowardly:"
echo "$(ls /tank/vm/$vmname*)"
exit 1
fi
virsh list --name --all | grep -q -E "^$vmname$"
if [ "$?" -eq 1 ]; then
echo "Error: $vmname not found"
exit 1
fi
virsh list --name --state-running | grep -q -E "^$vmname$"
if [ "$?" -eq 0 ]; then
printf ' stopping vm'; three_dots
virsh destroy "$vmname" > /dev/null
fi
printf ' undefining xml.'; three_dots
if ! [ -f "/tank/vm/$vmname.qcow2" ]; then
echo "/tank/vm/$vmname.qcow2 is not a file, exiting cowardly"
exit 1
fi
virsh undefine "$vmname" > /dev/null
printf ' deleting disks.' ; three_dots
rm /tank/vm/$vmname*
echo "success"

View File

@ -0,0 +1,16 @@
#!/bin/sh
vmname="$1"
if echo "$vmname" | grep -vqE '^capsul-[a-z0-9]{10}$'; then
echo "vmname $vmname must match "'"^capsul-[a-z0-9]{10}$"'
exit 1
fi
if virsh list --name --all | grep -vqE "^$vmname$" ; then
echo "Error: $vmname not found"
exit 1
fi
# this gets the ipv4
virsh domifaddr "$vmname" | awk '/vnet/ {print $4}' | cut -d'/' -f1

View File

@ -0,0 +1,3 @@
#!/bin/sh
virsh list --all | grep running | grep -v ' Id' | grep -v -- '----' | awk '{print $2}' | sort