Installing the Virtualization Stack and Preparing Cloud-init Configuration

This guide explains how to install the virtualization stack and prepare an Ubuntu cloud image and cloud-init configuration for a virtual machine.

Prerequisites

Complete Enabling SR-IOV Virtual Functions before continuing. You need a user account with sudo privileges and network access to download packages and the Ubuntu cloud image.

Installing and preparing the virtualization environment

  1. Update the system and install QEMU/KVM, libvirt, and supporting tools.

    sudo apt update && sudo apt upgrade -y
    sudo apt autoremove
    sudo apt install qemu-kvm bridge-utils virt-manager \
      libvirt-daemon-system libvirt-clients -y
    
  2. Enable and start the libvirt service.

    sudo systemctl enable libvirtd
    sudo systemctl start libvirtd
    sudo systemctl status libvirtd
    
  3. Add your user account to the libvirt and kvm groups. Log out and back in, or start a new login session, for the new group memberships to take effect.

    sudo usermod -aG libvirt,kvm $USER
    
  4. Install genisoimage, which you will use to create the cloud-init seed ISO for the virtual machine.

    sudo apt install genisoimage
    
  5. Download the Ubuntu 24.04 cloud image to the libvirt images directory. The cloud image is a preinstalled disk image that is configured on first boot by cloud-init, so no interactive installation is needed.

    sudo wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img \
    -P /var/lib/libvirt/images/
    
  6. Create the virtual machine disk from the cloud image. The disk uses the cloud image as a read-only backing file, so the same image can be reused for multiple virtual machines. The disk is grown to the requested size automatically on first boot.

    sudo qemu-img create -f qcow2 -F qcow2 \
      -b /var/lib/libvirt/images/noble-server-cloudimg-amd64.img \
      /var/lib/libvirt/images/ubuntu-vm1.qcow2 100G
    
  7. Generate an SSH key pair so you can log in to the virtual machine without a password.

    ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N ""
    
  8. Display the public key and copy it for use in the user-data file in step 10.

    cat ~/.ssh/id_ed25519.pub
    
  9. Create a working directory.

    mkdir -p ~/vm-init && cd ~/vm-init
    
  10. Create user-data, replacing YOUR_PUBLIC_KEY with the public key you copied earlier. This configuration creates the vmadmin user with passwordless sudo privileges and membership in the render and video groups for GPU access.

    cat > user-data <<'EOF'
    #cloud-config
    users:
      - name: vmadmin
        sudo: ALL=(ALL) NOPASSWD:ALL
        groups: sudo,video,render
        shell: /bin/bash
        ssh_authorized_keys:
          - YOUR_PUBLIC_KEY
    ssh_pwauth: false
    EOF
    

    If the host accesses the internet through a proxy, add the proxy settings to user-data so the virtual machine can install packages. For example:

    apt:
      http_proxy: http://<PROXY_HOST>:<PORT>
      https_proxy: http://<PROXY_HOST>:<PORT>
    
  11. Create meta-data.

    cat > meta-data <<'EOF'
    instance-id: vm1
    local-hostname: ubuntu-vm1
    EOF
    
  12. Package user-data and meta-data into a cloud-init seed ISO that the virtual machine reads on first boot.

    sudo genisoimage -output /var/lib/libvirt/images/vm1-seed.iso \
      -volid cidata -joliet -rock user-data meta-data