2017-05-10

Resize Linux VM system disk on Azure

So you have created a Linux VM in Azure with too small disk? Resizing the disk is possible, but requires couple of steps which I will describe here.

My resource group is named "group1" and the VM is named "rhel7a". The Linux VM I used is a RHEL, but the commands are applicable for at least CentOS and Fedora as well.




Running Azure CLI 2.0 on your laptop (or management server):
First step is the enlarge the device on Azure.

Turn off your VM by stopping it, otherwise it's not possible to modify the disk:
# az vm stop --resource-group group1 --name rhel7a

Deallocate the VM:
# az vm deallocate --resource-group group1 --name rhel7a
Check your VM disk name and expand it to for example 40GB:
# az disk list -g group1 --output table

# az disk update --resource-group group1 --name rhel7a_disk1_30de1433bf824608b0c3ee6c8b2b52a0 --size-gb 40

Start the VM:
# az vm start --resource-group group1 --name rhel7a

Now login and continue inside the VM:
# ssh username@13.95.xx.xx


This part happens inside the Linux VM:

First we will make expand the partition and check the disk name you are about to modify:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        19G  1.4G   16G   4% /
devtmpfs        1.7G     0  1.7G   0% /dev
tmpfs           1.7G     0  1.7G   0% /dev/shm
tmpfs           1.7G  8.3M  1.7G   1% /run
tmpfs           1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/sda1       976M  140M  769M  16% /boot
/dev/sdb1       6.8G  2.1G  4.4G  32% /mnt/resource
tmpfs           345M     0  345M   0% /run/user/1000

Modify the partition by deleting and re-creating it. Data will not be lost:

# fdisk /dev/sda
Command (m for help): p
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83875364    40888082+  83  Linux

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


Partition 2 is deleted

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 sector (2099200-83886079, default 2099200): (press enter here)
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-83886079, default 83886079): (press enter here)
Using default value 83886079
Partition 2 of type Linux and of size 40 GiB is set

Command (m for help): p

At this point you'll need to reboot the server:
# reboot

Now log in again with ssh and continue by resizing the ext4 filesystem:
# resize2fs /dev/sda2
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 5
The filesystem on /dev/sda2 is now 10222020 blocks long.

For xfs filesystem you can do: xfs_growfs -d /dev/sda2


Verify the filesystem is seen at correct size:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        39G  1.4G   36G   4% /
devtmpfs        1.7G     0  1.7G   0% /dev
tmpfs           1.7G     0  1.7G   0% /dev/shm
tmpfs           1.7G  8.3M  1.7G   1% /run
tmpfs           1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/sda1       976M  140M  769M  16% /boot
/dev/sdb1       6.8G  2.1G  4.4G  32% /mnt/resource
tmpfs           345M     0  345M   0% /run/user/1000

And we are ready!


No comments:

Post a Comment