Ubuntu cloud images with virt-install

· 280 words · 2 minute read

Earlier we looked at how we could use Ubuntu cloud images with KVM. Now we will use the libvirt cli virsh and virt-install to do the same process. virt-install is a part of the virt-manager supporting tools.

Fetch the Ubuntu Cloud image 🔗

This only need to be done once, or when you want to update the cloud image.

  1. Download a Ubuntu Focal image from Ubuntu Cloud Images.
    wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
    
  2. Create an empty image in the default storage pool. This is a storage space where we upload the cloud image. Note that we use the correct postfix on the name here.
    virsh vol-create-as default focal-server-cloudimg-amd64.qcow2 20G
    
  3. Upload the downloaded cloud image to the storage pool in the reserved spaced.
    virsh vol-upload focal-server-cloudimg-amd64.qcow2 focal-server-cloudimg-amd64.img --sparse --pool default
    

Create the VM 🔗

Now we use the cloud image as a template for all our new VMs. Change the size and virt-install parameters to make it your own.

  1. Clone the cloud image to my-vm01.qcow2. The clone is our bootable VM.
    virsh vol-clone focal-server-cloudimg-amd64.qcow2 my-vm01.qcow2 --pool default
    
  2. Resize the new image to the prefered size, in our example 20GB.
    virsh vol-resize my-vm01.qcow2 20G --pool default
    
  3. Create the VM.
    virt-install --os-variant short-id=ubuntu20.04 --import --disk vol=default/my-vm01.qcow2,bus=virtio,driver.discard=unmap --cloud-init --network bridge=virbr0,model=virtio --cpu host --vcpus 2 --memory 1024 --name my-vm01
    

Short explanation of the last command parameters.

  • --import— import an existing image
  • --disk — use my-vm01.qcow2 in default pool with virtio og and hole punching
  • --cloud-init — use cloud init, installation will set a random root password
  • --network — use the default bridge virbr0 with virtio
  • --cpu — copy host cpu information, change if you need better compatibility
  • --vcpus — number of virtual cpus
  • --memory— MB memory
  • --name — VM name