LVM2 refers to the userspace toolset that provide logical volume management facilities on Linux. It is reasonably backwards-compatible with the original LVM toolset.
Resizing volumes with the LVM2 toolset is easy. First you have to adjust your physical partition table using fdisk. Let’s say you have a 60GB physical disk and a 50GB physical partition on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# fdisk -l Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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 Disklabel type: dos Disk identifier: 0x60c4de5a Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 104857599 104855552 50G 8e Linux LVM Disk /dev/mapper/system-swap: 1 GiB, 1073741824 bytes, 2097152 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 /dev/mapper/system-root: 12 GiB, 12884901888 bytes, 25165824 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 /dev/mapper/system-home: 37 GiB, 39720058880 bytes, 77578240 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 |
You want to resize the partition to take the whole disk. To do that you delete the partition and create it again with the new geometry.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# fdisk /dev/sda Command (m for help): i Selected partition 1 Device: /dev/sda1 Boot: * Start: 2048 End: 104857599 Sectors: 104855552 Cylinders: 6527 Size: 50G Id: 8e Type: Linux LVM Start-C/H/S: 0/32/33 End-C/H/S: 1023/254/63 Attrs: 80 Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): Using default response p. Partition number (1-4, default 1): First sector (2048-125829119, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-125829119, default 125829119): Created a new partition 1 of type 'Linux' and of size 60 GiB. Partition #1 contains a LVM2_member signature. Do you want to remove the signature? [Y]es/[N]o: N Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): i Selected partition 1 Device: /dev/sda1 Start: 2048 End: 125829119 Sectors: 125827072 Cylinders: 7833 Size: 60G Id: 8e Type: Linux LVM Start-C/H/S: 0/32/33 End-C/H/S: 664/127/39 Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: 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). |
The highlighted rows show the commands issued to fdisk. After the partition table is rewritten reboot the system.
The new partition table should now look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# fdisk -l Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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 Disklabel type: dos Disk identifier: 0x60c4de5a Device Boot Start End Sectors Size Id Type /dev/sda1 2048 125829119 125827072 60G 8e Linux LVM Disk /dev/mapper/system-swap: 1 GiB, 1073741824 bytes, 2097152 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 /dev/mapper/system-root: 12 GiB, 12884901888 bytes, 25165824 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 /dev/mapper/system-home: 37 GiB, 39720058880 bytes, 77578240 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 |
Now that the new partition is setup you can assign the new space to a PV (physical volume). You can check the physical volumes with the pvs command.
1 2 3 |
# pvs PV VG Fmt Attr PSize PFree /dev/sda1 system lvm2 a-- 50.00g 4.00m |
In this case you have only one PV (physical volume) of size 50GB. Since you know your partition is 60GB you can very easily resize the PV to take all space with the pvresize command.
1 2 3 4 5 6 |
# pvresize /dev/sda1 Physical volume "/dev/sda1" changed 1 physical volume(s) resized / 0 physical volume(s) not resized # pvs PV VG Fmt Attr PSize PFree /dev/sda1 system lvm2 a-- 60.00g 10.00g |
As you can see now the PV is of size 60GB and there is 10GB of free space. You can now use the free space to resize an LV (logical volume) using the lvextend command.
1 2 3 4 |
# lvextend -r -l +100%FREE system/root Size of logical volume system/root changed from 12.00 GiB (3072 extents) to 22.00 GiB (5633 extents). Logical volume system/root successfully resized. Resize '/' of '1:23626514432' |
The -r option tells lvextend to resize the underlying filesystem (e.g. ext4 or btrfs) along with the LV. The -l option tells lvextend to set the LV size in units of logical extents. In this case the argument to -l is +100%FREE which means that 100% of the free space will be used. In other words all free space will be added to the system/root LV. After lvextend returns the file system should be resized and operational without system reboot.