preface

Logical Volume Manager (LVM), also translated as Logical Volume Manager, Logical sector Manager, Logical disk Manager, is a Logical Volume management function provided by the Linux core. It creates a logical layer on top of the disk partition to facilitate the system to manage the disk partition system.

Originally developed by IBM and implemented on AIX systems, it is also supported on OS/2 and HP-UX. In 1998 Heinz Mauelshagen wrote the first Linux version of the logical volume manager based on the logical volume manager on HP-UX.

Basic TERMS for LVM:

  • PV: a physical volume is at the lowest layer of the LVM system. It can be a hard disk or a device (such as RAID) that has the same function as a disk partition but has more management parameters related to LVM than basic physical storage media
  • VG: a volume group that is created on a PV and consists of one or more PVS. One or more LVM partitions (logical volumes) can be created on a VG. The functions are similar to those of physical disks in a non-LVM system
  • LV: A logical volume divided from a VG. After being created, the volume can be scaled up. File systems (such as /var and /home) can be created on the LV.
  • PE: Physical area. Each PV is divided into a base unit (also known as a PE). A PE with a unique number is the smallest storage unit that can be addressed by LVM

LVM can be used to manage disks in the system flexibly and conveniently, and target disks can be expanded quickly and sensitively according to demand, with almost no performance impact.

process

The method of creating LVM partition is actually very simple, mainly divided into the following steps:

1. Create disk partition part

The recommended tool for LVM partitioning is fdisk. This is not to say that Parted cannot be done, but fdisk is probably more convenient. The process is as follows:

# fdisk /dev/nvme0n1
Command (m for help): n # # new
Command action
   e   extended
   p   primary partition (1-4)
p # # primary partition
 
Partition number (1-4): 1 # # # partition
First cylinder (2048-543352123, default 2048):  The default is 1
Last cylinder, +cylinders or +size{K,M,G} (2048-543352123, default 543352123): Enter the default size
 
Command (m for help): t Change the toggle type
Selected partition 1
Hex code (type L to list codes): 8e LVM partition code 8E
Changed system type of partition 1 to 8e (Linux LVM)
Copy the code

2. Create a physical volume PV

After the partition is completed, you need to create a physical volume and pvcreate the partition directly.

# pvcreate /dev/nvme0n1p1
Copy the code

3. Create virtual volume VG

After creating a physical volume, create a virtual volume for the disk. Note that a virtual volume can consist of multiple physical volumes. Such as:

# vgcreate nvme /dev/nvme0n1p1 /dev/nvme1n1p1 /dev/nvme2n1p1
Copy the code

The purpose of this command is to create a virtual volume called nvMe, which consists of three pv partitions: /dev/nvme0n1p1, /dev/nvme1N1p1, and /dev/nvme2n1p1

4. Create logical volume LG

At this point, the creation of LVM is basically over, and the last step is to create a logical volume from the virtual volume. As follows:

# lvcreate -L 2.91T -n data nvme
Copy the code

Create a 2.91TB logical volume from nvMe as LVM partition and name it data. If you want to use all the capacity at once, you can also use this command:

# lvcreate -l -n data nvme
Copy the code

The /nvme/data file structure can be seen under the /dev/folder on Linux.

5. Mount and update the file

After creating the LVM partition, we need to hang the partition in a path to the system. Assume that the path you want to mount is /data.

# mkdir /data #
# mount /dev/nvme/nvme/data # mount /dev/nvme/nvme/data # mount /dev/nvme/nvme/data
Copy the code

Then run the blkid command to obtain the UUID of the new LVM partition and update it to /etc/fstab

scripting

The above LVM implementation process can be directly implemented by shell script:

#! /bin/bash
This example uses the nvMe disk /dev/nvme0n1 of the server as an example
OPTS="noatime,nobarrier,errors=remount-ro,nofail"   ## part of the fixed variable written to /etc/fstab
umount /data/ && parted /dev/nvme0n1 rm 1   First make sure the target disk has no residual partition information.
echo "n p 1 t 8e w " | fdisk /dev/nvme0n1  Create a partition using the fdisk tool
echo "y
" | pvcreate /dev/nvme0n1p1     Create a physical volume
echo "y
y
y
" | vgcreate nvme /dev/nvme0n1      Create a virtual volume
echo "y
"| lvcreate - 2.91 L T - n data nvme && MKFS. Corruption/dev/nvme/data && mount/dev/mapper/nvme - data/data /Create a logical volume, format it, and mount it to the destination path
sed -i '$d' /etc/fstab      ## update the UUID in /etc/fstab
#do
    if[$?-eq0];then
        UUID=$(blkid /dev/mapper/nvme-data | sed -e 's/.*\(UUID=.*\) TYPE.*/\1/')
        echo -e "${UUID}\t\t/data\t\text4\t\t${OPTS}\t\t0 0" >> /etc/fstab
    fi
    seq=$(($seq+ 1))#done
Copy the code