피드 구독

Do you plan to run Red Hat Enterprise Linux (RHEL) on Azure, or are you considering it, but you want to create your own images? In this article, I’ll focus on creating an RHEL 7 image to run on Azure Mv2 (aka Hyper-V Generation 2) instances. The main difference from a standard image is that you’ll use the Unified Extensible Firmware Interface (UEFI), and therefore there are additional options required.

Apart from the main focus on Mv2, I’ll also give you a good starter set of commands so you can automate the process of creating those images and eventually integrate that process into your CI/CD pipeline.

Most of these instructions should also be applicable for building Red Hat Enterprise Linux 8 images, but that version wasn’t my primary focus.

Setting up the environment

As the build environment, Red Hat Enterprise Linux 8 provides us with the correct set of tools in order to build UEFI-bootable images. In order to reproduce everything here, you’ll need the following packages installed:

You will also need a copy of the Red Hat Enterprise Linux 7.7 binaries DVD, which you can download here if you have a valid Red Hat subscription. Save this DVD as /var/lib/libvirt/images/rhel-server-7.7-x86_64-dvd.iso.

Note: Any other Red Hat Enterprise Linux 7- or 8-based version should do it, but this walk-through has been tested with 7.7.

In order for the above to work if you’re running on a VM, you’ll need KVM enabled on the host or passed through to the VM. If you want to run this example on a remote VM, or in another CI/CD pipeline, I’ve done exactly that in order to be able to automate the process, so you can run this headless (no X, graphical UI, and X forwarding needed).

Building an image in an automated fashion

We need a few packages installed in our image plus some configurations set, and it’s recommended to have the Azure client installed. While I was searching for the best way to get this client installed in an automated fashion, the only option I found was to obtain it from the CentOS 7 extras repository (reach out to me and let me know if you know a better way).

A sample kickstart.cfg file can be obtained here. Adapt it to your needs, but make sure to leave at least the network configuration set to DHCP.

Building the image with the Kickstart file and a simple command

To build the image: 

$ sudo virt-install \
       --name rhel77 \
       --memory 4096 \
       --vcpus 4 \
       --boot uefi \
       --os-variant rhel7.7 \
       -l /var/lib/libvirt/images/rhel-server-7.7-x86_64-dvd.iso \
       --disk size=4 \
       --initrd-inject $(pwd)/kickstart.cfg \
       --extra-args="ks=file:/kickstart.cfg console=tty0 console=ttyS0,115200n8" \
       --graphics=none \
       --network network=default \
       --noreboot

As mentioned above, you’ll need to have the DVD image in the correct place. My example assumes that you:

  • Have kickstart.cfg in your current $PWD.
  • Want a 4GB disk.
  • Are using your local libvirt/KVM install.
  • Have no special requirements regarding the network interface (I’m using the defaults).

Converting the image to the right format

I struggled to convert the image to the right format, but with friendly colleagues and Google, I finally figured out the format required: VHD aligned to 1MB.

The last built image should be the correct one:

$ image=$(ls -1tr /var/lib/libvirt/images/rhel*|tail -1)

Use sudo to access the image, convert it to RAW and put it into the current $PWD

$ sudo qemu-img convert -f qcow2 -O raw $image rhel-7.7.raw

Make sure we can access it - after this point, a “normal” (eg. non-root use can continue)

$ sudo chown $(whoami) *.raw

Calculate the size of the image.

$ MB=$((1024*1024)) size=$(qemu-img info -f raw --output json "rhel-7.7.raw" | \ gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}') rounded_size=$((($size/$MB + 1)*$MB))

Resize the image.

$ sudo qemu-img resize -f raw rhel-7.7.raw $rounded_size

Convert the image from RAW to VHD and force the correct size

$ sudo qemu-img convert -f raw -o subformat=fixed,force_size -O vpc rhel-7.7.raw rhel-7.7.vhd

Creating a group

Now you have your own customized Red Hat Enterprise Linux 7 image. You’re probably wondering how to upload it to Azure and use it to run an instance. Read further. Since I was had to test my whole build process, I’ve done this as well.

You’ll always need a resource group in Azure. I’ve chosen westus2 as my region, but any other should do as well (if they have Mv2 instances). The name for my resource group is simply myresourcegroup. If you want to use another name, you’ll have to replace it everywhere that I used it.

To create your group, create AZ resource group in westus2 and name “myresourcegroup”.

$ sudo az group create -l westus2 -n myresourcegroup

Uploading the disk image and making it an Azure image

To upload the disk image and make it into an Azure image, create the disk in Azure with the name ‘rhel-7’, in our previously created resource group ‘myresourcegroup’.

$ sudo az disk delete --name rhel-7 -g myresourcegroup -y

Make sure it’s Hyper-V Gen 2 and operating system type Linux with the correct size.

$ sudo az disk create --hyper-v-generation V2 -g myresourcegroup -n rhel-7 --os-type Linux -l westus2 --for-upload --upload-size-bytes $(wc -c rhel-7.7.vhd|awk '{ print $1 }') --sku standard_lrs

You need a SASURI for uploading.

$ SASURI=$(az disk grant-access -n rhel-7 -g myresourcegroup --access-level Write --duration-in-seconds 86400 --query [accessSas] -o tsv)

This uses azcopy to copy the image (azcopy needs to be in your $PATH somewhere)

$ sudo azcopy $(pwd)/rhel-7.7.vhd $SASURI --blob-type PageBlob

After the upload is completed, we need to revoke the access, else the disk image cannot be used.

$ sudo az disk revoke-access -n rhel-7 -g myresourcegroup

Finally, mark it a Hyper-V, Gen 2 OS image.

$ sudo az image create -g myresourcegroup -n rhel-7 --os-type Linux --hyper-v-generation V2 --source rhel-7

Running the image

One would imagine that at this point in time, everything was clear and running an image would be easy. If you got everything correctly in place, yes, that’s true. But when I worked through this example, I completely forgot about the last step in the previous paragraph and didn’t mark the disk image an Azure image, which resulted in not being able to provide the SSH key or admin-username.

Anyway, with the above instructions, you should be all set to run the image. The following command assumes that you’re running this tool as the root user and using the root user’s SSH key. Adapt what I have here to your needs:

$ sudo az vm create  \
      --resource-group myresourcegroup \
      --location westus2 \
      --name rhel-7 \
      --image rhel-7 \
      --admin-username cloud-user \
      --ssh-key-value @/root/.ssh/id_rsa.pub

If you need boot diagnostics, you’ll need to create a so-called storage account with the name "storageaccount123" using the following command:

$ sudo az storage account create -n storageaccount123 -g myresourcegroup -l westus2 --sku Standard_LRS

The above az vm create command needs to be extended with the following parameter in order to log the boot diagnostics and messages to the storage account we just created:

--boot-diagnostics-storage storageaccount123

Note: You do not need to enable boot diagnostics, and if you do, you’ll definitely have to adapt the storage account name.

Logging into the Azure instance

From the az vm create command, you should get output containing the IP address. You should now be able to log in automatically, given that the SSH key is provided with:

$ ssh cloud-user@<azure-ip>

Wrapping up

Before exploring the possibilities of creating Red Hat Enterprise Linux 7 images for Azure Mv2 instances in an automated fashion, I had no previous experience with Azure, and therefore it was quite a journey. I’d still not consider myself an expert when it comes to Azure, and there are probably things missing. I’d love to hear your opinions to enhance this setup if necessary.

The scripts and commands I used for this article can be found here. Pull requests are welcome, as well as clones!

Want to try out Red Hat Enterprise Linux? Download it now for free. 


저자 소개

I have worked in IT for more than 20 years. Most of my experience is as a UNIX/Linux Systems Engineer/Administrator. Now I work for Red Hat as a senior technical account manager (TAM).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Original series icon

오리지널 쇼

엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리