LVM – create, resize, remove, scan, reduce, etc.

LVM enables flexible distribution of hard disk space over several file systems. It was developed because the need to change the segmentation of hard disk space might arise only after the initial partitioning has already been done during installation. Because it is difficult to modify partitions on a running system, LVM provides a virtual pool (volume group or VG) of memory space from which logical volumes (LVs) can be created as needed. The operating system accesses these LVs instead of the physical partitions.

Volume groups can span more than one disk, so that several disks or parts of them can constitute one single VG. In this way, LVM provides a kind of abstraction from the physical disk space that allows its segmentation to be changed in a much easier and safer way than through physical repartitioning.
Figure 1, compares physical partitioning (left) with LVM segmentation (right). On the left side, one single disk has been divided into three physical partitions (PART), each with a mount point (MP) assigned so that the operating system can access them. On the right side, two disks have been divided into two and three physical partitions each. Two LVM volume groups (VG 1 and VG 2) have been defined. VG 1 contains two partitions from DISK 1 and one from DISK 2. VG 2 contains the remaining two partitions from DISK 2.
Figure 1 Physical Partitioning versus LVM (it will be added later)

In LVM, the physical disk partitions that are incorporated in a volume group are called physical volumes (PVs). Within the volume groups in Figure 1, four logical volumes (LV 1 through LV 4) have been defined, which can be used by the operating system via the associated mount points. The border between different logical volumes need not be aligned with any partition border. See the border between LV 1 and LV 2 in this example.

LVM features:
Several hard disks or partitions can be combined in a large logical volume. Provided the configuration is suitable, an LV (such as /usr) can be enlarged when the free space is exhausted. Using LVM, it is possible to add hard disks or LVs in a running system. However, this requires hot‐swappable hardware that is capable of such actions. It is possible to activate a striping mode that distributes the data stream of a logical volume over several physical volumes. If these physical volumes reside on different disks, this can improve the reading and writing performance just like RAID 0. The snapshot feature enables consistent backups (especially for servers) in the running system. With these features, using LVM already makes sense for heavily used home PCs or small servers. If you have a growing data stock, as in the case of databases, music archives, or user directories, LVM is especially useful. It allows file systems that are larger than the physical hard disk. Another advantage of LVM is that up to 256 LVs can be added. However, keep in mind that working with LVM is different from working with conventional partitions. Starting from kernel version 2.6, LVM version 2 is available, which is downward‐compatible with the previous LVM and enables the continued management of old volume groups. When creating new volume groups, decide whether to use the new format or the downward‐compatible version. LVM 2 does not require any kernel patches. It makes use of the device mapper integrated in kernel 2.6. This kernel only supports LVM version 2. Therefore, when talking about LVM, this section always refers to LVM version 2.
The lvresize, lvextend, and lvreduce commands are used to resize logical volumes. See the man pages for each of these commands for syntax and options information. To extend an LV there must be enough unallocated space available on the VG. LVs can be extended or shrunk while they are being used, but this may not be true for a file system on them. Extending or shrinking the LV does not automatically modify the size of file systems in the volume. You must use a different command to grow the file system afterwards.

Yes, online resizing isn’t possible just with xfs but starting with Linux kernel 2.6.10 you can do it with ext3 as well. With e2fsprogs >=1.39-1 new filesystems are created with directory indexing and on-line resizing enabled by default (see /etc/mke2fs.conf).

A short howto online-resizing running on top of LVM works:
root@deb:~#cfdisk /dev/sdb                                                              # create a partition with type 8e (lvm)
root@deb:~#pvcreate /dev/sdb1                                                      # create a physical volume
root@deb:~#vgcreate resizeme /dev/sdb1                              # create volume group
root@deb:~#lvcreate -n disc -L100 resizeme                   # create a logical volume
root@deb:~#mkfs.ext3 /dev/resizeme/disc                           # now create a new filesystem
root@deb:~#mount /dev/resizeme/disc /mnt/test           # mount the new fs for demonstrating online resizing
root@deb:~#df -h                                                                                        # check the size of the partition
root@deb:~#lvextend -L+100M /dev/resizeme/disc        # let’s extend the logical volume
root@deb:~#resize2fs /dev/resizeme/disc                           # and finally resize the filesystem
root@deb:~#df -h                                                                                        # now we have a filesystem with 200MB size
root@deb:~#pvdisplay                                                                             # display attributies of a psyhical volume.


--- Physical volume ---
PV Name               /dev/sdb1
VG Name               resizeme
PV Size               2.10 GiB / not usable 1.29 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              537
Free PE               537
Allocated PE          0
PV UUID               VFhFNm-nlLl-OTA1-auH3-Yd1l-Mapt-OtWIHr

How to add another disc to LVM:

root@deb:~#fdisk -l
Disk /dev/sdf doesn't contain a valid partition table

Disk /dev/dm-0: 104 MB, 104857600 bytes
255 heads, 63 sectors/track, 12 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

root@deb:~#cfdisk /dev/sdf                                                                       # create a partition with type 8e (lvm)
root@deb:~#mkfs.ext3 /dev/sdf1                                                            # create a new filesystem (ext3)
root@deb:~# pvcreate /dev/sdf1
root@deb:~# vgextend resizeme /dev/sdf1                                    # add psyhical volumes to a volume group
No physical volume label read from /dev/sdf1
Physical volume "/dev/sdf1" successfully created
Volume group "resizeme" successfully extended

root@deb:~# pvscan                                                                        #scan all disks for psyhical volumes
PV /dev/sdd1 VG home lvm2 [2.30 GiB / 2.20 GiB free]
PV /dev/sde1 VG home lvm2 [2.39 GiB / 2.39 GiB free]
PV /dev/sda3 VG resizeme lvm2 [3.00 GiB / 1.97 GiB free]
PV /dev/sdb1 VG resizeme lvm2 [2.10 GiB / 2.10 GiB free]
PV /dev/sdc1 VG resizeme lvm2 [2.20 GiB / 2.20 GiB free]
PV /dev/sdf1 VG resizeme lvm2 [2.50 GiB / 2.50 GiB free]
Total: 6 [14.48 GiB] / in use: 6 [14.48 GiB] / in no VG: 0 [0 ]

Reduce size of LVM:
root@deb:~# umount /mnt/test                                                     # umount the mounted partition
root@deb:~# resize2fs /dev/resizeme/disc 524288  # resize the filesystem
root@deb:~# lvreduce -L-1G /dev/resizeme/disc       # reduce the logical volume
root@deb:~# mount /mnt/test                                                       # mount the partition

Remove the psyhical volume:

root@deb:~# pvremove -ff /dev/sdf1
                               #remove a psyhical volume. (-ff force)
Really WIPE LABELS from physical volume "/dev/sdf1" of volume group "resizeme" [y/n]? y
WARNING: Wiping physical volume label from /dev/sdf1 of volume group "resizeme"
Labels on physical volume "/dev/sdf1" successfully wiped

root@deb:~# pvscan                                                                           # scan all disks for psyhical volumes.
Couldn't find device with uuid s0Ssad-TEj4-eSMy-mFtT-RlkG-EB1q-sgRcCs.
PV /dev/sdd1 VG home lvm2 [2.30 GiB / 2.20 GiB free]
PV /dev/sde1 VG home lvm2 [2.39 GiB / 2.39 GiB free]
PV /dev/sda3 VG resizeme lvm2 [3.00 GiB / 1.97 GiB free]
PV /dev/sdb1 VG resizeme lvm2 [2.10 GiB / 2.10 GiB free]
PV /dev/sdc1 VG resizeme lvm2 [2.20 GiB / 2.20 GiB free]
PV unknown device VG resizeme lvm2 [2.50 GiB / 2.50 GiB free]
Total: 6 [14.48 GiB] / in use: 6 [14.48 GiB] / in no VG: 0 [0 ]

root@deb:~# vgreduce resizeme --removemissing             #reduce a volume group and remove missing volumes.
Couldn't find device with uuid s0Ssad-TEj4-eSMy-mFtT-RlkG-EB1q-sgRcCs.
Wrote out consistent volume group resizeme

root@deb:~# pvscan                                                                                      # scan all disks for psyhical volumes
PV /dev/sdd1 VG home lvm2 [2.30 GiB / 2.20 GiB free]
PV /dev/sde1 VG home lvm2 [2.39 GiB / 2.39 GiB free]
PV /dev/sda3 VG resizeme lvm2 [3.00 GiB / 1.97 GiB free]
PV /dev/sdb1 VG resizeme lvm2 [2.10 GiB / 2.10 GiB free]
PV /dev/sdc1 VG resizeme lvm2 [2.20 GiB / 2.20 GiB free]
Total: 5 [11.98 GiB] / in use: 5 [11.98 GiB] / in no VG: 0 [0 ]

Leave a Reply

Your email address will not be published. Required fields are marked *

*