扩展Azure VM OS disk(windows 和Linux)

在Azure里面创建的虚拟机,windows的OS disk默认大小是127G, Linux的OS disk默认大小是30G。

有些情况下我们需要扩展OS 的磁盘,介绍下windows和Linux的扩展方法。

需要在电脑上安装Azure Powershell较新的版本,我使用的是V 3.7.0,当前最新的是4.1.0

扩展Windows虚拟机:

先获取OS disk的信息:

PS C:\Users\jason>$rg = "test"
PS C:\Users\jason>$vmname = "jasonwin"
PS C:\Users\jason>$vm = get-azurermvm -ResourceGroupName $rg -Name $vmname
PS C:\Users\jason>$vm = get-azurermvm -ResourceGroupName $rg -Name $vmname
PS C:\Users\jason>$vm.StorageProfile.OsDisk
OsType             : Windows
EncryptionSettings :
Name               : jasonwin
Vhd                : Microsoft.Azure.Management.Compute.Models.VirtualHardDisk
Image              :
Caching            : ReadWrite
CreateOption       : FromImage
DiskSizeGB         :

更新磁盘大小需要 关闭(shutdown)虚拟机:

PS C:\Users\jason> stop-azurermvm -ResourceGroupName $rg -Name $vmname

Virtual machine stopping operation
This cmdlet will stop the specified virtual machine. Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y


OperationId :
Status      : Succeeded
StartTime   : 6/22/2017 4:26:22 PM
EndTime     : 6/22/2017 4:28:23 PM
Error       :

更改磁盘大小:

PS C:\Users\jason>$vm.StorageProfile.OSDisk.DiskSizeGB = 1023
PS C:\Users\jason>Update-AzureRmVM -ResourceGroupName $rg -VM $vm

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

更新完成后启动虚拟机:

PS C:\Users\jason>Start-AzureRmVM -ResourceGroupName $rg -Name $vmname
OperationId :
Status      : Succeeded
StartTime   : 6/22/2017 4:32:56 PM
EndTime     : 6/22/2017 4:33:49 PM
Error       :

虚拟机启动以后,RDP到这台机器上,到磁盘管理里面将磁盘扩展到最大(1023G),结果如下图:

扩展Azure VM OS disk(windows 和Linux)_第1张图片



下面介绍Linux的磁盘扩展方法(Powershell部分是相同,不同的是虚拟机内部操作)

PS C:\Users\jason> $rg = "test2"
PS C:\Users\jason> $vmname = "jasonlinux"
PS C:\Users\jason> $vm = Get-AzureRmVM -ResourceGroupName $rg -Name $vmname
PS C:\Users\jason> $vm.StorageProfile.OsDisk[0].DiskSizeGB = 1023
PS C:\Users\jason> Stop-AzureRmVM -ResourceGroupName $rg -Name $vmname
PS C:\Users\jason>$vm.StorageProfile.OsDisk[0].DiskSizeGB = 1023
PS C:\Users\jason>Update-AzureRmVM -ResourceGroupName $rg -VM $vm
PS C:\Users\jason>Start-AzureRmVM -ResourceGroupName $rg -Name $vmname
Virtual machine stopping operation
This cmdlet will stop the specified virtual machine. Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y


OperationId :
Status      : Succeeded
StartTime   : 6/22/2017 4:58:55 PM
EndTime     : 6/22/2017 5:01:07 PM
Error       :
扩展Azure VM OS disk(windows 和Linux)_第2张图片

扩展Azure VM OS disk(windows 和Linux)_第3张图片


[root@jasonlinux ~]# fdisk -l

Disk /dev/sdb: 75.2 GB, 75161927680 bytes, 146800640 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x643bf3c8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   146798591    73398272   83  Linux

Disk /dev/sda: 1098.4 GB, 1098437885952 bytes, 2145386496 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bd020

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    62914559    30944256   83  Linux

扩展Azure VM OS disk(windows 和Linux)_第4张图片

扩展Azure VM OS disk(windows 和Linux)_第5张图片


[root@jasonlinux ~]# fdisk /dev/sda
[root@jasonlinux ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): u
Changing display/entry units to cylinders (DEPRECATED!).

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): p

Disk /dev/sda: 1098.4 GB, 1098437885952 bytes, 2145386496 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bd020

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First cylinder (64-133544, default 64): 
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-133544, default 133544): 
Using default value 133544
Partition 2 of type Linux and of size 1022.5 GiB is set

Command (m for help): a
Partition number (1,2, default 2): 2    

Command (m for help): p

Disk /dev/sda: 1098.4 GB, 1098437885952 bytes, 2145386496 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bd020

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2   *          64      133544  1072179156   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

扩展Azure VM OS disk(windows 和Linux)_第6张图片


扩展Azure VM OS disk(windows 和Linux)_第7张图片

这个warning是正常的,忽略即可。

然后reboot这个虚拟机:

[root@jasonlinux ~]# sudo reboot

重启完成后,SSH 进去,扩展磁盘:

扩展Azure VM OS disk(windows 和Linux)_第8张图片



Note:

CentOS/RHEL 6.X 用sudo resize2fs /dev/sdaX

CentOS/RHEL 7.x 用xfs_growfs -d /dev/sdaX



你可能感兴趣的:(扩展Azure VM OS disk(windows 和Linux))