Reusing a Prepared Virtual Machine Image

Use this guide after Creating and Configuring Virtual Machines with GPU Virtual Functions to prepare a base image for additional virtual machines.

This procedure provides an optional way to reuse a virtual machine configured with GPU software, a supported kernel, and tools such as Docker Engine. Using its disk as the base image for additional virtual machines avoids installing the GPU software separately in each one.

Preparing and reusing the image

  1. In the prepared virtual machine, add the user who runs GPU workloads to the render and video groups.

    sudo usermod -aG render,video vmadmin
    
  2. If the virtual machine was installed with the Ubuntu Server installer instead of from the cloud image, re-enable cloud-init, which the installer disables after the first boot. If cloud-init remains disabled, new virtual machines ignore the configuration in their cloud-init seed ISO, including the SSH key and hostname.

    sudo rm -f /etc/cloud/cloud-init.disabled \
      /etc/cloud/cloud.cfg.d/99-installer.cfg \
      /etc/cloud/cloud.cfg.d/90-installer-network.cfg
    
  3. Reset the cloud-init state and the machine ID so that each new virtual machine is initialized as a new instance, then shut down the prepared virtual machine. Empty the /etc/machine-id file; do not remove it. Without this file, networking in new virtual machines fails to start with Failed to configure DHCPv4 client: No such file or directory.

    sudo cloud-init clean --logs
    sudo truncate -s 0 /etc/machine-id
    sudo poweroff
    
  4. On the host, create each new virtual machine’s disk as an overlay backed by the prepared disk instead of the cloud image. After creating the overlays, do not start the prepared virtual machine again: changes to its disk corrupt the overlays.

    sudo qemu-img create -f qcow2 -F qcow2 \
      -b /var/lib/libvirt/images/<PREPARED_VM_DISK>.qcow2 \
      /var/lib/libvirt/images/ubuntu-vm2.qcow2 100G
    
  5. Create a cloud-init seed ISO and the virtual machine as described in Installing the Virtualization Stack and Preparing Cloud-init Configuration and Creating and Configuring Virtual Machines with GPU Virtual Functions, using a unique instance-id and hostname. The GPU is available in the new virtual machine as soon as it boots.