Managing Physical Volumes (PV), Logical Volumes (LV), and Volume Groups (VG) in Linux

padluo

Reading time required: 4 minutes

Quick read only takes 2 minutes

Related Concepts

Let’s first look at several concepts of LVM (Logical Volume Manager):

In LVM (Logical Volume Manager), the relationships between various components can be summarized as follows:

  • Physical Volume (PV): This is an actual disk partition or an entire disk that, after initialization, is used by LVM to provide raw storage space.

  • Volume Group (VG): This combines one or more physical volumes into a unified storage pool. The space in this storage pool can be flexibly allocated later.

  • Logical Volume (LV): This is a virtual partition allocated from a volume group, used just like traditional partitions. Logical volumes can be used to create file systems, mounted for use, and support advanced features such as dynamic resizing and snapshots.

  • LVM: This is the software layer that manages all the above components, providing unified management of physical volumes, volume groups, and logical volumes, enabling flexible allocation and expansion of storage space.

Managing Physical Volumes (PV), Logical Volumes (LV), and Volume Groups (VG) in Linux

The requirement is as follows: merge the spaces of nvme0n1 and nvme1n1 disks and mount them to the /data directory.

Configuring and Managing Physical Volumes (PV), Logical Volumes (LV), and Volume Groups (VG)

  1. Initialize Physical Volumes: Use pvcreate to initialize the two disks as physical volumes for LVM (assuming there is no important data on the disks):

    sudo pvcreate /dev/nvme0n1 /dev/nvme1n1
    
  2. Create a Volume Group: Add the two physical volumes to the same volume group (for example, named data_vg):

    sudo vgcreate data_vg /dev/nvme0n1 /dev/nvme1n1
    
  3. Create a Logical Volume: Create a logical volume in the volume group (for example, named data_lv), using all remaining space in the volume group:

    sudo lvcreate -l 100%FREE -n data_lv data_vg
    
  4. Format the Logical Volume: Format the newly created logical volume with a file system (for example, ext4 or xfs):

    sudo mkfs.ext4 /dev/data_vg/data_lv
    

    or:

    sudo mkfs.xfs /dev/data_vg/data_lv
    
  5. Create a Mount Point and Mount: Create a new mount point /data and mount the logical volume:

    sudo mkdir /data
    sudo mount /dev/data_vg/data_lv /data
    
  6. Set Up Automatic Mounting at Boot: Edit /etc/fstab and add the following line (it is recommended to use UUID for mounting, which can be queried using blkid):

    /dev/data_vg/data_lv   /data   xfs    defaults    0 0
    # UUID="xxxxxxxx"   /data   xfs    defaults    0 0
    

    If using the ext4 file system, change xfs to ext4.

  7. View and Verify:

    # View Physical Volumes
    sudo pvs
    sudo pvdisplay
    # View Volume Groups
    sudo vgs
    sudo vgdisplay
    # View Logical Volumes
    sudo lvs
    sudo lvdisplay
    

WeChat public account “padluo” shares the self-cultivation of data scientists. Since we have met, let’s grow together. Follow the public account “Lao Luo Talks AI” and reply with “article” to obtain the complete collection of articles from “Lao Luo Talks AI”.

Leave a Comment