Linux--LVM logical volume and disk quota


One, manage LVM logical volumes

  • Many Linux users will encounter this dilemma when installing the operating system: how to accurately evaluate and allocate the capacity of each hard disk partition. If the initial estimation is not accurate, once the system partition is not enough, the relevant data may have to be backed up, deleted, or even forced Re-plan the partition and reinstall the operating system to meet the needs of the application system
  • This article will learn how to dynamically adjust the capacity of Linux partitions by learning the LVM logical volume management mechanism IDE

1. LVM overview

  • LVM is a logical mechanism for disk partition management in Linux systems. It is built on hard disks and partitions, and a logical layer below the file system. When the file system is created, the next disk partition layout can be shielded. Dynamically adjust the disk capacity while keeping the existing data unchanged, thereby improving the flexibility of disk management
  • When you select automatic partitioning during the installation of the CentOS system, the LVM partitioning scheme will be used by default and no manual configuration is required
  • Host: "/boot" partition cannot be created based on LVM, it must be independent

1.1 PV (physical Volume, physical volume)

  • The physical volume is the basic storage device of the LVM mechanism, which usually corresponds to a normal 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 the physical volume

1.2 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
    mark

1.3 LV (Logical Volume, logical volume)

  • Logical volumes are built on volume groups and have no direct relationship with physical volumes
  • For logical volumes, each volume group is a whole, and a small piece of space is "cut out" from this whole. As the basis for users to create a file system, this small piece of space is called a logical volume
  • After using tools such as mkfs to create a file system on the logical volume, it can be mounted to the directory in the Linux system for use
    mark

1.4 Summary

Through the above explanation of physical volumes, volume groups, and logical volumes, we can see that the process of establishing the LVM partition management mechanism:

  1. First, create a normal partition or the entire hard disk as a physical volume
  2. Next, the storage space of each physical volume that is physically scattered is formed into a logical whole, and the volume group
  3. Finally, based on the volume group as a whole, separate different data storage spaces to form logical volumes
  4. Logical volume is the storage unit that can be formatted and mounted by end users

2. Manage LVM

LVM management commands are mainly divided into three categories: PV physical volume management, VG volume group management, and LV logical volume management. The corresponding command programs start with "pv", "vg", and "lv"

Features PV management commands VG management commands LV management commands
Scan pvscan vgscan lvscan
Create pvcreate vgcreate lvcreate
Display pvdisplay vgdisplay Ivdisplay
Remove pvremove vgremove lvremove
Extend —— vgextend Ivextend
Reduce —— vgreduce Ivreduce

Note: When you want to extend the size of a logical volume and use the "lvextend" command to extend, it cannot be in the logical volume before the extension. The logical volume can be edited without unmounting, or it can be edited after unmounting, and the expanded space There must be a "+" before, otherwise the original space is changed to 20G instead of expansion, here the logical volume is expanded by 20G

umount 逻辑卷

lvextend -L +20G 逻辑卷

lvdispaly 逻辑卷
#查看扩容后的逻辑卷空间大小

3. LVM application examples

  • Case environment: The company plans to build a mail server (CentOS7.3 system platform) on the Internet to provide e-mail space for employees across the country and some VIP customers
  • Demand description: Due to the large number of users, mail storage requires a lot of space. Taking into account the need for dynamic expansion, we plan to add three SCSI hard disks and build LVM logical volumes (mounted to a directory under the "/" directory) for special use Store mail data
  • According to the above case environment and requirements, the recommended operation steps are as follows:
  1. Shut down the server host, correctly mount three new SCSI hard disks, each hard disk size is 40G, a total of 120G
    mark
  2. Turn on the server host and execute the "fdisk -l" command to check to confirm that the newly added hard disk has been recognized
    mark
  3. Partition in the new disk, divide all the space of each hard disk into an independent primary partition, and change the partition type to "8e". The
    mark
    other two disks are also the same operation. After all modifications are completed, check the confirmation result
    mark
  4. Convert partition to physical volume
    mark
  5. Consolidate the above physical volumes to create a volume group named zxc
    mark
  6. Create a logical volume named zxc123 in the zxc volume group with a capacity of 90G
    mark
  7. Use the mkfs command to format the logical volume zxc and mount it to a directory
    mark
  8. Use the lvextend command to expand the logical volume zxc123 by 20G, and then use the xfs_growfs /dev/zxc/zxc123 command to update the file system size recognized by the system
    mark
  9. Supplement: If formatted as EXT4 file system type, use the resize2fs command to update the file system size recognized by the system

2. Disk quota

1. Conditions for realizing disk quota

  1. Need Linux kernel support
  2. Installation package (generally, the system is installed by default)
    mark

2. Features of Linux Disk Quota

  1. Scope: for the specified file system (partition)
  2. Restricted object: user account, group account
  3. Restriction type: disk capacity, number of files (node ​​number)
  4. Limitation method: soft limit, hard limit

3. Set 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 designated 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

4. Set the disk based on CentOS7

4.1 Check whether the relevant software packages have been installed

rpm -q xfsprogs quota

yum -y install xfsprogs quota

mark

4.2 Mount the file system in a way that supports the quota function

#自动挂载
vim /etc/fstab

/dev/zxc/zxc123                           /zxc123                 xfs     defaults,usrquota,grpquota    0 0


mount -a

mark
mark

4.3 Edit quota settings for user and group accounts

useradd xiaoxu
#创建一个新用户
passwd xiaoxu
#设置密码来激活该新用户

xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=40 ihard=50 xiaoxu' /zxc111/

mark

Common parameters Explanation
-x Indicates that the expert mode is started, all management commands that allow modification of the quota system are available in the current mode
-c Means to directly call management commands
-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
#仅限制磁盘容量
xfs_quota -x -c 'limit -u bsoft=80M bhard=100M xiaoxu' /zxc111/

#仅限制文件数
xfs_quota -x -c 'limit -u isoft=40 ihard=50 xiaoxu' /zxc111/

#查看 xiaoxu 磁盘容量限制
xfs_quota -c 'quota -uv xiaoxu' /zxc111/

mark

4.4 Verify disk quota function

chmod 777 /zxc111/
su xiaoxu
cd /zxc111/

#验证磁盘容量超限
dd if=/dev/zero of=/opt/ddtest.txt bs=10M count=12
(每次给10M,连续给12次)

mark
mark

Parameter command Explanation
dd command Is a device conversion and continuous copy command
“if=” Specify input device (or file)
“of=” Specify 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 The "zero" device file can provide unlimited null characters. Often used to generate a file of a specific size
xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=4 ihard=5 xiaoxu' /zxc111/
##限制文件数,文件数软限制为4个,硬限制为5个

xfs_quota -c 'quota -i -uv xiaoxu' /zxc111/
#查看文件数限制

#验证磁盘文件数超限
touch {1..101}.txt

mark
mark

4.5 Check quota usage

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

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

mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/113001865