In this note I am going to explain how to resize a partition in LVM, that is something that sometimes I need to do. In this scenario I add more space to a partition with a lvm, I will put here my steps.
Run fdisk -l
to list partitions.
The disk in which I have the lvm is in /dev/sda. So I enter with fdisk /dev/sda
* p command prints the disk info, same as running
* d command delete the last partition
* n command creates a new partition; e makes that an extended partition
* n to create the logical partition
* x to enter in expert mode and b to change the beginning of the partition. It is necessary to put the same as in the beginning of the process
* r to exit from expert mode
* t changes the type of partition. 8e is the lvm partition type
* w writes the changes to disk and exits fdisk
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x55306f06
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 67106815 66105346 31.5G 5 Extended
/dev/sda5 1001472 67106815 66105344 31.5G 8e Linux LVM
Command (m for help): d
Partition number (1,2,5, default 5): 2
Partition 2 has been deleted.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2):
First sector (999424-83886079, default 999424):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (999424-83886079, default 83886079):
Created a new partition 2 of type 'Extended' and of size 39.5 GiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1001472-83886079, default 1001472):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1001472-83886079, default 83886079):
Created a new partition 5 of type 'Linux' and of size 39.5 GiB.
Partition #5 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help): x
Expert command (m for help): b
Partition number (1,2,5, default 5): 5
New beginning of data (999425-83886079, default 999425): 1001472
Expert command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x55306f06
Device Boot Start End Sectors Id Type Start-C/H/S End-C/H/S Attrs
/dev/sda1 * 2048 999423 997376 83 Linux 4/4/1 1023/254/2 80
/dev/sda2 999424 83886079 82886656 5 Extended 935/167/1 642/129/2
/dev/sda5 1001472 83886079 82884608 83 Linux 939/171/1 642/129/2
Expert command (m for help): r
Command (m for help): t
Partition number (1,2,5, default 5):
Hex code or alias (type L to list all): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x55306f06
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 999424 83886079 82886656 39.5G 5 Extended
/dev/sda5 1001472 83886079 82884608 39.5G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks
root@fran:~# vgs
VG #PV #LV #SN Attr VSize VFree
vg 1 4 0 wz--n- <39.52g 8.00g
root@fran:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 vg lvm2 a-- <39.52g 8.00g
root@fran:~# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home vg -wi-ao---- 9.31g
root vg -wi-ao---- 11.96g
tmp vg -wi-ao---- 2.79g
var vg -wi-ao---- <7.45g
root@fran:~# lvdisplay
--- Lot of information about the patition ---
root@fran:~# lvextend -l +100%FREE /dev/vg/var
Size of logical volume vg/var changed from <7.45 GiB (1907 extents) to <15.45 GiB (3955 extents).
Logical volume vg/var successfully resized.
root@fran:~# resize2fs /dev/vg/var
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/vg/var is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg/var is now 4049920 (4k) blocks long.