Create a virtual machine template manually

To create a template from an existing virtual machine (VM), you can manually remove specific configurations from a guest VM to prepare it for cloning.

Before you begin

  • Ensure that you know the location of the disk image for the source VM and are the owner of the VM’s disk image file.

    Note that disk images for VMs created in the system connection of libvirt are by default located in the /var/lib/libvirt/images directory and owned by the root user:

    # ls -la /var/lib/libvirt/images
    -rw-------.  1 root root  9665380352 Jul 23 14:50 a-really-important-vm.qcow2
    -rw-------.  1 root root  8591507456 Jul 26  2017 an-actual-vm-that-i-use.qcow2
    -rw-------.  1 root root  8591507456 Jul 26  2017 totally-not-a-fake-vm.qcow2
    -rw-------.  1 root root 10739318784 Sep 20 17:57 another-vm-example.qcow2
  • Ensure that the VM is shut down.
  • Optional: Any important data on the VM’s disk has been backed up. If you want to preserve the source VM intact, clone it first and edit the clone to create a template.

Procedure

  1. Configure the VM for cloning:
    1. Install any software needed on the clone.
    2. Configure any non-unique settings for the operating system.
    3. Configure any non-unique application settings.
  2. Remove the network configuration:
    1. Remove any persistent udev rules by using the following command:
      # rm -f /etc/udev/rules.d/70-persistent-net.rules
      Note

      If udev rules are not removed, the name of the first NIC might be eth1 instead of eth0.

    2. Remove unique information from the NMConnection files in the /etc/NetworkManager/system-connections/ directory.
      1. Remove MAC address, IP address, DNS, gateway, and any other unique information or unwanted settings.
        *ID=ExampleNetwork
        BOOTPROTO="dhcp"
        HWADDR="AA:BB:CC:DD:EE:FF"                  <- REMOVE
        NM_CONTROLLED="yes"
        ONBOOT="yes"
        TYPE="Ethernet"
        UUID="954bd22c-f96c-4b59-9445-b39dd86ac8ab" <- REMOVE
      2. Remove similar unique information and unwanted settings from the /etc/hosts and /etc/resolv.conf files.
  3. Remove registration details:
    • For VMs registered on the Red Hat Network (RHN):
      # rm /etc/sysconfig/rhn/systemid
    • For VMs registered with Red Hat Subscription Manager (RHSM):
      • If you do not plan to use the original VM:
        # subscription-manager unsubscribe --all
        # subscription-manager unregister
        # subscription-manager clean
      • If you plan to use the original VM:
        # subscription-manager clean

        The original RHSM profile remains in the Portal, along with your ID code. To reactivate your RHSM registration on the VM after it is cloned, use the following command:

        # subscription-manager register --consumerid=71rd64fx-6216-4409-bf3a-e4b7c7bd8ac9
  4. Remove other unique details:
    1. Remove SSH public and private key pairs:
      # rm -rf /etc/ssh/ssh_host_example
    2. Remove the configuration of LVM devices:
      # rm /etc/lvm/devices/system.devices
    3. Remove any other application-specific identifiers or configurations that might cause conflicts if running on multiple machines.
  5. Remove the gnome-initial-setup-done file to configure the VM to run the configuration wizard on the next boot:
    # rm ~/.config/gnome-initial-setup-done

    Note that the wizard that runs on the next boot depends on the configurations removed from the VM. In addition, during the clone’s first boot, change the hostname to avoid duplication issues.