Location>code7788 >text

LVM Usage and Scaling Summary

Popularity:87 ℃/2024-11-12 00:21:27

Reprinted with attribution:

LVM (Logical Volume Manager) is a disk management tool for Linux systems. It provides a more flexible storage management mechanism that allows for easy disk expansion, reduction, snapshotting, and migration.

basic concept

  1. Physical Volume (PV): a physical disk or partition, such as a/dev/sda1
  2. Volume Group (VG): a collection of one or more physical volumes.
  3. Logical Volume (LV): A logical disk allocated from a volume group that can be formatted by the file system and used to store data.

Installing LVM

On ubuntu systems it can be installed with the following command

# Ubuntu/Debian  
sudo apt-get install lvm2  

Creating LVM

Step 1: Create a physical volume (PV)

Suppose there is a new disk/dev/sdb, you need to initialize it as a physical volume first:

sudo pvcreate /dev/sdb  

Application Example:

root@swan2:~# sudo pvcreate /dev/vdb
  Physical volume "/dev/vdb" successfully created.
root@sdwan2:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <96.95 GiB
  PE Size               4.00 MiB
  Total PE              24818
  Alloc PE / Size       12409 / 48.47 GiB
  Free  PE / Size       12409 / 48.47 GiB
  VG UUID               RCjkb6-7ngM-9nss-OWOL-eqMR-9MDp-JCyLjk

Step 2: Create a Volume Group (VG)

Create a file namedvg_datavolume group to which the newly created physical volume will be added:

sudo vgcreate vg_data /dev/sdb  

Step 3: Create a Logical Volume (LV)

Create a file namedlv_dataof logical volumes with a size of 10G:
sudo lvcreate -n lv_data -L 10G vg_data  

Step 4: Formatting the Logical Volume

Format the logical volume, for example, using the ext4 file system:
sudo mkfs.ext4 /dev/vg_data/lv_data  

Step 5: Mount the logical volume

Create a mount point and then mount the logical volume to that mount point:
mkdir /mnt/data  
sudo mount /dev/vg_data/lv_data /mnt/data  

Expanded LVM

Suppose we need to convert the logical volumelv_dataTo expand to 20G, you can follow the steps below:

Step 1: Add a physics volume

Assuming that in the physics paper/dev/sdbAdditional space has been added (e.g., a second physical volume has been added)./dev/sdc), the new physical volume first needs to be initialized:

sudo pvcreate /dev/sdc  

Then, add it to the volume group:

sudo vgextend vg_data /dev/sdc  

Application Example:

root@sdwan2:~# sudo vgextend ubuntu-vg /dev/vdb
  Volume group "ubuntu-vg" successfully extended
root@swan2:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1.09 TiB
  PE Size               4.00 MiB
  Total PE              286961
  Alloc PE / Size       12409 / 48.47 GiB
  Free  PE / Size       274552 / <1.05 TiB
  VG UUID               RCjkb6-7ngM-9nss-OWOL-eqMR-9MDp-JCyLjk
   

Step 2: Extend the logical volume

Use the following command to set the logical volumelv_dataExpanded to 20G:
sudo lvextend -L 20G /dev/vg_data/lv_data  

Or, if you want to use all available space:

sudo lvextend -l +100%FREE /dev/vg_data/lv_data  

Application Example:

root@swan2:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 48.47 GiB (12409 extents) to 1.09 TiB (286961 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@sdwan2:~#

Step 3: Extend the file system

After expanding the logical volume, you need to expand the file system to utilize the additional space:
sudo resize2fs /dev/vg_data/lv_data  

Application Example:

root@swan2:~# sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 141
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 293848064 (4k) blocks long.

root@swan2:~#

Viewing information about LVM

You can use the following command to view LVM information:

  • View all physical volumes:
    sudo pvdisplay  

  • View all volume groups:
    sudo vgdisplay  

  • View all logical volumes:
    sudo lvdisplay  

  • View detailed LVM status:
    lvs