Create and upload a CentOS-based Linux VHD - Azure Virtual Machines (2023)

  • Article
  • 10 minutes to read

Applies to: ✔️ Linux VMs ✔️ Flexible scale sets

Learn to create and upload an Azure virtual hard disk (VHD) that contains a CentOS-based Linux operating system.

  • Prepare a CentOS 6.x virtual machine for Azure
  • Prepare a CentOS 7.0+ virtual machine for Azure

Prerequisites

This article assumes that you have already installed a CentOS (or similar derivative) Linux operating system to a virtual hard disk. Multiple tools exist to create .vhd files, for example a virtualization solution such as Hyper-V. For instructions, see Install the Hyper-V Role and Configure a Virtual Machine.

CentOS installation notes

  • Please see also General Linux Installation Notes for more tips on preparing Linux for Azure.
  • The VHDX format is not supported in Azure, only fixed VHD. You can convert the disk to VHD format using Hyper-V Manager or the convert-vhd cmdlet. If you are using VirtualBox this means selecting Fixed size as opposed to the default dynamically allocated when creating the disk.
  • When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). This will avoid LVM name conflicts with cloned VMs, particularly if an OS disk ever needs to be attached to another identical VM for troubleshooting. LVM or RAID may be used on data disks.
  • Kernel support for mounting UDF file systems is required. At first boot on Azure the provisioning configuration is passed to the Linux VM via UDF-formatted media that is attached to the guest. The Azure Linux agent must be able to mount the UDF file system to read its configuration and provision the VM.
  • Linux kernel versions below 2.6.37 do not support NUMA on Hyper-V with larger VM sizes. This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in RHEL 6.6 (kernel-2.6.32-504). Systems running custom kernels older than 2.6.37, or RHEL-based kernels older than 2.6.32-504 must set the boot parameter numa=off on the kernel command-line in grub.conf. For more information see Red Hat KB 436883.
  • Do not configure a swap partition on the OS disk. More information about this can be found in the steps below.
  • All VHDs on Azure must have a virtual size aligned to 1MB. When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1MB before conversion. See Linux Installation Notes for more information.

CentOS 6.x

  1. In Hyper-V Manager, select the virtual machine.

  2. Click Connect to open a console window for the virtual machine.

  3. In CentOS 6, NetworkManager can interfere with the Azure Linux agent. Uninstall this package by running the following command:

    sudo rpm -e --nodeps NetworkManager
  4. Create or edit the file /etc/sysconfig/network and add the following text:

    NETWORKING=yesHOSTNAME=localhost.localdomain
  5. Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

    DEVICE=eth0ONBOOT=yesBOOTPROTO=dhcpTYPE=EthernetUSERCTL=noPEERDNS=yesIPV6INIT=no
  6. Modify udev rules to avoid generating static rules for the Ethernet interface(s). These rules can cause problems when cloning a virtual machine in Microsoft Azure or Hyper-V:

    sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rulessudo rm -f /etc/udev/rules.d/70-persistent-net.rules
  7. Ensure the network service will start at boot time by running the following command:

    sudo chkconfig network on
  8. If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. This will also add the [openlogic] repository that includes additional packages such as the Azure Linux agent:

    [openlogic]name=CentOS-$releasever - openlogic packages for $basearchbaseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/enabled=1gpgcheck=0[base]name=CentOS-$releasever - Base#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6#released updates[updates]name=CentOS-$releasever - Updates#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6#additional packages that may be useful[extras]name=CentOS-$releasever - Extras#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6#additional packages that extend functionality of existing packages[centosplus]name=CentOS-$releasever - Plus#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6#contrib - packages by Centos Users[contrib]name=CentOS-$releasever - Contrib#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/contrib/$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

    Note

    (Video) How to Bring Your Own Operating System to Azure with Virtual Machine Hard Disk Images (VHD)

    The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  9. Add the following line to /etc/yum.conf:

     http_caching=packages
  10. Run the following command to clear the current yum metadata and update the system with the latest packages:

    yum clean all

    Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

    sudo yum -y update

    A reboot may be required after running this command.

  11. (Optional) Install the drivers for the Linux Integration Services (LIS).

    Important

    The step is required for CentOS 6.3 and earlier, and optional for later releases.

    sudo rpm -e hypervkvpd ## (may return error if not installed, that's OK)sudo yum install microsoft-hyper-v

    Alternatively, you can follow the manual installation instructions on the LIS download page to install the RPM onto your VM.

  12. Install the Azure Linux Agent and dependencies. Start and enable waagent service:

    sudo yum install python-pyasn1 WALinuxAgentsudo service waagent startsudo chkconfig waagent on

    The WALinuxAgent package will remove the NetworkManager and NetworkManager-gnome packages if they were not already removed as described in step 3.

  13. Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. To do this, open /boot/grub/menu.lst in a text editor and ensure that the default kernel includes the following parameters:

    console=ttyS0 earlyprintk=ttyS0 rootdelay=300

    This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues.

    In addition to the above, it is recommended to remove the following parameters:

    (Video) Upload a Local VM Image to Microsoft Azure

    rhgb quiet crashkernel=auto

    Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. The crashkernel option may be left configured if desired, but note that this parameter will reduce the amount of available memory in the VM by 128MB or more, which may be problematic on the smaller VM sizes.

    Important

    CentOS 6.5 and earlier must also set the kernel parameter numa=off. See Red Hat KB 436883.

  14. Ensure that the SSH server is installed and configured to start at boot time. This is usually the default.

  15. Do not create swap space on the OS disk.

    The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. Note that the local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. After installing the Azure Linux Agent (see previous step), modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=yResourceDisk.Filesystem=ext4ResourceDisk.MountPoint=/mnt/resourceResourceDisk.EnableSwap=yResourceDisk.SwapSizeMB=2048 ## NOTE: set this to whatever you need it to be.
  16. Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

    sudo waagent -force -deprovision+userexport HISTSIZE=0logout
  17. Click Action -> Shut Down in Hyper-V Manager. Your Linux VHD is now ready to be uploaded to Azure.

CentOS 7.0+

Changes in CentOS 7 (and similar derivatives)

Preparing a CentOS 7 virtual machine for Azure is very similar to CentOS 6, however there are several important differences worth noting:

  • The NetworkManager package no longer conflicts with the Azure Linux agent. This package is installed by default and we recommend that it is not removed.

  • GRUB2 is now used as the default bootloader, so the procedure for editing kernel parameters has changed (see below).

  • XFS is now the default file system. The ext4 file system can still be used if desired.

  • Since CentOS 8 Stream and newer no longer include network.service by default, you will need to install it manually:

    (Video) Az 104 Microsoft Azure Administrator - Upload VHD to Azure Portal, Create VM

     sudo yum install network-scripts sudo systemctl enable network.service

Configuration Steps

  1. In Hyper-V Manager, select the virtual machine.

  2. Click Connect to open a console window for the virtual machine.

  3. Create or edit the file /etc/sysconfig/network and add the following text:

     NETWORKING=yes HOSTNAME=localhost.localdomain
  4. Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

     DEVICE=eth0 ONBOOT=yes BOOTPROTO=dhcp TYPE=Ethernet USERCTL=no PEERDNS=yes IPV6INIT=no NM_CONTROLLED=no
  5. Modify udev rules to avoid generating static rules for the Ethernet interface(s). These rules can cause problems when cloning a virtual machine in Microsoft Azure or Hyper-V:

     sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
  6. If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. This will also add the [openlogic] repository that includes packages for the Azure Linux agent:

    [openlogic]name=CentOS-$releasever - openlogic packages for $basearchbaseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/enabled=1gpgcheck=0[base]name=CentOS-$releasever - Base#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#released updates[updates]name=CentOS-$releasever - Updates#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that may be useful[extras]name=CentOS-$releasever - Extras#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that extend functionality of existing packages[centosplus]name=CentOS-$releasever - Plus#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infrabaseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    Note

    The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  7. Run the following command to clear the current yum metadata and install any updates:

     sudo yum clean all

    Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

     sudo yum -y update

    A reboot maybe required after running this command.

  8. Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. To do this, open /etc/default/grub in a text editor and edit the GRUB_CMDLINE_LINUX parameter, for example:

     GRUB_CMDLINE_LINUX="rootdelay=300 console=ttyS0 earlyprintk=ttyS0 net.ifnames=0"

    This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues. It also turns off the new CentOS 7 naming conventions for NICs. In addition to the above, it is recommended to remove the following parameters:

    (Video) Create Azure VM image for Azure Marketplace Virtual Machine Offer using Packer with VHD destination

     rhgb quiet crashkernel=auto

    Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. The crashkernel option may be left configured if desired, but note that this parameter will reduce the amount of available memory in the VM by 128MB or more, which may be problematic on the smaller VM sizes.

  9. Once you are done editing /etc/default/grub per above, run the following command to rebuild the grub configuration:

     sudo grub2-mkconfig -o /boot/grub2/grub.cfg
  10. If building the image from VMware, VirtualBox or KVM: Ensure the Hyper-V drivers are included in the initramfs:

    Edit /etc/dracut.conf, add content:

    add_drivers+=" hv_vmbus hv_netvsc hv_storvsc "

    Rebuild the initramfs:

    sudo dracut -f -v
  11. Install the Azure Linux Agent and dependencies for Azure VM Extensions:

    sudo yum install python-pyasn1 WALinuxAgentsudo systemctl enable waagent
  12. Install cloud-init to handle the provisioning

    yum install -y cloud-init cloud-utils-growpart gdisk hyperv-daemons# Configure waagent for cloud-initsed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=y/g' /etc/waagent.confsed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.confecho "Adding mounts and disk_setup to init stage"sed -i '/ - mounts/d' /etc/cloud/cloud.cfgsed -i '/ - disk_setup/d' /etc/cloud/cloud.cfgsed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfgsed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfgecho "Allow only Azure datasource, disable fetching network setting via IMDS"cat > /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg <<EOFdatasource_list: [ Azure ]datasource: Azure: apply_network_config: FalseEOFif [[ -f /mnt/resource/swapfile ]]; thenecho Removing swapfile - RHEL uses a swapfile by defaultswapoff /mnt/resource/swapfilerm /mnt/resource/swapfile -ffiecho "Add console log file"cat >> /etc/cloud/cloud.cfg.d/05_logging.cfg <<EOF# This tells cloud-init to redirect its stdout and stderr to# 'tee -a /var/log/cloud-init-output.log' so the user can see output# there without needing to look on the console.output: {all: '| tee -a /var/log/cloud-init-output.log'}EOF
  13. Swap configurationDo not create swap space on the operating system disk.

    Previously, the Azure Linux Agent was used automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. However this is now handled by cloud-init, you must not use the Linux Agent to format the resource disk create the swap file, modify the following parameters in /etc/waagent.conf appropriately:

    sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.confsed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf

    If you want mount, format and create swap you can either:

    • Pass this in as a cloud-init config every time you create a VM

    • Use a cloud-init directive baked into the image that will do this every time the VM is created:

      echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.confcat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF#cloud-config# Generated by Azure cloud image builddisk_setup: ephemeral0: table_type: mbr layout: [66, [33, 82]] overwrite: Truefs_setup: - device: ephemeral0.1 filesystem: ext4 - device: ephemeral0.2 filesystem: swapmounts: - ["ephemeral0.1", "/mnt"] - ["ephemeral0.2", "none", "swap", "sw,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]EOF
  14. Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

    Note: if you are migrating a specific virtual machine and do not wish to create a generalized image, skip the deprovision step

    # sudo rm -f /var/log/waagent.log# sudo cloud-init clean# waagent -force -deprovision+user# rm -f ~/.bash_history# export HISTSIZE=0# logout
  15. Click Action -> Shut Down in Hyper-V Manager. Your Linux VHD is now ready to be uploaded to Azure.

    (Video) How to Create a CentOS 7.5 Virtual Machine on Azure

Next steps

You're now ready to use your CentOS Linux virtual hard disk to create new virtual machines in Azure. If this is the first time that you're uploading the .vhd file to Azure, see Create a Linux VM from a custom disk.

FAQs

How do I upload a VHD to Azure VM? ›

Click Upload and select the Azure-compliant Security Access Manager VHD file to upload.
...
Create an image using the Azure Portal.
  1. In the Azure Portal, select Images.
  2. Click Add to create a new image. Give the image a name. ...
  3. When the process has completed, return to the Images panel and verify that the new image was created.

How do I upload a file from Linux VM to Azure? ›

How to transfer data to or from a Linux VM in Azure?
  1. Transfer data from Local PC to Linux VM. To configure WinScp, go to Advanced, SSH, Authentication and select the private key file. ...
  2. Install vsftpd and test locally. ...
  3. VSFTPD setup: ...
  4. anonymous_enable=YES. ...
  5. anonymous_enable=NO. ...
  6. anymous_enable=YES. ...
  7. local_enable=YES. ...
  8. write_enable=YES.
May 9, 2019

How do I create a virtual machine in CentOS 7? ›

Follow installation steps of KVM on CentOS 7/RHEL 7 headless sever
  1. Step 1: Install kvm. Type the following yum command: ...
  2. Step 2: Verify kvm installation. ...
  3. Step 3: Configure bridged networking. ...
  4. Step 4: Create your first virtual machine. ...
  5. Step 5: Using cloud images.
May 25, 2022

How do I create Azure Linux virtual machine? ›

How to Create Linux VM in Azure | Create a Linux Virtual Machine

How do I create a VHD virtual machine? ›

After you have the managed disk VHD that you want to use, you can create the VM in the portal: From the Azure portal, on the left menu, select All services. In the All services search box, enter disks and then select Disks to display the list of available disks. Select the disk that you would like to use.

How do I upload to Azure virtual machine? ›

Check the drives you want the Azure VM to access then click OK and Connect to launch your Azure RDP session. Once the Azure RDP connection has opened you can use File Explorer to open the local drive and free drag-and-drop file between the local drive and the drives configured in the Azure VM.

Videos

1. Creating Virtual Machines (VMs) in Microsoft Azure - AZ-800
(BurningIceTech)
2. 06. Module 2 - Lesson 2 - Azure Virtual Machines - Creating Virtual Machines
(AzureBytes)
3. Create Virtual Machine On Linux Operating System Using Microsoft Azure Part 1
(PROGRAMMING VEINS)
4. Cloud-Init for Azure VM’s
(WintellectNOW)
5. Using Red Hat Enterprise Linux Gold Images in Microsoft Azure
(Azure in the Enterprise)
6. Azure - RDP to a Linux Virtual Machine from scratch
(ROCITWORKS)
Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated: 12/18/2022

Views: 5698

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.