Chapter 4. Cloning Virtual Machines

There are two types of guest virtual machine instances used in creating guest copies:
  • Clones are instances of a single virtual machine. Clones can be used to set up a network of identical virtual machines, and they can also be distributed to other destinations.
  • Templates are instances of a virtual machine that are designed to be used as a source for cloning. You can create multiple clones from a template and make minor modifications to each clone. This is useful in seeing the effects of these changes on the system.
Both clones and templates are virtual machine instances. The difference between them is in how they are used.
For the created clone to work properly, information and configurations unique to the virtual machine that is being cloned usually has to be removed before cloning. The information that needs to be removed differs, based on how the clones will be used.
The information and configurations to be removed may be on any of the following levels:
  • Platform level information and configurations include anything assigned to the virtual machine by the virtualization solution. Examples include the number of Network Interface Cards (NICs) and their MAC addresses.
  • Guest operating system level information and configurations include anything configured within the virtual machine. Examples include SSH keys.
  • Application level information and configurations include anything configured by an application installed on the virtual machine. Examples include activation codes and registration information.

    Note

    This chapter does not include information about removing the application level, because the information and approach is specific to each application.
As a result, some of the information and configurations must be removed from within the virtual machine, while other information and configurations must be removed from the virtual machine using the virtualization environment (for example, Virtual Machine Manager or VMware).

Note

For information on cloning storage volumes, see Section 14.3, “Cloning Volumes”.

4.1. Preparing Virtual Machines for Cloning

Before cloning a virtual machine, it must be prepared by running the virt-sysprep utility on its disk image, or by using the following steps:

Procedure 4.1. Preparing a virtual machine for cloning

  1. Setup the virtual machine

    1. Build the virtual machine that is to be used for the clone or template.
      • Install any software needed on the clone.
      • Configure any non-unique settings for the operating system.
      • Configure any non-unique application settings.
  2. Remove the network configuration

    1. Remove any persistent udev rules 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 may be eth1 instead of eth0.
    2. Remove unique network details from ifcfg scripts by making the following edits to /etc/sysconfig/network-scripts/ifcfg-eth[x]:
      1. Remove the HWADDR and Static lines

        Note

        If the HWADDR does not match the new guest's MAC address, the ifcfg will be ignored. Therefore, it is important to remove the HWADDR from the file.
        DEVICE=eth[x]
        BOOTPROTO=none
        ONBOOT=yes
        #NETWORK=10.0.1.0       <- REMOVE
        #NETMASK=255.255.255.0  <- REMOVE
        #IPADDR=10.0.1.20       <- REMOVE
        #HWADDR=xx:xx:xx:xx:xx  <- REMOVE
        #USERCTL=no             <- REMOVE
        # Remove any other *unique* or non-desired settings, such as UUID.
        
      2. Ensure that a DHCP configuration remains that does not include a HWADDR or any unique information.
        DEVICE=eth[x]
        BOOTPROTO=dhcp
        ONBOOT=yes
        
      3. Ensure that the file includes the following lines:
        DEVICE=eth[x]
        ONBOOT=yes
        
    3. If the following files exist, ensure that they contain the same content:
      • /etc/sysconfig/networking/devices/ifcfg-eth[x]
      • /etc/sysconfig/networking/profiles/default/ifcfg-eth[x]

      Note

      If NetworkManager or any special settings were used with the virtual machine, ensure that any additional unique information is removed from the ifcfg scripts.
  3. Remove registration details

    1. Remove registration details using one of the following:
      • For Red Hat Network (RHN) registered guest virtual machines, run the following command:
        # rm /etc/sysconfig/rhn/systemid
      • For Red Hat Subscription Manager (RHSM) registered guest virtual machines:
        • If the original virtual machine will not be used, run the following commands:
          # subscription-manager unsubscribe --all
          # subscription-manager unregister
          # subscription-manager clean
        • If the original virtual machine will be used, run only the following command:
          # subscription-manager clean

          Note

          The original RHSM profile remains in the portal.
  4. Removing other unique details

    1. Remove any sshd public/private key pairs using the following command:
      # rm -rf /etc/ssh/ssh_host_*

      Note

      Removing ssh keys prevents problems with ssh clients not trusting these hosts.
    2. Remove any other application-specific identifiers or configurations that may cause conflicts if running on multiple machines.
  5. Configure the virtual machine to run configuration wizards on the next boot

    1. Configure the virtual machine to run the relevant configuration wizards the next time it is booted by doing one of the following:
      • For Red Hat Enterprise Linux 6 and below, create an empty file on the root file system called .unconfigured using the following command:
        # touch /.unconfigured
      • For Red Hat Enterprise Linux 7, enable the first boot and initial-setup wizards by running the following commands:
        # sed -ie 's/RUN_FIRSTBOOT=NO/RUN_FIRSTBOOT=YES/' /etc/sysconfig/firstboot
        # systemctl enable firstboot-graphical
        # systemctl enable initial-setup-graphical

      Note

      The wizards that run on the next boot depend on the configurations that have been removed from the virtual machine. In addition, on the first boot of the clone, it is recommended that you change the hostname.