Extend osDisk partition on Azure Linux CentOS 7.x VM

Azure VMs usually comes with the default osDisk 30-50GB and often you will find that needs to be increased. The following steps can be used to extend the disk as well as the partition.

Assumptions
Here we assume that the VM is on CentOS 7.x version. (The same steps should work for lower version as well, not tested at the moment)
OS Disk is unmanaged. Azure has the latest disks called Managed Disk. It is recommended to use it for all new VMS. The steps should be the same.

First verify the current disk size.


[root@centos ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  256M  0 part /boot
└─sda2   8:2    0 49.8G  0 part /
sdb      8:16   0  112G  0 disk 
└─sdb1   8:17   0  112G  0 part /mnt/resource
[root@centos ~]# lsblk

Here we have two partitions on /dev/sda

To resize the disk, follow the instruction

  • Login to Azure Portal.
  • Goto the VM -> Shutdown
  • Goto VM -> Disks -> Select the disk that you want to expand -> Enter new diskspace and save
  • Start the VM
  • Login to the VM and execute the following commands

Once logged in, we will want to delete the partition 2 (sda2) and then re-create the partition table. Do not worry, this will not remove the data in the partition.

fdisk /dev/sda
u
p
d -> then enter 2 for the 2nd partition 
n -> then enter p for primary partition and then enter again when it asks for the data size. By default it will be selecting 100% of available disk
w 

[root@centos ~]#  fdisk /dev/sda

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
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): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x0009f94e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          33      262144   83  Linux
/dev/sda2              33        6528    52165632   83  Linux

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

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x0009f94e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          33      262144   83  Linux

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

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x0009f94e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          33      262144   83  Linux
/dev/sda2              33       13054   104593087   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.
[root@centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        50G  963M   49G   2% /
devtmpfs         28G     0   28G   0% /dev
tmpfs            28G     0   28G   0% /dev/shm
tmpfs            28G  8.3M   28G   1% /run
tmpfs            28G     0   28G   0% /sys/fs/cgroup
/dev/sda1       240M   89M  135M  40% /boot
/dev/sdb1       111G  2.1G  103G   2% /mnt/resource
tmpfs           5.6G     0  5.6G   0% /run/user/1000
[root@centos ~]# reboot

After this, reboot your instance. Reboot helps the OS to reload the new partiton table. Once rebooted, log back in. You would now see that the partiton is extended, however OS doesn’t recognize this.


[root@centos ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  256M  0 part /boot
└─sda2   8:2    0 99.8G  0 part /
sdb      8:16   0  112G  0 disk 
└─sdb1   8:17   0  112G  0 part /mnt/resource
[root@centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        50G  963M   49G   2% /
devtmpfs         28G     0   28G   0% /dev
tmpfs            28G     0   28G   0% /dev/shm
tmpfs            28G  8.3M   28G   1% /run
tmpfs            28G     0   28G   0% /sys/fs/cgroup
/dev/sda1       240M   89M  135M  40% /boot
/dev/sdb1       111G  2.1G  103G   2% /mnt/resource
tmpfs           5.6G     0  5.6G   0% /run/user/1000
[root@centos ~]#

To fix this, we run


[root@centos ~]# xfs_growfs -d /dev/sda2
meta-data=/dev/sda2              isize=512    agcount=4, agsize=3260352 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13041408, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6367, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13041408 to 26148271
[root@centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       100G  963M   99G   1% /
devtmpfs         28G     0   28G   0% /dev
tmpfs            28G     0   28G   0% /dev/shm
tmpfs            28G  8.3M   28G   1% /run
tmpfs            28G     0   28G   0% /sys/fs/cgroup
/dev/sda1       240M   89M  135M  40% /boot
/dev/sdb1       111G  2.1G  103G   2% /mnt/resource
tmpfs           5.6G     0  5.6G   0% /run/user/1000
[root@centos ~]# 

As you can see, OS now sees 100GB instead of 50GB

Leave a Reply

Your email address will not be published. Required fields are marked *