Creating a VM with KVM and libvirt

1 minute read

Disk

Creating a 10GB disk for the guest:

1user $ qemu-img create -f qcow2 main.qcow2 10G

Booting the ISO

After you have downloaded the ISO you want to install, you can boot it:

user $ virt-install --name internal --ram=8192 --vcpus=2 --cpu host --hvm --autostart --network bridge=br0 --disk /data/vm-internal/main.qcow2 --cdrom ~/ubuntu-20.04.4-live-server-amd64.iso --graphics vnc,listen=0.0.0.0,password=<mypassword>

Here is a description of the main option, please see man virt-install for a complete list:

  • --name: name of your virtual machine
  • --ram: RAM size allocated to the VM (here we chose 8GB)
  • --vcpus: how many virtual CPUs allocated
  • --cpu: chosing host enables the guest to take advantage of many of the host CPUs features
  • --autostart: starts automatically the VM once the host has been rebooted
  • --network bridge=: we specify the bridge that we created earlier
  • --disk: the disk file that we created earlier
  • --cdrom: the ISO to boot from for installing the VM
  • --graphics: how should the graphics be handled. This is mainly used for the installation process, once the server is up and running, you would normally connect through ssh. listen=0.0.0.0 allows for remote connection as by default, only the host is allowed to connect

In order to connect via VNC, you need to know on which port the VM is listening on (you may also force the port allocation during the above command):

user $ virsh dumpxml openstack-controller | grep vnc
<graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>