LVM逻辑卷管理测试——逻辑卷扩展、收缩、快照及删除
一、逻辑卷扩展
[root@lxjtest /]# umount /testLVM/ [root@lxjtest /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 3.7G 896M 2.9G 24% / devtmpfs 910M 0 910M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.4M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 297M 114M 184M 39% /boot tmpfs 184M 0 184M 0% /run/user/0 [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, 2017-08-07 23:35:10 -0400 LV Status available # open 0 LV Size 200.00 MiB Current LE 50 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 [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 (50 extents) to 300.00 MiB (75 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.9 (28-Dec-2013) 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/testVG/fuck_lv1: 12/51200 files (0.0% non-contiguous), 12116/204800 blocks [root@lxjtest /]# resize2fs /dev/testVG/fuck_lv1 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/testVG/fuck_lv1 to 307200 (1k) blocks. The filesystem on /dev/testVG/fuck_lv1 is now 307200 blocks long.
第3步:重新挂载硬盘设备并查看挂载状态
[root@lxjtest /]# mount -a [root@lxjtest /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 3.7G 897M 2.9G 24% / devtmpfs 910M 0 910M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.4M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 297M 114M 184M 39% /boot tmpfs 184M 0 184M 0% /run/user/0 /dev/mapper/testVG-fuck_lv1 287M 2.1M 266M 1% /testLVM
二、逻辑卷收缩
缩小逻辑卷之前,先备份数据,然后检查文件系统完整性,再卸载文件系统,之后再来压缩。
[root@lxjtest /]# umount /testLVM/ [root@lxjtest /]# e2fsck -f /dev/testVG/fuck_lv1 e2fsck 1.42.9 (28-Dec-2013) 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/testVG/fuck_lv1: 12/77824 files (0.0% non-contiguous), 15988/307200 blocks [root@lxjtest /]# resize2fs /dev/testVG/fuck_lv1 100M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/testVG/fuck_lv1 to 102400 (1k) blocks. The filesystem on /dev/testVG/fuck_lv1 is now 102400 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 (75 extents) to 100.00 MiB (25 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 3.7G 896M 2.9G 24% / devtmpfs 910M 0 910M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.4M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 297M 114M 184M 39% /boot tmpfs 184M 0 184M 0% /run/user/0 /dev/mapper/testVG-fuck_lv1 93M 1.6M 85M 2% /testLVM
三、逻辑卷快照
LVM逻辑卷管理器还具备有“快照卷”的功能,这项功能很类似于虚拟机软件的还原时间点功能。例如可以对某一个LV逻辑卷设备做一次快照,如果今后发现数据被改错了,咱们可以把之前做好的快照卷进行覆盖还原,LVM逻辑卷管理器的快照功能有两项特点,第一是快照卷的大小应该尽量等同于LV逻辑卷的容量,第二是快照功能仅一次有效,一旦被还原后则会被自动立即删除,首先应当查看下卷组的信息:
[root@lxjtest testLVM]# ls -lht total 46M drwx------. 2 root root 12K Aug 7 23:51 lost+found -rw-r--r--. 1 root root 46M Apr 5 2015 firefox-37.0.1.tar.bz2 [root@lxjtest testLVM]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 3.7G 898M 2.9G 24% / devtmpfs 910M 0 910M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.4M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 297M 114M 184M 39% /boot tmpfs 184M 0 184M 0% /run/user/0 /dev/mapper/testVG-fuck_lv1 93M 48M 39M 55% /testLVM /dev/sr0 3.8G 3.8G 0 100% /mnt
在创建快照前,先在/testLVM中放入一些文件,以对比快照还原结果
[root@lxjtest testLVM]# pwd /testLVM [root@lxjtest testLVM]# ls -lht total 46M drwx------. 2 root root 12K Aug 7 23:51 lost+found -rw-r--r--. 1 root root 46M Apr 5 2015 firefox-37.0.1.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, 2017-08-07 23:35:10 -0400 LV snapshot status source of SNAP [active] LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- 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, 2017-08-08 04:20:52 -0400 LV snapshot status active destination for fuck_lv1 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 COW-table size 100.00 MiB COW-table LE 25 Allocated to snapshot 0.09% Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:5 (略)
快照创建完成之后,删除之前在/testLVM中放入的文件。下面我们使用快照卷SNAP来还原之前的文件。
第2步:为了校验SNAP快照卷的效果,需要对逻辑卷进行快照合并还原操作,在这之前记得先卸载掉逻辑卷设备与目录的挂载~
[root@lxjtest /]# cd testLVM/ [root@lxjtest testLVM]#rm firefox-37.0.1.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.1.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, 2017-08-07 23:35:10 -0400 LV Status available # open 1 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 (略) [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 1 04:28:47 2017 # # 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 # /dev/mapper/rhel-root / xfs defaults 0 0 UUID=e7987771-c54c-4b36-8a5c-8e71f129c3fe /boot xfs defaults 0 0 /dev/mapper/rhel-swap swap swap defaults 0 0 #/dev/testVG/fuck_lv1 /testLVM ext4 defaults 0 0 #删除或注释该行
第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, 2017-08-07 23:35:10 -0400 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 (略) [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 2 Metadata Sequence No 10 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 0 / 0 Free PE / Size 1022 / 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 1204 Free PE 0 Allocated PE 1204 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 0 Total PE 0 Free PE 0 Allocated PE 0 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 0 Total PE 0 Free PE 0 Allocated PE 0 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命令来查看逻辑卷管理器信息,操作正确则会不能再看到我们创建的逻辑卷设备信息了。