laitimes

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

author:Cloud Computing Trainee

First, the LVM principle review

A summary of how LVM works:

(1) The physical disk is formatted as PV, and the space is divided into PE

(2) Different PVs are added to the same VG, and the PEs of different PVs all enter the PE pool of VG

(3) LVs are created based on PE and are integer multiples of PE, and the PEs that make up LV may come from different physical disks

(4) LV can now be formatted and mounted and used

(5) The expansion reduction of LV is actually increasing or decreasing the number of PEs that make up the LV, without losing the original data

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

We see that if we want to expand LV here, directly add an SDC hard disk, and then format it into PE, and then add the PV to VG, at this time we can dynamically expand LV by increasing the number of PE in LV, as long as the size of our LV does not exceed the size of our VG free space! !

2. Create LVM logical volumes

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

The following will demonstrate how to create our LVM, in the previous article we have already familiarized ourselves with how LVM works, first to format our physical hard disk into PV, then add multiple PVs to the created VG, and finally create our LV from VG. So our first step is to format our physical hard disk into PV (physical volume)

2.1 Create a new partition for backup

2.1.1 Shut down the virtual machine and add two hard disks

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

2.1.2 Create a new partition after booting

View the newly added hard drive

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

Create a new partition, dev/sdb1

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

Create a new partition dev/sdc1

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

2.2 Here we format the /dev/sdb1 and /dev/sdc1 hard disks into PV according to the newly created partition

2.3 After creating a PV, we can use the pvdisplay (show details) and pvs commands to view the information of the current pv

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

With these two commands we can see the information of the PVs that we have created, both PVs are 20G and are not currently in use, and PFree is both 20G.

2.4 Create a Volume Group (VG) and add PVs to the volume group

Through the vgcreate command, after creating the PV, we need to create a VG, and then add our PVs to this volume group, and give the volume group a name when creating the volume group.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

Similarly, after creating a VG, we can also use vgdisplay or vgs to view VG information

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

2.5 Creating Logical Volumes (LVs) Based on Volume Groups (VGs)  

Through the lvcreate command, because the created PV and VG are the underlying things, our upper layer uses logical volumes, so we have to create our logical volumes based on VG

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

lvcreate -n mylv -l 100% free ikun allocates all remaining space to mylv

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

There is also a way to write it: lvcreate -n myweb -l 100% ikun allocates all space to myweb, which is not demonstrated here because vg's resources are allotted.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

We found that the information of VG and PV changes from time to time every time we create an LV, and the size of the LV we create is determined according to the size of the current VG, and cannot exceed the remaining size of the current VG!

Each time a logical volume is created, a folder named after the volume group will appear in the /dev directory, and all logical volumes created based on the volume group are stored under this folder, which we can check.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

Every time we create a new logical volume, there will be an additional device in the VG directory.

Format and use our logical volume

Now that we've created our PV, VG, and LV, if we want to use a logical volume, we have to format it into the file system we need, mount it, and then we can use the logical volume like a partition.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

After formatting our logical volume, we can mount it using the mount command, we mount it to the media directory, and create a mount point first.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)
Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

We see that our volume group is mounted and can be manipulated as if it were a partition. To facilitate automatic mounting after server restart, you need to add the created file system mount information to /etc/fstab. The UUID can be queried via the blkid command.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

3. Delete the logical volume

After we create a logical volume, we can use it by creating a file system, mounting the logical volume, and deleting it if we don't want to use it.

Note: We have strict order for creating physical volumes, creating volume groups, and creating logical volumes, as well as deleting logical volumes, deleting volume groups, and deleting physical volumes

(1) First unmount the logical volume in use through the umount command

(2) Delete the logical volume first through the lvremove command

(3) Delete the volume group through the vgremove command

(4) Finally, let's delete our physical volume via the pvremove command

3.1 Uninstall MYLV

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

3.2 Delete MYLV and ikunlv

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

3.3 Delete VG

3.4 Delete PV

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)

At this point, our newly created logical volume mylv, volume group ikun, and physical volume /dev/sdb have been deleted from our current operating system and can be viewed through the lvs, vgs, pvs commands.

Linux Disk Management (II): Creation and Use of LVM (Ultra Detailed)