Linux creates logical volumes and expands them (super detailed)

Table of Contents

?edit

1. Concept analysis

1. LV logical volume

2. PV physical volume

3. VG volume group

2. Preparation before expansion

3. Create a logical volume and expand it

1. Open the virtual machine

2. Enter the root user

3. Check the newly added hard drive

4. Create a primary partition

5. Create a physical volume

6. Pack into a volume group

7. Create a logical volume

8. Format logical volume

9. Mount the logical volume – auto-start mounting at boot (permanent mounting)

10. Logical volume expansion


1. Concept Analysis

1. LV logical volume

LV (Logical Volume) is a logical volume in Linux systems. It is created on top of a physical volume (PV) and a volume group (VG) and can dynamically adjust the size and migrate data. Typically, LV is formatted as a file system to provide a high level of data management and access. LV provides better flexibility and availability, allowing you to increase or decrease storage space as needed, while providing data redundancy and recovery capabilities. In Linux systems, LVM (Logical Volume Manager) provides management and configuration tools for LV.

2. PV physical volume

PV (Physical Volume) is a concept used in LVM (Logical Volume Manager), which represents a hard disk or partition. In LVM, multiple PV physical volumes can form a VG (Volume Group) volume group, and a VG volume group can be divided into multiple LV (Logical Volume) logical volumes. PV physical volumes are usually created using the pvcreate command, and can then be added to the VG volume group using the vgextend command.

3. VG Volume Group

VG is the abbreviation of Volume Group in Linux. VG is the basis of logical volumes (Logical Volumes), which combines one or more physical volumes (Physical Volumes) to form a single volume group. VG provides a flexible way to manage storage space on one or more physical hard drives. All physical volumes in a VG must belong to the same system and must use the same block size. VG can be divided into multiple logical volumes (Logical Volumes), and each logical volume can be formatted as a file system or used for other purposes, such as MySQL data directories.

2. Preparation before capacity expansion

What the author does here is to first create a logical volume, and then expand the logical volume. Here, I first create a 5G logical volume, and then expand the 5G logical volume to 10G, because the original hard disk has been completely Finished, so here I chose to add the hard drive. In order to make it easier to see the details, the author did not choose to add a 10G hard drive directly. I chose to add two 5G hard drives for operation and expansion

Add a hard drive

Select SCSI

Create new virtual disk

Choose size 5G, you can change it according to your needs

After this, we will get a 5G hard drive. If we repeat the operation again, we can get two such hard drives. The final result is as shown below

3. Create a logical volume and expand it

1. Open the virtual machine

There should be nothing more to say here

2. Enter the root user

su root

3. View the newly added hard disk

lsblk

We can See the two 5G hard drives we just added

4. Create primary partition

Before creating an LVM logical volume we need to create a primary partition

Use fdisk /dev/sdb to create the primary partition. Note: I have two newly added hard disks here, so this operation needs to be performed twice, but the hard disk names are different. Enter n, p, 1 in sequence, then press Enter, enter w and save.

Note that the sdb command below needs to be consistent with the name of the empty hard disk we saw above. First create a primary partition for one of them, and then create the other one. The steps are the same.

fdisk /dev/sdb

Enter n, p, 1 in sequence, then press Enter twice to select the default size.

Then enter w to save

Execute the same method on sdc. We need to create primary partitions on both disks.

5. Create physical volume

The following statement is the command to create a physical volume. Pay attention to the process just now. We chose 1 as the number, so after sdb becomes the primary partition, it is sdb1, and sdc becomes sdc1. If you write other numbers, it will be displayed. others.

If you are still not sure what you are, you can run lsblk to check

lsblk

This is the main partition we just created

 pvcreate /dev/sdb1 /dev/sdc1

Our physical volume has been created successfully.

6. Pack into a volume group

  • Format: vgcreate+volume group name+device 1+device 2+device. . .

The name of the volume group was randomly chosen, and the author just randomly typed in a few letters.

vgcreate ysh /dev/sdb1 /dev/sdc1

Take a look at the volume groups we packed

vgdisplay ysh

You can see that we have 9.99G of space

7. Create logical volume

  • Format: lvcreate -L specified size -n specified logical volume name volume group name

The first ysh is the name of my current logical volume, and the second is the name of the previous volume group. You can understand it by referring to the above format.

lvcreate -L + 5G -n ysh ysh

View the logical volumes we created

lvdisplay /dev/ysh/ysh

8. Format logical volume

  • Format: mkfs.xfs+ created LVM volume group
mkfs.xfs /dev/ysh/ysh

9. Mount logical volume–Auto-start at boot Mount (permanent mount)

//The method of self-starting mounting at boot is not the only one. The blkid-UUID method is used here.

//Use mkdir to create a mounted directory mkdir /ysh

//Use blkid to check your own UUID code, and then copy the bottom UUID code without double quotes.

Create a mounting directory and name it yourself.

mkdir /ysh

View blkid-UUID

blkid

Note that the last one is the UUID

Now copy the UUID and save it

a99e6802-1cc8-4e9c-88d2-fd301e078ed5

Be careful not to copy into double quotes

//Edit vim /etc/fstab and then copy the format of the /boot line at the bottom, replace the UUID with the UUID just copied, replace /boot with your own mounting directory, save and exit

vim /etc/fstab

Add this statement, replace the UUID with your own, and replace /ysh with the directory you just created.

UUID=a99e6802-1cc8-4e9c-88d2-fd301e078ed5 /ysh xfs defaults 0 0

//Use mount -a command to refresh, then df -h to view

//Mounted successfully, the size is 5G. You can then expand the capacity, or not, depending on your needs.

mount -a
df -h

You can see that the logical volume was created successfully

10. Logical volume expansion

Requires manual operation

lvextend -L + 4.9G /dev/ysh/ysh
xfs_growfs /dev/ysh/ysh
df -h

Just execute the above command

If the following error occurs, let’s look at the save information. 5G requires 1280 memory blocks, but we only have 1278. So changing 5G to 4.9G is ok. If it still doesn’t work, try smaller.

Okay, you’re done, the flowers are finished