扩展Azure VM OS disk(windows 和Linux)

本文介绍了如何在Azure环境中扩展Windows和Linux虚拟机的OS磁盘。对于Windows,需要通过Azure PowerShell获取磁盘信息,并在RDP连接后在磁盘管理中扩展;而对于Linux,扩展过程包括使用Powershell进行操作以及根据不同的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),结果如下图:




下面介绍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       :



[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




[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.




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

然后reboot这个虚拟机:

[root@jasonlinux ~]# sudo reboot

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




Note:

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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值