Linux-based LVM and disk management

1. LVM overview

1.1 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

1.2 Basic concepts of LVM mechanism

Insert picture description here

  • 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 a whole composed of one or more physical volumes

  • 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 a file system on a logical volume.

2. LVM management commands

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
命令格式:
pvcreate 设备名1 [设备名2 ... ...]
vgcreate 卷组名 物理卷名1 物理卷名2
vgextend 卷组名 /dev/sdc2
lvcreate -L 容量大小 -n 逻辑卷名 卷组名
lvextend -L +大小  /dev/卷组名/逻辑卷名

3. LVM application examples

1. Preparation

  • Shut down the host, add two new hard disks, and restart the host
    Insert picture description here

Insert picture description here

2. 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

Insert picture description here
sdc is the same
Insert picture description here

3. Create physical volumes, create volume groups, create logical volumes

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

Insert picture description here

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

vgcreate vgname1 /dev/sdb1 /dev/sdc1
Insert picture description here

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

lvcreate -L 30G -n lvname1 vgname1

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

mkfs -t xfs /dev/vgname1/lvname1----------format the logical volume and create an XFS file system
mount /dev/vgname1/lvname1 /data ---------- -Mount to the /data directory

df -hT

7. Further expansion

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

4. Overview of Disk Quota

1. Why do I need to use disk quotas?
When the disk space of the Linux root partition is exhausted, new files cannot be created, and service program crashes and system failures may occur.
In order to avoid problems like 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 the user 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 software package
    3. Features of Linux disk quota
    Scope:
    Restricted objects for specified file system (partition) : user account, group account
    Limit type: disk capacity, number of files
    Limit method: soft limit, hard limit

5. Steps to set xfs 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       安装缺少的软件

Insert picture description 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”
                                                  用于增加对用户、组配额功能的支持

或者

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=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 “零”设备文件,可以无限的提供空字符。常用来生成一个特定大小的文件。

Insert picture description here
To test here, first delete the file just created
Insert picture description here

5. Check the 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/zhangyuebk/article/details/113665350