共用方式為


如何部署 Azure VM 的暫時性 OS 磁碟

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

本文說明如何透過入口網站、ARM 範本部署、CLI 和 PowerShell 建立具有暫時性 OS 磁碟的虛擬機器或虛擬機器擴展集。

入口網站

在 Azure 入口網站中,您可以開啟 [磁碟] 索引標籤的 [進階] 區段,選擇在部署虛擬機器或虛擬機器擴展集時使用暫時性磁碟。若要選擇暫時性 OS 磁碟的放置位置,請選取 [OS 快取放置] 或 [暫存磁碟放置]

Screenshot showing the radio button for choosing to use an ephemeral OS disk

如果使用暫時性磁碟或 OS 快取放置或暫存磁碟放置的選項呈現灰色,可能您已選取的 VM 大小沒有大於 OS 映像或不支援進階版儲存體的快取/暫存大小。 返回至 [基本]頁面,然後嘗試選擇另一個 VM 大小。

擴展集範本部署

建立使用暫時性 OS 磁碟擴展集的流程是將 diffDiskSettings 屬性新增至範本中的 Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile 資源類型。 此外,快取原則也必須針對暫時性 OS 磁碟設定為 ReadOnly。 放置可以針對 OS 快取磁碟放置變更為 CacheDisk

{
  "type": "Microsoft.Compute/virtualMachineScaleSets",
  "name": "myScaleSet",
  "location": "East US 2",
  "apiVersion": "2019-12-01",
  "sku": {
    "name": "Standard_DS2_v2",
    "capacity": "2"
  },
  "properties": {
    "upgradePolicy": {
      "mode": "Automatic"
    },
    "virtualMachineProfile": {
       "storageProfile": {
        "osDisk": {
          "diffDiskSettings": {
            "option": "Local" ,
            "placement": "ResourceDisk"
          },
          "caching": "ReadOnly",
          "createOption": "FromImage"
        },
        "imageReference":  {
          "publisher": "publisherName",
          "offer": "offerName",
          "sku": "skuName",
          "version": "imageVersion"
        }
      },
      "osProfile": {
        "computerNamePrefix": "myvmss",
        "adminUsername": "azureuser",
        "adminPassword": "P@ssw0rd!"
      }
    }
  }
}

注意

請據此取代所有其他的值。

VM 範本部署

您可以使用範本來部署具有暫時性 OS 磁碟的 VM。 建立使用暫時性 OS 磁碟的 VM 流程,是將 diffDiskSettings 屬性新增至範本中的 Microsoft.Compute/virtualMachines 資源類型。 此外,快取原則也必須針對暫時性 OS 磁碟設定為 ReadOnly。 放置選項可以針對 OS 快取磁碟放置變更為 CacheDisk

{
  "type": "Microsoft.Compute/virtualMachines",
  "name": "myVirtualMachine",
  "location": "East US 2",
  "apiVersion": "2019-12-01",
  "properties": {
       "storageProfile": {
            "osDisk": {
              "diffDiskSettings": {
                "option": "Local" ,
                "placement": "ResourceDisk"
              },
              "caching": "ReadOnly",
              "createOption": "FromImage"
            },
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "2016-Datacenter-smalldisk",
                "version": "latest"
            },
            "hardwareProfile": {
                 "vmSize": "Standard_DS2_v2"
             }
      },
      "osProfile": {
        "computerNamePrefix": "myvirtualmachine",
        "adminUsername": "azureuser",
        "adminPassword": "P@ssw0rd!"
      }
    }
 }

CLI

若要針對 CLI VM 部署使用暫時性磁碟,請將 az vm create 中的 --ephemeral-os-disk 參數設定為 true,並針對暫存磁碟放置,將 --ephemeral-os-disk-placement 參數設定為 ResourceDisk 或針對快取磁碟放置的 CacheDisk,並將 --os-disk-caching 參數設定為 ReadOnly

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image imageName \
  --ephemeral-os-disk true \
  --ephemeral-os-disk-placement ResourceDisk \
  --os-disk-caching ReadOnly \
  --admin-username azureuser \
  --generate-ssh-keys

注意

請據此取代 myVMmyResourceGroupimageNameazureuser

針對擴展集,您會針對 az-vmss-create 使用相同的 --ephemeral-os-disk true 參數,並將 --os-disk-caching 參數設定為 ReadOnly,並針對暫存磁碟放置將 --ephemeral-os-disk-placement 參數設定為 ResourceDisk,或針對快取磁碟放置將參數設定為 CacheDisk

使用 REST 重新安裝 VM 的映像

您可以使用下方說明的 REST API,並透過前往 VM 的 [概觀] 窗格使用 Azure 入口網站,以暫時性 OS 磁碟重新安裝映像虛擬機器執行個體。 針對擴展集,已透過 PowerShell、CLI 和入口網站提供重新安裝映像。

POST https://management.azure.com/subscriptions/{sub-
id}/resourceGroups/{rgName}/providers/Microsoft.Compute/VirtualMachines/{vmName}/reimage?api-version=2019-12-01"

PowerShell

若要針對 PowerShell VM 部署使用暫時性磁碟,請在 VM 設定中使用 Set-AzVMOSDisk。 將 -DiffDiskSetting 設定為 Local 和將 -Caching 設定為ReadOnly,並將 -DiffDiskPlacement 設定為 ResourceDisk

Set-AzVMOSDisk -DiffDiskSetting Local -DiffDiskPlacement ResourceDisk -Caching ReadOnly

若要針對 PowerShell VM 部署使用快取磁碟上的暫時性磁碟,請在 VM 設定中使用 Set-AzVMOSDisk。 將 -DiffDiskSetting 設定為 Local 和將 -Caching 設定為 ReadOnly,並將 -DiffDiskPlacement 設定為 CacheDisk

Set-AzVMOSDisk -DiffDiskSetting Local -DiffDiskPlacement CacheDisk -Caching ReadOnly

針對擴展集部署,請在設定中使用 Set-AzVmssStorageProfile Cmdlet。 將 -DiffDiskSetting 設定為 Local,將-Caching 設定為 ReadOnly,將 -DiffDiskPlacement 設定為 ResourceDiskCacheDisk

Set-AzVmssStorageProfile -DiffDiskSetting Local -DiffDiskPlacement ResourceDisk -OsDiskCaching ReadOnly

下一步

如需暫時性 OS 磁碟的詳細資訊。