變更 Azure 受控磁碟的磁碟類型

適用於:✔️ Linux VMs ✔️ Windows

Azure 受控磁碟有五種磁碟類型:Azure Ultra 磁碟、進階 SSD v2、進階 SSD、標準 SSD 和標準 HDD。 您可以根據自己的效能需求,在進階 SSD、標準 SSD 和標準 HDD 之間輕鬆切換。 進階 SSD 和標準 SSD 也可搭配區域備援儲存體使用。 您尚無法切換 Ultra 磁碟或進階 SSD v2,您必須部署具有現有磁碟快照集的新磁碟。 如需詳細資訊,請參閱移轉至進階 SSD v2 或 Ultra 磁碟

非受控磁碟不支援此功能。 但是,您可以使用 CLIPowerShell,輕鬆地將非受控磁碟轉換成受控磁碟,而得以在磁碟類型之間切換。

開始之前

轉換需要重新啟動虛擬機器 (VM),因此請在預先存在的維護期間排定磁碟移轉。

Restrictions

  • 您每天只能變更磁碟類型兩次。
  • 您只能變更受控磁碟的磁碟類型。 如果您的磁碟為非受控,請使用 CLIPowerShell 將其轉換成受控磁碟,以在磁碟類型之間切換。

在不同帳戶之間切換 VM 的所有受控磁碟

此範例說明如何將 VM 的所有磁碟轉換成進階儲存體。 不過,藉由變更此範例中的 $storageType 變數,您可以將 VM 的磁碟類型轉換為標準 SSD 或標準 HDD。 若要使用進階受控磁碟,VM 必須使用可支援進階儲存體的 VM 大小。 此範例也會切換成可支援進階儲存體的大小:

# Name of the resource group that contains the VM
$rgName = 'yourResourceGroup'

# Name of the your virtual machine
$vmName = 'yourVM'

# Choose between Standard_LRS, StandardSSD_LRS, StandardSSD_ZRS, Premium_ZRS, and Premium_LRS based on your scenario
$storageType = 'Premium_LRS'

# Premium capable size
# Required only if converting storage from Standard to Premium
$size = 'Standard_DS2_v2'

# Stop and deallocate the VM before changing the size
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force

$vm = Get-AzVM -Name $vmName -resourceGroupName $rgName

# Change the VM size to a size that supports Premium storage
# Skip this step if converting storage from Premium to Standard
$vm.HardwareProfile.VmSize = $size
Update-AzVM -VM $vm -ResourceGroupName $rgName

# Get all disks in the resource group of the VM
$vmDisks = Get-AzDisk -ResourceGroupName $rgName 

# For disks that belong to the selected VM, convert to Premium storage
foreach ($disk in $vmDisks)
{
	if ($disk.ManagedBy -eq $vm.Id)
	{
		$disk.Sku = [Microsoft.Azure.Management.Compute.Models.DiskSku]::new($storageType)
		$disk | Update-AzDisk
	}
}

Start-AzVM -ResourceGroupName $rgName -Name $vmName

變更個別受控磁碟的類型

對於您的開發/測試工作負載,您可能會想要混用標準和進階磁碟,以降低成本。 您可以選擇僅升級需要較高效能的磁碟。 此範例說明如何將單一 VM 磁碟從標準儲存體轉換成進階儲存體。 不過,藉由變更此範例中的 $storageType 變數,您可以將 VM 的磁碟類型轉換為標準 SSD 或標準 HDD。 若要使用進階受控磁碟,VM 必須使用可支援進階儲存體的 VM 大小。 您也可以使用這些範例,將磁碟從本地備援儲存體 (LRS) 磁碟變更為區域備援儲存體 (ZRS) 磁碟,反之亦然。 此範例也會說明如何切換至支援進階儲存體的大小:


$diskName = 'yourDiskName'
# resource group that contains the managed disk
$rgName = 'yourResourceGroupName'
# Choose between Standard_LRS, StandardSSD_LRS, StandardSSD_ZRS, Premium_ZRS, and Premium_LRS based on your scenario
$storageType = 'Premium_LRS'
# Premium capable size 
$size = 'Standard_DS2_v2'

$disk = Get-AzDisk -DiskName $diskName -ResourceGroupName $rgName

# Get parent VM resource
$vmResource = Get-AzResource -ResourceId $disk.ManagedBy

# Stop and deallocate the VM before changing the storage type
Stop-AzVM -ResourceGroupName $vmResource.ResourceGroupName -Name $vmResource.Name -Force

$vm = Get-AzVM -ResourceGroupName $vmResource.ResourceGroupName -Name $vmResource.Name 

# Change the VM size to a size that supports Premium storage
# Skip this step if converting storage from Premium to Standard
$vm.HardwareProfile.VmSize = $size
Update-AzVM -VM $vm -ResourceGroupName $rgName

# Update the storage type
$disk.Sku = [Microsoft.Azure.Management.Compute.Models.DiskSku]::new($storageType)
$disk | Update-AzDisk

Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name

移轉至進階 SSD v2 或 Ultra 磁碟

您目前只能透過在標準儲存體上儲存的快照集 (累加式標準 HDD 快照集),將現有的磁碟移轉至 Ultra 磁碟或進階 SSD v2。 您無法移轉在進階儲存體和其他選項中儲存的快照集。

進階 SSD v2 磁碟和 Ultra 磁碟各自都有一組限制。 例如,這兩者都無法作為 OS 磁碟,也無法在所有區域中使用。 如需詳細資訊,請參閱其文章中的進階 SSD v2 限制Ultra 磁碟 GA 範圍和限制章節。

重要

將標準 HDD、標準 SSD 或進階 SSD 移轉至 Ultra 磁碟或進階 SSD v2 時,邏輯磁區大小必須是 512。

下列指令碼會將標準 HDD、標準 SSD 或進階 SSD 的快照集移轉至 Ultra 磁碟或進階 SSD v2。

$diskName = "yourDiskNameHere"
$resourceGroupName = "yourResourceGroupNameHere"
$snapshotName = "yourDesiredSnapshotNameHere"

# Valid values are 1, 2, or 3
$zone = "yourZoneNumber"

#Provide the size of the disks in GB. It should be greater than the VHD file size.
$diskSize = '128'

#Provide the storage type. Use PremiumV2_LRS or UltraSSD_LRS.
$storageType = 'PremiumV2_LRS'

#Provide the Azure region (e.g. westus) where Managed Disks will be located.
#This location should be same as the snapshot location
#Get all the Azure location using command below:
#Get-AzLocation

#Select the same location as the current disk
#Note that Premium SSD v2 and Ultra Disks are only supported in a select number of regions
$location = 'eastus'

#When migrating a Standard HDD, Standard SSD, or Premium SSD to either an Ultra Disk or Premium SSD v2, the logical sector size must be 512
$logicalSectorSize=512

# Get the disk that you need to backup by creating an incremental snapshot
$yourDisk = Get-AzDisk -DiskName $diskName -ResourceGroupName $resourceGroupName

# Create an incremental snapshot by setting the SourceUri property with the value of the Id property of the disk
$snapshotConfig=New-AzSnapshotConfig -SourceUri $yourDisk.Id -Location $yourDisk.Location -CreateOption Copy -Incremental 
$snapshot = New-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -Snapshot $snapshotConfig

$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id -DiskSizeGB $diskSize -LogicalSectorSize $logicalSectorSize -Zone $zone
 
New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

下一步

使用快照集來製作 VM 的唯讀複本。