逻辑卷管理器是Linux系统用于对硬盘分区进行管理的一种机制,理论性较强,其创建初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。尽管对传统的硬盘分区进行强制扩容或缩容从理论上来讲是可行的,但是却可能造成数据的丢失。而LVM技术是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。这样一来,用户不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整。
一、创建 步骤:让硬盘设备支持LVM技术(pvcreate)—硬盘设备加入到卷组(vgcreate)—切割一定空间作为逻辑卷(lvcreate)—创建成功的逻辑卷进行格式化然后挂载使用(mkfs、mount)
[root@lizhiqiang Desktop]# pvcreate /dev/sdc /dev/sdd #支持 Physical volume "/dev/sdc" successfully created Physical volume "/dev/sdd" successfully created [root@lizhiqiang Desktop]# vgcreate aa /dev/sdc /dev/sdd #加入到卷组 Volume group "aa" successfully created [root@lizhiqiang Desktop]# lvcreate -n bb -l 30 aa #逻辑卷 Logical volume "bb" created [root@lizhiqiang Desktop]# lvdisplay 查看信息 --- Logical volume --- LV Path /dev/aa/bb LV Name bb VG Name aa LV UUID qvOElL-qV5N-9V4V-ggFe-CqBm-hnaz-BK4Uij LV Write Access read/write LV Creation host, time lizhiqiang, 2020-10-22 08:34:17 -0400 LV Status available # open 0 LV Size 120.00 MiB Current LE 30 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 [root@lizhiqiang Desktop]# mkfs.ext4 /dev/aa/bb #格式化 [root@lizhiqiang Desktop]# mkdir -p /cc #挂载所需目录 [root@lizhiqiang Desktop]# mount /dev/aa/bb /cc #一次性挂载 [root@lizhiqiang Desktop]# echo "/dev/aa/bb /cc ext4 defaults 0 0" >> /etc/fstab #挂载永久性挂载,/etc/fstab配置文件二、扩容 扩容前,先卸载设备和挂载点的关联。 查看硬盘完整性 同步逻辑卷容量
[root@lizhiqiang Desktop]# umount /cc 卸载挂载 [root@lizhiqiang Desktop]# e2fsck -f /dev/aa/bb 查看硬盘完整性 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/aa/bb: 11/30720 files (9.1% non-contiguous), 9529/122880 blocks [root@lizhiqiang Desktop]# resize2fs /dev/aa/bb 同步逻辑卷容量 resize2fs 1.42.9 (28-Dec-2013) The filesystem is already 122880 blocks long. Nothing to do! [root@lizhiqiang Desktop]# lvextend -L 300M /dev/aa/bb 扩容逻辑卷 -L指定大小 Extending logical volume bb to 300.00 MiB Logical volume bb successfully resized [root@lizhiqiang Desktop]# mount /cc 挂载 [root@lizhiqiang Desktop]# df -h 查看 /dev/mapper/aa-bb 287M 2.0M 266M 1% /cc三、缩容 先卸载挂载 然后查看完整性 向系统报备一下即将要执行逻辑卷缩容的操作
[root@lizhiqiang Desktop]# umount /cc 卸载 [root@lizhiqiang Desktop]# e2fsck -f /dev/aa/bb 查看系统完整性 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/aa/bb: 11/77824 files (9.1% non-contiguous), 15979/307200 blocks [root@lizhiqiang Desktop]# resize2fs /dev/aa/bb 150M 向系统报备一下即将要执行逻辑卷缩容 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/aa/bb to 153600 (1k) blocks. The filesystem on /dev/aa/bb is now 153600 blocks long. [root@lizhiqiang Desktop]# lvreduce -L 150M /dev/aa/bb 缩容指定到150M Rounding size to boundary between physical extents: 152.00 MiB WARNING: Reducing active logical volume to 152.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce bb? [y/n]: y 确认缩容这一步操作 Reducing logical volume bb to 152.00 MiB Logical volume bb successfully resized [root@lizhiqiang Desktop]# mount -a 挂载 [root@lizhiqiang Desktop]# df -h 查看挂载信息 pper/aa-bb 142M 1.6M 130M 2% /cc四、快照(注意:快照是一次性的)
[root@lizhiqiang Desktop]# echo " Hello " > /dyp/aa 往逻辑卷设备(dyp挂载的目录)写入一个文件 [root@lizhiqiang Desktop]# lvcreate -L 150M -s -n bb /dev/lzq/wn 为逻辑卷wn生成一个快照卷,命名为bb,指定切割大小为150M,快照卷和逻辑卷容量大小相等 Rounding up size to full physical extent 152.00 MiB Logical volume "bb" created [root@lizhiqiang Desktop]# dd if=/dev/zero of=/dyp/file bs=100M count=1 给逻辑卷dyp创建一个100M的名为mfile文件 1+0 records in 1+0 records out 104857600 bytes (105 MB) copied, 1.16478 s, 90.0 MB/s [root@lizhiqiang Desktop]# lvdisplay 查看逻辑卷信息 截取了一部分 --- Logical volume --- LV Path /dev/lzq/bb LV Name bb VG Name lzq LV UUID GidKVE-XgUQ-zaeG-CpBZ-EPeN-Zoaa-L7kPxZ LV Write Access read/write LV Creation host, time lizhiqiang, 2020-10-23 05:50:54 -0400 LV snapshot status active destination for wn LV Status available # open 0 LV Size 152.00 MiB Current LE 38 COW-table size 152.00 MiB COW-table LE 38 Allocated to snapshot 45.64% 占用率 Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 [root@lizhiqiang Desktop]# umount /dyp 先卸载再恢复快照 [root@lizhiqiang Desktop]# lvconvert --merge /dev/lzq/bb 恢复快照(一次性) Merging of volume bb started. wn: Merged: 43.9% wn: Merged: 100.0% Merge of snapshot into logical volume wn has finished. Logical volume "bb" successfully removed [root@lizhiqiang Desktop]# mount -a 挂载 [root@lizhiqiang Desktop]# ls /dyp 查看逻辑卷目录,可以发现创建快照后再创建的100M文件不见了,逻辑卷恢复成设置快照前的状态 aa lost+found [root@lizhiqiang Desktop]# cd /dyp [root@lizhiqiang dyp]# cat aa 查看之前创建快照前的文件 Hello删除顺序与创建逻辑卷相反(逻辑卷-卷组-物理卷)
[root@lizhiqiang Desktop]# reboot 先重启 [root@lizhiqiang Desktop]# umount /dyp 卸载挂载 [root@lizhiqiang Desktop]# vim /etc/fstab 删除配置文件 [root@lizhiqiang Desktop]# lvremove /dev/lzq/wn 删除逻辑卷 Do you really want to remove active logical volume wn? [y/n]: y 确定删除 Logical volume "wn" successfully removed [root@lizhiqiang Desktop]# vgremove lzq 删除卷组 Volume group "lzq" successfully removed [root@lizhiqiang Desktop]# pvremove /dev/sd[c-d] 删除物理组(用的sdc与sdd) Labels on physical volume "/dev/sdc" successfully wiped Labels on physical volume "/dev/sdd" successfully wiped