Expand disk partitions LVM
Extending disks partitions/LVMs is a usual task, and without proper care, we’ll end up loosing valuable data. So below is three likely scenarios
1. No LVM
2. Extend LVM partition
3. AWS EC2 partition
Scenario 1: NO LVM
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 600G 0 disk
├─vda1 253:1 0 512M 0 part /boot
└─vda2 253:2 0 499.5G 0 part /
In this case, 100G was added and we need to extend the root partition without any reboots. The root partition is using the block device /dev/vda2 and we’ve to extend it. For extending the partitions, we use fdisk command utility.
Start the fdisk utility like below:
# fdisk /dev/vda
Print the current partition layout by typing p and note down the start sector for /dev/vda2
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux /dev/vda2 1050624 1048575999 523762688 83 Linux
Now we’ll delete the partition /dev/vda2. [This won’t delete the data but delete the concerned entry from the partition table
Command (m for help): d Partition number (1,2, default 2): 2 Partition 2 is deleted
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux
Now we can see that /dev/vda2 is absent, and now we’ll recreate it with the new sector size.
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 (1050624-1258291199, default 1050624): Using default value 1050624 Last sector, +sectors or +size{K,M,G} (1050624-1258291199, default 1258291199): Using default value 1258291199 Partition 2 of type Linux and of size 599.5 GiB is set
We used the default value for First & Last Sector, Since the default First Sector was 1050624 and if its different, we must manually put 1050624 in place of First Sector. For the Last Sector we could use the default value and it’ll take the rest of the space.
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux /dev/vda2 1050624 1258291199 628620288 83 Linux
When the partition layout is identical to that of the previous one, except for End sector and blocks, then we could write the changes to the partition table.
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.
The above step has re-written the partition layout. However, to make it active we need to reboot the server or instruct the kernel to re-read it. Rebooting the server is not recommended, so we use partx command line utility to instruct kernel about the partition layout change.
# partx -u /dev/vda2
Please note, in centos7 the above command works. But for other older servers, -u option won’t be present. So we could use -a flag, or use other utilities like partprobe, kpartx etc.
Now the partition has grown by a 100G
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 600G 0 disk
├─vda1 253:1 0 512M 0 part /boot
└─vda2 253:2 0 599.5G 0 part /
The block device is resized, however, the filesystem hasn’t. So we either reboot or use resize2fs
# resize2fs /dev/vda2 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vda2 is mounted on /; on-line resizing required old_desc_blocks = 63, new_desc_blocks = 75 The filesystem on /dev/vda2 is now 157155072 blocks long.
If the filesystem is in xfs format, use xfs_growfs instead
# xfs_growfs /dev/vda2
Now our disk extension is complete
Scenario 2: Extend LVM partition
Run the below commands and note down the results:
# lsblk # df -hT # pvs # vgs # lvs
Start the fdisk utility like below:
# fdisk /dev/vda
Print the current partition layout by typing p and note down the start sector for /dev/vda2
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux /dev/vda2 1050624 1048575999 523762688 8e Linux LVM
Now we’ll delete the partition /dev/vda2. [This won’t delete the data but delete the concerned entry from the partition table]
Command (m for help): d Partition number (1,2, default 2): 2 Partition 2 is deleted
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux
Now we can see that /dev/vda2 is absent, and now we’ll recreate it with the new sector size.
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 (1050624-1258291199, default 1050624): Using default value 1050624 Last sector, +sectors or +size{K,M,G} (1050624-1258291199, default 1258291199): Using default value 1258291199 Partition 2 of type Linux and of size 599.5 GiB is set
We used the default value for First & Last Sector, Since the default First Sector was 1050624 and if its different, we must manually put 1050624 in place of First Sector. For the Last Sector we could use the default value and it’ll take the rest of the space.
Command (m for help): p Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000a8f87 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1050623 524288 83 Linux /dev/vda2 1050624 1258291199 628620288 83 Linux
Now we change the partition type of /dev/vda2 from 83-Linux to 8e-Linux LVM
Command (m for help): t
Partition number (2-4, default 2): 2 Hex code (type L to list all codes): 8e
When the partition layout is identical to that of the previous one, except for End sector and blocks, then we could write the changes to the partition table.
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.
The above step has re-written the partition layout. However, to make it active we need to reboot the server or instruct the kernel to re-read it. Rebooting the server is not recommended, so we use partx command line utility to instruct kernel about the partition layout change.
# partx -u /dev/vda2
Please note, in centos7 the above command works. But for other older servers, -u option won’t be present. So we could use -a flag, or use other utilities like partprobe, kpartx etc.
Now the partition has grown by a 100G
Run pvresize
pvresize /dev/vda2
Run lvextend
lvextend -l +100%FREE /dev/vg_cpanel/lv_root
Resize the file system
resize2fs /dev/vg_cpanel/lv_root
[If format type is xfs, then the above will fail. Use xfs_growfs]
resize2fs /dev/vg_cpanel/lv_root
Finally check whether fstab is using a valid UUID of the extended lvm or else ignore
cat /etc/fstab
Scenario 3: AWS EC2 instance
The old statistics:
# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext4 50G 42G 7.6G 85% /
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 70G 0 disk
└─xvda1 202:1 0 50G 0 part /
From the above we can see that there is 70G disk in total, but only 50G is used by the partition.
Commands executed as below:
# growpart --dry-run /dev/xvda 1
The above is a dryrun and will let you know about the changes going to made
# growpart /dev/xvda 1 CHANGED: disk=/dev/xvda partition=1: start=4096 old: size=104853470,end=104857566 new: size=146796510,end=146800606
Block device has been now resized.
# growpart /dev/xvda 1 CHANGED: disk=/dev/xvda partition=1: start=4096 old: size=104853470,end=104857566 new: size=146796510,end=146800606
To resize the filesystem
# resize2fs /dev/xvda1 resize2fs 1.42.12 (29-Aug-2014) Filesystem at /dev/xvda1 is mounted on /; on-line resizing required old_desc_blocks = 4, new_desc_blocks = 5 The filesystem on /dev/xvda1 is now 18349563 (4k) blocks long.
Filesystem and block device has been resized.
Final stats:
# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 56K 2.0G 1% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/xvda1 ext4 69G 42G 28G 61% /
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 70G 0 disk
└─xvda1 202:1 0 70G 0 part /
Very useful…Thanks