Expanding a LVM disk (without changing its structure)¶

Note

The logical volume manager (LVM) is a subsystem of the Linux and OS/2 operating systems that allows you to use different areas of the same hard disk and/or areas of different hard disks as a single logical volume. It is implemented with the aid of the device mapper subsystem. LVM adds a layer of abstraction between the physical/logical disks (conventional partitions that are managed by fdisk and similar programs) and the file system. This is achieved by a means of breaking down the original partitions into blocks, using either individual partitions or block structures (physical volume (pv)) and combining them into a single virtual volume, or more precisely a volume group (vg), which is further divided up into logical volumes (lv). For the file system, the logical volume appears as an ordinary block structure, although the individual pv of the volume may be situated on a number of physical structures (and even the pv itself may be distributed similarly to RAID).

Source data

 Operating system Linux (Ubuntu 16.04.6 LTS) File system ext4 Method of allocation of hard disk space LVM Initial size of volume 5ГБ Size of volume after expansion 10ГБ

Using the fdisk command it can be seen that the size of the disk from the side of the operating system (/dev/vda) is 10 GB, but the size of the partition from the LVM (/dev/vda1) is 5 GB

fdisk -l /dev/vda

Disk /dev/vda: 10 GiB, 10737418240 bytes, 20971520 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: 0x71473ce9

Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 10483711 10481664   5G 8e Linux LVM


The next series of operations will result in the root partition occupying all available disk space.

1. Look at the current state of the root partition: before the change, its size is 4.8GB

df -h /

Filesystem                      Size   Used  Avail Use% Mounted on
/dev/mapper/vg0-rootfs          4.8G  1.8G  2.8G  40% /

1. Expand the vda1 partition to 10 GB using Parted software
• ‘Open’ the disk using the ‘Parted’ command
parted /dev/vda

• Examine the state of the disk overall and the partitions on it
(parted) print

Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
1      1049kB  5368MB  5367MB  primary               boot, lvm

• Change the size of partition no. 1 (current size 5367 MB) to the maximum possible. Take the number for this from the description of the disk in the previous point (10.7GB)
(parted) resizepart 1

End?  [5368MB]? 10.7GB

• Check that partition vda1 now occupies all the available space (10 GB)
(parted) print

Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
1      1049kB  10.7GB  10.7GB  primary               boot, lvm

• Exit the software
(parted) quit

1. Expand the LVM that contains the root partition
• Expand the physical volume
pvresize /dev/vda1

Physical volume "/dev/vda1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

• Re-scan the logical partitions
lvscan

ACTIVE            '/dev/vg0/rootfs' [5.00 GiB] inherit

• Expand the logical volume to the maximum available size. Take the name of the volume from the previous point
lvextend /dev/vg0/rootfs -l +100%FREE

Size of logical volume vg0/rootfs changed from 5.00 GiB (1279 extents) to 9.96 GiB (2550 extents).
Logical volume rootfs successfully resized.


4. Expand the file system to the maximum available size (take the name of the volume from point no. 1)

resize2fs /dev/mapper/vg0-rootfs

resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/vg0-rootfs is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg0-rootfs is now 2611200 (4k) blocks long.


5. Check that the changes were made successfully. To do this, look at the current size of the root partition. Now it is equal to 9.7 GB

df -h /

Filesystem                      Size   Used  Avail Use% Mounted on
/dev/mapper/vg0-rootfs          9.7G  1.9G  7.5G  20% /