2020-05-10 23:59:30 +00:00
|
|
|
#!/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"
|
2020-05-10 23:59:30 +00:00
|
|
|
vcpus="$3"
|
|
|
|
memory="$4"
|
|
|
|
pubkeys="$5"
|
2021-07-12 17:19:56 +00:00
|
|
|
network_name="$6"
|
|
|
|
public_ipv4="$7"
|
2020-06-28 16:27:26 +00:00
|
|
|
root_volume_size="25G"
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
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
|
2020-05-10 23:59:30 +00:00
|
|
|
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
|
2020-05-10 23:59:30 +00:00
|
|
|
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
|
2020-05-10 23:59:30 +00:00
|
|
|
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
|
2021-05-12 16:48:55 +00:00
|
|
|
echo "pubkey \"$line\" must match \"^(ssh|ecdsa)-[0-9A-Za-z+/_=@:. -]+$\""
|
2020-05-10 23:59:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2021-07-12 17:19:56 +00:00
|
|
|
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
|
|
|
|
|
2020-05-10 23:59:30 +00:00
|
|
|
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
|
|
|
|
|
2020-10-22 23:34:09 +00:00
|
|
|
cp "$template_file" "$disk"
|
2020-05-10 23:59:30 +00:00
|
|
|
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
|
|
|
|
|
2020-06-28 16:27:26 +00:00
|
|
|
qemu-img resize "$disk" "$root_volume_size"
|
2020-05-10 23:59:30 +00:00
|
|
|
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 \
|
2021-07-12 17:19:56 +00:00
|
|
|
--network network=$network_name,filterref=clean-traffic,model=virtio \
|
2020-05-10 23:59:30 +00:00
|
|
|
--import \
|
|
|
|
--print-xml > "$xml"
|
|
|
|
|
|
|
|
chmod 0600 "$xml" "$disk" "$cdrom"
|
|
|
|
virsh define "$xml"
|
|
|
|
virsh start "$vmname"
|
|
|
|
|
|
|
|
echo "success"
|