To prepare a Linux image for Azure, you'll need to create a generalizable image that can be used across different environments. This means setting up a base image with the necessary packages and configurations.
First, you'll need to create a base image with the Linux distribution of your choice. For example, you can use Ubuntu Server 18.04 LTS as a base image. This image should be configured with the necessary packages, such as SSH and the Azure CLI.
Next, you'll need to configure the image to use the Azure cloud provider. You can do this by installing the Azure cloud provider package and configuring the image to use it. This will allow the image to interact with Azure services and resources.
Make sure to also configure the image's networking settings to use Azure's virtual network and subnet. This will allow the image to communicate with other Azure resources and services.
Preparation Steps
To prepare a Linux image for Azure, you'll need to remove local identifiers. This is crucial to avoid network conflicts and duplicate SSH keys.
Azure advises against using swap files on VM images, so you'll need to disable swap. To do this, run the command to disable swap.
You'll also need to replace swap entries in the /etc/fstab file to prevent swapping from re-enabling at the next boot.
After completing these steps, you can capture the VM image.
Image Requirements
To prepare a Linux image for Azure, you need to ensure it meets the image requirements.
Azure requires a Linux image to have a supported file system, such as ext4 or XFS, as mentioned in the "File System Support" section.
The image must be compressed using a supported algorithm, like gzip or xz, to reduce its size and improve deployment speed, as explained in the "Image Compression" section.
Azure also requires a Linux image to have a supported kernel version, such as Ubuntu 18.04 or later, which provides the necessary drivers and features for Azure, as mentioned in the "Kernel Requirements" section.
The image should also have a supported distribution, like Ubuntu or CentOS, to ensure compatibility with Azure's infrastructure, as explained in the "Distribution Requirements" section.
Azure Image Upload
To upload a Linux image to Azure, you'll need to create a managed disk from your VHD file. This can be done by uploading the VHD file to Azure to create a managed disk.
You can use Azure CLI or PowerShell to upload the image to your Azure Storage account. The format for Linux images should be VHD (fixed) as a default rule of thumb. Make sure the image fulfills Azure provisions, such as having a disk size of 1 TiB or less.
Here are the general steps for uploading a Linux image to Azure:
- Upload the VHD file to Azure to create a managed disk.
- Create an image definition and version in a compute gallery.
Alternatively, you can use an Azure shared image repository to share the built images with certain subscriptions and even different tenants. This involves creating an Azure image repository via the portal, Azure CLI, or PowerShell, and then referencing the image definition in your Packer template to upload the image to the gallery.
Upload to Azure
Uploading to Azure is a crucial step in creating and deploying custom images. You can upload a custom image to a compute gallery by creating a managed disk from a VHD file.
To create a managed disk, you need to upload the VHD file to Azure. This can be done using the Azure CLI or PowerShell. Once uploaded, you can create an image definition and version in a compute gallery.
For Linux images, the format should be VHD (fixed) as a default rule of thumb. The image should also fulfill Azure provisions, such as a disk size of 1 TiB or less.
You can also use Azure CLI or PowerShell to upload the image to your Azure Storage account. This is a straightforward process that involves copying the Hyper-V image of the VM as an image file.
To create a custom image in Azure, you can use Packer, a tool that automates the creation of images. You'll need to create a new JSON file to create a Packer image from a service principal.
Here are the steps to upload an image to Azure:
- Upload the VHD file to Azure to create a managed disk.
- Create an image definition and version in a compute gallery.
- Use Azure CLI or PowerShell to upload the image to your Azure Storage account.
- Use Packer to create a custom image in Azure.
- Create a new JSON file to create a Packer image from a service principal.
By following these steps, you can successfully upload your custom image to Azure and create a managed disk, image definition, and version in a compute gallery.
Azure Agent
The Azure Linux Agent is a crucial component when uploading an image to Azure. It provisions a Linux virtual machine in Azure.
You can get the latest version of the Azure Linux Agent from the Linux Agent GitHub repo. The agent is released under the Apache 2.0 license.
The Azure Linux Agent requires Python v2.6+ and the python-pyasn1 module. Most distributions provide the python-pyasn1 module as a separate package to be installed.
Some distributions configure NetworkManager as a conflict to the waagent package. In these cases, the agent will uninstall NetworkManager when you install the Linux agent package.
Make sure the udf and vfat modules are enabled. Disabling the udf module will cause a provisioning failure, while disabling the vfat module will cause both provisioning and boot failures.
Cloud-init version 21.2 or later can provision VMs without requiring UDF if you meet two conditions: you created the VM by using SSH public keys and not passwords, and you didn't provide any custom data.
Packer Configuration
To create a custom Linux image for Azure, you'll need to create a new JSON file, as mentioned in the Azure documentation. This file will serve as a template for your Packer image.
You can start by creating a Packer template, like the one shown in the example, called azure-ubuntu-nginx-packer.json. This template will create a custom Ubuntu 18.04 LTS image with nginx installed for Azure.
The template includes a line that speeds up the delete process, which can be set to true to ensure resources are deleted efficiently. This is a good practice to avoid any potential issues.
After running a successful build, the image will be created in the resource group specified in the RESOURCE_GROUP_NAME environment variable. This is where you can find the image details and create a VM from it.
The image will include nginx installed and running by default, which can be useful for load balancers or other scenarios.
Installation and Setup
To prepare a Linux image for Azure, you'll need to install and configure Linux on a physical PC or virtual host. Install the Linux distribution on a physical PC or virtual host with minimal resource usage.
Update your system packages by running the following commands: update system packages
Next, connect to the Azure Linux Agent, known as waagent, to enable features like provisioning, networking, and resizing of the VM. Most distributions include the agent, but if it's not available, install it using the following command: install waagent using
To set up SSH access, allow initial SSH for administration and configure the following security settings: open the ssh configuration file at /etc/ssh/sshd_configcreate ssh keys and incorporate into VM avoiding logins with passwords
Finally, set your network interfaces to auto-obtain an IP address using DHCP, which is used by Azure to assign IPs to VMs. Check that the configured interfaces are set to obtain their configuration via DHCP by using the /etc/network/interfaces file or any distro-specific file.
Set Up
To set up your environment for building and deploying images, you'll need to install a few tools. Packer is a great tool for building images, and you can install it via a Packer install guide.
You'll also need to install PowerShell Core, which is a cross-platform version of PowerShell. You can find the install guide for PowerShell Core online.
Once you have Packer and PowerShell Core installed, you'll need to install the Azure CLI, which is a command-line interface for Azure. The Azure CLI install guide is a good resource for this.
To interact with Azure, you'll also need to install the PowerShell AZ module, which provides a set of cmdlets for managing Azure resources.
You can authenticate to Azure in two ways when using Packer to build images. One way is to set environment variables in PowerShell using the following code:
You can also use the Azure CLI to authenticate to Azure.
Here's a list of the tools you'll need to install:
- Packer
- PowerShell Core
- Azure CLI
- PowerShell AZ module
These tools will give you the foundation you need to start building and deploying images with Packer.
Install Kernel Modules
To install kernel modules without Hyper-V, you might need to rebuild the initrd image. This is because Linux installers might not include the drivers for Hyper-V in the initial RAM disk, unless the VM detects that it's running in a Hyper-V environment.
To rebuild the initrd image, you'll need to use a utility like mkinitrd. For example, on a Red Hat distribution, you can use the following command: `sudo mkinitrd --preload=hv_storvsc --preload=hv_vmbus -v -f initrd-`uname -r`.img `uname -r`.
You'll also need to back up the existing initrd image first, by running `cd /boot` and `sudo cp initrd-`uname -r`.img initrd-`uname -r`.img.bak`.
You should also check your distribution's documentation for the proper procedure, as the mechanism for rebuilding the initrd image can vary.
Here are the specific kernel modules you'll need to include in the initrd image: `hv_vmbus` and `hv_storvsc`.
Sources
- https://learn.microsoft.com/en-us/azure/lab-services/how-to-bring-custom-linux-image-vhd
- https://learn.microsoft.com/en-us/azure/virtual-machines/linux/create-upload-generic
- https://technobursts.com/how-to-prepare-a-linux-image-for-azure-template/
- https://www.flexmind.co/use-hashicorp-packer-to-automate-machine-images-in-azure/
- https://matthewdavis111.com/packer/packer-images-azure/
Featured Images: pexels.com