LVM(Logical volume Manager)逻辑卷管理通过将底层物理硬盘抽象封装起来,以逻辑卷的形式表现给上层系统,逻辑卷的大小可以动态调整,而且不会丢失现有数据。新加入的硬盘也不会改变现有上层的逻辑卷。
作为一种动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。
PE ( physical extend) 物理扩展,是逻辑卷磁盘管理的基础单位,默认是4M大小 PV (physical volumes) 物理卷 VG (volume groups) 卷组 LV (logical volumes) 逻辑卷
在虚拟机中增加两块10G的磁盘/dev/sdb和/dev/sdc
[root@localhost ~]# yum install lvm2 -y # 如果是最小化安装系统,需要先安装lvm2 [root@localhost ~]# pvcreate /dev/sdb /dev/sdc # 将一块或多块物理磁盘初始化为物理卷 Physical volume "/dev/sdb" successfully created. Physical volume "/dev/sdc" successfully created. [root@localhost ~]# vgcreate lzfn /dev/sdb /dev/sdc # 创建卷组,并将PV加入到卷组中 Volume group "lzfn" successfully created [root@localhost ~]# lvcreate -n data01 -L 2G lzfn # 基于卷组创建逻辑卷data01,大小设置为2G Logical volume "data01" created. [root@localhost ~]# mkfs.ext4 /dev/lzfn/data01 # 将创建好的逻辑卷格式化文件系统 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# mkdir /data01 [root@localhost ~]# mount /dev/lzfn/data01 /data01/ # 挂载到目录进行使用 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 28G 1.2G 27G 5% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 97M 92M 4.9M 95% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/lzfn-data01 2.0G 6.0M 1.8G 1% /data01
# 查看物理卷信息 [root@localhost ~]# pvdisplay # 详细 --- Physical volume --- PV Name /dev/sdb VG Name lzfn PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 2559 Free PE 2047 Allocated PE 512 PV UUID YtXz3s-LbJK-4WcI-FcHh-WYh3-CfGU-B2l2yw --- Physical volume --- PV Name /dev/sdc VG Name lzfn PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 2559 Free PE 2559 Allocated PE 0 PV UUID eyLvyn-ckUh-8ES1-ejdh-JZaU-vc8x-NYwoeV [root@localhost ~]# pvs # 简单 PV VG Fmt Attr PSize PFree /dev/sdb lzfn lvm2 a-- <10.00g <8.00g /dev/sdc lzfn lvm2 a-- <10.00g <10.00g # 查看卷组信息 [root@localhost ~]# vgdisplay --- Volume group --- VG Name lzfn System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 19.99 GiB PE Size 4.00 MiB Total PE 5118 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 4606 / 17.99 GiB VG UUID qQjlvL-k10O-N23m-J9zV-6ufY-Xzzx-QYzLJX [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree lzfn 2 1 0 wz--n- 19.99g 17.99g # 查看逻辑卷信息 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/lzfn/data01 LV Name data01 VG Name lzfn LV UUID mmpUBB-4uMY-HLlL-8EBm-1CIP-GYi1-Mvjvdx LV Write Access read/write LV Creation host, time localhost.localdomain, 2021-09-28 22:32:25 +0800 LV Status available # open 1 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data01 lzfn -wi-ao---- 2.00g
一定要按此顺序删除,如果要删除卷组,就要先把逻辑卷删除完成之后,才能继续删除卷组,最后是物理卷。
[root@localhost ~]# umount /data01 [root@localhost ~]# lvremove /dev/lzfn/data01 Do you really want to remove active logical volume lzfn/data01? [y/n]: y Logical volume "data01" successfully removed [root@localhost ~]# vgremove lzfn Volume group "lzfn" successfully removed [root@localhost ~]# pvremove /dev/sdb /dev/sdc Labels on physical volume "/dev/sdb" successfully wiped. Labels on physical volume "/dev/sdc" successfully wiped.
将data01由5G扩容到9G
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 28G 1.2G 27G 5% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 97M 92M 4.9M 95% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/lzfn-data01 4.8G 20M 4.6G 1% /data01 # 要扩容的逻辑卷 [root@localhost ~]# vgs # 查看是否还有可用卷组空间进行扩容 VG #PV #LV #SN Attr VSize VFree lzfn 2 1 0 wz--n- 19.99g 14.99g [root@localhost ~]# lvextend -L +4G /dev/lzfn/data01 # 将逻辑卷扩容4G,也可以使用 -n lzfn 参数 Size of logical volume lzfn/data01 changed from 5.00 GiB (1280 extents) to 9.00 GiB (2304 extents). Logical volume lzfn/data01 successfully resized. [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 28G 1.2G 27G 5% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 97M 92M 4.9M 95% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/lzfn-data01 4.8G 20M 4.6G 1% /data01 [root@localhost ~]# resize2fs /dev/lzfn/data01 # 扩容后要调整文件系统后才会生效 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/lzfn/data01 is mounted on /data01; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/lzfn/data01 is now 2359296 blocks long. [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 28G 1.2G 27G 5% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 97M 92M 4.9M 95% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/lzfn-data01 8.8G 23M 8.3G 1% /data01
再将一块10G的磁盘加入到物理卷中
[root@localhost ~]# fdisk -l /dev/sdd Disk /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 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 [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb lzfn lvm2 a-- <10.00g 1020.00m /dev/sdc lzfn lvm2 a-- <10.00g <10.00g [root@localhost ~]# pvcreate /dev/sdd # 将要添加的vg硬盘格式化成pv Physical volume "/dev/sdd" successfully created. [root@localhost ~]# vgextend lzfn /dev/sdd # 将新的pv加到指定卷组中 Volume group "lzfn" successfully extended [root@localhost ~]# vgs # 扩容成功 VG #PV #LV #SN Attr VSize VFree lzfn 3 1 0 wz--n- <29.99g <20.99g [root@localhost ~]# pvs # 已经扩容成功 PV VG Fmt Attr PSize PFree /dev/sdb lzfn lvm2 a-- <10.00g 1020.00m /dev/sdc lzfn lvm2 a-- <10.00g <10.00g /dev/sdd lzfn lvm2 a-- <10.00g <10.00g