capsul-flask/capsulflask/shell_scripts/create.sh

91 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/sh -e
#
# create VMs for the capsul service
# developed by Cyberia Heavy Industries
# POSIX or die
vmname="$1"
2020-10-22 23:34:09 +00:00
template_file="/tank/img/$2"
qemu_tank_dir="/home/f/Projects/Cyberia/tank"
vcpus="$3"
memory="$4"
pubkeys="$5"
network_name="$6"
public_ipv4="$7"
root_volume_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
2021-05-15 14:49:36 +00:00
if [ ! -f "$template_file" ]; then
echo "template $template_file not found"
exit 1
fi
2021-05-15 14:49:36 +00:00
if echo "$vcpus" | grep -vqE "^[0-9]+$"; then
echo "vcpus \"$vcpus\" must be an integer"
exit 1
fi
2021-05-15 14:49:36 +00:00
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
2021-05-15 14:49:36 +00:00
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
if echo "$network_name" | grep -vqE "^[a-zA-Z0-9_-]+"; then
echo "network_name \"$network_name\" must match ^[a-zA-Z0-9_-]+"
exit 1
fi
if echo "$public_ipv4" | grep -vqE "^[0-9.]+$"; then
echo "public_ipv4 \"$public_ipv4\" must match ^[0-9.]+$"
exit 1
fi
disk="$vmname.qcow2"
cdrom="$vmname.iso"
xml="$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
cp "$template_file" "/tank/vm/$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 "/tank/vm/$cdrom" /tmp/cloudinit.yml
qemu-img resize "/tank/vm/$disk" "$root_volume_size"
virt-install \
--memory "$memory" \
--vcpus "$vcpus" \
--name "$vmname" \
--disk "$qemu_tank_dir/vm/$disk",bus=virtio \
--disk "$qemu_tank_dir/vm/$cdrom",device=cdrom \
--os-type Linux \
--os-variant generic \
--virt-type kvm \
--graphics vnc,listen=127.0.0.1 \
--network network=$network_name,filterref=clean-traffic,model=virtio \
--import \
--print-xml > "/tank/vm/$xml"
chmod 0600 "/tank/vm/$xml" "/tank/vm/$disk" "/tank/vm/$cdrom"
virsh define "/tank/vm/$xml"
virsh start "$vmname"
echo "success"