LVM逻辑卷管理测试——逻辑卷扩展、收缩、快照及删除

时间:2023-03-08 17:40:46
LVM逻辑卷管理测试——逻辑卷扩展、收缩、快照及删除

一、逻辑卷扩展

[root@lxjtest /]# umount /testLVM/
[root@lxjtest /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root .7G 896M .9G % /
devtmpfs 910M 910M % /dev
tmpfs 920M 920M % /dev/shm
tmpfs 920M 8.4M 912M % /run
tmpfs 920M 920M % /sys/fs/cgroup
/dev/sda1 297M 114M 184M % /boot
tmpfs 184M 184M % /run/user/
[root@lxjtest /]#

第1步:把上个试验中的逻辑卷fuck_lv1扩展至300MB

[root@lxjtest /]# lvdisplay testVG
--- Logical volume ---
LV Path /dev/testVG/fuck_lv1
LV Name fuck_lv1
VG Name testVG
LV UUID RKzHdO-NX2i-Za40-kWNg-RIox-yi9j-z9251R
LV Write Access read/write
LV Creation host, time lxjtest.rusky.com, -- :: -
LV Status available
# open
LV Size 200.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : [root@lxjtest /]# lvs testVG
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
fuck_lv1 testVG -wi-a----- 200.00m
[root@lxjtest /]# lvextend -L 300M /dev/testVG/fuck_lv1
Size of logical volume testVG/fuck_lv1 changed from 200.00 MiB ( extents) to 300.00 MiB ( extents).
Logical volume fuck_lv1 successfully resized.
[root@lxjtest /]# lvs testVG
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
fuck_lv1 testVG -wi-a----- 300.00m
[root@lxjtest /]#

第2步:检查磁盘容量,重置磁盘容量

[root@lxjtest /]# e2fsck -f /dev/testVG/fuck_lv1
e2fsck 1.42. (-Dec-)
Pass : Checking inodes, blocks, and sizes
Pass : Checking directory structure
Pass : Checking directory connectivity
Pass : Checking reference counts
Pass : Checking group summary information
/dev/testVG/fuck_lv1: / files (0.0% non-contiguous), / blocks
[root@lxjtest /]# resize2fs /dev/testVG/fuck_lv1
resize2fs 1.42. (-Dec-)
Resizing the filesystem on /dev/testVG/fuck_lv1 to (1k) blocks.
The filesystem on /dev/testVG/fuck_lv1 is now blocks long.

第3步:重新挂载硬盘设备并查看挂载状态

[root@lxjtest /]# mount -a
[root@lxjtest /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root .7G 897M .9G % /
devtmpfs 910M 910M % /dev
tmpfs 920M 920M % /dev/shm
tmpfs 920M 8.4M 912M % /run
tmpfs 920M 920M % /sys/fs/cgroup
/dev/sda1 297M 114M 184M % /boot
tmpfs 184M 184M % /run/user/
/dev/mapper/testVG-fuck_lv1 287M 2.1M 266M % /testLVM

二、逻辑卷收缩

缩小逻辑卷之前,先备份数据,然后检查文件系统完整性,再卸载文件系统,之后再来压缩。

[root@lxjtest /]# umount /testLVM/
[root@lxjtest /]# e2fsck -f /dev/testVG/fuck_lv1
e2fsck 1.42. (-Dec-)
Pass : Checking inodes, blocks, and sizes
Pass : Checking directory structure
Pass : Checking directory connectivity
Pass : Checking reference counts
Pass : Checking group summary information
/dev/testVG/fuck_lv1: / files (0.0% non-contiguous), / blocks
[root@lxjtest /]# resize2fs /dev/testVG/fuck_lv1 100M
resize2fs 1.42. (-Dec-)
Resizing the filesystem on /dev/testVG/fuck_lv1 to (1k) blocks.
The filesystem on /dev/testVG/fuck_lv1 is now blocks long. [root@lxjtest /]# lvreduce -L 100M /dev/testVG/fuck_lv1
WARNING: Reducing active logical volume to 100.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce fuck_lv1? [y/n]: y
Size of logical volume testVG/fuck_lv1 changed from 300.00 MiB ( extents) to 100.00 MiB ( extents).
Logical volume fuck_lv1 successfully resized.
[root@lxjtest /]# lvs testVG
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
fuck_lv1 testVG -wi-a----- 100.00m
[root@lxjtest /]#

最后,重新挂载并查看文件系统:

[root@lxjtest /]# mount -a
[root@lxjtest /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root .7G 896M .9G % /
devtmpfs 910M 910M % /dev
tmpfs 920M 920M % /dev/shm
tmpfs 920M 8.4M 912M % /run
tmpfs 920M 920M % /sys/fs/cgroup
/dev/sda1 297M 114M 184M % /boot
tmpfs 184M 184M % /run/user/
/dev/mapper/testVG-fuck_lv1 93M 1.6M 85M % /testLVM

三、逻辑卷快照

LVM逻辑卷管理器还具备有“快照卷”的功能,这项功能很类似于虚拟机软件的还原时间点功能。例如可以对某一个LV逻辑卷设备做一次快照,如果今后发现数据被改错了,咱们可以把之前做好的快照卷进行覆盖还原,LVM逻辑卷管理器的快照功能有两项特点,第一是快照卷的大小应该尽量等同于LV逻辑卷的容量,第二是快照功能仅一次有效,一旦被还原后则会被自动立即删除,首先应当查看下卷组的信息:

[root@lxjtest testLVM]# ls -lht
total 46M
drwx------. root root 12K Aug : lost+found
-rw-r--r--. root root 46M Apr firefox-37.0..tar.bz2
[root@lxjtest testLVM]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root .7G 898M .9G % /
devtmpfs 910M 910M % /dev
tmpfs 920M 920M % /dev/shm
tmpfs 920M 8.4M 912M % /run
tmpfs 920M 920M % /sys/fs/cgroup
/dev/sda1 297M 114M 184M % /boot
tmpfs 184M 184M % /run/user/
/dev/mapper/testVG-fuck_lv1 93M 48M 39M % /testLVM
/dev/sr0 .8G .8G % /mnt

在创建快照前,先在/testLVM中放入一些文件,以对比快照还原结果

[root@lxjtest testLVM]# pwd
/testLVM
[root@lxjtest testLVM]# ls -lht
total 46M
drwx------. root root 12K Aug : lost+found
-rw-r--r--. root root 46M Apr firefox-37.0..tar.bz2

第1步:使用-s参数来生成一个快照卷,使用-L参数来指定切割的大小,另外要记得在后面写上这个快照是针对那个LV逻辑卷设备做的。

[root@lxjtest testLVM]# lvcreate -L 100M -s -n SNAP /dev/testVG/fuck_lv1
Logical volume "SNAP" created.
  --- Logical volume ---
LV Path /dev/testVG/fuck_lv1
LV Name fuck_lv1
VG Name testVG
LV UUID RKzHdO-NX2i-Za40-kWNg-RIox-yi9j-z9251R
LV Write Access read/write
LV Creation host, time lxjtest.rusky.com, -- :: -
LV snapshot status source of
SNAP [active]
LV Status available
# open
LV Size 100.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : --- Logical volume ---
LV Path /dev/testVG/SNAP
LV Name SNAP
VG Name testVG
LV UUID 7eeuaw-FMmH-KA1o-45CG-0x4J-B3tK-AOJqFv
LV Write Access read/write
LV Creation host, time lxjtest.rusky.com, -- :: -
LV snapshot status active destination for fuck_lv1
LV Status available
# open
LV Size 100.00 MiB
Current LE
COW-table size 100.00 MiB
COW-table LE
Allocated to snapshot 0.09%
Snapshot chunk size 4.00 KiB
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device :
(略)

快照创建完成之后,删除之前在/testLVM中放入的文件。下面我们使用快照卷SNAP来还原之前的文件。

第2步:为了校验SNAP快照卷的效果,需要对逻辑卷进行快照合并还原操作,在这之前记得先卸载掉逻辑卷设备与目录的挂载~

[root@lxjtest /]# cd testLVM/
[root@lxjtest testLVM]#rm firefox-37.0..tar.bz2 --删除原文件
[root@lxjtest testLVM]#touch testNewFile --创建新文件
[root@lxjtest testLVM]# ls
lost+found tesstNewFile
[root@lxjtest testLVM]# cd /
[root@lxjtest /]# umount /testLVM/ --卸载
[root@lxjtest /]# lvconvert --merge /dev/testVG/SNAP --快照还原
Merging of volume SNAP started.
fuck_lv1: Merged: 99.9%
fuck_lv1: Merged: 100.0%
[root@lxjtest /]# mount -a
[root@lxjtest /]# cd testLVM/
[root@lxjtest testLVM]# ls --原文件已被还原出来。
firefox-37.0..tar.bz2 lost+found

lvconvert之后,快照卷/dev/testVG/SNAP会被自动删除掉,并且刚刚在逻辑卷设备被快照后再创建出来的100M垃圾文件也被清除了。

[root@lxjtest testLVM]# lvdisplay
--- Logical volume ---
LV Path /dev/testVG/fuck_lv1
LV Name fuck_lv1
VG Name testVG
LV UUID RKzHdO-NX2i-Za40-kWNg-RIox-yi9j-z9251R
LV Write Access read/write
LV Creation host, time lxjtest.rusky.com, -- :: -
LV Status available
# open
LV Size 100.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device :
(略)
[root@lxjtest testLVM]#

四、删除逻辑卷

当生产环境中想要重新部署或者不需要再继续使用LVM逻辑卷管理器了,除了提前备份好重要数据信息,还必须依次删除LV逻辑卷、VG卷组后再移除PV物理卷设备,这样的顺序不可颠倒。

第1步:取消逻辑卷与目录的挂载关联,删除配置文件中永久生效的设备参数。

[root@lxjtest ~]# umount /testLVM/
[root@lxjtest ~]# vi /etc/fstab #
# /etc/fstab
# Created by anaconda on Tue Aug ::
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(), findfs(), mount() and/or blkid() for more info
#
/dev/mapper/rhel-root / xfs defaults
UUID=e7987771-c54c-4b36-8a5c-8e71f129c3fe /boot xfs defaults
/dev/mapper/rhel-swap swap swap defaults
#/dev/testVG/fuck_lv1 /testLVM ext4 defaults
#删除或注释该行

第2步:把LV逻辑卷设备删除,需要手工输入y来确认操作

[root@lxjtest ~]# lvdisplay
--- Logical volume ---
LV Path /dev/testVG/fuck_lv1
LV Name fuck_lv1
VG Name testVG
LV UUID RKzHdO-NX2i-Za40-kWNg-RIox-yi9j-z9251R
LV Write Access read/write
LV Creation host, time lxjtest.rusky.com, -- :: -
LV Status available
# open
LV Size 100.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device :
(略)
[root@lxjtest ~]# lvremove /dev/testVG/fuck_lv1
Do you really want to remove active logical volume fuck_lv1? [y/n]: y
Logical volume "fuck_lv1" successfully removed
[root@lxjtest ~]#

第3步:把VG卷组删除,此处只需写卷组名称即可,而无需设备完整绝对路径

[root@lxjtest ~]# vgdisplay
--- Volume group ---
VG Name testVG
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 3.99 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / 3.99 GiB
VG UUID b3mykq-kcsp-Hdh4-rAos-vutt-ontI-6AWJ5K
(略)
[root@lxjtest ~]#
[root@lxjtest ~]# vgremove testVG
Volume group "testVG" successfully removed
[root@lxjtest ~]#

第4步:把PV物理卷设备移除:

[root@lxjtest ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size 4.71 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE
Free PE
Allocated PE
PV UUID V3kA1X-l7dr-03p4-HZcu-Ebj3-FRvb-r5vGMd "/dev/sdb" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size
Total PE
Free PE
Allocated PE
PV UUID InfACr-fq1t-yi95-K1K3-dOHU-uezl-gfiPVa "/dev/sdc" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size
Total PE
Free PE
Allocated PE
PV UUID aOtJga-uWop-ldnv-xIcj-TcLE-aq18-SrJO3p [root@lxjtest ~]# pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped
Labels on physical volume "/dev/sdc" successfully wiped
[root@lxjtest ~]#

可以再分别执行下lvdisplay、vgdisplay、pvdisplay命令来查看逻辑卷管理器信息,操作正确则会不能再看到我们创建的逻辑卷设备信息了。