準備適用於 Azure Stack Hub 的 Red Hat 型虛擬機器Prepare a Red Hat-based virtual machine for Azure Stack Hub

本文說明如何準備 Red Hat Enterprise Linux (RHEL) 虛擬機器 (VM) 以在 Azure Stack Hub 中使用。This article describes how to prepare a Red Hat Enterprise Linux (RHEL) virtual machine (VM) for use in Azure Stack Hub. 本文涵蓋的 RHEL 版本為 7.1 或更新版本。The versions of RHEL that are covered in this article are 7.1 or later. 本文章所述之準備作業使用 Hyper-V、核心為基礎之虛擬機器 (KVM) 及 VMware 等 Hypervisor。The hypervisors for preparation that are covered in this article are Hyper-V, kernel-based virtual machine (KVM), and VMware.

如需 Red Hat Enterprise Linux 支援資訊,請參閱 Red Hat 和 Azure Stack:常見問題集 (英文)。For Red Hat Enterprise Linux support information, see Red Hat and Azure Stack: Frequently Asked Questions.

從 Hyper-V 管理員準備 Red Hat 型 VMPrepare a Red Hat-based VM from Hyper-V Manager

本節假設您已經從 Red Hat 網站取得 ISO 檔案並將 RHEL 映像安裝至虛擬硬碟 (VHD)。This section assumes that you already have an ISO file from the Red Hat website and have installed the RHEL image to a virtual hard disk (VHD). 如需有關如何使用 Hyper-v 管理員來安裝作業系統映射的詳細資訊,請參閱 安裝 hyper-v 角色和設定 VMFor more information about how to use Hyper-V Manager to install an operating system image, see Install the Hyper-V role and configure a VM.

RHEL 安裝注意事項RHEL installation notes

  • Azure Stack Hub 不支援 VHDX 格式。Azure Stack Hub does not support the VHDX format. Azure 只支援固定 VHD。Azure supports only fixed VHD. 您可以使用 Hyper-V 管理員將磁碟轉換為 VHD 格式,或者使用 convert-vhd Cmdlet。You can use Hyper-V Manager to convert the disk to VHD format, or you can use the convert-vhd cmdlet. 如果您使用 VirtualBox,請選取 [固定大小] ,而不是預設在建立磁碟時動態配置的選項。If you use VirtualBox, select Fixed size as opposed to the default dynamically allocated option when you create the disk.
  • Azure Stack Hub 僅支援第 1 代 VM。Azure Stack Hub supports only generation 1 VMs. 您可以將第 1 代 VM 從 VHDX 轉換為 VHD 檔案格式,並從動態擴充轉換為固定大小的磁碟。You can convert a generation 1 VM from VHDX to the VHD file format, and from dynamically expanding to a fixed-size disk. 您無法變更 VM 的世代。You cannot change a VM's generation. 如需詳細資訊,請參閱應該在 Hyper-V 中建立第 1 代還是第 2 代的 VM?For more information, see Should I create a generation 1 or 2 VM in Hyper-V?.
  • 允許的 VHD 大小上限為 1023 GB。The maximum size allowed for the VHD is 1,023 GB.
  • 安裝 Linux 作業系統時,我們建議您使用標準磁碟分割而不是邏輯磁碟區管理員 (LVM),其經常是許多安裝的預設設定。When you install the Linux operating system, we recommend that you use standard partitions rather than Logical Volume Manager (LVM), which is often the default for many installations. 此練習可避免 LVM 名稱與複製的 VM 發生衝突,特別是為了疑難排解而需要將作業系統磁碟連結至另一部相同 VM 時。This practice avoids LVM name conflicts with cloned VMs, particularly if you ever need to attach an operating system disk to another identical VM for troubleshooting.
  • 需要掛接通用磁碟格式 (UDF) 檔案系統的核心支援。Kernel support for mounting Universal Disk Format (UDF) file systems is required. 第一次開機時,連結至客體的 UDF 格式媒體會將佈建組態傳遞至 Linux VM。At first boot, the UDF-formatted media attached to the guest passes the provisioning configuration to the Linux VM. Azure Linux 代理程式必須裝載 UDF 檔案系統,才能讀取其設定並佈建 VM。The Azure Linux Agent must mount the UDF file system to read its configuration and provision the VM.
  • 請勿在作業系統磁碟上設定交換磁碟分割。Do not configure a swap partition on the operating system disk. 您可以設定 Linux 代理程式在暫存資源磁碟上建立交換檔。The Linux Agent can be configured to create a swap file on the temporary resource disk. 在下列步驟中可以找到與此有關的詳細資訊。More information about can be found in the following steps.
  • Azure 上的所有 VHD 必須具有與 1 MB 對應的虛擬大小。All VHDs on Azure must have a virtual size aligned to 1 MB. 從原始磁碟轉換成 VHD 時,您必須在轉換前先確定原始磁碟大小是 1 MB 的倍數。When converting from a raw disk to VHD, you must ensure that the raw disk size is a multiple of 1 MB before conversion. 如需更多詳細資料,請參閱以下步驟。More details can be found in the following steps.
  • Azure Stack Hub 支援 cloud-init。Azure Stack Hub supports cloud-init. Cloud-init (英文) 是在 Linux VM 初次開機時,廣泛用來自訂它們的方法。Cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. 您可以使用 cloud-init 來安裝封裝和寫入檔案,或者設定使用者和安全性。You can use cloud-init to install packages and write files, or to configure users and security. 因為在初次開機程序期間時會呼叫 Cloud-init,因此不需要使用任何額外的步驟或必要的代理程式,就可以套用您的設定。Because cloud-init is called during the initial boot process, there are no additional steps or required agents to apply your configuration. 如需將 cloud-init 新增至映像的指示,請參閱準備現有的 Linux Azure VM 映像以搭配 cloud-init 使用For instructions on adding cloud-init to your image, see Prepare an existing Linux Azure VM image for use with cloud-init.

從 Hyper-V 管理員準備 RHEL 7 VMPrepare an RHEL 7 VM from Hyper-V Manager

  1. 在 Hyper-V 管理員中,選取 VM。In Hyper-V Manager, select the VM.

  2. 選取 [連線] ,以開啟 VM 的主控台視窗。Select Connect to open a console window for the VM.

  3. 建立或編輯 /etc/sysconfig/network 檔案,然後新增下列文字:Create or edit the /etc/sysconfig/network file, and add the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  4. 建立或編輯 /etc/sysconfig/network-scripts/ifcfg-eth0 檔案,然後視需要新增下列文字:Create or edit the /etc/sysconfig/network-scripts/ifcfg-eth0 file, and add the following text as needed:

    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    
  5. 若要確保開機時會啟動網路服務,可執行下列命令:Ensure that the network service starts at boot time by running the following command:

    sudo systemctl enable network
    
  6. 透過執行以下命令來註冊 Red Hat 訂用帳戶,以便從 RHEL 儲存機制安裝封裝:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

    sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  7. 修改 grub 組態中的核心開機那一行,使其額外包含用於 Azure 的核心參數。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 若要進行此修改,請在文字編輯器中開啟 /etc/default/grub,然後修改 GRUB_CMDLINE_LINUX 參數。To make this modification, open /etc/default/grub in a text editor, and modify the GRUB_CMDLINE_LINUX parameter. 例如:For example:

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

    此修改會確保所有主控台訊息都會傳送到第一個序列埠,以協助 Azure 支援團隊進行問題偵錯程序。This modification ensures all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此組太也會關閉新的 RHEL 7 對 NIC 的命名慣例。This configuration also turns off the new RHEL 7 naming conventions for NICs.

    在雲端環境中,我們會將所有記錄傳送到序列埠,因此不適合使用圖形化和無訊息啟動。Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. 您可以視需要保留 crashkernel 選項的設定。You can leave the crashkernel option configured if desired. 此參數會減少 VM 中約 128 MB 或以上的可用記憶體數量,這可能會對小型 VM 造成問題。This parameter reduces the amount of available memory in the VM by 128 MB or more, which might be problematic on smaller VM sizes. 我們建議移除下列參數:We recommend that you remove the following parameters:

    rhgb quiet crashkernel=auto
    
  8. 完成 /etc/default/grub 的編輯之後,請執行下列命令以重建 grub 組態:After you're done editing /etc/default/grub, run the following command to rebuild the grub configuration:

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  9. 選用:停止和卸載 cloud-initOptional: stop and Uninstall cloud-init:

    systemctl stop cloud-init
    yum remove cloud-init
    
  10. 確定已安裝 SSH 伺服器,並已設定為在開機時啟動 (這通常為預設值)。Ensure that the SSH server is installed and configured to start at boot time, which is usually the default. 修改 /etc/ssh/sshd_config 以包含下面一行:Modify /etc/ssh/sshd_config to include the following line:

    ClientAliveInterval 180
    
  11. 針對 Azure Stack Hub 建立自訂 vhd 時,請注意2.2.20 和2.2.35 之間的 WALinuxAgent 版本 (在1910版之前的 Azure Stack Hub 環境中,都無法使用獨佔) 。When creating a custom vhd for Azure Stack Hub, note that WALinuxAgent versions between 2.2.20 and 2.2.35 (both exclusive) do not work on Azure Stack Hub environments before the 1910 release. 您可以使用版本 2.2.20/2.2.35 來準備您的映射。You can use versions 2.2.20/2.2.35 to prepare your image. 若要使用比 2.2.35 更新的版本來準備自訂映像,請將您的 Azure Stack Hub 更新至 1903 版本或更新版本或套用 1901/1902 Hotfix。To use versions later than 2.2.35 to prepare your custom image, update your Azure Stack Hub to the 1903 release or later, or apply the 1901/1902 hotfix.

    1910之前的版本:請依照下列指示下載相容的 WALinuxAgentBefore 1910 release: follow these instructions to download a compatible WALinuxAgent:

    1. 下載安裝工具:Download the setup tools:

      wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz --no-check-certificate
      tar xzf setuptools-7.0.tar.gz
      cd setuptools-7.0
      
    2. 從 GitHub 下載並解壓縮代理程式的2.2.20 版本:Download and unzip the 2.2.20 version of the agent from GitHub:

      wget https://github.com/Azure/WALinuxAgent/archive/v2.2.20.zip
      unzip v2.2.20.zip
      cd WALinuxAgent-2.2.20
      
    3. 安裝 setup.pyInstall setup.py:

      sudo python setup.py install
      
    4. 重新開機 waagentRestart waagent:

      sudo systemctl restart waagent
      
    5. 測試代理程式版本是否符合您下載的版本。Test if the agent version matches the one you downloaded. 在此範例中,它應該是2.2.20:For this example, it should be 2.2.20:

      waagent -version
      

    在1910版本之後,請依照下列指示下載相容的 WALinuxAgentAfter the 1910 release, follow these instructions to download a compatible WALinuxAgent:

    1. WALinuxAgent 套件已 WALinuxAgent-<version> 推送至 Red Hat 額外存放庫。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 執行下列命令以啟用 extras 儲存機制:Enable the extras repository by running the following command:

      subscription-manager repos --enable=rhel-7-server-extras-rpms
      
    2. 執行以下命令來安裝 Azure Linux 代理程式:Install the Azure Linux Agent by running the following command:

      sudo yum install WALinuxAgent
      sudo systemctl enable waagent.service
      
  12. 請勿在作業系統磁碟上建立交換空間。Do not create swap space on the operating system disk.

    Azure Linux 代理程式可在 VM 佈建於 Azure 後,使用附加至 VM 的本機資源磁碟自動設定交換空間。The Azure Linux Agent can automatically configure swap space by using the local resource disk that's attached to the VM after the VM is provisioned on Azure. 本機資源磁碟是暫存磁碟,可能會在 VM 取消佈建時清空。The local resource disk is a temporary disk, and it might be emptied when the VM is de-provisioned. 在上一個步驟安裝 Azure Linux 代理程式後,請在 /etc/waagent.conf 中適當修改下列參數:After you install the Azure Linux Agent in the previous step, modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=y
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=y
    ResourceDisk.SwapSizeMB=2048    #NOTE: set this to whatever you need it to be.
    
  13. 如果您想要取消註冊訂用帳戶,請執行下列命令:If you want to un-register the subscription, run the following command:

    sudo subscription-manager unregister
    
  14. 如果您使用的系統是使用企業憑證授權單位來部署,RHEL VM 將不會信任 Azure Stack Hub 根憑證。If you're using a system that was deployed using an Enterprise Certificate Authority, the RHEL VM won't trust the Azure Stack Hub root certificate. 您必須將該憑證放入受信任的根存放區。You must place that certificate into the trusted root store. 如需詳細資訊,請參閱將信任的根憑證新增到伺服器For more information, see Adding trusted root certificates to the server.

  15. 執行下列命令以取消佈建 VM,並準備將其佈建於 Azure 上:Run the following commands to de-provision the VM and prepare it for provisioning on Azure:

    sudo waagent -force -deprovision
    export HISTSIZE=0
    logout
    
  16. 在 Hyper-V 管理員中,選取 [動作] > [關閉] 。Select Action, then Shut Down in Hyper-V Manager.

  17. 使用 Hyper-v 管理員 編輯磁片 功能或 PowerShell 命令,將 VHD 轉換為固定大小的 vhd Convert-VHDConvert the VHD to a fixed size VHD using either the Hyper-V Manager Edit disk feature, or the Convert-VHD PowerShell command. 您現在可以將 Linux VHD 上傳至 Azure。Your Linux VHD is now ready to be uploaded to Azure.

從 KVM 準備 Red Hat 型虛擬機器Prepare a Red Hat-based virtual machine from KVM

  1. 從 Red Hat 網站下載 RHEL 7 的 KVM 映像。Download the KVM image of RHEL 7 from the Red Hat website. 此程序會使用 RHEL 7 做為範例。This procedure uses RHEL 7 as the example.

  2. 設定根密碼。Set a root password.

    產生加密的密碼,並複製命令的輸出:Generate an encrypted password, and copy the output of the command:

    openssl passwd -1 changeme
    

    使用 guestfish 設定根密碼:Set a root password with guestfish:

    guestfish --rw -a <image-name>
    > <fs> run
    > <fs> list-filesystems
    > <fs> mount /dev/sda1 /
    > <fs> vi /etc/shadow
    > <fs> exit
    

    將根使用者的第二個欄位從 "!!"Change the second field of root user from "!!" 變更為加密的密碼。to the encrypted password.

  3. 在 KVM 中從 qcow2 映射建立 VM。Create a VM in KVM from the qcow2 image. 將磁碟類型設定為 qcow2,再將虛擬網路介面裝置模型設定為 virtioSet the disk type to qcow2, and set the virtual network interface device model to virtio. 然後啟動 VM 並以 root 身分登入。Then, start the VM, and sign in as root.

  4. 建立或編輯 /etc/sysconfig/network 檔案,然後新增下列文字:Create or edit the /etc/sysconfig/network file, and add the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  5. 建立或編輯檔案 /etc/sysconfig/network-scripts/ifcfg-eth0 ,然後新增下列文字:Create or edit the /etc/sysconfig/network-scripts/ifcfg-eth0 file, and then add the following text:

    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    
  6. 若要確保開機時會啟動網路服務,可執行下列命令:Ensure that the network service starts at boot time by running the following command:

    sudo systemctl enable network
    
  7. 透過執行以下命令來註冊 Red Hat 訂用帳戶,以便從 RHEL 儲存機制安裝封裝:Register your Red Hat subscription to enable installation of packages from the RHEL repository by running the following command:

    subscription-manager register --auto-attach --username=XXX --password=XXX
    
  8. 修改 grub 組態中的核心開機那一行,使其額外包含用於 Azure 的核心參數。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 若要進行此設定,請在文字編輯器中開啟 /etc/default/grub,然後修改 GRUB_CMDLINE_LINUX 參數。To do this configuration, open /etc/default/grub in a text editor, and modify the GRUB_CMDLINE_LINUX parameter. 例如:For example:

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

    此命令也會確保所有主控台訊息都會傳送給第一個序列埠,以協助 Azure 支援團隊進行問題偵錯程序。This command also ensures that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 而且也會關閉新的 RHEL 7 對 NIC 的命名慣例。The command also turns off the new RHEL 7 naming conventions for NICs.

    在雲端環境中,所有記錄都會傳送到序列埠,因此不適合使用圖形化和無訊息啟動。Graphical and quiet boot are not useful in a cloud environment where all the logs are sent to the serial port. 您可以視需要保留 crashkernel 選項的設定。You can leave the crashkernel option configured if desired. 此參數會減少 VM 中約 128 MB 或以上的可用記憶體數量,這可能會對小型 VM 造成問題。This parameter reduces the amount of available memory in the VM by 128 MB or more, which might be problematic on smaller VM sizes. 我們建議移除下列參數:We recommend you remove the following parameters:

    rhgb quiet crashkernel=auto
    
  9. 完成 /etc/default/grub 的編輯之後,請執行下列命令以重建 grub 組態:After you're done editing /etc/default/grub, run the following command to rebuild the grub configuration:

    grub2-mkconfig -o /boot/grub2/grub.cfg
    
  10. 將 Hyper-v 模組新增至 initramfsAdd Hyper-V modules into initramfs.

    編輯 /etc/dracut.conf 並加入內容:Edit /etc/dracut.conf and add content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重建 initramfs:Rebuild initramfs:

    dracut -f -v
    
  11. 1910版之後的選擇性:停止和卸載 cloud-initOptional after 1910 release: stop and uninstall cloud-init:

    systemctl stop cloud-init
    yum remove cloud-init
    
  12. 確定您已安裝 SSH 伺服器,並已設定為在開機時啟動:Ensure that the SSH server is installed and configured to start at boot time:

    systemctl enable sshd
    

    修改 /etc/ssh/sshd_config 以包含下面一行:Modify /etc/ssh/sshd_config to include the following lines:

    PasswordAuthentication yes
    ClientAliveInterval 180
    
  13. 針對 Azure Stack Hub 建立自訂 VHD 時,請注意2.2.20 和2.2.35 之間的 WALinuxAgent 版本 (在1910版之前的 Azure Stack Hub 環境中,都無法使用獨佔) 。When creating a custom VHD for Azure Stack Hub, note that WALinuxAgent versions between 2.2.20 and 2.2.35 (both exclusive) do not work on Azure Stack Hub environments before the 1910 release. 您可以使用版本 2.2.20/2.2.35 來準備您的映射。You can use versions 2.2.20/2.2.35 to prepare your image. 若要使用2.2.35 之後的版本來準備自訂映射,請將您的 Azure Stack Hub 更新為1903版或更新版本,或套用1901/1902 的修正程式。To use versions later than 2.2.35 to prepare your custom image, update your Azure Stack Hub to 1903 release or later, or apply the 1901/1902 hotfix.

    1910版之前:請依照下列指示下載相容的 WALinuxAgentBefore the 1910 release: follow these instructions to download a compatible WALinuxAgent:

    1. 下載安裝工具:Download the setup tools:

      wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz --no-check-certificate
      tar xzf setuptools-7.0.tar.gz
      cd setuptools-7.0
      
    2. 從 GitHub 下載並解壓縮代理程式的2.2.20 版本:Download and unzip the 2.2.20 version of the agent from GitHub:

      wget https://github.com/Azure/WALinuxAgent/archive/v2.2.20.zip
      unzip v2.2.20.zip
      cd WALinuxAgent-2.2.20
      
    3. 安裝 setup.pyInstall setup.py:

      sudo python setup.py install
      
    4. 重新開機 waagentRestart waagent.

      sudo systemctl restart waagent
      
    5. 測試代理程式版本是否符合您下載的版本。Test if the agent version matches the one you downloaded. 在此範例中,它應該是2.2.20:For this example, it should be 2.2.20:

      waagent -version
      

    1910版本之後:請依照下列指示下載相容的 WALinuxAgentAfter the 1910 release: follow these instructions to download a compatible WALinuxAgent:

    1. WALinuxAgent 套件已 WALinuxAgent-<version> 推送至 Red Hat 額外存放庫。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 執行下列命令以啟用 extras 儲存機制:Enable the extras repository by running the following command:

      subscription-manager repos --enable=rhel-7-server-extras-rpms
      
    2. 執行下列命令來安裝 Azure Linux 代理程式:Install the Azure Linux Agent by running the following commands:

      sudo yum install WALinuxAgent
      sudo systemctl enable waagent.service
      
  14. 請勿在作業系統磁碟上建立交換空間。Do not create swap space on the operating system disk.

    Azure Linux 代理程式可在 VM 佈建於 Azure 後,使用附加至 VM 的本機資源磁碟自動設定交換空間。The Azure Linux Agent can automatically configure swap space by using the local resource disk that's attached to the VM after the VM is provisioned on Azure. 本機資源磁碟是暫存磁碟,可能會在 VM 取消佈建時清空。The local resource disk is a temporary disk, and it might be emptied when the VM is de-provisioned. 在上一個步驟安裝 Azure Linux 代理程式後,請在 /etc/waagent.conf 中適當修改下列參數:After you install the Azure Linux Agent in the previous step, modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=y
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=y
    ResourceDisk.SwapSizeMB=2048    #NOTE: set this to whatever you need it to be.
    
  15. 如有必要,請執行下列命令來取消註冊訂用帳戶 () :Un-register the subscription (if necessary) by running the following command:

    subscription-manager unregister
    
  16. 如果您使用的系統是使用企業憑證授權單位來部署,RHEL VM 將不會信任 Azure Stack Hub 根憑證。If you're using a system that was deployed using an Enterprise Certificate Authority, the RHEL VM won't trust the Azure Stack Hub root certificate. 您必須將該憑證放入受信任的根存放區。You must place that certificate into the trusted root store. 如需詳細資訊,請參閱將信任的根憑證新增到伺服器For more information, see Adding trusted root certificates to the server.

  17. 執行下列命令以取消佈建 VM,並準備將其佈建於 Azure 上:Run the following commands to de-provision the VM and prepare it for provisioning on Azure:

    sudo waagent -force -deprovision
    export HISTSIZE=0
    logout
    
  18. 在 KVM 中關閉 VM。Shut down the VM in KVM.

  19. 將 qcow2 映像轉換成 VHD 格式。Convert the qcow2 image to the VHD format.

    注意

    Qemu-img convert 中有一個已知的 bug,>= 2.2.1,導致 VHD 格式不正確。There is a known bug in qemu-img versions >=2.2.1 that results in an improperly formatted VHD. 此問題已在 QEMU 2.6 中修正。The issue has been fixed in QEMU 2.6. 建議使用 qemu-img 2.2.0 或更舊版本,或更新至 2.6 或更新版本。It's recommended that you use either qemu-img 2.2.0 or lower, or update to 2.6 or higher. 如需詳細資訊,請 參閱這 qemu-img convert 文章For more information, see this QEMU post.

    先將映像轉換成原始格式:First convert the image to raw format:

    qemu-img convert -f qcow2 -O raw rhel-7.4.qcow2 rhel-7.4.raw
    

    確認原始映像的大小符合 1 MB,Make sure that the size of the raw image is aligned with 1 MB. 否則將大小四捨五入為 1 MB︰Otherwise, round up the size to align with 1 MB:

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

    將原始磁碟轉換成固定大小的 VHD:Convert the raw disk to a fixed-sized VHD:

    qemu-img convert -f raw -o subformat=fixed -O vpc rhel-7.4.raw rhel-7.4.vhd
    

    或者,使用 qemu 版本 2.6+ ,包含 force_size 選項:Or, with qemu version 2.6+, include the force_size option:

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

從 VMware 準備 Red Hat 架構的 VMPrepare a Red Hat-based VM from VMware

本節假設您已在 VMware 中安裝 RHEL VM。This section assumes that you've already installed an RHEL VM in VMware. 如需有關如何在 VMware 中安裝作業系統的詳細資訊,請參閱 VMware 客體作業系統安裝指南For details about how to install an operating system in VMware, see VMware Guest Operating System Installation Guide.

  • 安裝 Linux 作業系統時,我們建議您使用標準磁碟分割而不是 LVM (這通常是許多安裝的預設設定)。When you install the Linux operating system, we recommend that you use standard partitions rather than LVM, which is often the default for many installations. 此方法可避免 LVM 與複製之虛擬機器的名稱衝突,特別是為了疑難排解而需要將作業系統磁碟連接至其他虛擬機器時。This method avoids LVM name conflicts with cloned VMs, particularly if an operating system disk ever needs to be attached to another VM for troubleshooting. 如果願意,您可以在資料磁碟上使用 LVM 或 RAID。LVM or RAID can be used on data disks if preferred.
  • 請勿在作業系統磁碟上設定交換磁碟分割。Do not configure a swap partition on the operating system disk. 您可以設定 Linux 代理程式以在暫存資源磁碟上建立交換檔。You can configure the Linux agent to create a swap file on the temporary resource disk. 您可以在以下步驟中找到與此組態相關的詳細資訊。You can find more information about this configuration in the following steps.
  • 建立虛擬硬碟時,請選取 [將虛擬磁碟儲存為單一檔案] 。When you create the virtual hard disk, select Store virtual disk as a single file.

從 VMware 準備 RHEL 7 VMPrepare an RHEL 7 VM from VMware

  1. 建立或編輯 /etc/sysconfig/network 檔案,然後新增下列文字:Create or edit the /etc/sysconfig/network file, and add the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  2. 建立或編輯 /etc/sysconfig/network-scripts/ifcfg-eth0 檔案,然後新增下列文字:Create or edit the /etc/sysconfig/network-scripts/ifcfg-eth0 file, and add the following text:

    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    
  3. 若要確保開機時會啟動網路服務,可執行下列命令:Ensure that the network service starts at boot time by running the following command:

    sudo chkconfig network on
    
  4. 透過執行以下命令來註冊 Red Hat 訂用帳戶,以便從 RHEL 儲存機制安裝封裝:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

    sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  5. 修改 grub 組態中的核心開機那一行,使其額外包含用於 Azure 的核心參數。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 若要進行這項修改,請在文字編輯器中開啟 /etc/default/grubTo make this modification, open /etc/default/grub in a text editor. 修改 GRUB_CMDLINE_LINUX 參數。Modify the GRUB_CMDLINE_LINUX parameter. 例如:For example:

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

    此組態也會確保所有主控台訊息都會傳送給第一個序列埠,以協助 Azure 支援團隊進行問題偵錯程序。This configuration also ensures that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 也會關閉新的 RHEL 7 對 NIC 的命名慣例。It also turns off the new RHEL 7 naming conventions for NICs. 我們建議移除下列參數:We recommend that you remove the following parameters:

    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. 您可以視需要保留 crashkernel 選項的設定。You can leave the crashkernel option configured if desired. 此參數會減少 VM 中約 128 MB 或以上的可用記憶體數量,這可能會對小型 VM 造成問題。This parameter reduces the amount of available memory in the VM by 128 MB or more, which might be problematic on smaller VM sizes.

  6. 完成 /etc/default/grub 的編輯之後,請執行下列命令以重建 grub 組態:After you're done editing /etc/default/grub, run the following command to rebuild the grub configuration:

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  7. 將 Hyper-V 模組新增至 initramfs。Add Hyper-V modules to initramfs.

    編輯 /etc/dracut.conf,新增內容:Edit /etc/dracut.conf, add content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重建 initramfs:Rebuild initramfs:

    dracut -f -v
    
  8. 1910版之後的選擇性:停止和卸載 cloud-initOptional after 1910 release: stop and uninstall cloud-init:

    systemctl stop cloud-init
    yum remove cloud-init
    
  9. 確定您已安裝 SSH 伺服器,並已設定為在開機時啟動。Ensure that the SSH server is installed and configured to start at boot time. 此設定通常是預設值。This setting is usually the default. 修改 /etc/ssh/sshd_config 以包含下面一行:Modify /etc/ssh/sshd_config to include the following line:

    ClientAliveInterval 180
    
  10. 針對 Azure Stack Hub 建立自訂 VHD 時,請注意2.2.20 和2.2.35 之間的 WALinuxAgent 版本 (獨佔) 無法在1910版之前的 Azure Stack Hub 環境中運作。When creating a custom VHD for Azure Stack Hub, note that WALinuxAgent versions between 2.2.20 and 2.2.35 (both exclusive) don't work on Azure Stack Hub environments before the 1910 release. 您可以使用版本 2.2.20/2.2.35 來準備您的映射。You can use versions 2.2.20/2.2.35 to prepare your image. 若要使用比 2.2.35 更新的版本來準備自訂映像,請將您的 Azure Stack Hub 更新至 1903 版本或更新版本或套用 1901/1902 Hotfix。To use versions later than 2.2.35 to prepare your custom image, update your Azure Stack Hub to the 1903 release or later, or apply the 1901/1902 hotfix.

    1910之前的版本:請依照下列指示下載相容的 WALinuxAgentBefore 1910 release: follow these instructions to download a compatible WALinuxAgent:

    1. 下載安裝工具:Download the setup tools:

      wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz --no-check-certificate
      tar xzf setuptools-7.0.tar.gz
      cd setuptools-7.0
      
    2. 從 GitHub 下載並解壓縮代理程式的2.2.20 版本:Download and unzip the 2.2.20 version of the agent from GitHub:

      wget https://github.com/Azure/WALinuxAgent/archive/v2.2.20.zip
      unzip v2.2.20.zip
      cd WALinuxAgent-2.2.20
      
    3. 安裝 setup.pyInstall setup.py:

      sudo python setup.py install
      
    4. 重新開機 waagentRestart waagent:

      sudo systemctl restart waagent
      
    5. 測試代理程式版本是否符合您下載的版本。Test if the agent version matches the one you downloaded. 在此範例中,它應該是2.2.20:For this example, it should be 2.2.20:

      waagent -version
      

    1910版本之後:請依照下列指示下載相容的 WALinuxAgentAfter the 1910 release: follow these instructions to download a compatible WALinuxAgent:

    1. WALinuxAgent 套件已 WALinuxAgent-<version> 推送至 Red Hat 額外存放庫。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 執行下列命令以啟用 extras 儲存機制:Enable the extras repository by running the following command:

      subscription-manager repos --enable=rhel-7-server-extras-rpms
      
    2. 執行以下命令來安裝 Azure Linux 代理程式:Install the Azure Linux Agent by running the following command:

      sudo yum install WALinuxAgent
      sudo systemctl enable waagent.service
      
  11. 請勿在作業系統磁碟上建立交換空間。Do not create swap space on the operating system disk.

    Azure Linux 代理程式可在 VM 佈建於 Azure 後,使用附加至 VM 的本機資源磁碟自動設定交換空間。The Azure Linux Agent can automatically configure swap space by using the local resource disk attached to the VM after the VM is provisioned on Azure. 請注意,本機資源磁碟是暫存磁碟,可能會在 VM 取消佈建時清空。Note that the local resource disk is a temporary disk, and it might be emptied when the VM is de-provisioned. 在上一個步驟安裝 Azure Linux 代理程式後,請在 /etc/waagent.conf 中適當修改下列參數:After you install the Azure Linux Agent in the previous step, modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=y
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=y
    ResourceDisk.SwapSizeMB=2048    NOTE: set this to whatever you need it to be.
    
  12. 如果您想要取消註冊訂用帳戶,請執行下列命令:If you want to un-register the subscription, run the following command:

    sudo subscription-manager unregister
    
  13. 如果您使用的系統是使用企業憑證授權單位來部署,RHEL VM 將不會信任 Azure Stack Hub 根憑證。If you're using a system that was deployed using an Enterprise Certificate Authority, the RHEL VM won't trust the Azure Stack Hub root certificate. 您必須將其放入信任的根存放區。You must place that into the trusted root store. 如需詳細資訊,請參閱將信任的根憑證新增到伺服器For more information, see Adding trusted root certificates to the server.

  14. 執行下列命令以取消佈建 VM,並準備將其佈建於 Azure 上:Run the following commands to de-provision the VM and prepare it for provisioning on Azure:

    sudo waagent -force -deprovision
    export HISTSIZE=0
    logout
    
  15. 關閉 VM,並將 VMDK 檔案轉換成 VHD 格式。Shut down the VM, and convert the VMDK file to the VHD format.

    注意

    qemu-img>= 2.2.1 版本中有已知的錯誤,會導致 VHD 格式不正確。There is a known bug in qemu-img versions >=2.2.1 that results in an improperly formatted VHD. 此問題已在 QEMU 2.6 中修正。The issue has been fixed in QEMU 2.6. 建議使用 qemu-img 2.2.0 或更舊版本,或更新至 2.6 或更新版本。It's recommended that you use either qemu-img 2.2.0 or lower, or update to 2.6 or higher.

    先將映像轉換成原始格式:First convert the image to raw format:

    qemu-img convert -f qcow2 -O raw rhel-7.4.qcow2 rhel-7.4.raw
    

    確認原始映像的大小符合 1 MB,Make sure that the size of the raw image is aligned with 1 MB. 否則將大小四捨五入為 1 MB︰Otherwise, round up the size to align with 1 MB:

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

    將原始磁碟轉換成固定大小的 VHD:Convert the raw disk to a fixed-sized VHD:

    qemu-img convert -f raw -o subformat=fixed -O vpc rhel-7.4.raw rhel-7.4.vhd
    

    或者,使用 qemu 版本 2.6+ ,包含 force_size 選項:Or, with qemu version 2.6+, include the force_size option:

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

使用 kickstart 檔案自動從 ISO 準備 Red Hat 型 VMPrepare a Red Hat-based VM from an ISO by using a kickstart file automatically

  1. 建立包含以下內容的 kickstart 檔案,然後儲存此檔案。Create a kickstart file that includes the following content, and save the file. 停止和卸載 cloud init 是選擇性的 (在1910版) 之後,Azure Stack Hub 支援 cloud initStopping and uninstalling cloud-init is optional (cloud-init is supported on Azure Stack Hub after the 1910 release). 只有在 1910 版本之後,才會從 redhat 存放庫安裝代理程式。Install the agent from the redhat repo only after the 1910 release. 在 1910 之前,請使用上一節中的 Azure 存放庫。Prior to 1910, use the Azure repo as done in the previous section. 如需有關 kickstart 安裝的詳細資訊,請參閱 kickstart 安裝指南For details about kickstart installation, see the Kickstart installation guide.

    Kickstart for provisioning a RHEL 7 Azure VM
    
    System authorization information
    auth --enableshadow --passalgo=sha512
    
    Use graphical install
    text
    
    Do not run the Setup Agent on first boot
    firstboot --disable
    
    Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    
    System language
    lang en_US.UTF-8
    
    Network information
    network  --bootproto=dhcp
    
    Root password
    rootpw --plaintext "to_be_disabled"
    
    System services
    services --enabled="sshd,waagent,NetworkManager"
    
    System timezone
    timezone Etc/UTC --isUtc --ntpservers 0.rhel.pool.ntp.org,1.rhel.pool.ntp.org,2.rhel.pool.ntp.org,3.rhel.pool.ntp.org
    
    Partition clearing information
    clearpart --all --initlabel
    
    Clear the MBR
    zerombr
    
    Disk partitioning information
    part /boot --fstype="xfs" --size=500
    part / --fstyp="xfs" --size=1 --grow --asprimary
    
    System bootloader configuration
    bootloader --location=mbr
    
    Firewall configuration
    firewall --disabled
    
    Enable SELinux
    selinux --enforcing
    
    Don't configure X
    skipx
    
    Power down the machine after install
    poweroff
    
    %packages
    @base
    @console-internet
    chrony
    sudo
    parted
    -dracut-config-rescue
    
    %end
    
    %post --log=/var/log/anaconda/post-install.log
    
    #!/bin/bash
    
    Register Red Hat Subscription
    subscription-manager register --username=XXX --password=XXX --auto-attach --force
    
    Install latest repo update
    yum update -y
    
    Stop and Uninstall cloud-init
    systemctl stop cloud-init
    yum remove cloud-init
    
    Enable extras repo
    subscription-manager repos --enable=rhel-7-server-extras-rpms
    
    Install WALinuxAgent
    yum install -y WALinuxAgent
    
    Unregister Red Hat subscription
    subscription-manager unregister
    
    Enable waaagent at boot-up
    systemctl enable waagent
    
    Disable the root account
    usermod root -p '!!'
    
    Configure swap in WALinuxAgent
    sed -i 's/^\(ResourceDisk\.EnableSwap\)=[Nn]$/\1=y/g' /etc/waagent.conf
    sed -i 's/^\(ResourceDisk\.SwapSizeMB\)=[0-9]*$/\1=2048/g' /etc/waagent.conf
    
    Set the cmdline
    sed -i 's/^\(GRUB_CMDLINE_LINUX\)=".*"$/\1="console=tty1 console=ttyS0 earlyprintk=ttyS0 rootdelay=300"/g' /etc/default/grub
    
    Enable SSH keepalive
    sed -i 's/^#\(ClientAliveInterval\).*$/\1 180/g' /etc/ssh/sshd_config
    
    Build the grub cfg
    grub2-mkconfig -o /boot/grub2/grub.cfg
    
    Configure network
    cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    EOF
    
    Deprovision and prepare for Azure
    waagent -force -deprovision
    
    %end
    
  2. 將 kickstart 檔案放在安裝系統可以從中存取的位置。Place the kickstart file in a location from which the installation system can access it.

  3. 在 Hyper-V 管理員中建立新的 VM。In Hyper-V Manager, create a new VM. 在 [連線虛擬硬碟] 頁面上,選取 [稍後連接虛擬硬碟] ,並完成 [新增虛擬機器精靈]。On the Connect Virtual Hard Disk page, select Attach a virtual hard disk later, and complete the New Virtual Machine Wizard.

  4. 開啟 VM 設定:Open the VM settings:

    a.a. 將新增的虛擬硬碟連接到 VM。Attach a new virtual hard disk to the VM. 請務必選取 [VHD 格式] 和 [固定大小] 。Make sure to select VHD Format and Fixed Size.

    b.b. 將安裝 ISO 連接到 DVD 光碟機。Attach the installation ISO to the DVD drive.

    c.c. 將 BIOS 設定成從 CD 開機。Set the BIOS to boot from CD.

  5. 啟動 VM。Start the VM. 當安裝指南出現時,請按 Tab 鍵以設定開機選項。When the installation guide appears, press Tab to configure the boot options.

  6. 在開機選項結尾輸入 inst.ks=<the location of the kickstart file> ,然後按 Enter 鍵。Enter inst.ks=<the location of the kickstart file> at the end of the boot options, and press Enter.

  7. 等待安裝完成。Wait for the installation to finish. 完成後,VM 會自動關閉。When it's finished, the VM is shut down automatically. 您現在可以將 Linux VHD 上傳至 Azure。Your Linux VHD is now ready to be uploaded to Azure.

已知問題Known issues

使用非 Hyper-V Hypervisor 時,初始 RAM 磁碟未包含 Hyper-V 驅動程式The Hyper-V driver couldn't be included in the initial RAM disk when using a non-Hyper-V hypervisor

在某些情況下,Linux 安裝程式可能不會在初始 RAM 磁碟 (initrd 或 initramfs) 中包含 Hyper-V 的驅動程式,除非 Linux 偵測到自己在 Hyper-V 環境中執行。In some cases, Linux installers might not include the drivers for Hyper-V in the initial RAM disk (initrd or initramfs) unless Linux detects that it's running in a Hyper-V environment.

使用不同的虛擬化系統 (如 Oracle VM VirtualBox、Xen Project 等) 來準備 Linux 映像時,您可能需要重新建置 initrd,以確保在初始 RAM 磁碟上至少有 hv_vmbus 和 hv_storvsc 核心模組可以使用。When you're using a different virtualization system (like Oracle VM VirtualBox, Xen Project, and so on) to prepare your Linux image, you might need to rebuild initrd to ensure that at least the hv_vmbus and hv_storvsc kernel modules are available on the initial RAM disk. 目前至少已知在以上游 Red Hat 散發套件為基礎的系統上有此問題。This is a known issue at least on systems that are based on the upstream Red Hat distribution.

若要解決這個問題,請將 Hyper-V 模組新增至 initramfs 並加以重建︰To resolve this issue, add Hyper-V modules to initramfs and rebuild it:

編輯 /etc/dracut.conf 檔案並新增下列內容:Edit /etc/dracut.conf, and add the following content:

add_drivers+="hv_vmbus hv_netvsc hv_storvsc"

重建 initramfs:Rebuild initramfs:

dracut -f -v

如需詳細資訊,請參閱重建 initramfsFor more information, see rebuilding initramfs.

後續步驟Next steps

您現在可以開始使用您的 Red Hat Enterprise Linux 虛擬硬碟在 Azure Stack Hub 建立新的 VM。You're now ready to use your Red Hat Enterprise Linux virtual hard disk to create new VMs in Azure Stack Hub. 如果您是第一次將 VHD 檔案上傳至 Azure Stack Hub,請參閱建立及發行 Marketplace 項目If this is the first time that you're uploading the VHD file to Azure Stack Hub, see Create and publish a Marketplace item.

如需已通過認證可執行 Red Hat Enterprise Linux 的 Hypervisor 詳細資訊,請參閱 Red Hat 網站For more information about the hypervisors that are certified to run Red Hat Enterprise Linux, see the Red Hat website.