Linux Elementary Introduction to LVM and Disk Quota


Logical Volume Manager

LVM overview

LVM logical volume management

Many users of the Linux operating system will encounter such a dilemma when installing the operating system: how to accurately evaluate and allocate the capacity of each hard disk partition. If the original estimate is not accurate, once the system partition is not enough, you may have to back up, delete related data, or even be forced to re-plan the partition and reinstall the operating system to meet the needs of the application system

Dynamically adjust the disk capacity to improve the flexibility of disk management.
/boot partition is used to store boot files and cannot be created based on LVM

Graphical interface management tool
system-config-lvm

Basic concepts of LVM mechanism

PV (Physical Volume, physical volume)

The physical volume is the basic storage device of the LVM mechanism, and usually corresponds to an ordinary partition or the 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.
Insert picture description here

VG (Volume Group, volume group)

One or more physical volumes form a whole, which is called a volume group, in which physical volumes can be dynamically added or removed.
Insert picture description here

LV (Logical Volume, logical volume)

A piece of space divided from the volume group forms a logical volume. Use tools such as mkfs to create file systems on logical volumes.

Insert picture description here

LVM management commands

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

Remarks: pvcreate device name 1 [device name 2… …]
vgcreate volume group name physical volume name 1 physical volume name 2
lvcreate -L capacity size -n logical volume name volume group name
lvextend -L +size/dev/volume group name/ Logical volume name

The main process of LVM's actual operation

LVM operation main command steps
1. Shut down the host, add two new hard disks, and restart the host

2. First use the fdisk tool to divide the disk devices /dev/sdb and /dev/sdc into primary partitions sdb1 and sdc1, and change the ID mark number of the partition type to "8e"

fdisk /dev/sdb
fdisk /dev/sdc

3. Create a physical volume

pvcreate /dev/sdb1 /dev/sdc1

4. Create a volume group, the volume group is named vgname1

vgcreate vgname1 /dev/sdb1 /dev/sdb2

5. Create a logical volume, the logical volume is named lvname1, the capacity is 20GB, and the
generated file path is /dev/vgname1/lvname1

vcreate -L 20G -n lvname1 vgname1

6. Format the logical volume, create an XFS file system, and mount it to the /opt directory

mkfs -t xfs /dev/vgname1/lvname1
mount /dev/vgname1/lvname1 /opt

7. Continue to expand (expand)

vgextend vgname1 /dev/sdc2
lvextend -L +10G /dev/vgname1/lvname1
resize2fs /dev/vgname1/lvname1			#刷新文件系统容量

Overview of Disk Quota

When the disk space of the Linux root partition is exhausted, the Linux operating
system will no longer be able to create new files. At the same time, failures such as service program crashes and system failures may occur.
In order to avoid problems like insufficient disk space in the server, the disk quota function can be enabled to limit the disk space and the number of files used by the user in the specified file system (partition) to prevent individual users from maliciously or unintentionally occupying a large number of disks. Space to maintain the stability and continuous availability of system storage space.

Conditions for achieving disk quota

Requires Linux kernel support to
install xfsprogs and quota software packages

Features of Linux Disk Quota

Scope:
Restricted objects for the specified file system (partition) : user account, group account
Restriction type: disk capacity, number of files
Restriction method: soft limit, hard limit

The theoretical steps of disk quotas

Insert picture description here

Disk quota management

Verify disk quota function

Switch to the partition where the quota is set (mount directory) to
create a specified number of files: use the touch command or the cp command to
create a file of the specified capacity: use the dd command or the cp command

command Description
dd It is a device conversion and continuous copy command
if= Specify input device (or file)
of= Specify the output device (or file)
bs= Specify the size of the read data block
count= Specify the number of data blocks to be read
/dev/zero 'Zero' device file, can provide unlimited null characters, commonly used to generate a file of a specific size; used for testing

View quota usage

xfs quota-xc'report option'

Common options

Options Description
-a Represents all user accounts and group accounts
-b Representative capacity
-i Number of representative files
-h Represents a humanized display

The actual operation steps of disk quota

When the disk space of the Linux root partition is exhausted, the Linux operating system will no longer be able to create new files. At the same time, failures such as service program crashes and system failures may occur.

In order to avoid problems like insufficient disk space in the server, the disk quota function can be enabled to limit the disk space and the number of files used by the user in the specified file system (partition) to prevent individual users from maliciously or unintentionally occupying a large number of disks. Space to maintain the stability and continuous availability of system storage space.

In the CentOS system, different file systems use different disk quota configuration management tools. For example, the XFS file system is managed by the xfs_quota tool; the EXT3/4 file system is managed by the quota tool.

Steps to set disk quota in CentOS7

1. Check whether the xfsprogs and xfs_quota packages have been installed

rpm -q xfsprogs quota
yum install -y xfsprogs quota

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

umount /dev/vgname1/lvname1
mount -a					#-a选项,将/etc/fstab的所有内容重新加载

3. Edit the quota settings of user and group accounts

useradd zhangsan
passwd zhangsan
xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=40 ihard=50 zhangsan' /opt/(挂载点)

-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 the group account object
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 hard limit value for the number of disk files.
ihard: Set the soft limit value for the number of disk files.

#仅限制磁盘容量

xfs_quota -x -c 'limit -u bsoft=80M bhard=100M zhangsan' /opt/

#仅限制文件数

xfs_quota -x -c 'limit -u isoft=40 ihard=50 zhangsan' /opt/

#查看 zhangsan 磁盘容量限制

xfs_quota -c 'quota -uv zhangsan' /opt/

#查看 zhangsan 文件数限制

xfs_quota -c 'quota -i -uv zhangsan' /opt/

4. Verify the disk quota function

chmod 777 /opt(赋予权限)
su zhangsan
cd /opt

#验证磁盘容量超限
dd if=/dev/zero of=/opt/ddtest.txt bs=10M count=12

#验证磁盘文件数超限
dd if=/dev/zero of=/opt/ddtest.txt bs=1k count=6
touch {
    
    1,2,3,4,5}.txt

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 read The number of data blocks
/dev/zero The "zero" device file can provide unlimited null characters. It is often used to generate a file of a specific size.

5. Check the quota usage

#查看所有可用分区的磁盘容量配额使用情况
xfs_quota -x -c 'report -a'

#查看磁盘容量和文件数的报告
xfs_quota -x -c 'report -abih'

To refresh after extending LVM, to refresh the xfs type file system, use the xfs_growfs command to refresh

View quota usage
quota -u username
quota -g group name

requota /yun01

Guess you like

Origin blog.csdn.net/m0_53497201/article/details/113378191