Skip to content

Instantly share code, notes, and snippets.

@mkulke
Created December 3, 2024 07:58
Show Gist options
  • Save mkulke/ba50d44451aa0f7de68b4c6211dc6048 to your computer and use it in GitHub Desktop.
Save mkulke/ba50d44451aa0f7de68b4c6211dc6048 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
VM_NAME="${1:-mgns}"
SCREEN_SESSION="vm-console"
virt-install \
--name "$VM_NAME" \
--osinfo name=fedora40 \
--ram 1024 \
--vcpus 2 \
--disk path=~/tmp/image/system.qcow2,format=qcow2 \
--import \
--network network=default \
--graphics none \
--virt-type=kvm \
--boot loader=/usr/share/OVMF/OVMF_CODE.fd \
--disk path=~/tmp/cloud-init.iso,device=cdrom \
--noautoconsole
i=0
wait_time=40
while [ $i -le $wait_time ]; do
printf "\rWaiting for VM to boot...(%*d/${wait_time}s)" 2 $i
sleep 1
i=$(( $i + 1 ))
done
echo ""
echo "Sending commands to $VM_NAME..."
screen -S $SCREEN_SESSION -X stuff "virsh console $VM_NAME\n"
sleep 1
screen -S $SCREEN_SESSION -X stuff "\n"
screen -S $SCREEN_SESSION -X stuff "systemctl status run-media-cidata.mount --no-pager -l\n"
sleep 1
screen -S $SCREEN_SESSION -X stuff "\035"
read -p "Press any key to continue... " -n1 -s
virsh destroy "$VM_NAME"
virsh undefine "$VM_NAME"
echo "VM destroyed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment