Linux: LVM and disk quotas

One, LVM overview

(1) LVM logical volume Logical Volume Manager

1. It can dynamically adjust the disk capacity while keeping the existing data unchanged, thereby improving the flexibility of disk management.
2. The /boot partition is used to store boot files and cannot be created based on LVM

(2) The basic concept of LVM mechanism

  • PV (physical volume): A physical volume is the basic storage device of the LVM mechanism, usually corresponding to an ordinary 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
  • VG (Volume Group): consists of one or more physical volumes as a whole, which is called a volume group, in which physical volumes can be dynamically added or removed
  • 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

(3) LVM management commands

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

(4) The main command steps of LVM operation

1. Shut down the host, add two new hard drives, and restart the host
Insert picture description here
Insert picture description here

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 —>n —>t 8e
fdisk /dev/sdc—>n —>t 8e
Insert picture description here
Insert picture description here

3. Create a physical volume

 pvcreate /dev/sdb1 /dev/sdc1

Insert picture description here

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

vgcreate abc1 /dev/sdb1 /dev/sdb2
或 vgcreate abc1 /dev/sd[bc]1

Insert picture description here
Insert picture description here

5. Create a logical volume, the logical volume is named abc, the capacity is 8G, and the generated file path is /dev/abc1/abc

 vcreate -L 8G -n abc abc1

Insert picture description here

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

 mkfs -t xfs /dev/abc/abc1
 mkdir /ABC
 mount /dev/abc1/abc /ABC
 df -Th

Insert picture description here

7. Further expansion

 vgextend abc1 /dev/sdc2  (如果sdc没有载分区直接输入sdc)
 lvextend -L +5G /dev/abc1/abc  没有+号则表示扩展到指定容量
 xfs_growfs /dev/abc1/abc       ext4:resize2fs /dev/abc1/abc
 

2. Disk quota

(1) Conditions and characteristics of realizing disk quota

1. Need Linux kernel support
2. Install xfsprogs and quota software package
3. Features of disk quota

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

(2) Steps to set disk quota

When the disk space of the Linux 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

1. Check whether the xfsprogs and xfs_quota software packages have been installed. 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

  rpm -q xfsprogs quota
  yum install -y xfsprogs

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

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

or

vim /etc/fstab   添加参数
 /dev/abc1/abc        /ABC     xfs         defaults,usrquota,grpquota    0 0 
 :wq
 umount /dev/abc1/abc
 mount -a        将/etc/fstab的所有内容重新加载
在这里插入代码片

3. Edit the quota settings of user and group accounts

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

Explanation of commonly used options
-x means to start expert mode, all management commands that allow modification of the quota system in the current mode are available
-c means to directly call management commands -u designated user account object
-g designated group account object
bsoft sets the disk capacity software Limit value (default unit KB)
bhard sets the hard limit value for disk capacity (default unit is KB)
isoft sets the soft limit value for the
number of disk files ihard sets the hard limit value for the number of disk files

——>Only limit disk capacity

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

——>Only limit the number of files

 xfs_quota -x -c 'limit -u isoft=4 ihard=5 zhangsan' /ABC/

View zhangsan disk capacity limit

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

View zhangsan file limit

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

4. Verify the disk quota function

 chmod 777 /ABC  配置所有用户可读可写可执行
 su zhangsan     
 cd /ABC
 dd if=/dev/zero of=/opt/ddtest.txt bs=10M count=12   验证磁盘容量超限
  touch {
    
    aa,bb,cc,dd,ee,ff}.TXT             验证磁盘文件数超限 

**Supplement: **The dd command is a device conversion and continuous copy command
if= specifies the device to fetch the file from the input device (or file)
of= specifies the device to write to the output device (or file)
bs= specifies to read the data The size of the block
count= specifies the number of read data blocks
/dev/zero "zero" device file, can provide unlimited null characters. Often used to generate a file of a specific size
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/113695321