The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 LVM Logical Volumes

1.0 LVM Logical Volumes
        LVM Principles
        pvcreate : Creating Physical Volumes
        vgcreate : Creating Volume Groups
        lvcreate : Creating Logical Volumes
           Linear Volume Mode
           Stripe Volume Mode
           Mirror Volume Mode
           Snapshot Volume Mode
        LVM Migration/Backup

ENG⇒中ENG⇒中
 
 1.0 LVM Logical Volumes

Even experienced experts encounter the challenge of planning partitions during system installation. Assessing the required space for "/home" to accommodate numerous accounts years in the future is a complex task. The process of resizing hard drive partitions using tools like fdisk or parted is a daunting endeavor for system administrators. Although there are commercial software options available for dynamically adjusting partition sizes without data loss, these operations necessitate halting the running of Linux. This poses a considerable challenge for servers that must maintain uninterrupted operation 24/7..

Developed in 1989, the Logical Volume Manager (LVM) offers an effective solution for changing the size of filesystems while a Linux system continues to run, addressing the limitations of traditional fixed-size partitions.

Here are some of the benefits of LVM:

  1. Flexibility and Dynamic Adjustment: LVM allows you to create virtual logical volumes (LVs) that can span multiple physical disks or disk partitions. This provides greater flexibility, enabling you to adjust the size of volumes on-the-fly without shutting down the system or affecting running applications.

  2. Data Migration and Mobility: LVM enables data migration without disrupting services. You can move data between physical disks or from one logical volume to another without interrupting operations. This is useful for upgrading disks or reorganizing storage configurations.

  3. Snapshots and Backups: LVM offers snapshot functionality, allowing you to create point-in-time snapshots of disk volumes without interrupting system operations. This is beneficial for backups and data recovery, as you can perform operations on the snapshot while keeping the original data intact.

  4. Fault Tolerance and Redundancy: LVM supports mirroring mode, allowing you to create redundant copies across multiple physical disks, enhancing data fault tolerance. In case of a disk failure, data remains accessible.

  5. Efficient Capacity Management: LVM abstracts the capacity of physical disks, enabling more efficient management and allocation of available space.

  6. Real-time Expansion: You can instantly expand the size of logical volumes without the need for disk re-partitioning or service interruption.

  7. Multiple File Systems: LVM enables the creation of multiple logical volumes on the same set of physical disks, each supporting different file systems to meet varied requirements.

In summary, LVM provides a more flexible, efficient, and manageable disk storage solution, particularly for scenarios requiring dynamic capac

 





LVM Principles
LVM enables dynamic partition resizing through the use of "Storage Virtualization" technology. Furthermore, LVM goes a step further by combining multiple partitions or hard drives into a larger virtual entity known as a "Volume Group" (VG), as illustrated in the diagram below.

In the example VG shown in the diagram, it resembles a virtual large hard drive created from physical hard drives ("Physical Volumes") such as "/dev/sda2", "/dev/sda3", "/dev/sdb2", and an entire hard drive "/dev/sdc". This Volume Group (VG) can have partitions or hard drives added or removed to achieve scalability.

The virtual large hard drive, VG, is then subdivided into virtual partitions known as "Logical Volumes" (LVs). Since LVs are also virtual partitions, they can also be resized.



lvm

Understanding the principles of LVM, let's delve into some terminology commonly used in LVM:
Having grasped the principles, the implementation becomes straightforward, following these steps:
  1. Create PV:
    Use the pvcreate command to transform an entire hard drive or partition into a PV. For partitions, set the ID to "LVM" before converting to a PV.
  2. Create VG:
    Employ vgcreate to combine one or more PVs into a VG.
  3. Create LV:
    Finally, use lvcreate to carve out LVs from the VG.
  4. Mounting LVM:
    To ensure LVM is mounted on startup, edit "/etc/fstab" or consider relocating directories prone to size changes, such as "/home", to LVM.
Following these operations, the resulting Logical Volumes (LVs), which function like virtual partitions, can be formatted using mkfs and mounted using mount in a manner akin to traditional hard drives.

pvcreate : Creating Physical Volumes
A Physical Volume (PV) is easily created by utilizing the pvcreate command, whether by transforming an entire hard drive or a partition. It's remarkably straightforward! However, if dealing with partitions, it's advisable to set the ID to "lvm". It's important to note that converting a partition to a PV may result in the evaporation of the existing filesystem and data. If actual disks are unavailable for practice, you can utilize virtual storage devices.

For example, let's consider partitioning a new 640GB hard drive, "/dev/sdc", into four partitions and subsequently converting partition #1 through partition #3 into PVs.

Example:
# parted /dev/sdc print ←Display partiton info
Model: ST964032 2AS (scsi)
Disk /dev/sdc: 640GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size   File system  Name  Flags
1      17.4kB  160GB  160GB               p1   
2      160GB   320GB  160GB               p2        
3      320GB   480GB  160GB               p3        
4      480GB   640GB  160GB               p4     
# parted /dev/sdc set 1 lvm on Set partition #1 ID to "lvm" (use "8e" with fdisk)
# parted /dev/sdc set 2 lvm on ←Set partition #2 ID to "lvm"
# parted /dev/sdc set 3 lvm on ←Set partition #3 ID to "lvm"
# pvcreate /dev/sdc1 /dev/sdc2 /dev/sdc3 ←Convert partition #1~3 into PVs
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc2" successfully created
Physical volume "/dev/sdc3" successfully created

For converting an entire hard drive to PV:
# parted /dev/sdb mklabel loop ←Clear partitions on "/dev/sdb"
# pvcreate /dev/sdb ←Convert the entire "/dev/sdb" to PV

Creating a PV is indeed that simple. You can also use curly braces "{ }" to list differences, for example, pvcreate /dev/sdc{1,2,3} is equivalent to pvcreate /dev/sdc1 /dev/sdc2 /dev/sdc3. Similarly, pvcreate /dev/sd{a,b} is equivalent to pvcreate /dev/sda /dev/sdb.

In the above examples, the parted parameter "mklabel loop" erases partitions and marks the storage device as non-partitionable. This function clears the partition table of the storage device and prevents further partitioning, but allows direct conversion to a PV. In cases where additional physical disks are unavailable but you wish to experiment with LVM, you can create a loop-device using dd and losetup .

Example:
# dd if=/dev/zero of=disk-image bs=1 count=0 seek=100M ←Create a 100MB image file
# losetup -fv disk-image ←Map the image file to a loop device "/dev/loopN"
loop device is /dev/loop0
# parted /dev/loop0 mklabel loop ← Mark "/dev/loop0" as non-partitionable
# pvcreate /dev/loop0 ← Convert "/dev/loop0" to PV
Physical volume "dev/loop0" successfully created

Other PV-related commands include:

^ back on top ^

vgcreate : Creating Volume Groups
vgcreate 負責將 一個以上的 PV 組成 VG,用法和 pvcreate 類似但要指定一個名稱給 VG,如下例把 PV〝/dev/sdc1〞和〝/dev/sdc3〞所組成 VG 取名為〝 MyVG〞。

The vgcreate command is responsible for combining one or more PVs into a VG. Its usage is similar to pvcreate, but you need to specify a name for the VG. For instance, the following example combines PVs "/dev/sdc1" and "/dev/sdc3" into a VG named "MyVG."

Example:
# vgcreate MyVG /dev/sdc1 /dev/sdc3 ←Creates VG "MyVG" from PVs "/dev/sdc1" and "/dev/sdc3"
  Volume group "VG1" using metadata type lm2

Following these operations, a VG named "MyVG" has been created. You can envision "MyVG" as a large hard drive formed by PVs "/dev/sdc1" and "/dev/sdc3". Next, you can use vgcreate to divide it into LVs.

When creating a VG with vgcreate, the system determines the size of PEs (Physical Extents) based on the VG's size. This size typically ranges from 4MB to 32MB. If you want to specify the size of PEs, you can use the "-s #[KMGT]" option. For example, vgcreate VolGroup -s 8M /dev/sda creates a VG with a PE size of 8MB. However, the PE size must be in the form of 2N (N being a positive integer greater than 10, e.g., 212,=4K). Therefore, the minimum PE size is 1KiB, and determining PE size is analogous to determining block size in ext2/ext3.

Other VG-related commands include:

^ back on top ^


  lvcreate : Creating Logical Volumes
lvcreate is akin to the traditional partitioning tools like fdisk or parted , but it is used for partitioning within a VG. The option "-L" or "--size" specifies the size of the LV (default is in MB). For instance, lvcreate -L 10G MyVG creates a 10GB LV within VG "MyVG." The size of the LV must be a multiple of the PE size, and if it's not evenly divisible, it will be adjusted to the nearest compatible size.

You can also use the option "-l" (lowercase "L") to specify the number of PEs. For example, lvcreate -l 10000 MyVG creates an LV with a size of PE-size × 1000. The total number of PEs and PE sizecan be obtained from vgdisplay. To simplify, you can also use percentages, such as lvcreate -l 40%VG MyVolGrop to allocate 40% of VG "MyVolGroup" for the LV.

By default, LV names are in the form "lvol#," where "#" is a number. If you want to specify a custom LV name, use the "-n" option. For example, lvcreate -L 1000 -n MyLV1 MyVG creates an LV named "MyLV1" with a size of 1GB within VG "MyVG."

Once an LV is created, a corresponding storage device is generated in the "/dev" directory, such as "VG_NAME/LV_NAME" (you can use lvscan -v to see this information). Subsequent operations can treat these storage device names as physical storage devices. You can format and mount them. The actual device file name for an LV is similar to the mapping used by kpartx and is located in "/dev/mapper/" with the format "VG_NAME-LV_NAME."

Here's an example of creating an LV, formatting it, and mounting it:

Example:
# lvcreate -L 2G -n MyLV1 MyVG ←Create an LV named "MyLV1" of 2GB within VG "MyVG"
Logical volume "MyLV1" created
# lvscan -v ← Scan for LV device names
Finding all logical volumes
ACTIVE '/dev/MyVG/MyLV1' [2.00 GB] inherit
# mkfs -j /dev/MyVG/MyLV1 ←Format the LV
# mount /dev/MyVG/MyLV1 /mnt ←Mount the LV
# df -h ← Check the mounted filesystem
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/MyVG-MyLV1  2.0G  68M  1.9G  4%   /mnt

The greatest advantage of LVM lies in its ability to flexibly adjust the size of the filesystem without disrupting data integrity. It enables seamless capacity expansion, even during online operations, allowing for non-disruptive adjustments without requiring system downtime. The steps involved are as follows:


Increasing LV Size:
  1. To increase the size of an LV, you can use the lvextend command, which is similar to lvcreate. You can use the "-L" or "-l" options to specify the new LV size (refer to the "lvextend" command example).
  2. rezise2fs (只支援 ext2/ext3/ext4 filesystem) lvextend 增加 LV 容量完成後記得後 rezise2fs 更改 LV 上的 filesystem 大小 。(xfs filesystem 相對應的指令為 xfs_growfs,例: xfs_growfs /dev/myVG/myLV)
Shrinking LV Size:
Shrinking an LV involves temporarily unmounting the filesystem and requires some additional steps to ensure data integrity.

  1. Unmount the filesystem on the LV.
  2. Use e2fsck to check the filesystem on the LV.。
  3. Use resize2fs to adjust the filesystem size on the LV.
  4. Use lvreduce to shrink the LV (note that shrinking may not be supported for XFS filesystems).
  5. Remount the filesystem.。

The following example continues from the previous one and demonstrates the process of increasing and decreasing the filesystem capacity of a mounted LV.

Example (Continuation from Previous Example):
# cp -a /etc/*.conf /mnt ←Intentionally store some files to observe if they are affected during the size adjustment
# lvextend -L 3G /dev/MyVG/MyLV1 ←Increase the LV from its original size of 2G to 3G
# df -h ←Verify if the filesystem has grown
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/MyVG-MyLV1  2.0G  68M  1.9G  4%   /mnt ←Is the size still the same? Although the LV has grown, the filesystem hasn't been adjusted yet
# rezise2fs /dev/MyVG/MyLV1←Adjust the filesystem size
# df -h ←Verify the filesystem size again
/dev/mapper/MyVG-MyLV1  3.0G  68M  2.8G  3%   /mnt ←The filesystem has increased in size! (Now 3G)
# ls /mnt ←heck if the files in the filesystem have been affected

The following example continues from the previous one and demonstrates the process of increasing and decreasing the filesystem capacity of a mounted LV.

Example (Continuation from Previous Example):
# umount /mnt ←Unmount the filesystem first
# e2fsck -f /dev/MyVG/MyLV1 ←Check the integrity of the LV device-mapper
# resize2fs /dev/MyVG/MyLV1 1G ← Shrink the filesystem on the LV to 1G
# lvreduce -L 1G /dev/MyVG/MyLV1 ←Shrink the LV to 1G
   WARNING: Reducing active logical volume to 1.00 GB
   THIS MAY DESTORY YOUR DATA(filesystem etc.)
Do you really want to reduce MyLV1? [y/n]: y ← A warning about potential data loss will be displayed (press <Y> to proceed)
# mount /dev/MyVG/MyLV1 /mnt ← Remount the LV
# ls /mnt ←Check if the files in the filesystem have been affected

Please note that when reducing an LV's size, it's essential to avoid going below the amount of used space to prevent data loss. It's recommended to perform a backup before attempting to shrink an LV to mitigate any potential risks.


^ back on top ^

Other commands related to Logical Volumes (LV) in LVM are as follows: Default LV mode is Linear volume. However, LVs can also operate in Stripe volume and Mirror volume, modes, with the additional functionality of Snapshot volume. Each of these modes serves a different purpose, as explained below:

LVM Migration/Backup
If you need to move a disk with LVM to another host or want to safeguard against accidental data loss during maintenance, you can use vgexport to export the Volume Group (VG) before performing the transfer. Once the disk is moved to the other host, you can use vgimport to import the VG. Before exporting, make sure to unmount any mounted file systems and deactivate the VG using vgchange. When importing, activate the VG and mount the file systems again.

Here is an example of exporting an LVM from the current host:
# umount /mnt ←Unmount the filesystem
# vgchange -an my_vg ←Deactivate the VG (make it inactive)
# vgexport my_vg ←Export the VG
Volume group "my_vg" successfully exported

After moving the disk to another host, you can import the LVM as shown below:
# vgimport vg_u01 Import the VG
Volume group "my_vg" successfully imported
# vgchange -ay my_vg ←Activate the VG
# mount /dev/my_vg/lv_0 /mnt←Mount the filesystem

Additionally, if you are concerned about potential configuration damage in LVM (such as VG composition and LV sizes), you can use vgcfgbackup to create a backup and vgcfgrestore to restore it. The "-f" option is used to specify the filename.

Example:
# vgcfgrestor vg01 -f myvg_backup ←Backup the metadata of VG "vg01" to the file "myvg_backup"
Volume group "vg01" successfully backed up.
# vgcfgrestore vg01 -f myvg_backup ←Restore the metadata of VG "vg01"
Restored volume group vg01


^ back on top ^




[Note 1.0] You can use dmsetup table to observe which PV serves as the mirror-log or mirror-leg.

[Note 1.0A] The snapshot functionality in Fedora 8 seems to have a bug and is unable to create a snap-volume.。