準備適用於 Azure 的 SLES 或 openSUSE Leap 虛擬機器

適用於:✔️ Linux VM ✔️ 彈性擴展集 適用於:✔️ 統一擴展集

在某些情況下,您可能會想要在 Azure 環境中使用自訂的 SUSE Linux Enterprise Server (SLES) 或 openSUSE Leap Linux 虛擬機器 (VM),並能夠透過自動化建置這些類型的 VM。 本文將展示如何建立及上傳包含 SUSE Linux 作業系統的 Azure 虛擬硬碟 (VHD)。

必要條件

本文假設您已將 SLES 或 openSUSE Leap Linux 作業系統安裝到虛擬硬碟。 有多項工具可用來建立 .vhd 檔案。 例如,您可以使用虛擬化解決方案,例如 Hyper-V。 如需相關指示,請參閱 安裝 HYPER-V 和建立虛擬機器

SLES/openSUSE Leap 安裝注意事項

  • 如需有關針對 Azure 準備 Linux 的更多秘訣,請參閱一般 Linux 安裝注意事項
  • Azure 不支援 Windows 硬碟映像 (.vhdx) 檔案。 虛擬機器外部僅支援 VHD (.vhd) 檔案。 您可以使用 Hyper-V 管理員或 Convert-VHD Cmdlet,將磁碟轉換為 VHD 格式。
  • Azure 支援 Gen1 (BIOS 開機) 和 Gen2 (UEFI 開機) 虛擬機器。
  • 必須在核心中啟用虛擬檔案配置表 (VFAT) 核心模組。
  • 請勿在 OS 磁碟上設定交換磁碟分割。 您可以設定 Linux 代理程式以在暫存資源磁碟上建立交換檔。 本文稍後的步驟將提供有關設定交換空間的詳細資訊。
  • Azure 上的所有 VHD 必須具有與 1 MB 對應的虛擬大小。 您從原始磁碟轉換成 VHD 時,請在轉換前先確定原始磁碟大小是 1 MB 的倍數。 如需詳細資訊,請參閱一般 Linux 安裝注意事項

注意

Cloud-init 21.2 版或更新版本會移除使用者定義函式(UDF) 需求。 不過,若未啟用 udf 模組,CD-ROM 就不會在佈建期間掛接,就無法套用自訂資料。 因應措施是套用使用者資料。 然而,與自訂資料不同,使用者資料並未加密。 如需詳細資訊,請參閱 cloud-init 文件中的使用者資料格式

使用 SUSE Studio

SUSE Studio 可讓您輕鬆建立及管理 Azure 和 Hyper-V 的 SLES 與 openSUSE Leap 映像。 SUSE Studio 是建議用來自訂您自己的 SLES 和 openSUSE Leap 映像的方法。

SUSE 是建置您自己的 VHD 的替代選項,其也可在 VM Depot 發佈 SLES 的 BYOS (自備訂用帳戶,Bring Your Own Subscription) 映像。

準備適用於 Azure 的 SLES

  1. 視需要設定 Azure 和 Hyper-V 模組。

    如果您的軟體 Hypervisor 不是 Hyper-V,則必須將其他模組新增至初始 RAM 磁碟 (initramfs),才能在 Azure 中成功開機。

    編輯 /etc/dracut.conf 檔案並新增下面這一行:

    add_drivers+=" hv_vmbus hv_netvsc hv_storvsc "
    

    執行 dracut 命令以重建 initramfs 檔案:

    sudo dracut --verbose --force
    
  2. 設定序列主控台。

    若要順利使用序列主控台,您必須在 /etc/defaults/grub 檔案中設定數個變數,並在伺服器上重新建立 GRUB:

    # Add console=ttyS0 and earlyprintk=ttS0 to the variable.
    # Remove "splash=silent" and "quiet" options.
    GRUB_CMDLINE_LINUX_DEFAULT="audit=1 no-scroll fbcon=scrollback:0 mitigations=auto security=apparmor crashkernel=228M,high crashkernel=72M,low console=ttyS0 earlyprintk=ttyS0"
    
    # Add "console serial" to GRUB_TERMINAL.
    GRUB_TERMINAL="console serial"
    
    # Set the GRUB_SERIAL_COMMAND variable.
    
    GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
    
    /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
    
  3. 註冊您的 SUSE Linux Enterprise 系統,以允許下載更新並安裝封裝。

  4. 為系統更新最新的修補程式:

    sudo zypper update
    
  5. 安裝 Azure Linux VM 代理程式 (waagent) 和 cloud-init:

    sudo SUSEConnect -p sle-module-public-cloud/15.2/x86_64  (SLES 15 SP2)
    sudo zypper refresh
    sudo zypper install python-azure-agent
    sudo zypper install cloud-init
    
  6. 啟用在開機時啟動 waagent 和 cloud-init 的功能:

    sudo systemctl enable  waagent
    sudo systemctl enable cloud-init-local.service
    sudo systemctl enable cloud-init.service
    sudo systemctl enable cloud-config.service
    sudo systemctl enable cloud-final.service
    sudo systemctl daemon-reload
    sudo cloud-init clean
    
  7. 更新 cloud-init 設定:

    cat <<EOF | sudo tee /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg
    datasource_list: [ Azure ]
    datasource:
        Azure:
            apply_network_config: False
    
    EOF
    
    sudo cat <<EOF | sudo tee  /etc/cloud/cloud.cfg.d/05_logging.cfg
    # 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
    
    # Make sure mounts and disk_setup are in the init stage:
    echo "Adding mounts and disk_setup to init stage"
    sudo sed -i '/ - mounts/d' /etc/cloud/cloud.cfg
    sudo sed -i '/ - disk_setup/d' /etc/cloud/cloud.cfg
    sudo sed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfg
    sudo sed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfg
    
  8. 如果您想要掛接、格式化及建立交換分割區,其中一個選項是每次建立 VM 時都傳入 cloud-init 設定。

    另一個選項是使用映像中的 cloud-init 指示詞,以在每次建立 VM 時設定交換空間:

    cat  <<EOF | sudo tee -a /etc/systemd/system.conf
    'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"'
    EOF
    
    cat <<EOF | sudo tee /etc/cloud/cloud.cfg.d/00-azure-swap.cfg
    #cloud-config
    # Generated by Azure cloud image build
    disk_setup:
      ephemeral0:
        table_type: mbr
        layout: [66, [33, 82]]
        overwrite: True
    fs_setup:
      - device: ephemeral0.1
        filesystem: ext4
      - device: ephemeral0.2
        filesystem: swap
    mounts:
      - ["ephemeral0.1", "/mnt"]
      - ["ephemeral0.2", "none", "swap", "sw,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]
    EOF
    
  9. 在此之前,Azure Linux 代理程式會在虛擬機器佈建於 Azure 後,使用連結至虛擬機器的本機資源磁碟自動設定交換空間。 不過 cloud-init 現在會處理此步驟,因此請您務必不要 使用 Azure Linux 代理程式來格式化資源磁碟或建立分頁檔。 請使用這些命令對 /etc/waagent.conf 進行適合的修改:

    sudo sed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=auto/g' /etc/waagent.conf
    sudo sed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.conf
    sudo sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
    sudo sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
    

    注意

    如果您使用早於 21.2 的 cloud-init 版本,請確定已啟用 udf 模組。 移除或停用該模組將導致佈建或開機失敗。 Cloud-init 21.2 版或更新版本會移除 UDF 需求。

  10. 確定 /etc/fstab 檔案會使用磁碟的 UUID (by-uuid) 來參考磁碟。

  11. 移除 udev 規則和網路介面卡組態檔可防止產生乙太網路介面的靜態規則。 在 Microsoft Azure 或 Hyper-V 中複製虛擬機器時,這些規則可能會造成問題。

    sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
    sudo rm -f /etc/udev/rules.d/85-persistent-net-cloud-init.rules
    sudo rm -f /etc/sysconfig/network/ifcfg-eth*
    
  12. 建議您編輯檔案 /etc/sysconfig/network/dhcp 檔案,並將 DHCLIENT_SET_HOSTNAME 參數變更如下:

    DHCLIENT_SET_HOSTNAME="no"
    
  13. /etc/sudoers 中,註解化或移除下列程式碼行 (如果存在的話):

    Defaults targetpw   # Ask for the password of the target user i.e. root
    ALL    ALL=(ALL) ALL   # WARNING! Only use this setting together with 'Defaults targetpw'!
    
  14. 確定安全殼層 (SSH) 伺服器已安裝並設定為在開機時啟動:

    sudo systemctl enable sshd
    
  15. 清除 cloud-init 階段:

    sudo cloud-init clean --seed --logs
    
  16. 執行下列命令,以取消佈建虛擬機器,並準備將其佈建於 Azure 上。

    如果您要遷移特定的虛擬機器,但不想建立一般化映像,請略過取消佈建步驟。

    sudo rm -f /var/log/waagent.log
    sudo waagent -force -deprovision+user
    sudo export HISTSIZE=0
    sudo rm -f ~/.bash_history
    

準備 openSUSE 15.2+

  1. 選取 Hyper-V 管理員中間窗格上的虛擬機器。

  2. 選取 [連接],以開啟虛擬機器的視窗。

  3. 接著,在終端機上執行命令 zypper lr。 如果此命令傳回的輸出與下列範例類似,則該儲存機制已如預期設定,不需進行調整。 (版本號碼可能會不同。)

    # Alias 名稱 已啟用 Refresh
    1 Cloud:Tools_15.2 Cloud:Tools_15.2 Yes Yes
    2 openSUSE_15.2_OSS openSUSE_15.2_OSS Yes Yes
    3 openSUSE_15.2_Updates openSUSE_15.2_Updates Yes Yes

    如果命令傳回 "No repositories defined,",請使用下列命令來新增這些儲存機制:

    sudo zypper ar -f http://download.opensuse.org/repositories/Cloud:Tools/openSUSE_15.2 Cloud:Tools_15.2
    sudo zypper ar -f https://download.opensuse.org/distribution/15.2/repo/oss openSUSE_15.2_OSS
    sudo zypper ar -f http://download.opensuse.org/update/15.2 openSUSE_15.2_Updates
    

    您接著可以重新執行命令 zypper lr 來驗證已新增的儲存機制。 如果有其中一個相關的更新儲存機制未啟用,請使用下列命令加以啟用:

    sudo zypper mr -e [NUMBER OF REPOSITORY]
    
  4. 將核心更新為最新的可用版本:

    sudo zypper up kernel-default
    

    或使用所有最新的修補程式來更新作業系統:

    sudo zypper update
    
  5. 安裝 Azure Linux 代理程式:

    sudo zypper install WALinuxAgent
    
  6. 修改 GRUB 設定中的核心開機那一行,使其包含其他用於 Azure 的核心參數。 作法是,在文字編輯器中開啟 /boot/grub/menu.lst,並確定預設核心包含以下參數:

     console=ttyS0 earlyprintk=ttyS0
    

    此選項會確保所有主控台訊息都會傳送到第一個序列埠,以協助 Azure 支援團隊進行問題偵錯程序。 此外,請從核心開機程式行中移除以下參數 (如果有):

     libata.atapi_enabled=0 reserve=0x1f0,0x8
    
  7. 建議您編輯檔案 /etc/sysconfig/network/dhcp 檔案,並將 DHCLIENT_SET_HOSTNAME 參數變更為下列設定:

     DHCLIENT_SET_HOSTNAME="no"
    
  8. /etc/sudoers 中,註解化或移除下列程式碼行 (如果存在的話)。 這是重要步驟。

    Defaults targetpw   # ask for the password of the target user i.e. root
    ALL    ALL=(ALL) ALL   # WARNING! Only use this together with 'Defaults targetpw'!
    
  9. 確定您已安裝 SSH 伺服器,並已設定為在開機時啟動。

  10. 請不要在 OS 磁碟上建立交換空間。

    Azure Linux 代理程式可在 VM 佈建於 Azure 後,使用附加至 VM 的本機資源磁碟自動設定交換空間。 本機資源磁碟是「暫存」磁碟,將在 VM 取消佈建時清空。

    安裝 Azure Linux 代理程式之後,請在 /etc/waagent.conf 中如下修改參數:

    ResourceDisk.Format=n
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=n
    ResourceDisk.SwapSizeMB=2048    ## NOTE: set the size to whatever you need it to be.
    
  11. 確定 Azure Linux 代理程式會在啟動時執行:

    sudo systemctl enable waagent.service
    
  12. 執行下列命令,以取消佈建虛擬機器,並準備將其佈建於 Azure 上。

    如果您要遷移特定的虛擬機器,但不想建立一般化映像,請略過取消佈建步驟。

        sudo rm -f ~/.bash_history # Remove current user history
        sudo rm -rf /var/lib/waagent/
        sudo rm -f /var/log/waagent.log
        sudo waagent -force -deprovision+user
        sudo rm -f ~/.bash_history # Remove root user history
        sudo export HISTSIZE=0
    
  13. 在 Hyper-V 管理員中,選取 [動作]>[關閉]

下一步

您現在可以開始使用您的 SUSE Linux VHD 在 Azure 中建立新的虛擬機器。 如果您是第一次將 .vhd 檔案上傳至 Azure,請參閱從自訂磁碟建立 Linux VM