Prepare an Ubuntu image for Azure Local virtual machines

Applies to: Azure Local, version 23H2

This article describes how to prepare an Ubuntu image to create a virtual machine (VM) on your Azure Local. You use the Azure CLI for the VM image creation.

Prerequisites

Before you begin, meet the following prerequisites:

  • Have access to an Azure Local instance. This system is deployed, registered, and connected to Azure Arc. Go to the Overview page in the Azure Local resource. On the Server tab on the right pane, Azure Arc should appear as Connected.
  • Download the latest supported Ubuntu server image on your Azure Local system. The supported OS versions are Ubuntu 18.04, 20.04, and 22.04 LTS. You prepare this image to create a VM image.

Workflow

To prepare an Ubuntu image and create a VM image from that image:

  1. Create an Ubuntu VM
  2. Configure the VM
  3. Clean up the residual configuration
  4. Create an Ubuntu VM image

The following sections provide detailed instructions for each step in the workflow.

Create a VM image from an Ubuntu image

Important

  • Do not use an Azure Virtual Machine VHD disk to prepare the VM image for Azure Local.
  • We recommend that you prepare an Ubuntu image if you intend to enable guest management on the VMs.

Follow these steps on your Azure Local to create a VM image by using the Azure CLI.

Step 1: Create an Ubuntu VM

To use the downloaded Ubuntu image to provision a VM:

  1. Use the downloaded image to create a VM with the following specifications:

    1. Provide a friendly name for your VM.

      Screenshot that shows the New Virtual Machine Wizard on the Specify Name and Location page.

    2. Specify Generation 2 for your VM as you're working with a VHDX image here.

      Screenshot that shows the New Virtual Machine Wizard on the Specify Generation page.

    3. Select Install operating system from a bootable image. Point to the ISO that you downloaded earlier.

      Screenshot that shows the New Virtual Machine Wizard on the Installation Options page.

    For step-by-step instructions, see Provision a VM by using Hyper-V Manager.

  2. Use the UEFI certificate to secure boot the VM.

    1. After the VM is created, it shows up in Hyper-V Manager. Select the VM, right-click it, and then select Settings.
    2. On the left pane, select the Security tab. Then under Secure Boot, from the Template dropdown list, select Microsoft UEFI Certificate Authority.
    3. Select OK to save the changes.

    Screenshot that shows the Secure Boot options for the VM on the Settings page.

Step 2: Configure the VM

Follow these steps on your Azure Local to configure the VM that you provisioned earlier:

  1. Sign in to the VM. See the steps in Connect to a Linux VM.

  2. To download all the latest package lists from the repositories, run the following command:

    sudo apt update
    
  3. Install the Azure tailored kernel. This step is required for your VM to get an IP for the network interface.

    sudo apt install linux-azure -y
    
  4. Install the SSH server. Run the following command:

    sudo apt install openssh-server openssh-client -y
    
  5. Configure passwordless sudo. Add the following command at the end of the /etc/sudoers file by using visudo:

    ALL ALL=(ALL) NOPASSWD:ALL
    

Step 3: Clean up the residual configuration

Delete machine-specific files and data from your VM so that you can create a clean VM image without any history or default configurations. Follow these steps on your Azure Local to clean up the residual configuration.

Note

Depending on the version of Ubuntu you are using, some of these files may not exist.

  1. Clean cloud-init default configurations.

    sudo rm -f /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg /etc/cloud/cloud.cfg.d/curtin-preserve-sources.cfg /etc/cloud/cloud.cfg.d/99-installer.cfg /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg
    sudo rm -f /etc/cloud/ds-identify.cfg
    sudo rm -f /etc/netplan/*.yaml
    
  2. Clean up the logs and cache.

    sudo cloud-init clean --logs --seed
    sudo rm -rf /var/lib/cloud/ /var/log/* /tmp/*
    sudo apt-get clean
    
  3. Remove the bash history.

    rm -f ~/.bash_history 
    export HISTSIZE=0 
    logout
    
  4. Shut down the VM. In Hyper-V Manager, go to Action > Shut Down.

Step 4: Create the VM image

Follow these steps on your Azure Local to create the VM image from the VHDX that you created earlier.

Use the Azure CLI to create the VM image:

  1. Run PowerShell as an administrator.

  2. Sign in. Run the following cmdlet:

    az login
    
  3. Set your subscription. Run the following cmdlet:

    az account set --subscription <Subscription ID>
    
  4. Set parameters for your subscription, resource group, custom location, location, OS type for the image, name of the image, and the path where the image is located. Replace the parameters in < > with the appropriate values.

    $subscription = "<Subscription ID>"
    $resource_group = "<Resource group>"
    $customLocation = "<Custom location>"
    $location = "<Location for your Azure Local>"
    $osType = "<OS of source image>"
    

    Parameters are described in the following table.

    Parameter Description
    subscription Subscription associated with your Azure Local instance.
    resource_group Resource group for the Azure Local instance that you associate with this image.
    location Location for your Azure Local instance. For example, the location could be eastus or westreurope.
    os-type Operating system associated with the source image. This system can be Windows or Linux.
  5. Use the VHDX of the VM to create a gallery image. Use this VM image to create Azure Arc virtual machines on Azure Local.

    Make sure to copy the VHDX in user storage in the cluster shared volume of Azure Local. For example, the path could look like C:\ClusterStorage\UserStorage_1\linuxvhdx.

    $imagePath = "Path to user storage in CSV" 
    $imageName = "mylinuxvmimg" 
    $osType = "Linux"
    
    az stack-hci-vm image create --subscription $subscription -g $resource_group --custom-location $customLocation --location $location --image-path $imagePath --name $imageName --debug --os-type $osType 
    
  6. Verify that the image is created.