LVM概述
Logical Volume Manager,逻辑卷管理
能够在保持现有数据不变的情况下动态调整磁盘容量,从而提高磁盘管理的灵活性
/boot分区用于存放引导文件,不能基于LVM创建
它是 Linux 下对硬盘分区的一种管理机制。LVM 适合于管理大存储设备,
并允许用户动态调整文件系统的大小。此外,LVM 的快照功能可以帮助我们快速备份数据。
LVM 为我们提供了逻辑概念上的磁盘,使得文件系统不再关心底层物理磁盘的概念。
物理卷(Physical Volume,PV):就是真正的物理硬盘或分区。
卷组 (Volume Group,VG):将多个物理卷合起来就组成了卷组。组成同一个卷组的物理卷可以是 同一块硬盘的不同分区,也可以是不同硬盘上的不同分区。我们可以 把卷组想象为一块逻辑硬盘。
逻辑卷(Logical Volume,LV):卷组是一块逻辑硬盘,硬盘必须分区之后才能使用,我们把这个分 区称作逻辑卷。逻辑卷可以被格式化和写入数据。我们可以把逻辑 卷 想象为分区。
物理扩展(Physical Extend,PE):PE 是用来保存数据的最小单元,我们的数据实际上都是写入 PE 当中的。PE 的大小是可以配置的,默认是 4MB。
1.分区是磁盘上的连续空间,一个硬盘上
2.有了raid各种raid解决了一些问题(分完就无法扩容修改了,4块变一块,关机会影响服务,进入raid 配置界面)
3.平滑扩容,动态扩容
LVM 的管理命令
功能 PV管理命令 VG管理命令 LV管理命令
Scan(扫描) pvscan vgscan lvscan
Create(建立) pvcreate vgcreate lvcreate
Display(显示) pvdisplay vgdisplay lvdisplay
Remove(移除) pvremove vgremove lvremove
Extend(扩展) vgextend lvextend
Reduce(减少) vgreduce lvreduce
在每个磁盘里只创建一个分区,每个分区使用对应磁盘的全部的空间,即全部按回车键就好。
注意在分好分区大小以后需要使用“t”选项修改分区类型,
我们需要创建动态逻辑卷即LVM,所以将分区类型修改为“8e”,
如果不清楚分区类型的代码,可以使用“l”选项查看所有类型的代码。修改完成后记得使用“w”保存。
格式:pvcreate 设备名1 [设备名2 .....]
vgcreate 卷组名 物理卷名1 物理卷名2
lvcreate -L 容量大小 -n 逻辑卷名 卷组名
lvextend -L +大小 /dev/卷组/各逻辑卷名
1.修改类型
[root@localhost ~]# alias scan='echo "- - -" > /sys/class/scsi_host/host0/scan;echo "- - -" >/sys/class/scsi_host/host1/scan;echo "- - -" > /sys/class/scsi_host/host2/scan'
[root@localhost ~]# scan
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 3G 0 part [SWAP]
└─sda3 8:3 0 46.7G 0 part /
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sr0 11:0 1 54M 0 rom
sr1 11:1 1 4.2G 0 rom /run/media/richard/CentOS 7 x86_64
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0d316383.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): q
[root@localhost ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xdfcc404f.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 3G 0 part [SWAP]
└─sda3 8:3 0 46.7G 0 part /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 10G 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 10G 0 part
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 10G 0 part
sr0 11:0 1 54M 0 rom
sr1 11:1 1 4.2G 0 rom /run/media/richard/CentOS 7 x86_64
2.配置物理卷
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdd1" successfully created.
3.配置卷组
[root@localhost ~]# vgcreate xu /dev/sdb1 /dev/sdc1 /dev/sdd1
Volume group "xu" successfully created
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name xu
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size <29.99 GiB
PE Size 4.00 MiB
Total PE 7677
Alloc PE / Size 0 / 0
Free PE / Size 7677 / <29.99 GiB
VG UUID SIlwf2-9IUa-X1Pc-h63G-AbjE-CsTY-XtV4IX
4.配置逻辑卷
[root@localhost ~]# lvcreate -L +20G -n xny xu
Logical volume "xny" created.
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/xu/xny
LV Name xny
VG Name xu
LV UUID Icwbf6-LIzG-8wL0-fo14-Jt5i-DzmK-3OGpzX
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2023-02-23 00:28:23 -0800
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
5.格式化逻辑卷
[root@localhost ~]# mkfs.xfs /dev/xu/xny
meta-data=/dev/xu/xny isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
6.挂载+扩容
[root@localhost opt]# mkdir xxu
[root@localhost opt]# ls
httpd-2.4.29 httpd-2.4.29.tar.bz2 rh xxu
[root@localhost opt]# cd -
/root
[root@localhost ~]# mount /dev/xu/xny /opt/xxu
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 47G 5.2G 42G 11% /
devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs 1.4G 0 1.4G 0% /dev/shm
tmpfs 1.4G 9.2M 1.4G 1% /run
tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 297M 157M 141M 53% /boot
tmpfs 280M 8.0K 280M 1% /run/user/42
tmpfs 280M 56K 280M 1% /run/user/1000
/dev/sr1 4.3G 4.3G 0 100% /run/media/richard/CentOS 7 x86_64
tmpfs 280M 0 280M 0% /run/user/0
/dev/mapper/xu-xny 20G 33M 20G 1% /opt/xxu
[root@localhost ~]# lvextend -L 22G /dev/xu/xny -r
Size of logical volume xu/xny changed from 20.00 GiB (5120 extents) to 22.00 GiB (5632 extents).
Logical volume xu/xny successfully resized.
meta-data=/dev/mapper/xu-xny isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 5767168
[root@localhost ~]# lvextend -L 22G /dev/xu/xny
Size of logical volume xu/xny changed from 20.00 GiB (5120 extents) to 22.00 GiB (5632 extents).
Logical volume xu/xny successfully resized.
[root@localhost ~]# xfs_growfs /dev/xu/xny
meta-data=/dev/mapper/xu-xny isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 5767168
#如果扩容最后没有 -r,就需要xfs_growfs 刷新
Mapper----映射机制
ext4可以xfs无法缩减,缩减会影响业务。
1.解挂载
2.检查文件系统完整性
3.缩减文件系统
4.缩减逻辑卷上下一致
5.再挂载回去
磁盘配额概述:
实现磁盘限额的条件
需要Linux内核支持
安装xfsprogs与quota软件包
Linux磁盘限额的特点
作用范围:针对指定的文件系统(分区)
限制对象:用户账号、组账号
限制类型:磁盘容量、文件数量
限制方法:软限制、硬限制
当Linux根分区的磁盘空间耗尽时,Linux操作系统将无法再建立新的文件,同时也可能会出现服务程序崩溃、系统无法启动等故障。
为了避免在服务器中出现类似磁盘空间不足的问题,可以启用磁盘配额功能,对用户在指定文件系统(分区)中使用的磁盘空间、文件数量进行限制,以防止个别用户恶意或无意间占用大量磁盘空间,从而保持系统存储空间的稳定性和持续可用性。
在Centos系统中,不同的文件系统使用不同磁盘配额配置管理工具。例如, XFS文件系统通过xfs_quota工具进行管理;
EXT3/4 文件系统通过quota 工具进行管理。
配置如下:
1.建挂载目录、监测软件包是否安装
[root@localhost opt]# mkdir xxu
[root@localhost opt]# ls
httpd-2.4.29 httpd-2.4.29.tar.bz2 rh xxu
[root@localhost ~]# rpm -q xfsprogs quota
xfsprogs-4.5.0-12.el7.x86_64
quota-4.01-14.el7.x86_64
2.挂载
[root@localhost ~]# vim /etc/fstab
/dev/xu/xny /opt/xxu xfs defaults,usrquota,grpquota 0 0
mount -a #刷新
3.磁盘限额+修改包用户权限
[root@localhost /]# xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=50 ihard=70 xnyy' /opt/xxu
[root@localhost /]# chmod 777 /opt/xxu
[root@localhost /]# chmod 777 opt
4.测试
[xnyy@localhost ~]$ dd if=/dev/zero of=/opt/xxu/test.txt bs=10M count=12
dd: error writing ‘/opt/xxu/test.txt’: Disk quota exceeded
11+0 records in
10+0 records out
104857600 bytes (105 MB) copied, 0.141085 s, 743 MB/s
[xnyy@localhost ~]$ cd /opt
[xnyy@localhost opt]$ cd xxu
[xnyy@localhost xxu]$ ll
total 102400
-rw-rw-r--. 1 xnyy xnyy 104857600 Feb 23 00:49 test.txt
[xnyy@localhost xxu]$ touch {1..80}.txt
touch: cannot touch ‘71.txt’: Disk quota exceeded
touch: cannot touch ‘72.txt’: Disk quota exceeded
touch: cannot touch ‘73.txt’: Disk quota exceeded
touch: cannot touch ‘74.txt’: Disk quota exceeded
touch: cannot touch ‘75.txt’: Disk quota exceeded
touch: cannot touch ‘76.txt’: Disk quota exceeded
touch: cannot touch ‘77.txt’: Disk quota exceeded
touch: cannot touch ‘78.txt’: Disk quota exceeded
touch: cannot touch ‘79.txt’: Disk quota exceeded
touch: cannot touch ‘80.txt’: Disk quota exceeded
[xnyy@localhost xxu]$ ls
10.txt 17.txt 23.txt 2.txt 36.txt 42.txt 49.txt 55.txt 61.txt 68.txt
11.txt 18.txt 24.txt 30.txt 37.txt 43.txt 4.txt 56.txt 62.txt 69.txt
12.txt 19.txt 25.txt 31.txt 38.txt 44.txt 50.txt 57.txt 63.txt 6.txt
13.txt 1.txt 26.txt 32.txt 39.txt 45.txt 51.txt 58.txt 64.txt 70.txt
14.txt 20.txt 27.txt 33.txt 3.txt 46.txt 52.txt 59.txt 65.txt 7.txt
15.txt 21.txt 28.txt 34.txt 40.txt 47.txt 53.txt 5.txt 66.txt 8.txt
16.txt 22.txt 29.txt 35.txt 41.txt 48.txt 54.txt 60.txt 67.txt 9.txt