Linux-based LVM and disk management (the commands are all demonstrated)

One, LVM overview

1. LVM (Logical Volume Manager), logical volume management

  • Ability to dynamically adjust the disk capacity while keeping the existing data unchanged, thereby improving the flexibility of disk management
  • 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, physical volume)
    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.

  • VG (Volume Group) is
    composed of one or more physical volumes as a whole, which is called a volume group. Physical volumes can be dynamically added or removed from the volume group.

  • LV (Logical Volume)
    is a piece of space divided from a volume group to form a logical volume. Use tools such as mkfs to create file systems on logical volumes.

Insert picture description here

Two, LVM management commands

The following table shows the main commands for managing LVM. The most commonly used of these commands is to create.

Features PV management commands VG management commands LV management commands
Scan pvscan vgscan lvscan
Create pvcreate vgcreate lvcreate
Display pvdisplay vgdisplay lvdisplay
Remove pvremove vgremove lvremove
Extend ----- vgextend lvextend
Reduce ----- vgreduce lvreduce
命令格式:
pvcreate 设备名1 [设备名2 ... ...]
vgcreate 卷组名 物理卷名1 物理卷名2
vgextend 卷组名 /dev/sdc2
lvcreate -L 容量大小 -n 逻辑卷名 卷组名
lvextend -L +大小  /dev/卷组名/逻辑卷名

Three, LVM command application

1. Preparation

  • Shut down the host, add two new hard drives, and restart the host
  • 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

I have one fdisk, the other is the same

Insert picture description here
Insert picture description here

2. LVM command establishment operation

  • Create physical volume
pvcreate /dev/sdb1 /dev/sdc1

Insert picture description here

  • Create a volume group, the volume group name is vgname1
vgcreate vgname1 /dev/sdb1 /dev/sdc1

Insert picture description here

  • Create a logical volume, the logical volume is named lvname1, the capacity is 20GB, and the generated file path is /dev/vgname1/lvname1
lvcreate -L 20G -n lvname1 vgname1

Insert picture description here

3. Use logical volumes

mkfs -t xfs /dev/vgname1/lvname1----------对逻辑卷进行格式化,创建 XFS 文件系统
mount /dev/vgname1/lvname1 /opt -----------挂载到/opt目录下
df -hT

Insert picture description here
Insert picture description here
Insert picture description here

4. Expansion

lvextend -L +10G /dev/vgname1/lvname1  #在xfs格式中,可在线扩容,无需解挂
xfs_growfs /dev/vgname1/lvname1			#刷新文件系统容量,需要在挂载的时候使用,不使用可以显示

Insert picture description here
I added a few more times before the screenshot, but the size is not right, so please take a look.
Insert picture description here

Four, disk quota overview

1. Why do we need to use disk quotas

When the disk space of the Linux root partition is exhausted, no new files can be created. At the same time, failures such as service program crashes and system failures may occur.
In order to avoid problems such as insufficient disk space in the server, it is necessary to enable the disk quota function to limit the disk space and the number of files used by users in the specified file system (partition) to prevent individual users from maliciously or unintentionally occupying a large amount Disk 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.

2. Conditions for realizing the disk quota

  • Need Linux kernel support
  • Install xfsprogs and quota packages

3. Features of Linux Disk Quota

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

Steps to set xfs disk quota in CentOS7

1. Check the software

Check if the xfsprogs and xfs_quota packages are installed

rpm -q xfsprogs quota               #检查,没有安装就执行安装命令
yum install -y xfsprogs quota       #安装缺少的软件

Install the software, I have installed it here, and install it with yum
Insert picture description here

2. Mount the file system

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

方法一:临时挂载
umount /dev/vgname1/lvname1                #如果已挂载,需要解挂
mount -o usrquota,grpquota /dev/vgname1/lvname1 /opt 	#添加挂载参数“usrquota,grpquota”用于增加对用户、组配额功能的支持
方法二:永久挂载
vim /etc/fstab 
/dev/vgname1/lvname1      /opt    xfs   defaults,usrquota,grpquota   0  0

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

Here is a temporary mount. If you want to use a permanent mount, you can try it yourself. The test is effective.
Insert picture description here

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=4 ihard=5 zhangsan' /opt/   #限制磁盘容量和文件数
-x:表示启动专家模式,在当前模式下允许对配额系统进行修改的所有管理命令可用。
-c:表示直接调用管理命令。
-u:指定用户账号对象
-g:指定组账号对象
bsoft:设置磁盘容量的软限制数值(默认单位为 KB)。
bhard:设置磁盘容量的硬限制数值(默认单位为 KB)。
isoft:设置磁盘文件数的硬限制数值。
ihard:设置磁盘文件数的软限制数值。

xfs_quota -x -c 'limit -u bsoft=80M bhard=100M zhangsan' /opt/       #仅限制磁盘容量
xfs_quota -x -c 'limit -u isoft=4 ihard=5 zhangsan' /opt/          #仅限制文件数
xfs_quota -c 'quota -uv zhangsan' /opt/                     #查看 zhangsan 磁盘容量限制
xfs_quota -c 'quota -i -uv zhangsan' /opt/                  #查看 zhangsan 文件数限制

Insert picture description here

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        #验证磁盘容量超限
touch {1,2,3,4,5}.txt                #验证磁盘文件数超限(这里还没涉及if语句,先用touch建立)

dd 命令是一个设备转换和连续复制命令
“if=” 指定输入设备(或文件)
“of=” 指定输出设备(或文件)
“bs=” 指定读取数据块的大小
“count=” 指定读取数据块的数量
/dev/zero “零”设备文件,可以无限的提供空字符。常用来生成一个特定大小的文件。

An error was reported after the dd command. It was written 11 times, the size reached 100M at the 10th time, and an error was reported at the 11th time.
Insert picture description here
To test here, first delete the file just created
Insert picture description here

5. Check quota usage

xfs_quota -x -c 'report -a'         #查看所有可用分区的磁盘容量配额使用情况
xfs_quota -x -c 'report -abih'      #查看磁盘容量和文件数的报告

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/110088505