【问题标题】:Reducing size of Azure VM OS Disk for download/export减小用于下载/导出的 Azure VM OS 磁盘的大小
【发布时间】:2021-01-23 07:43:27
【问题描述】:

我想创建一个操作系统磁盘小于默认 127gb 的 Azure VM。我一直无法在 Azure 门户中找到这样的选项,因此我尝试缩小磁盘。我没有成功。

我知道我可以修剪(使用碎片整理工具)和缩小卷(使用磁盘管理),但这不会改变硬盘的“物理”大小。也就是说,如果我将磁盘缩小到 40gb,则只有 87gb 未分配,而 blob 仍会报告 127gb。

我试图实现的是缩小 blob 以匹配分配的空间,从而促进更小的 VM 映像下载/导出(例如 40 与 127gb)。

感谢所有帮助。

【问题讨论】:

    标签: azure azure-blob-storage azure-virtual-machine


    【解决方案1】:

    我已经写了一篇博文,详细说明了这个答案。但这里的主要问题是能够减小默认为 127gb 的 Azure VM 的大小,以便快速导出/下载。我实现这一点的方法是修整硬盘驱动器,然后使用Disk2VHD 创建正在运行的 VM 的 VHD 文件。 Disk2VHD 将创建一个可扩展磁盘,该磁盘仅与磁盘上的当前数据一样大,而不是整个可用磁盘。就我而言,40gb 与 127gb。如果将此 VHD 文件保存到附加磁盘(读取:blob 存储),则整个团队可以通过 HTTP 轻松下载它。因此,下载现在是 40gbs 而不是 127gbs。更多内容,请阅读我的详细博文:

    http://www.kevinmcloutier.com/?p=263

    原来的链接失效了: https://web.archive.org/web/20161027213258/http://kevinmcloutier.com/post/4

    【讨论】:

      【解决方案2】:

      在 Azure 中,如果您创建一个 VM,它将使用一些默认配置创建。目前不支持从 Azure 门户减少/缩小 Azure VM 的 OS 磁盘(托管或非托管)大小(例如从 128Gb 到 32Gb),我们可以使用以下过程将其存档,并减少磁盘成本。

      步骤 1. 打开您的虚拟机并转到磁盘管理。

      步骤 2. 打开 PowerShell 并执行以下命令。

      执行成功后可以发现如下

      第 3 步。现在从 Azure 门户取消分配 VM

      第 4 步。现在转到磁盘的属性刀片并复制资源 ID

      第 5 步。现在从您的本地系统执行以下 PowerShell 脚本。必须将 $DiskID、$VMName、$AzSubscription 更改为您的值

          # Variables
      $DiskID = ""# eg. "/subscriptions/203bdbf0-69bd-1a12-a894-a826cf0a34c8/resourcegroups/rg-server1-prod-1/providers/Microsoft.Compute/disks/Server1-Server1"
      $VMName = "VM-Server1"
      $DiskSizeGB = 32
      $AzSubscription = "Prod Subscription"
      
      # Script
      # Provide your Azure admin credentials
      Connect-AzAccount
      
      #Provide the subscription Id of the subscription where snapshot is created
      Select-AzSubscription -Subscription $AzSubscription
      
      # VM to resize disk of
      $VM = Get-AzVm | ? Name -eq $VMName
      
      #Provide the name of your resource group where snapshot is created
      $resourceGroupName = $VM.ResourceGroupName
      
      # Get Disk from ID
      $Disk = Get-AzDisk | ? Id -eq $DiskID
      
      # Get VM/Disk generation from Disk
      $HyperVGen = $Disk.HyperVGeneration
      
      # Get Disk Name from Disk
      $DiskName = $Disk.Name
      
      # Get SAS URI for the Managed disk
      $SAS = Grant-AzDiskAccess -ResourceGroupName $resourceGroupName -DiskName $DiskName -Access 'Read' -DurationInSecond 600000;
      
      #Provide the managed disk name
      #$managedDiskName = "yourManagedDiskName" 
      
      #Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
      #$sasExpiryDuration = "3600"
      
      #Provide storage account name where you want to copy the snapshot - the script will create a new one temporarily
      $storageAccountName = "shrink" + [system.guid]::NewGuid().tostring().replace('-','').substring(1,18)
      
      #Name of the storage container where the downloaded snapshot will be stored
      $storageContainerName = $storageAccountName
      
      #Provide the key of the storage account where you want to copy snapshot. 
      #$storageAccountKey = "yourStorageAccountKey"
      
      #Provide the name of the VHD file to which snapshot will be copied.
      $destinationVHDFileName = "$($VM.StorageProfile.OsDisk.Name).vhd"
      
      #Generate the SAS for the managed disk
      #$sas = Grant-AzureRmDiskAccess -ResourceGroupName $resourceGroupName -DiskName $managedDiskName -Access Read -DurationInSecond $sasExpiryDuration
      
      #Create the context for the storage account which will be used to copy snapshot to the storage account 
      $StorageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -SkuName Standard_LRS -Location $VM.Location
      $destinationContext = $StorageAccount.Context
      $container = New-AzStorageContainer -Name $storageContainerName -Permission Off -Context $destinationContext
      
      #Copy the snapshot to the storage account and wait for it to complete
      Start-AzStorageBlobCopy -AbsoluteUri $SAS.AccessSAS -DestContainer $storageContainerName -DestBlob $destinationVHDFileName -DestContext $destinationContext
      while(($state = Get-AzStorageBlobCopyState -Context $destinationContext -Blob $destinationVHDFileName -Container $storageContainerName).Status -ne "Success") { $state; Start-Sleep -Seconds 20 }
      $state
      
      # Revoke SAS token
      Revoke-AzDiskAccess -ResourceGroupName $resourceGroupName -DiskName $DiskName
      
      # Emtpy disk to get footer from
      $emptydiskforfootername = "$($VM.StorageProfile.OsDisk.Name)-empty.vhd"
      
      # Empty disk URI
      #$EmptyDiskURI = $container.CloudBlobContainer.Uri.AbsoluteUri + "/" + $emptydiskforfooter
      
      $diskConfig = New-AzDiskConfig `
          -Location $VM.Location `
          -CreateOption Empty `
          -DiskSizeGB $DiskSizeGB `
          -HyperVGeneration $HyperVGen
      
      $dataDisk = New-AzDisk `
          -ResourceGroupName $resourceGroupName `
          -DiskName $emptydiskforfootername `
          -Disk $diskConfig
      
      $VM = Add-AzVMDataDisk `
          -VM $VM `
          -Name $emptydiskforfootername `
          -CreateOption Attach `
          -ManagedDiskId $dataDisk.Id `
          -Lun 63
      
      Update-AzVM -ResourceGroupName $resourceGroupName -VM $VM
      
      $VM | Stop-AzVM -Force
      
      
      # Get SAS token for the empty disk
      $SAS = Grant-AzDiskAccess -ResourceGroupName $resourceGroupName -DiskName $emptydiskforfootername -Access 'Read' -DurationInSecond 600000;
      
      # Copy the empty disk to blob storage
      Start-AzStorageBlobCopy -AbsoluteUri $SAS.AccessSAS -DestContainer $storageContainerName -DestBlob $emptydiskforfootername -DestContext $destinationContext
      while(($state = Get-AzStorageBlobCopyState -Context $destinationContext -Blob $emptydiskforfootername -Container $storageContainerName).Status -ne "Success") { $state; Start-Sleep -Seconds 20 }
      $state
      
      # Revoke SAS token
      Revoke-AzDiskAccess -ResourceGroupName $resourceGroupName -DiskName $emptydiskforfootername
      
      # Remove temp empty disk
      Remove-AzVMDataDisk -VM $VM -DataDiskNames $emptydiskforfootername
      Update-AzVM -ResourceGroupName $resourceGroupName -VM $VM
      
      # Delete temp disk
      Remove-AzDisk -ResourceGroupName $resourceGroupName -DiskName $emptydiskforfootername -Force;
      
      # Get the blobs
      $emptyDiskblob = Get-AzStorageBlob -Context $destinationContext -Container $storageContainerName -Blob $emptydiskforfootername
      $osdisk = Get-AzStorageBlob -Context $destinationContext -Container $storageContainerName -Blob $destinationVHDFileName
      
      $footer = New-Object -TypeName byte[] -ArgumentList 512
      write-output "Get footer of empty disk"
      
      $downloaded = $emptyDiskblob.ICloudBlob.DownloadRangeToByteArray($footer, 0, $emptyDiskblob.Length - 512, 512)
      
      $osDisk.ICloudBlob.Resize($emptyDiskblob.Length)
      $footerStream = New-Object -TypeName System.IO.MemoryStream -ArgumentList (,$footer)
      write-output "Write footer of empty disk to OSDisk"
      $osDisk.ICloudBlob.WritePages($footerStream, $emptyDiskblob.Length - 512)
      
      Write-Output -InputObject "Removing empty disk blobs"
      $emptyDiskblob | Remove-AzStorageBlob -Force
      
      
      #Provide the name of the Managed Disk
      $NewDiskName = "$DiskName" + "-new"
      
      #Create the new disk with the same SKU as the current one
      $accountType = $Disk.Sku.Name
      
      # Get the new disk URI
      $vhdUri = $osdisk.ICloudBlob.Uri.AbsoluteUri
      
      # Specify the disk options
      $diskConfig = New-AzDiskConfig -AccountType $accountType -Location $VM.location -DiskSizeGB $DiskSizeGB -SourceUri $vhdUri -CreateOption Import -StorageAccountId $StorageAccount.Id -HyperVGeneration $HyperVGen
      
      #Create Managed disk
      $NewManagedDisk = New-AzDisk -DiskName $NewDiskName -Disk $diskConfig -ResourceGroupName $resourceGroupName
      
      $VM | Stop-AzVM -Force
      
      # Set the VM configuration to point to the new disk  
      Set-AzVMOSDisk -VM $VM -ManagedDiskId $NewManagedDisk.Id -Name $NewManagedDisk.Name
      
      # Update the VM with the new OS disk
      Update-AzVM -ResourceGroupName $resourceGroupName -VM $VM
      
      $VM | Start-AzVM
      
      start-sleep 180
      # Please check the VM is running before proceeding with the below tidy-up steps
      
      # Delete old Managed Disk
      Remove-AzDisk -ResourceGroupName $resourceGroupName -DiskName $DiskName -Force;
      
      # Delete old blob storage
      $osdisk | Remove-AzStorageBlob -Force
      
      # Delete temp storage account
      $StorageAccount | Remove-AzStorageAccount -Force
      

      【讨论】:

      • 您能否简要说明一下该脚本的有效作用?
      【解决方案3】:

      您必须创建自己的 VM 映像,然后使用该映像进行部署。此模板向您展示如何使用自己的映像进行部署。

      https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image

      目前,图库中的图片均为 127GB。由于 Azure VM 只使用固定大小的磁盘,因此不能只选择大小。

      【讨论】:

      • 感谢您的反馈,这是def。建议的路线,但需要上传 40GB。根据上传速度,这可能需要 8 个小时以上(~10mbps)。我们试图在不上传的情况下实现这一目标,因为我们的下载速度为 120mbps+。我现在正在研究一个解决方案,在 Azure 中对其进行测试,一旦我确定它可以工作,就会在此处发布。
      • 如果您要在与最终存储位置相同的数据中心内的 Azure VM 上创建 VHD,您可以缩短上传时间。这可能是不可能的,但值得一试。
      【解决方案4】:

      Sapnandu 的解决方案有效。非常感谢! 我仍然不能 100% 确定让它工作的神奇标准是什么,但最后我有一个使用缩减磁盘运行的虚拟机。让它成为 Gen1 绝对是其中之一。 我尝试了许多类似的事情,但它们都在启动时卡住了。

      我的方法是:

      从图库图像创建虚拟机。您将拥有一个 128Gb 操作系统磁盘。

      根据您的需要调整虚拟机。在这里您可以运行调整大小脚本。因此,您最终会得到大约 90Gb 的未分配空间。

      经过几次失败的尝试后,我开始重视配置的虚拟机,我真的不想再次重复配置。

      制作此磁盘的快照,然后从快照制作磁盘。这些选项将通过快照和磁盘呈现给您。

      然后从磁盘创建一个新的虚拟机。检查它是否启动。启动诊断很快就会显示出来。

      停止新的 vm 后,我查找了 Sapnandu 脚本(他的帖子中的第 5 步)所需的 3 个参数(subscriptionID、diskID、vmName),并在 azure cloud shell 中执行了脚本。 (标题中的图标)

      这需要时间,大约 10 分钟左右。 脚本完成后,新虚拟机正在使用较小的磁盘运行。

      所以,有一个脚本可以完美运行的虚拟机设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-10
        • 2017-08-30
        • 2022-10-25
        • 2022-08-20
        • 2023-04-05
        • 1970-01-01
        • 2019-11-01
        • 2019-11-13
        相关资源
        最近更新 更多