Step 0 — Install qemu & kvm
We would need the following packages to successfully maintain VMs using qemu & kvm.
$ sudo apt update
$ sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
Step 1 — Download any linux distro iso
Let’s download the latest ubuntu server iso file, simply by copying the download url present on Ubuntu’s releases page.
$ wget https://releases.ubuntu.com/22.10/ubuntu-22.10-live-server-amd64.iso
Step 2 — Create the storage disk
Now, we need to create a storage disk which will be used by the VM.
We are creating the storage image here at home/foo/images/bar.img
. Remember to change it as per your use case.
$ qemu-img create -f qcow2 /home/foo/images/bar.img 80G
Now, we need appropriate permissions for the image created
$ sudo chown foo:libvirt /home/foo/images/bar.img
Step 3 — Install virt-inst
To install a VM using CLI we would need the virt-inst utility.
$ sudo apt install virtinst
Step 4 — Set appropriate permissions
libvirt is very particular about accessing storage disks with appropriate permissions. Firstly, we will add the user we intent to run the VMs as to the libvirt group.
$ sudo usermod -aG libvirt foo
Now, we will configure qemu to run the VMs with appropriate permissions for our user.
$ sudo vim /etc/libvirt/qemu.conf
...
user = "foo"
group = "libvirt"
...
Step 5 — Initiate installation
Now, we are good to initiate the installation for our VM.
$ sudo virt-install \
#not necessary, however being precise for example's sake
--connect qemu:///system \
--arch=x86_64 \
#Specify the domain name for the VM
--name=FOO_MACHINE \
--virt-type=kvm \
--boot cdrom,hd \
--network=default,model=virtio \
#This is the path of the image we created in the above steps
--disk path=/home/foo/images/bar.img,format=raw,device=disk,bus=virtio,cache=none \
#Change as per your use case
--memory=4096 \
--vcpu=4 \
#Specify the path to the download linux distro
--cdrom=/home/foo/ubuntu-22.10-live-server-amd64.iso \
#This is the local IP address of our HOST machine
--graphics vnc,listen=192.168.1.141 \
--check all=off
Step 6 — Connect to VNC
Now, we will use any vnc client of our liking to connect to vnc://192.168.1.141
. Here 192.168.1.141
is the IP address we specified for VNC while creating the VM above.
Step 7 — Install distro in VM
Now, just like we’d usually install a linux distro, we’d complete the installation in the vnc session and restart the VM.
Step 8 — Get IP of the VM
We can either use vnc to run ip addr inside the VM or run virsh net-dhcp-leases default
in the host machine to know the IP address assigned to our VM.
Step 9 — Configure ssh if needed
Install openssh-server
if needed, and configure ssh access by following the steps mentioned here if necessary.
Step 10 — Remove vnc support for VM
If we have configured secure ssh access to our VM, we don’t need VNC access anymore. To remove the VNC VM access, we will simply remove the VNC config entry for our VM.
$ sudo virsh edit FOO_MACHINE
This should open up the xml for our VM. We can now find the entry for vnc under <graphics> and just delete these lines. Congratulations, we have successfully created a VM using CLI in linux. To autostart it on host reboot follow this guide.
Afterthoughts
Although, all of this is powerful stuff, but a more convenient way is to use virt-manager
’s GUI to manage the VM present on remote QEMU system. No need to worry about the correct CLI parameters.
If the remote host on which qemu is running can be accessed via ssh, just add a new connection in virt-manager
using qemu+ssh://user@hostIP:port/system?keyfile=path_to_key_file
.