逻辑卷扩容和缩容及快照管理
1.给逻辑卷扩容:
首先在扩容前先确认卷组空间是否足够
[root@centos8 ~ 844]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 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 14.99 GiB PE Size 4.00 MiB Total PE 3838 Alloc PE / Size 381 / <1.49 GiB Free PE / Size 3457 / 13.50 GiB VG UUID iZQ0ry-tWHh-E20d-eTsZ-uQxb-H2g7-F0LiPr
卷组空间够就可以扩展了:
[root@centos8 ~ 845]#lvextend -l +50%free /dev/vg0/mysql Size of logical volume vg0/mysql changed from 1.00 GiB (256 extents) to 7.75 GiB (1985 extents). Logical volume vg0/mysql successfully resized. [root@centos8 ~ 849]#lvdisplay --- Logical volume --- LV Path /dev/vg0/mysql LV Name mysql VG Name vg0 LV UUID nXiN6D-zty4-jYjy-jG8A-1PUQ-VKDj-9u5WEW LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-16 10:46:49 +0800 LV Status available # open 1 LV Size 7.75 GiB Current LE 1985 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 --- Logical volume --- LV Path /dev/vg0/log LV Name log VG Name vg0 LV UUID fwOdMW-ZYRx-Vplp-rik6-CIxo-8Az1-X1PsVr LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-16 10:56:18 +0800 LV Status available # open 1 LV Size 500.00 MiB Current LE 125 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
但是我们看我们硬盘空间大小:
[root@centos8 ~ 848]#df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 869M 0 869M 0% /dev tmpfs tmpfs 896M 0 896M 0% /dev/shm tmpfs tmpfs 896M 9.5M 886M 2% /run tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup /dev/nvme0n1p2 xfs 50G 6.1G 44G 13% / /dev/nvme0n1p1 xfs 1014M 338M 677M 34% /boot /dev/nvme0n1p3 xfs 30G 251M 30G 1% /data tmpfs tmpfs 180M 28K 179M 1% /run/user/0 /dev/sr0 iso9660 11G 11G 0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd /dev/mapper/vg0-mysql ext4 976M 2.6M 907M 1% /mnt/mysql /dev/mapper/vg0-log xfs 495M 29M 466M 6% /mnt/log
发现并没有变化,但是我们明明已经扩容了,这是为什么?其实这里看到的1个G是有文件系统的1个G,也就是说我们虽然扩容了,但是扩容的这部分空间相当是没有文件系统的,所以df就看不见,df看见的是有文件系统的。所以,我们要把新扩容的文件系统创建出来才能完成真正的扩容。
扩容ext4文件系统:
[root@centos8 ~ 850]#resize2fs /dev/vg0/mysql resize2fs 1.45.6 (20-Mar-2020) Filesystem at /dev/vg0/mysql is mounted on /mnt/mysql; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/vg0/mysql is now 2032640 (4k) blocks long.
resize2fs仅仅应用于同步ext文件系统,我们现在再用df来看:
[root@centos8 ~ 851]#df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 869M 0 869M 0% /dev tmpfs tmpfs 896M 0 896M 0% /dev/shm tmpfs tmpfs 896M 9.5M 886M 2% /run tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup /dev/nvme0n1p2 xfs 50G 6.1G 44G 13% / /dev/nvme0n1p1 xfs 1014M 338M 677M 34% /boot /dev/nvme0n1p3 xfs 30G 251M 30G 1% /data tmpfs tmpfs 180M 28K 179M 1% /run/user/0 /dev/sr0 iso9660 11G 11G 0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd /dev/mapper/vg0-mysql ext4 7.6G 4.5M 7.3G 1% /mnt/mysql /dev/mapper/vg0-log xfs 495M 29M 466M 6% /mnt/log
所以,总的来说扩容就两条命令:第一步,先扩容逻辑卷;第二步,同步文件系统。
但是我们上述做法不太灵话,比方我们将逻辑卷/dev/vg0/log扩容:
第一步依旧是把空间扩容:
[root@centos8 ~ 852]#lvextend -L +1G /dev/vg0/log Size of logical volume vg0/log changed from 500.00 MiB (125 extents) to <1.49 GiB (381 extents). Logical volume vg0/log successfully resized.
第二步将新扩容的空间同步xfs文件系统:
[root@centos8 ~ 853]#xfs_growfs /dev/vg0/log meta-data=/dev/mapper/vg0-log isize=512 agcount=4, agsize=32000 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=128000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=1368, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 128000 to 390144 [root@centos8 ~ 854]#df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 869M 0 869M 0% /dev tmpfs tmpfs 896M 0 896M 0% /dev/shm tmpfs tmpfs 896M 9.5M 886M 2% /run tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup /dev/nvme0n1p2 xfs 50G 6.1G 44G 13% / /dev/nvme0n1p1 xfs 1014M 338M 677M 34% /boot /dev/nvme0n1p3 xfs 30G 251M 30G 1% /data tmpfs tmpfs 180M 28K 179M 1% /run/user/0 /dev/sr0 iso9660 11G 11G 0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd /dev/mapper/vg0-mysql ext4 7.6G 4.5M 7.3G 1% /mnt/mysql /dev/mapper/vg0-log xfs 1.5G 37M 1.5G 3% /mnt/log
我们扩容xfs文件系统命令则是xfs_growfs后加逻辑卷。
2.我们有没有一样的方法给不同的文件系统扩容:
先查看卷组空间:
[root@centos8 ~ 855]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 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 14.99 GiB PE Size 4.00 MiB Total PE 3838 Alloc PE / Size 2366 / 9.24 GiB Free PE / Size 1472 / 5.75 GiB VG UUID iZQ0ry-tWHh-E20d-eTsZ-uQxb-H2g7-F0LiPr
我们这次将剩余的空间全部扩容:
[root@centos8 ~ 857]#lvextend -r -l +100%free /dev/vg0/mysql Size of logical volume vg0/mysql changed from 7.75 GiB (1985 extents) to 13.50 GiB (3457 extents). Logical volume vg0/mysql successfully resized. resize2fs 1.45.6 (20-Mar-2020) Filesystem at /dev/mapper/vg0-mysql is mounted on /mnt/mysql; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/mapper/vg0-mysql is now 3539968 (4k) blocks long
lvextend -r这个命令可以将扩容的空间直接同步文件系统。
我们分别用df和lvdisplay查看:
[root@centos8 ~ 858]#df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 869M 0 869M 0% /dev tmpfs tmpfs 896M 0 896M 0% /dev/shm tmpfs tmpfs 896M 9.5M 886M 2% /run tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup /dev/nvme0n1p2 xfs 50G 6.1G 44G 13% / /dev/nvme0n1p1 xfs 1014M 338M 677M 34% /boot /dev/nvme0n1p3 xfs 30G 251M 30G 1% /data tmpfs tmpfs 180M 28K 179M 1% /run/user/0 /dev/sr0 iso9660 11G 11G 0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd /dev/mapper/vg0-mysql ext4 14G 5.0M 13G 1% /mnt/mysql /dev/mapper/vg0-log xfs 1.5G 37M 1.5G 3% /mnt/log [root@centos8 ~ 859]#lvdisplay --- Logical volume --- LV Path /dev/vg0/mysql LV Name mysql VG Name vg0 LV UUID nXiN6D-zty4-jYjy-jG8A-1PUQ-VKDj-9u5WEW LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-16 10:46:49 +0800 LV Status available # open 1 LV Size 13.50 GiB Current LE 3457 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 --- Logical volume --- LV Path /dev/vg0/log LV Name log VG Name vg0 LV UUID fwOdMW-ZYRx-Vplp-rik6-CIxo-8Az1-X1PsVr LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-16 10:56:18 +0800 LV Status available # open 1 LV Size <1.49 GiB Current LE 381 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
所以lvcreate -r这个命令就省事了我们就不需要再关注是什么文件系统了,我们可以一步到位了。
3.现在卷组满了,我们扩建一下卷组:
我们需要增加物理卷到卷组
第一步创建分区:
[root@centos8 ~ 861]#fdisk /dev/sda 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. Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): 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): Created a new partition 2 of type 'Linux' and of size 15 GiB. Command (m for help): p Disk /dev/sda: 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: 0xec965e61 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 10487807 10485760 5G 8e Linux LVM /dev/sda2 10487808 41943039 31455232 15G 83 Linux Command (m for help): w The partition table has been altered. Syncing disks. [root@centos8 ~ 862]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 5G 0 part │ ├─vg0-mysql 253:0 0 13.5G 0 lvm /mnt/mysql │ └─vg0-log 253:1 0 1.5G 0 lvm /mnt/log └─sda2 8:2 0 15G 0 part sdb 8:16 0 10G 0 disk ├─vg0-mysql 253:0 0 13.5G 0 lvm /mnt/mysql └─vg0-log 253:1 0 1.5G 0 lvm /mnt/log sr0 11:0 1 10.1G 0 rom /run/media/root/CentOS-8-5-2111-x86_64-dvd nvme0n1 259:0 0 200G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot ├─nvme0n1p2 259:2 0 50G 0 part / ├─nvme0n1p3 259:3 0 30G 0 part /data ├─nvme0n1p4 259:4 0 1K 0 part └─nvme0n1p5 259:5 0 4G 0 part [SWAP]
第二步,由于我们第一步忘记改类型了所以将刚刚建立的分区类型改为lvm:
[root@centos8 ~ 863]#fdisk /dev/sda 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. Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): p Disk /dev/sda: 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: 0xec965e61 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 10487807 10485760 5G 8e Linux LVM /dev/sda2 10487808 41943039 31455232 15G 8e Linux LVM Command (m for help): w The partition table has been altered. Syncing disks.
第三步,将其变为物理卷:
[root@centos8 ~ 864]#pvcreate /dev/sda2 Physical volume "/dev/sda2" successfully created. [root@centos8 ~ 865]#pvs PV VG Fmt Attr PSize PFree /dev/sda1 vg0 lvm2 a-- <5.00g 0 /dev/sda2 lvm2 --- <15.00g <15.00g /dev/sdb vg0 lvm2 a-- <10.00g 0
第四步,将其加到vg0卷组中,用vgextend:
[root@centos8 ~ 866]#vgextend vg0 /dev/sda2 Volume group "vg0" successfully extended [root@centos8 ~ 867]#vgs VG #PV #LV #SN Attr VSize VFree vg0 3 2 0 wz--n- <29.99g <15.00g [root@centos8 ~ 868]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 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 3838 / 14.99 GiB Free PE / Size 3839 / <15.00 GiB VG UUID iZQ0ry-tWHh-E20d-eTsZ-uQxb-H2g7-F0LiPr
这就是扩建卷组的过程。
4.缩容逻辑卷,缩容必须离线且很危险可能会造成数据丢失
强烈建议缩容前备份!!!
还要注意一个:xfs不支持缩容,ext系列才支持缩容。
第一步,取消挂载:
[root@centos8 /etc 875]#umount /mnt/mysql
第二步,对其文件系统做检查:
[root@centos8 /etc 876]#fsck -f /dev/vg0/mysql fsck from util-linux 2.32.1 e2fsck 1.45.6 (20-Mar-2020) 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/mapper/vg0-mysql: 11/892928 files (0.0% non-contiguous), 65514/3539968 blocks
第三步,缩减文件系统:
[root@centos8 /etc 877]#resize2fs /dev/vg0/mysql 2G resize2fs 1.45.6 (20-Mar-2020) Resizing the filesystem on /dev/vg0/mysql to 524288 (4k) blocks. The filesystem on /dev/vg0/mysql is now 524288 (4k) blocks long.
第四步,缩减逻辑卷:
[root@centos8 /etc 878]#lvreduce -L 2G /dev/vg0/mysql WARNING: Reducing active logical volume to 2.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg0/mysql? [y/n]: y Size of logical volume vg0/mysql changed from 13.50 GiB (3457 extents) to 2.00 GiB (512 extents). Logical volume vg0/mysql successfully resized.
第五步,挂回去:
[root@centos8 /etc 879]#mount -a [root@centos8 /etc 880]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 5G 0 part │ ├─vg0-mysql 253:0 0 2G 0 lvm /mnt/mysql │ └─vg0-log 253:1 0 1.5G 0 lvm /mnt/log └─sda2 8:2 0 15G 0 part sdb 8:16 0 10G 0 disk └─vg0-log 253:1 0 1.5G 0 lvm /mnt/log sr0 11:0 1 10.1G 0 rom /run/media/root/CentOS-8-5-2111-x86_64-dvd nvme0n1 259:0 0 200G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot ├─nvme0n1p2 259:2 0 50G 0 part / ├─nvme0n1p3 259:3 0 30G 0 part /data ├─nvme0n1p4 259:4 0 1K 0 part └─nvme0n1p5 259:5 0 4G 0 part [SWAP] [root@centos8 /etc 881]#vgs VG #PV #LV #SN Attr VSize VFree vg0 3 2 0 wz--n- <29.99g 26.50g [root@centos8 /etc 882]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert log vg0 -wi-ao---- <1.49g mysql vg0 -wi-ao---- 2.00g
5.移除一个物理卷:
前提:PE个数小于其他成员剩余PE个数
第一步,将其PE移走到卷组其它成员:
[root@centos8 ~ 886]#pvmove /dev/sdb /dev/sdb: Moved: 3.52% /dev/sdb: Moved: 100.00%
第二步,从VG中删掉:
[root@centos8 ~ 888]#vgreduce vg0 /dev/sdb Removed "/dev/sdb" from volume group "vg0" [root@centos8 ~ 889]#vgs VG #PV #LV #SN Attr VSize VFree vg0 2 2 0 wz--n- 19.99g 16.50g
第三步,从PV中去除:
[root@centos8 ~ 890]#pvremove /dev/sdb Labels on physical volume "/dev/sdb" successfully wiped. [root@centos8 ~ 891]#pvs PV VG Fmt Attr PSize PFree /dev/sda1 vg0 lvm2 a-- <5.00g <1.51g /dev/sda2 vg0 lvm2 a-- <15.00g <15.00g
6.逻辑卷快照
1)快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝,采用COW技术(copy-on-write)。对于需要备份或者复制的现有数据临时拷贝以及其他操作来说,快照是最合适的选择。快照只有在它们和原来的逻辑卷不同时才会消耗空间。
2)在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变才会使用这些空间。当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。快照中只含有原来逻辑卷中更改的数据或者自生成快照后的快照更改的数据。建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可以使用lvextend放大快照。
3)快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移到快照区,没有改动的区域则由快照区和文件系统共享。
4)由于快照区与原本的LV共用很多PE的区块,因此快照与被快照的LV必须在同一个VG中。系统恢复的时候的文件数量不能高于快照区的实际容量。快照实际上也是一个逻辑卷,所以创建快照卷前,要保证现有LVM系统中VG由足够的空间。
5)最后强烈建议,如非临时需要,不要使用LVM的快照,因为它采用的时COW技术,创建一个快照,无论是对原LV还是对快照LV的IO写性能损失是巨大的,随着快照数量增多,写性能还将急剧下降。
7.创建ext文件系统快照:
第一步,创建快照卷,用lvcreate -s
[root@centos8 ~ 898]#lvcreate -s -n mysql-snapshot -L 200M /dev/vg0/mysql -p r Logical volume "mysql-snapshot" created. [root@centos8 ~ 899]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert log vg0 -wi-ao---- <1.49g mysql vg0 owi-aos--- 2.00g mysql-snapshot vg0 swi-a-s--- 200.00m mysql 0.01
第二步,挂载:
[root@centos8 ~ 904]#mkdir /mnt/snap [root@centos8 ~ 905]#mount /dev/vg0/mysql-snapshot /mnt/snap [root@centos8 ~ 906]#ls /mnt/snap f1.txt f2.txt f3.txt lost+found [root@centos8 ~ 907]#ls /mnt/mysql f1.txt f2.txt f3.txt lost+found
8.使用ext文件系统快照:
先修改逻辑卷内容:
[root@centos8 ~ 908]#vim /mnt/mysql/f1.txt [root@centos8 ~ 909]#rm-rf /mnt/mysql/f2.txt bash: rm-rf: command not found... [root@centos8 ~ 910]#rm -rf /mnt/mysql/f2.txt [root@centos8 ~ 911]#touch /mnt/mysql/f4.txt [root@centos8 ~ 912]#ll /mnt/mysql total 24 -rw-r--r-- 1 root root 3 Feb 16 19:48 f1.txt -rw-r--r-- 1 root root 899 Feb 16 19:24 f3.txt -rw-r--r-- 1 root root 0 Feb 16 19:49 f4.txt drwx------ 2 root root 16384 Feb 16 10:47 lost+found [root@centos8 ~ 913]#ll /mnt/snap/ total 28 -rw-r--r-- 1 root root 899 Feb 16 19:24 f1.txt -rw-r--r-- 1 root root 899 Feb 16 19:24 f2.txt -rw-r--r-- 1 root root 899 Feb 16 19:24 f3.txt drwx------ 2 root root 16384 Feb 16 10:47 lost+found
第一步,先取消挂载:
[root@centos8 ~ 914]#umount /mnt/snap /mnt/mysql
第二步,用lvconvert --merge合并:
[root@centos8 ~ 915]#lvconvert --merge /dev/vg0/mysql-snapshot Merging of volume vg0/mysql-snapshot started. vg0/mysql: Merged: 100.00%
查看合并没有:
[root@centos8 ~ 916]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert log vg0 -wi-ao---- <1.49g mysql vg0 -wi-a----- 2.00g [root@centos8 ~ 917]#mount -a [root@centos8 ~ 918]#ll /mnt/mysql total 28 -rw-r--r-- 1 root root 899 Feb 16 19:24 f1.txt -rw-r--r-- 1 root root 899 Feb 16 19:24 f2.txt -rw-r--r-- 1 root root 899 Feb 16 19:24 f3.txt drwx------ 2 root root 16384 Feb 16 10:47 lost+found
快照是一次性的。
9.对xfs文件系统做快照:
[root@centos8 ~ 924]#ll /mnt/log/ total 16 -rw-r--r-- 1 root root 2557 Feb 16 20:09 a.txt -rw-r--r-- 1 root root 2557 Feb 16 20:09 b.txt -rw-r--r-- 1 root root 2557 Feb 16 20:10 c.txt -rw-r--r-- 1 root root 2557 Feb 16 20:09 passwd
创建快照卷:
[root@centos8 ~ 925]#lvcreate -s -n log-snapshot /dev/vg0/log -L 100M Logical volume "log-snapshot" created.
挂载:
因为xfs有个特性是相同的uuid是不许挂载的,所以我们需要加一个选项nouuid
[root@centos8 ~ 946]#mount -o ro,nouuid /dev/vg0/log-snapshot /mnt/snap1/
2022-2-16 20:42