共用方式為


建立 Azure Linux VM 的 SWAP 磁碟分區

若要在 Azure Linux VM 上建立 SWAP 磁碟分區,您必須設定 cloud-init,以在 VM 的暫時 (資源) 磁碟上自動建立它。 資源磁碟預設會掛接在 底下 /mnt 。 它位於裝載 Linux VM 的實體伺服器上,而且延遲較低。 不建議在可能影響操作系統和應用程式效能的 OS 磁碟或數據磁碟上建立 SWAP 磁碟分區。 請務必記住,資源磁碟永遠不應該用來儲存一般數據,因為它只是暫存記憶體。 當 VM 移至另一部主機或停止/解除分配時,將會抹除寫入此磁碟的任何數據。 建議您只針對可移除的數據使用資源磁碟,例如 SWAP 和快取檔案。 如需詳細資訊,請參閱 暫存磁碟

停用 waagent 組態中的 SWAP 建立

如果在 /etc/waagent.conf 中設定 SWAP 建立,您必須停用它。

  1. 停用 /etc/waagent.conf 內的資源磁碟格式設定和 SWAP 設定,因為此工作現在由 Cloud-Init 處理。 設定參數,如下所示:

    # Format if unformatted. If 'n', resource disk will not be mounted.
    ResourceDisk.Format=n
    
    # Create and use SWAPfile on resource disk.
    ResourceDisk.EnableSWAP=n
    
    #Mount point for the resource disk
    ResourceDisk.MountPoint=/mnt
    
    #Size of the SWAPfile.
    ResourceDisk.SWAPSizeMB=0
    
  2. 重新啟動 Azure Linux 代理程式。 如需不同 Linux 發行版重新啟動命令的相關信息,請參閱 如何更新 VM 上的 Azure Linux 代理程式。

建立 SWAP 磁碟分區

您可以使用下列其中一個選項來建立 SWAP 分割區。

注意事項

建立 SWAP 磁碟分區時,也會在其中建立交換檔案。

選項 1:使用文稿在資源或自訂磁碟路徑下建立 SWAP 磁碟分區
  1. 使用下列腳本 ,在/var/lib/cloud/scripts/per-boot 下建立名為 swap.sh 的 SWAP 建立腳本:

    #!/bin/sh
    
    # Percent of space on the ephemeral disk to dedicate to swap. Here 30% is being used. Modify as appropriate.
    PCT=0.3
    
    # Location of the swap file. Modify as appropriate based on the location of the ephemeral disk.
    LOCATION=/mnt
    
    if [ ! -f ${LOCATION}/swapfile ]
    then
    
        # Get size of the ephemeral disk and multiply it by the percent of space to allocate
        size=$(/bin/df -m --output=target,avail | /usr/bin/awk -v percent="$PCT" -v pattern=${LOCATION} '$0 ~ pattern {SIZE=int($2*percent);print SIZE}')
        echo "$size MB of space allocated to swap file"
    
         # Create an empty file first and set correct permissions
        /bin/dd if=/dev/zero of=${LOCATION}/swapfile bs=1M count=$size
        /bin/chmod 0600 ${LOCATION}/swapfile
    
        # Make the file available to use as swap
        /sbin/mkswap ${LOCATION}/swapfile
    fi
    
    # Enable swap
    /sbin/swapon ${LOCATION}/swapfile
    /sbin/swapon -a
    
    # Display current swap status
    /sbin/swapon -s
    

    腳本會在每次開機時執行,並配置資源磁碟碟 30% 的可用空間。 您可以根據您的情況自訂值。

  2. 讓文稿成為可執行檔:

    chmod +x /var/lib/cloud/scripts/per-boot/swap.sh
    
  3. 停止並啟動 VM。 只有在您建立 SWAP 檔案之後第一次才需要停止和啟動 VM。

選項 2:使用 cloud-init 在資源磁碟路徑下建立 SWAP 磁碟分區
  1. CLOUD_CFG/systemd/system.conf 中建立變數,以設定 SWAP 和資源磁碟:

    sudo echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.conf
    
  2. 建立 YAML 檔案,以設定 SWAP、資源磁碟建立和裝入點:

    sudo cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
    #cloud-config
    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
    
  3. 停止並啟動 VM,或重新部署 VM,以在資源磁碟上建立 SWAP 磁碟分區。

選項 3:使用 cloud-init 在自訂資源磁碟路徑下建立 SWAP 磁碟分區
  1. CLOUD_CFG/systemd/system.conf 中建立變數,以設定 SWAP 和資源磁碟:

    sudo echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.conf
    
  2. 建立 YAML 檔案,以 (“azure” 設定 SWAP、資源磁碟建立和自定義裝入點,是範例) :

    sudo cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
    #cloud-config
    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", "/azure"]
      - ["ephemeral0.2", "none", "swap", "sw,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]
    EOF
    

    注意事項

    請確定自訂裝入點存在於 YAML 檔案中指定的位置。

  3. 停止並啟動 VM,或重新部署 VM,以在資源磁碟上建立 SWAP 磁碟分區。

與我們連絡,以取得說明

如果您有問題或需要相關協助,請建立支援要求,或詢問 Azure community 支援。 您也可以將產品意見反應提交給 Azure 意應見反社群