Skip to content

Instantly share code, notes, and snippets.

@oddstr13
Created January 23, 2020 22:27
Show Gist options
  • Save oddstr13/0e2dafba2addb224181e82f03afc8f3e to your computer and use it in GitHub Desktop.
Save oddstr13/0e2dafba2addb224181e82f03afc8f3e to your computer and use it in GitHub Desktop.
PVC Tools
#!/bin/bash
set -e
MIRROR="http://ftp.no.debian.org/debian/"
CEPH_UUID=$(virsh secret-list | grep 'ceph client.libvirt secret' | awk -F' ' '{ print $1 }')
SSH_PUBKEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDEisccxZL36XmW09U8u+96YoDZlUD59lloWGC4zmEGv oddstr13@OddYoga"
read -p 'VM Name > ' VM_NAME
if pvc vm list -r 2>/dev/null | grep -qx "${VM_NAME}"; then
exit 100
fi
read -p 'VM RAM (512)MiB > ' VM_RAM
read -p 'VM CPU (2) > ' VM_HDD
read -p 'VM HDD (16)GB > ' VM_HDD
TEMPDIR=$(mktemp -d)
echo
echo --------
echo
echo Name: ${VM_NAME}
echo RAM: ${VM_RAM:=512} MiB
echo CPU: ${VM_CPU:=2} v-cores
echo HDD: ${VM_HDD:=16} GiB
echo
read -p "Does this look correct? [y/N]" -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
pvc storage volume add vms ${VM_NAME}_disk0 ${VM_HDD}G
sudo rbd map vms/${VM_NAME}_disk0
sudo sgdisk -n 1:0:+2M --set-alignment=4096 /dev/rbd/vms/${VM_NAME}_disk0
sudo sgdisk -N 2 --set-alignment=4096 /dev/rbd/vms/${VM_NAME}_disk0
sudo partprobe /dev/rbd/vms/${VM_NAME}_disk0
sudo sgdisk -t 1:ef02 /dev/rbd/vms/${VM_NAME}_disk0
sudo sgdisk -A 2:set:2 /dev/rbd/vms/${VM_NAME}_disk0
sudo partprobe /dev/rbd/vms/${VM_NAME}_disk0
sudo mkfs -t ext4 /dev/rbd/vms/${VM_NAME}_disk0-part2
export $(sudo blkid /dev/rbd/vms/${VM_NAME}_disk0-part2 -o export | grep -v DEVNAME)
sudo mount /dev/rbd/vms/${VM_NAME}_disk0-part2 ${TEMPDIR}
sudo debootstrap --include=grub-pc,linux-image-amd64,ssh buster ${TEMPDIR} ${MIRROR}
sudo mount --bind /dev ${TEMPDIR}/dev
sudo mount --bind /dev/pts ${TEMPDIR}/dev/pts
sudo mount --bind /proc ${TEMPDIR}/proc
sudo mount --bind /sys ${TEMPDIR}/sys
sudo mount --bind /run ${TEMPDIR}/run
sudo chroot ${TEMPDIR} grub-install $(realpath /dev/rbd/vms/${VM_NAME}_disk0)
echo UUID=$UUID / ext4 errors=remount-ro 0 1 | sudo tee ${TEMPDIR}/etc/fstab
sudo mkdir -p ${TEMPDIR}/root/.ssh
sudo chmod 700 ${TEMPDIR}/root/.ssh
echo ${SSH_PUBKEY} | sudo tee ${TEMPDIR}/root/.ssh/authorized_keys
sudo sed -ie 's|GRUB_CMDLINE_LINUX=""|GRUB_CMDLINE_LINUX="console=ttyS0,115200 console=tty0"|g' ${TEMPDIR}/etc/default/grub
sudo chroot ${TEMPDIR} update-grub
sudo umount -R -v ${TEMPDIR}
sudo rbd unmap vms/${VM_NAME}_disk0
sudo rmdir ${TEMPDIR}
VM_UUID=$(uuidgen)
VM_MAC=$(echo ${VM_NAME}|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/54:52:\1:\2:\3:\4/')
VM_XML=$(mktemp tmp.XXXXXXXXXX.xml)
envsubst <<EOF > ${VM_XML}
<domain type="kvm">
<name>${VM_NAME}</name>
<uuid>${VM_UUID}</uuid>
<description>${VM_NAME}</description>
<memory unit="MiB">${VM_RAM}</memory>
<vcpu>${VM_CPU}</vcpu>
<os>
<type arch="x86_64" machine="pc-i440fx-2.7">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset="utc"/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/kvm</emulator>
<controller type="usb" index="0"/>
<controller type="pci" index="0" model="pci-root"/>
<serial type="pty">
<target port="0"/>
<serial type="pty">
<log file="/var/log/libvirt/${VM_NAME}.log" append="on"/>
</serial>
</serial>
<console type="pty">
<target port="0"/>
</console>
<!--graphics type="vnc" port="5901" listen="0.0.0.0" passwd="password"/-->
<disk type="network" device="disk">
<driver name="qemu" discard="unmap"/>
<auth username="libvirt">
<secret type="ceph" uuid="${CEPH_UUID}"/>
</auth>
<source protocol="rbd" name="vms/${VM_NAME}_disk0">
<host name="r01.pvc-cluster.local" port="6789"/>
</source>
<target dev="sda" bus="scsi"/>
</disk>
<interface type="bridge">
<mac address="${VM_MAC}"/>
<source bridge="vmbr1"/>
<model type="virtio"/>
</interface>
<controller type="scsi" index="0" model="virtio-scsi"/>
</devices>
</domain>
EOF
cat ${VM_XML}
read -p "Press enter to continue"
pvc vm define ${VM_XML}
pvc vm info ${VM_NAME}
pvc vm start ${VM_NAME}
pvc vm log -f ${VM_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment