LVM and disk quota (including disk quota and verification practice)

LVM and disk quotas

One, LVM overview

  • LVM (Logical Volume Manager) logical volume management

    Logical volume management is a logical layer added between the hard disk partition and the file system to shield the lower layer hard disk partition layout for the file system, and provide an abstract disk volume to establish a file system on the disk volume. When a new hard disk is added to the server, the administrator does not need to move the original files to the new hard disk, but directly expands the file system through LVM to span the physical hard disk. LVM can dynamically adjust the disk capacity while keeping the existing data unchanged, thereby improving the flexibility of disk management.

Note: The /boot partition is used to store boot files and cannot be created based on LVM.

Second, the basic concept of the LVM mechanism

1. The composition of LVM

Physical volume PV (physical volume): A physical volume is the basic storage device of the LVM mechanism, usually corresponding to a common partition or an entire hard disk. When creating a physical volume, a reserved block is created at the head of the partition or hard disk to record the attributes of LVM, and the storage space is divided into basic units (PE) with a default size of 4MB to form a physical volume.

Physical block PE (physical Extent): The smallest storage unit that can be allocated in a physical volume PV. The size of the PE can be specified, and the default is 4M.

Logical volume LV (logical volume): A piece of space divided from a volume group to form a logical volume. Use tools such as mkfs to create a file system on the logical volume LV.

Volume group VG (Volume Group): LVM volume group is similar to physical hard disks in non-LVM systems. A volume group VG is composed of one or more physical volumes PV. A logical volume LV can be created on the volume group VG.

2. Detailed explanation of LVM graphics

LVM encapsulates the underlying physical hard disks, and then presents them to upper-layer applications in the form of logical volumes. When we operate on the underlying physical hard disk, we no longer operate on partitions, but manage the underlying hard disk through logical volumes. As shown in the figure below, part of the physical volume hard disk space can be left to prevent the logical volume from being added at any time when the space is insufficient, and when a new hard disk is added to the server, the management operation can be performed directly through LVM.

Insert picture description here

Three, LVM management commands

1. Main commands and their functions

Features Physical volume management Volume group management Logical volume
Scan pvscan vgscan lvscan
Create pvcreate vgcreate lvcreate
Display pvdisplay vgdisplay lvdisplay
Remove pvremove vgremove lvremove
Extend vgextend lvextend
Reduce vgreduce lvreduce

2. Command format

pvcreate 设备名1 [设备名2……]

vgcreate 卷组名  物理卷名1  物理卷名2

lvcreate -L 容量大小 -n 逻辑卷名  卷组名

lvextend - L +大小 /dev/卷组名/逻辑卷名

Fourth, create LVM

1) After closing the virtual machine, click Edit Virtual Machine Settings→Add Hard Disk

Insert picture description here

2) The SCSI disk type is selected by default, and a new virtual disk is created by default (V)

Insert picture description here

Insert picture description here

3) Select 10G for the disk size, and the specified disk file will not change by default

Insert picture description here

Insert picture description here

4) Create 3 new hard disks, each with a size of 10G, you can see 3 new hard disks in the hardware.

Insert picture description here

5) Turn on the virtual machine, open Xshell and connect to the virtual machine, you can see the following figure, adding three new disks, sdb, sdc, and sdd.

Insert picture description here

6) Create three partitions, each of which directly uses the full size of the hard disk. Here I will demonstrate the same steps as sdb, sdc and sdd.

Insert picture description here

7) Then use fdisk -l to view the partition table information, you can see that the three partition types of sdb1, sdc1, and sdd1 have been set to LVM

Insert picture description here

8) Create a physical volume and view

Insert picture description here

9) You can also use vgcreate abc /dev/sd[bd]1 to create a volume group abc, which contains three physical volumes, sdb1, sdc1, and sdd1. And view

Insert picture description here

10) Create a logical volume and view

Insert picture description here

11) If you want to use a logical volume, you need to format it, then mount it, and then use it. Here are the specific steps:

Insert picture description here

Five, expand the logical volume

Two ways to expand

1) Online expansion

No need to unmount and format, after the expansion is completed, directly refresh the xfs type file system and it can be used directly

Insert picture description here

2) Unmount, expand, format (if important files need to be backed up in advance), mount

If you add a new hard disk sde, after the previous hard disk management, the partition type is changed to LVM, then the physical volume /dev/sde1 is created, and then the vgextend command is used to expand the volume group abc

Insert picture description here

Insert picture description here

I first expanded the capacity by 2G through Method 1, and currently it is 31G. Then use method 2 to unmount and expand the logical volume.

Insert picture description here

Mounting again found that the capacity of the logical volume did not increase because it has not been formatted yet.

Insert picture description here

Unmount it first, then format it. (Generally expand the capacity after unmounting, then format, finally mount, and check the file system usage.)

Insert picture description here

The expansion has been done before, and now you only need to mount and view the file system usage and you will find that the expansion has been successful.

Insert picture description here

Six, disk quota

1. Conditions for realizing the disk quota

1) Need Linux kernel support

2) Check whether the xfsprogs and quota software packages are installed, and install them if they are not installed

  • Use rpm-q or rpm-qa | grep to check whether xfsprogs and quota are installed

Insert picture description here

As shown in the figure above, the query found that these two software packages have been installed.

2. Features of Linux Disk Quota

1) Scope: for the specified file system (partition)

2) Restricted objects: user accounts, group accounts

3) Restriction type: disk capacity, number of files

4) Restriction method: soft restriction, hard restriction

3. Steps to set disk quota

1) Check whether the xfsprogs and xfs_quota software packages have been installed, which have been explained in the implementation conditions, so I won’t repeat them here.

2) Mount the file system in a way that supports the quota function

umount /dev/vgname1/lvname1
mount -o usrquota,grpquota /dev/vgname1/lvname1	 #添加挂载参数“usrquota,grpquota”用于增加对用户、组配额功能的支持

or

vim /etc/fstab 
/dev/vgname1/lvname1 /opt xfs defaults,usrquota,grpquota 0  0
mount -a	#-a选项,将/etc/fstab的所有内容重新加载

①Enter the /etc/fstab disk list vi editor, and add the mount parameter "usrquota, grpquota" to increase the support for user and group quota functions. Then: wq save and exit. mount -a reloads the /etc/fstab disk list once. Restart the virtual machine

Insert picture description here

②Add mounting parameters in the disk list editor. The last two 0s respectively represent not to do dumb backup and not to check whether the sector is complete.

Insert picture description here

③Check the file system usage after restarting, and display the usage and type in a humanized manner.

Insert picture description here

3) Edit the quota settings of user and group accounts

useradd lisi
passwd lisi
xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=40 ihard=50 lisi' /abc01/

View the disk capacity limit of the specified user (/abc01 is the mount directory)

xfs_quota -c 'quota -uv lisi' /abc01

View the limit on the number of files for a specified user (/abc01 is the mount directory)

xfs_quota -c 'quota -i -uv lisi' /abc01

Corresponding options and functions

Options function
-x Indicates that the expert mode is activated, and all management commands that allow modification of the quota system are available in the current mode.
-c Indicates that the management command is directly invoked.
-u Specify the user account object
-g Specify group account objects
bsoft Set the soft limit value of the disk capacity (the default unit is KB).
bhard Set the hard limit value of the disk capacity (the default unit is KB).
isoft Set the soft limit value for the number of disk files.
ihard Set the hard limit value for the number of disk files.

①Add user and set password.

Insert picture description here

②Set the limit of capacity and number of files, and check the corresponding settings

Insert picture description here

4) Verify the disk quota function

chmod 777 /abc01 #给挂载目录/abc01最大权限
su lisi #切换到用户lisi
cd /abc01 #切换到挂载目录

Insert picture description here

Verify that the disk capacity exceeds the limit

dd if=/dev/zero of=/abc01/test.txt bs=10M count=12 #一次发个10M的test.txt文件,发12次

Insert picture description here

Verify that the number of disk files exceeds the limit

touch {1..52}.txt #创建连续的多个文件,".."表示连续的。

Insert picture description here

supplement:

  • The dd command is a device conversion and continuous copy command

“If=” specifies the input device (or file)
“of=” specifies the output device (or file)
“bs=” specifies the size of the read data block
“count=” specifies the number of the read data block
/dev/zero “zero "The device file can provide unlimited null characters. It is often used to generate a file of a specific size.

5) View quota usage

  • View the disk capacity quota usage of all available partitions
xfs_quota -x -c 'report -a'

Insert picture description here

  • View report of disk capacity and file count
xfs_quota -x -c 'report -abih'

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/110533454