Setup Scripts

Prerequisites

Qemu, Libvirt and Virt-Manager can all be installed using the below command:

sudo apt install virt-manager

Download ubuntu-22.04.3-live-server-amd64.iso and move to /var/lib/libvirt/images/

Elevate your session:

sudo su 

then:

cd /var/lib/libvirt/images && wget https://learn.acuitservices.com/download/ubuntu/jammy/ubuntu-22.04.3-live-server-amd64.iso && exit

Create a network bridge in your /etc/netplan/00-installer-config.yaml:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s31f6:
      dhcp4: no
  bridges:
    br0:
      dhcp4: yes
      interfaces:
      - enp0s31f6

then restart netplan:

sudo netplan apply

Create a new .sh script in your home directory:

cd ~ && vi createvm.sh

Paste in the below contents:

#!/bin/sh
echo "Enter name for VM (no spaces):"
read name
echo "Enter CPU core count (ex 2):"
read cpu
echo "Enter memory allocation, in MBs, numbers only (ex 2048 for 2 GB):"
read memory
echo "Enter disk size, in GBs, numbers only (ex 120 for 120 GB):"
read disk
virt-install --name $name --os-variant ubuntu22.04  --vcpus $cpu  --memory $memory  --location /var/lib/libvirt/images/ubuntu-22.04.3-live-server-amd64.iso,kernel=casper/vmlinuz,initrd=casper/initrd  --network network=bridged-network,model=virtio  --disk size=$disk  --graphics none  --extra-args='console=ttyS0,115200n8 --- console=ttyS0,115200n8'  --debug

Run the above script using:

sudo bash createvm.sh

An interactive session will begin to create the virtual machine with Ubuntu 22.04.3.

Using Screen

Screen can be used to launch the above script so the session can run in the background and be detached to continue other work during install.

If you are new to Screen, check out the full article on using Screen.

Create a new .sh script in your home directory:

cd ~ && vi screen_createvm.sh

Paste in the below contents:

#!/bin/sh
screen -t screencreatevm sh createvm.sh

Run the above script using:

sudo bash screen_createvm.sh

A new screen window will appear and the same interactive session will begin to create the virtual machine with Ubuntu 22.04.3.