How to Resize LVM Partition Inside an Extended Partition
Resizing a logical volume in Linux is not very difficult but it can be tricky if the root is under an extended partition.
Resizing a logical volume in Linux is not very difficult and can be achieved through very straightforward approach. Here are the usual steps:
- Create a new partition on hard disk.
- Add the partition you just created as a physical volume.
- Add the new physical volume to the volume group.
- Assign space from the volume group to the logical volume.
- Resize the filesystem.
But in this scenario, you have the root filesystem (as an LVM partition) mounted under an extended partition, not within a primary partition. You just have one primary partition which is mounted on /boot and rest all space is part of that extended partition.
Sounds troublesome? Let me show you how to resize LVM inside extended partition.
Resizing LVM partition inside extended partition
I am using a Linux installed in virtual machine in this tutorial.
Step 1: Shut down your VM and increase the disk size
First, shut down your VM and increase the disk size. Here, I have increased the disk /dev/sda size by 20 GB to around 40 GB. Then start your VM and go to the console.
Have a look at our disk partitions.
root@Ubuntu14:~# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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 identifier: 0x000a975f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM
If you analyze the disk space with df command, here's what it shows for me:
root@Ubuntu14:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 989M 4.0K 989M 1% /dev
tmpfs tmpfs 201M 716K 200M 1% /run
/dev/dm-0 ext4 19G 1.5G 16G 9% /
none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup
none tmpfs 5.0M 0 5.0M 0% /run/lock
none tmpfs 1001M 0 1001M 0% /run/shm
none tmpfs 100M 0 100M 0% /run/user
/dev/sda1 ext2 236M 40M 184M 18% /boot
Here, the object is to increase the size of the partition /dev/dm-0 which is mounted on /dev/sda5.
Let me also show the current status of physical volumes, volume groups and logical volumes:
root@Ubuntu14:~# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root ubuntu14-vg -wi-ao--- 18.74g
swap_1 ubuntu14-vg -wi-ao--- 1020.00m
root@Ubuntu14:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m
root@Ubuntu14:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu14-vg 1 2 0 wz--n- 19.76g 20.00m
They all have around 20 GB of storage space assigned to them.
Step 2: Begin the LVM resizing process
Here are the steps for resizing the LVM partition:
Open fdisk utility and look at the partitions:
root@Ubuntu14:~# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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 identifier: 0x000a975f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM
Delete the extended partition (/dev/sda2) using command d
, which will automatically delete the underlying LVM partition which is /dev/sda5.
Command (m for help): d
Partition number (1-5): 2
Create a new partition again as extended using command n
with default start and end cylinder values.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (499712-83886079, default 499712):
Using default value 499712
Last sector, +sectors or +size{K,M,G} (499712-83886079, default 83886079):
Using default value 83886079
Command (m for help): p
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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 identifier: 0x000a975f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 499712 83886079 41693184 5 Extended
Create a logical partition (dev/sda5) using the default start and end cylinder values.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (501760-83886079, default 501760):
Using default value 501760
Last sector, +sectors or +size{K,M,G} (501760-83886079, default 83886079):
Using default value 83886079
Switch to expert mode by pressing x
.
Command (m for help): x
Run expert command b
to adjust the beginning of the partition (this changes the partition size, not where it ends). Enter the start value as it was earlier before deleting the partitions. Here it is 501760.
Expert command (m for help): b
Partition number (1-5): 5
New beginning of data (499713-83886079, default 501760): 501760
Then run r
to return to the main menu.
Expert command (m for help): r
Check the partition number just to make sure.
Command (m for help): p
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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 identifier: 0x000a975f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 499712 83886079 41693184 5 Extended
/dev/sda5 501760 83886079 41692160 83 Linux
Now change the partition type to LVM by pressing t
command and chose type 8e
.
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Press w
to write all the changes to the disk.
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.
Step 3: Make manual changes to physical and logical volume
Run partprobe command to inform OS about partition table changes:
root@Ubuntu14:~# partprobe /dev/sda
Run lsblk command to see that /dev/sda5 is now around 40 GB in size (for me).
root@Ubuntu14:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 243M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 39.8G 0 part
├─ubuntu14--vg-root (dm-0) 252:0 0 18.8G 0 lvm /
└─ubuntu14--vg-swap_1 (dm-1) 252:1 0 1020M 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
Run the df command and you'll notice that /dev/dm-0 still shows the old size details:
root@Ubuntu14:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 989M 4.0K 989M 1% /dev
tmpfs tmpfs 201M 716K 200M 1% /run
/dev/dm-0 ext4 19G 1.5G 16G 9% /
none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup
none tmpfs 5.0M 0 5.0M 0% /run/lock
none tmpfs 1001M 0 1001M 0% /run/shm
none tmpfs 100M 0 100M 0% /run/user
/dev/sda1 ext2 236M 40M 184M 18% /boot
Same is the case with Physical volumes, Volume groups and Logical volumes:
root@Ubuntu14:~# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root ubuntu14-vg -wi-ao--- 18.74g
swap_1 ubuntu14-vg -wi-ao--- 1020.00m
root@Ubuntu14:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m
root@Ubuntu14:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu14-vg 1 2 0 wz--n- 19.76g 20.00m
You'l have to do some manual effort here.
Resize the Physical volume:
root@Ubuntu14:~# pvresize /dev/sda5
Physical volume "/dev/sda5" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
Now check Physical Volume and Volume group status and see that it is properly showing the new size:
root@Ubuntu14:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 ubuntu14-vg lvm2 a-- 39.76g 20.02g
root@Ubuntu14:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu14-vg 1 2 0 wz--n- 39.76g 20.02g
Similarly, resize the logical volume:
root@Ubuntu14:~# lvextend -l +100%FREE /dev/ubuntu14-vg/root
Extending logical volume root to 38.76 GiB
Logical volume root successfully resized
Lastly, resize the filesystem:
root@Ubuntu14:~# resize2fs /dev/ubuntu14-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/ubuntu14-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu14-vg/root is now 10161152 blocks long.
Verify the disk status and see that LVM is now resized properly:
root@Ubuntu14:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 989M 4.0K 989M 1% /dev
tmpfs tmpfs 201M 716K 200M 1% /run
/dev/dm-0 ext4 39G 1.5G 35G 4% /
none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup
none tmpfs 5.0M 0 5.0M 0% /run/lock
none tmpfs 1001M 0 1001M 0% /run/shm
none tmpfs 100M 0 100M 0% /run/user
/dev/sda1 ext2 236M 40M 184M 18% /boot
That's all! You have successfully resized the LVM partition inside an extended partition.
Questions or suggestion? Leave a comment below.
Seasoned DevOps professional with 8+ years of real-world experience with tools like Docker, Kubernetes and more.