LVM管理
1. lvm的应用场景及其弊端
- 应用场景:
随着公司的发展数据增长较快,磁盘容量不够了 - 弊端:
数据不是直接放在硬盘上的,而是在硬盘的上面又虚拟出来一层逻辑卷存放数据,故而增加了磁盘数据恢复的困难
动态扩容: 不影响使用的情况下,扩大容量
2. 物理卷,卷组,逻辑卷
- 物理卷(PV):把常规的块设备(硬盘,分区等可以读写数据的设备)通过pvcreate命令对其进行初始化,就成了物理卷
- 卷组(VG):把多个物理卷的容量组成一个逻辑整体,可以从里面灵活分配容量,不能格式化不能挂载。
- 逻辑卷(LV):从卷组中划分部分空间成为一个可以读写数据的逻辑单元。需要对其格式化然后挂载使用
3. lvm管理
3.1 部署lvm
- 创建lvm步骤:
1.创建物理磁盘,创建物理卷
2.创建卷组,将物理卷加入卷组
3.在卷组中划分逻辑卷
4.格式化逻辑卷
5.挂载使用
演示如下:
准备:
- 在虚拟机中加入3块物理磁盘 // sdb.sdc.sdd是我刚才添加的磁盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
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 7.9G 0 rom
- 给sdb分区
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x186a0127.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +5G
Created a new partition 1 of type 'Linux' and of size 5 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943039, default 41943039): +7G
Created a new partition 2 of type 'Linux' and of size 7 GiB.
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (3,4, default 3):
First sector (25167872-41943039, default 25167872):
Last sector, +sectors or +size{K,M,G,T,P} (25167872-41943039, default 41943039):
Created a new partition 3 of type 'Linux' and of size 8 GiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x186a0127
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 25167871 14680064 7G 83 Linux
/dev/sdb3 25167872 41943039 16775168 8G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
开始:
- 把磁盘加入pv
[root@localhost ~]# pvcreate /dev/sdb1 //把sdb1创建为物理卷
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvs //检查pv创建情况
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 lvm2 --- 5.00g 5.00g //成功
[root@localhost ~]# pvcreate /dev/sdc //把sdc创建为物理卷
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdc lvm2 --- 20.00g 20.00g //成功
[root@localhost ~]#
- 创建名为vg0的卷组
[root@localhost ~]# vgcreate vg0 /dev/sdb1 //卷组名为vg0把sdb1加入进去
Volume group "vg0" successfully created
[root@localhost ~]# vgs //检查卷组
VG #PV #LV #SN Attr VSize VFree
rhel 1 3 0 wz--n- <99.00g 0
vg0 1 0 0 wz--n- <5.00g <5.00g //显示有一个5G的物理卷输入卷组vg0,这个5G是sdb1的内存大小
- 创建逻辑卷,分配名称,大小,指定卷组
[root@localhost ~]# lvcreate -n lv0 -L 4G vg0 //创建一个名为lv0的逻辑卷,大小为4G,指定卷组是vg0
Logical volume "lv0" created.
[root@localhost ~]# lvs //查看一下
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 50.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-a----- 4.00g //创建成功
[root@localhost ~]# ls /dev/vg0/lv0 //lvo的位置
/dev/vg0/lv0
[root@localhost ~]#
- 格式化文件系统
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=262144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1048576, 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
- 挂载并使用
[root@localhost ~]# blkid //查看UUID
/dev/sda1: UUID="013591bf-54fe-4a1a-ab2a-7d007887325e" TYPE="xfs" PARTUUID="4613967b-01"
/dev/sda2: UUID="g0fD0E-m6u6-uhfF-vkAI-KETA-VF5e-3jURNF" TYPE="LVM2_member" PARTUUID="4613967b-02"
/dev/sdc: UUID="A6Va9W-8eaL-IrxM-QupH-Z7ha-sxTv-srVN1g" TYPE="LVM2_member"
/dev/sdb1: UUID="9qHMQF-ri9i-23KW-66Ln-mXcM-KHTw-c5hlcM" TYPE="LVM2_member" PARTUUID="186a0127-01"
/dev/sdb2: PARTUUID="186a0127-02"
/dev/sdb3: PARTUUID="186a0127-03"
/dev/sr0: UUID="2020-04-04-08-21-15-00" LABEL="RHEL-8-2-0-BaseOS-x86_64" TYPE="iso9660" PTUUID="47055c33" PTTYPE="dos"
/dev/mapper/rhel-root: UUID="eaa45803-b92e-4622-9493-3b126d02e476" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="8bc12424-a40d-48aa-b163-dfac07e31685" TYPE="swap"
/dev/mapper/rhel-home: UUID="46e401ce-55e8-4c7f-9e52-6116ad90f07a" TYPE="xfs"
/dev/mapper/vg0-lv0: UUID="aa176617-e00d-4083-899e-d5e753058f50" TYPE="xfs" //复制vgo-lv0的UUID
[root@localhost ~]# mkdir /lvm //创建一个目录,等下挂载
[root@localhost ~]# mount UUID="aa176617-e00d-4083-899e-d5e753058f50" /lvm //用刚刚复制的UUID挂载到创建的lvm目录下
[root@localhost ~]# df -h //查看一下
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 4.0G 61M 4.0G 2% /lvm //已经挂载到lvm目录下
[root@localhost ~]# cd /lvm/ //进到lvm目录
[root@localhost lvm]# ls
[root@localhost lvm]# touch abc //创建一个abc文件
[root@localhost lvm]# ll //查看
total 0
-rw-r--r--. 1 root root 0 Dec 4 18:06 abc //创建成功,说明完成
[root@localhost lvm]#
上面那种挂载方式只是临时的,重启之后就会失效
永久挂载的方式演示如下:
- 修改配置文件
[root@localhost ~]# blkid //查看UUID
/dev/sda1: UUID="013591bf-54fe-4a1a-ab2a-7d007887325e" TYPE="xfs" PARTUUID="4613967b-01"
/dev/sda2: UUID="g0fD0E-m6u6-uhfF-vkAI-KETA-VF5e-3jURNF" TYPE="LVM2_member" PARTUUID="4613967b-02"
/dev/sdc: UUID="A6Va9W-8eaL-IrxM-QupH-Z7ha-sxTv-srVN1g" TYPE="LVM2_member"
/dev/sdb1: UUID="9qHMQF-ri9i-23KW-66Ln-mXcM-KHTw-c5hlcM" TYPE="LVM2_member" PARTUUID="186a0127-01"
/dev/sr0: UUID="2020-04-04-08-21-15-00" LABEL="RHEL-8-2-0-BaseOS-x86_64" TYPE="iso9660" PTUUID="47055c33" PTTYPE="dos"
/dev/mapper/rhel-root: UUID="eaa45803-b92e-4622-9493-3b126d02e476" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="8bc12424-a40d-48aa-b163-dfac07e31685" TYPE="swap"
/dev/mapper/rhel-home: UUID="46e401ce-55e8-4c7f-9e52-6116ad90f07a" TYPE="xfs"
/dev/mapper/vg0-lv0: UUID="aa176617-e00d-4083-899e-d5e753058f50" TYPE="xfs" //复制UUID
/dev/sdb2: PARTUUID="186a0127-02"
/dev/sdb3: PARTUUID="186a0127-03"
[root@localhost ~]# vi /etc/fstab //编辑fstab配置文件
#
# /etc/fstab
# Created by anaconda on Sat Nov 28 15:04:35 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=013591bf-54fe-4a1a-ab2a-7d007887325e /boot xfs defaults 0 0
/dev/mapper/rhel-home /home xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
UUID="aa176617-e00d-4083-899e-d5e753058f50" /lvm xfs defaults 0 0 //在文件最下面一行写上配置,挂载到lvm,文件系统类型xfs,挂载选项时defaults,0 0 代表不检查不备份
/*以上配置已经完成*/
- 已经配置完成,验证一下
[root@localhost ~]# umount /lvm //卸载lvm
[root@localhost ~]# df -h //检查一下
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
//没有显示挂载
[root@localhost ~]# mount -a //重新挂载
[root@localhost ~]# df -h //查看一下
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 4.0G 61M 4.0G 2% /lvm //挂载成功
[root@localhost ~]# lvscan //检查扫描逻辑卷
ACTIVE '/dev/vg0/lv0' [4.00 GiB] inherit
ACTIVE '/dev/rhel/swap' [<3.95 GiB] inherit
ACTIVE '/dev/rhel/home' [<45.05 GiB] inherit
ACTIVE '/dev/rhel/root' [50.00 GiB] inherit
[root@localhost ~]#
3.2 卷组管理
- 扩展卷组,将新磁盘加入卷组
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 1020.00m
/dev/sdc lvm2 --- 20.00g 20.00g
[root@localhost ~]# vgextend vg0 /dev/sdc //使用vgxtend把sdc扩展到vg0卷组
Volume group "vg0" successfully extended
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 1020.00m
/dev/sdc vg0 lvm2 a-- <20.00g <20.00g //属于vg0卷组
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 3 0 wz--n- <99.00g 0
vg0 2 1 0 wz--n- 24.99g 20.99g //有25G内存,空余20G
[root@localhost ~]#
- 缩减卷组,将指定磁盘从卷组中删除
注意:xfs格式不支持缩减
[root@localhost ~]# vgreduce vg0 /dev/sdb1 //把vg0的sb1迁移走
Removed "/dev/sdb1" from volume group "vg0"
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g 15.00g
[root@localhost ~]# pvremove /dev/sdb1 //移除sdb1
Labels on physical volume "/dev/sdb1" successfully wiped.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g 15.00g
[root@localhost ~]#
- 数据迁移卷组,同一卷组的磁盘才可以进行在线迁移
准备:
- 把sdd加到物理卷
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 0
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
[root@localhost ~]# pvcreate /dev/sdd
Physical volume "/dev/sdd" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 0
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd lvm2 --- 20.00g 20.00g
- 把sdd加到vg0卷组中去
[root@localhost ~]# vgextend vg0 /dev/sdd
Volume group "vg0" successfully extended
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 0
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g <20.00g
开始:
[root@localhost ~]# pvs //查看当前逻辑卷使用情况
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g 0
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g <20.00g
[root@localhost ~]# pvmove /dev/sdb1 //pvmove在线数据迁移至其他pv
/dev/sdb1: Moved: 0.86%
/dev/sdb1: Moved: 100.00%
[root@localhost ~]# pvs //检查迁移到sdd中去了,因为只有sdd有这么大内存,在线迁移不需要指定位置
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb1 vg0 lvm2 a-- <5.00g <5.00g
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g 15.00g
3.3 逻辑卷管理
3.3.1 逻辑卷扩展,逻辑卷的扩展取决于卷组的容量,逻辑卷扩展容量不能超过卷组的容量
- 扩展lv逻辑卷
- 增加6G分配给逻辑卷
[root@localhost ~]# lvextend -L 10G /dev/vg0/lv0 //这里-L后面跟的是10G,意思是扩展后是总的大小是10G
[root@localhost ~]# lvextend -L +6G /dev/vg0/lv0 //这里-L后面跟的是+6G,意思是在原本的大小上加6G
[root@localhost ~]# lvextend -L +6G /dev/vg0/lv0 //这里用的时+6G,意思是在原本的大小上加6G
Size of logical volume vg0/lv0 changed from 4.00 GiB (1024 extents) to 10.00 GiB (2560 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 50.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- 10.00g //原来分的4G 加了扩展的6G变成了10G
- 分配磁盘中多少百分比给逻辑卷
[root@localhost ~]# lvextend -l +50%free /dev/vg0/lv0 //用百分比的方式分配
Size of logical volume vg0/lv0 changed from 10.00 GiB (2560 extents) to <17.50 GiB (4479 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 50.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 10G 105M 9.9G 2% /lvm //没有生效
[root@localhost ~]# xfs_growfs /dev/vg0/lv0 //拉升一下xfs文件系统
meta-data=/dev/mapper/vg0-lv0 isize=512 agcount=10, agsize=262144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=2621440, 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
data blocks changed from 2621440 to 4586496
[root@localhost ~]# df -h //查看一下
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm//成功分配了18G,这里四舍五入了
[root@localhost ~]#
- 扩展fs文件系统
- xfs扩容
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 4.0G 61M 4.0G 2% /lvm //还是4G没有生效
[root@localhost ~]# df -hT //查看一下系统文件类型
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 xfs 4.0G 61M 4.0G 2% /lvm //是xfs格式
[root@localhost ~]# xfs_growfs /dev/vg0/lv0 //把xfs拉伸到最大化
meta-data=/dev/mapper/vg0-lv0 isize=512 agcount=4, agsize=262144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1048576, 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
data blocks changed from 1048576 to 2621440
[root@localhost ~]# df -h //查看一下
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 10G 105M 9.9G 2% /lvm //显示大小是10G,生效了。
[root@localhost ~]#
- ext4扩容
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 50.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
lv1 vg0 -wi-ao---- 6.00g
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
/dev/mapper/vg0-lv1 2.9G 9.0M 2.8G 1% /ext4 //没有生效
[root@localhost ~]# resize2fs /dev/vg0/lv1
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/vg0/lv1 is mounted on /ext4; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv1 is now 1572864 (4k) blocks long.
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
/dev/mapper/vg0-lv1 5.9G 12M 5.6G 1% /ext4 //显示扩充到6G
[root@localhost ~]#
如果根的磁盘空间不足可以用下面的方式进行扩充
[root@localhost ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb3 lvm2 --- <8.00g <8.00g
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g 15.00g
[root@localhost ~]# vgextend rhel /dev/sdb3
Volume group "rhel" successfully extended
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <99.00g 0
/dev/sdb3 rhel lvm2 a-- <8.00g <8.00g
/dev/sdc vg0 lvm2 a-- <20.00g <1.50g
/dev/sdd vg0 lvm2 a-- <20.00g 15.00g
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 2 3 0 wz--n- 106.99g <8.00g
vg0 2 2 0 wz--n- 39.99g <16.50g
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 50.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
lv1 vg0 -wi-ao---- 6.00g
[root@localhost ~]# lvextend -L +7G /dev/rhel/root
Size of logical volume rhel/root changed from 50.00 GiB (12800 extents) to 57.00 GiB (14592 extents).
Logical volume rhel/root successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 57.00g //显示添加了7G
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
lv1 vg0 -wi-ao---- 6.00g
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
/dev/mapper/vg0-lv1 5.9G 12M 5.6G 1% /ext4
[root@localhost ~]# xfs_growfs /dev/rhel/root
meta-data=/dev/mapper/rhel-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 14942208
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 57G 1.7G 56G 3% / //现在有57G内存
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
/dev/mapper/vg0-lv1 5.9G 12M 5.6G 1% /ext4
[root@localhost ~]#
3.3.2 对etx4文件系统的逻辑卷裁剪容量
- 首先自己创建一个逻辑卷作为裁剪的对象
- 如果已经挂载,必须先卸载
- 裁剪容量,必须是先检查文件系统
- 调整完毕后采取裁剪逻辑卷容量
- 强烈建议裁剪后,再次检测文件系统
- 挂载测试,如果成功,一般说明裁剪成功,文件没有损坏
演示如下:
[root@localhost ~]# umount /ext4/ //卸载ext4
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 57G 1.7G 56G 3% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
[root@localhost ~]# e2fsck -f /dev/vg0/lv1 //强制检查vg0/lv1文件系统
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv1: 13/393216 files (0.0% non-contiguous), 44144/1572864 blocks
[root@localhost ~]# resize2fs /dev/vg0/lv1 200M //缩减vg0/lv1为200M
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/vg0/lv1 to 51200 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 51200 (4k) blocks long. //可以缩减
[root@localhost ~]# lvreduce -L 200M /dev/vg0/lv1 //缩减为200M
WARNING: Reducing active logical volume to 200.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv1? [y/n]: y //输入y确认
Size of logical volume vg0/lv1 changed from 6.00 GiB (1536 extents) to 200.00 MiB (50 extents).
Logical volume vg0/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 57.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
lv1 vg0 -wi-a----- 200.00m //已经是200M了
[root@localhost ~]# e2fsck -f /dev/vg0/lv1 //检查文件系统
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv1: 13/8192 files (7.7% non-contiguous), 17292/17408 blocks
[root@localhost ~]# mount -a //挂载使用
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 57G 1.7G 56G 3% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/vg0-lv0 18G 159M 18G 1% /lvm
/dev/mapper/vg0-lv1 2.0M 1.6M 0 100% /ext4 //显示2M
[root@localhost ~]# lvs //查看lv1
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <45.05g
root rhel -wi-ao---- 57.00g
swap rhel -wi-ao---- <3.95g
lv0 vg0 -wi-ao---- <17.50g
lv1 vg0 -wi-ao---- 200.00m //显示为200m
pvdisplay命令 用于显示物理卷的属性。pvdisplay命令显示的物理卷信息包括:物理卷名称、所属的卷组、物理卷大小、PE大小、总PE数、可用PE数、已分配的PE数和UUID
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdc
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 1869
Allocated PE 3250
PV UUID A6Va9W-8eaL-IrxM-QupH-Z7ha-sxTv-srVN1g
--- Physical volume ---
PV Name /dev/sdd
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 3840
Allocated PE 1279
PV UUID yl0vBN-8JWp-lFyv-WCUT-zgAO-dK5M-VdXhbw
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size <99.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 25343
Free PE 0
Allocated PE 25343
PV UUID g0fD0E-m6u6-uhfF-vkAI-KETA-VF5e-3jURNF
--- Physical volume ---
PV Name /dev/sdb3
VG Name rhel
PV Size <8.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2047
Free PE 255
Allocated PE 1792
PV UUID kymAJa-bJ8P-SInz-9I0D-YdM7-jZdD-EcK4x9
vgdisplay命令 用于显示LVM卷组的信息。如果不指定”卷组”参数,则分别显示所有卷组的属性
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 13
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 4529 / 17.69 GiB
Free PE / Size 5709 / 22.30 GiB
VG UUID Wy01NV-aREZ-p67Y-TpYP-iZFM-FGlD-0kh9OS
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 106.99 GiB
PE Size 4.00 MiB
Total PE 27390
Alloc PE / Size 27135 / <106.00 GiB
Free PE / Size 255 / 1020.00 MiB
VG UUID kBk7Kj-wdPr-QA7i-92Uj-k9KP-CeTW-4iSpHs
lvdisplay命令用于显示LVM逻辑卷空间大小、读写状态和快照信息等属性
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID 6JtKrA-GVNs-E0Cv-GPup-vY7y-C0rO-cI3QGj
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-12-04 18:01:48 +0800
LV Status available
# open 1
LV Size <17.50 GiB
Current LE 4479
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
--- Logical volume ---
LV Path /dev/vg0/lv1
LV Name lv1
VG Name vg0
LV UUID xUL303-wKDv-tiYm-Y1Jy-Z7nd-eTZs-RP4MJP
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-12-04 19:03:36 +0800
LV Status available
# open 1
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
--- Logical volume ---
LV Path /dev/rhel/swap
LV Name swap
VG Name rhel
LV UUID 0nWsrm-MxVb-5LPx-1uVE-kwmT-MaW5-eHYpBx
LV Write Access read/write
LV Creation host, time localhost, 2020-11-28 15:04:33 +0800
LV Status available
# open 2
LV Size <3.95 GiB
Current LE 1011
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/rhel/home
LV Name home
VG Name rhel
LV UUID oihA6s-sPs5-50sc-6xla-3dx3-XDLV-omVerY
LV Write Access read/write
LV Creation host, time localhost, 2020-11-28 15:04:33 +0800
LV Status available
# open 1
LV Size <45.05 GiB
Current LE 11532
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/rhel/root
LV Name root
VG Name rhel
LV UUID Tv475v-7djx-YFQO-r1f9-OGMW-fePM-vJCtES
LV Write Access read/write
LV Creation host, time localhost, 2020-11-28 15:04:33 +0800
LV Status available
# open 1
LV Size 57.00 GiB
Current LE 14592
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@localhost ~]#