Creating a Template

For this example scenario, you seal the Oracle Linux virtual machine created in Creating a New Virtual Machine and then you create an Oracle Linux template based on that virtual machine. You then use that template as the basis for a Cloud-Init enabled template to automate the initial setup of a virtual machine.

A template is a copy of a virtual machine that you can use to simplify the subsequent, repeated creation of similar virtual machines. Templates capture the configuration of software, the configuration of hardware, and the software installed on the virtual machine on which the template is based, which is known as the source virtual machine.

Virtual machines that are created based on a template use the same NIC type and driver as the original virtual machine but are assigned separate, unique MAC addresses.

Sealing an Oracle Linux Virtual Machine for Use as a Template

Sealing is the process of removing all system-specific details from a virtual machine before creating a template based on that virtual machine. Sealing is necessary to prevent the same details from appearing on multiple virtual machines that are created based on the same template. It is also necessary to ensure the functionality of other features, such as predictable vNIC order.

To seal an Oracle Linux virtual machine for use as a template:

  1. Log in to the Oracle Linux virtual machine as the root user.

  2. Flag the system for reconfiguration.

     # touch /.unconfigured
  3. Remove the SSH host keys.

    # rm -rf /etc/ssh/ssh_host_*
  4. Set the host name value of the HOSTNAME=localhost.localdomain in the /etc/sysconfig/network file for Oracle Linux 6 or the /etc/hostname file for Oracle Linux 7.

  5. Remove /etc/udev/rules.d/70-*.

    rm -rf /etc/udev/rules.d/70-*
  6. Remove the HWADDR and UUID lines in the /etc/sysconfig/network-scripts/ifcfg-eth* file.

  7. (Optional) Delete all the logs from /var/log and build logs from /root.

  8. Cleanup the command history.

    # history -c
  9. Shutdown the virtual machine.

    # poweroff

    The Oracle Linux virtual machine is now sealed and ready to be made into a template.

Creating an Oracle Linux Template

When you create a template based on a virtual machine, a read-only copy of the virtual machine's disk is created. This read-only disk becomes the base disk image of the new template, and of any virtual machines that are created based on the template. As such, the template cannot be deleted while any virtual machines based on that template exist in the virtualization environment.

To create an Oracle Linux template:

  1. Go to Compute, and then click Virtual Machines.

    The Virtual Machines pane opens with the list of virtual machines that have been created.

  2. Click More Actions to expand the drop-down list and select Make Template from the drop-down list.

    The following screenshot shows the More Actions drop-down list expanded to display the Make Template option. The Make Template option is highlighted with a red rectangular box for emphasis.

    Figure 12 Make Template Option

    The More Actions drop-down list expanded to display the Make Template option, as described in the preceding text.

  3. For the Name field, enter a name for the new virtual machine template.

  4. In the Disc Allocation: section under the Alias column, rename the disk alias to be the same as the template name entered for the Name field.

  5. Click the Seal Template (Linux only) checkbox.

    The following screenshot shows the New Template dialog box completed for the new template named ol7-vm-template, which is being created in this example scenario. In the dialog box, the disk alias has been renamed to ol7-vm-template and the Seal Template (Linux only) checkbox is selected.

    Figure 13 New Template Dialog Box


  6. Click the OK button to create the template.

    The virtual machine displays a status of image Locked while the template is being created. The time it takes for the template to be created depends on the size of the virtual disk and the capabilities of your storage hardware. When the template creation process completes, the template is added to the list of templates displayed on the Templates pane.

    You can now create new Oracle Linux virtual machines that are based on this template.

Creating a Cloud-Init Enabled Template

For Oracle Linux 7 and later virtual machines, you can use the Cloud-Init tool to automate the initial setup of virtual machines. Common tasks, such as configuring host names, network interfaces, and authorized keys, can be automated by using this tool. When provisioning virtual machines that have been deployed based on a template, the Cloud-Init tool can be used to prevent conflicts on the network.

Before You Begin

Before you create Cloud-Init enabled templates, ensure the following prerequisites are met:

  • To use Cloud-Init, the cloud-init package must first be installed on the virtual machine. Once installed, the Cloud-Init service starts during the boot process and searches for instructions on what to configure. You can use options in the Run Once window to provide these instructions on a one-time only basis, or use the options in the New Virtual Machine, Edit Virtual Machine, and Edit Template dialog boxes to provide these instructions every time the virtual machine starts.

  • You must have seal an Oracle Linux for use as a template. For more information, refer to Sealing an Oracle Linux Virtual Machine for Use as a Template.

  • You must create a template. For more information, refer to Creating an Oracle Linux Template.

  1. Log in to a Oracle Linux virtual machine.

  2. List the cloud-init package.

    # yum list cloud-init
  3. Install the cloud-init package.

    # yum install cloud-init
  4. Run the following command to enable the cloud-init service.

    # systemctl enable cloud-init
  5. Run the following command to start the cloud-init service.

    # systemctl start cloud-init

Using Cloud-Init to Automate the Initial Setup of a Virtual Machine

To use Cloud-Init to automate the initial setup of a virtual machine:

  1. Go to Compute and then click Templates.

    The Templates pane opens with the list of templates that have been created.

  2. Select a template and click the Edit button.

  3. Click Show Advanced Options.

  4. Click the Initial Run tab and select the Use Cloud-Init/Sysprep check box.

  5. Enter a host name in the VM Hostname text field.

  6. Select the Configure Time Zone check box and select a time zone from the Time Zone drop-down list.

  7. Expand the Authentication section.

    • Select the Use already configured password check box to use the existing credentials, or clear that check box and enter a root password in the Password and Verify Password text fields to specify a new root password.

    • Enter any SSH keys to be added to the authorized hosts file on the virtual machine in the SSH Authorized Keys text area.

    • Select the Regenerate SSH Keys check box to regenerate SSH keys for the virtual machine.

  8. Expand the Networks section.

    • Enter any DNS servers in the DNS Servers text field.

    • Enter any DNS search domains in the DNS Search Domains text field.

    • Select the In-guest Network Interface check box and use the + Add new and - Remove selected buttons to add or remove network interfaces to or from the virtual machine.

      Important

      You must specify the correct network interface name and number (for example, eth0, eno3, enp0s); otherwise, the virtual machine’s interface connection will be up but will not have the Cloud-Init network configuration.

  9. Expand the Custom Script section and enter any custom scripts in the Custom Script text area.