逻辑卷 设备名:/dev/dm-# pv:物理卷 vg:卷组 lv:逻辑卷

首先准备一个盘是/dev/sda 20G fdisk /dev/sda -t 8e改成类型lvm

Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 8e Linux LVM /dev/sdb2 2099200 6293503 2097152 8e Linux LVM 创建物理卷 [root@centos7 ~]#pvs;pvcreate /dev/sd{b1,c} [root@centos7 ~]#pvs //查看 显示详细的物理卷 [root@centos7 ~]#pvdisplay 下面创建卷组名称,创建卷组大小 [root@centos7 ~]#vgcreate -s 16M vg0 /dev/sd{b1,c} Volume group "vg0" successfully created [root@centos7 ~]#vgs VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- <20.97g <20.97g [root@centos7 ~]#vgdisplay 激活卷组 [root@centos7 ~]#vgchange -ay vg0 pvdisplay命令用于显示物理卷的属性。 激活卷组 [root@centos7 ~]#vgchange -ay vg0 lvcreate命令用于创建LVM的逻辑卷 -L:指定逻辑卷的大小,单位为“kKmMgGtT”字节; -l:指定逻辑卷的大小(LE数)。

[root@centos7 ~]#lvdisplay --- Logical volume --- LV Path /dev/vg0/lv0 创建第二个逻辑卷 [root@centos7 ~]#lvcreate -n lv1 -l +100%FREE vg0 Logical volume "lv1" created.、

格式化

mkfs.ext4 /dev/vg0/lv1 [root@centos7 ~]#mkdir /mnt/lv0 [root@centos7 ~]#mkdir /mnt/lv1 [root@centos7 ~]#mount /dev/vg0/lv0 /mnt/lv0 [root@centos7 ~]#vim /etc/fstab [root@centos7 ~]#mount -a [root@centos7 ~]#cd /mnt/lv0 创建一个文件测试一下性能 [root@centos7 etc]#dd if=/dev/zero of=/mnt/lv0/bigfile bs=1M count=1024

逻辑卷加分区 先创建物理卷 [root@centos7 etc]#pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created 、、、、

扩展的时候把逻辑卷其他的部分沾满

[root@centos7 etc]#lvextend -l +100%FREE /dev/vg0/lv0 下面再做一次 [root@centos7 etc]#resize2fs /dev/vg0/lv0 esize2fs命令被用来增大或者收缩未加载的“ext2/ext3”文件系统的大小 再df -h查看 测试逻辑卷的性能 [root@centos7 etc]#dd if=/dev/zero of=/mnt/lv0/bigfile1 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 2.14629 s, 500 MB/s 再挂在一个 [root@centos7 etc]#mount /dev/vg0/lv1 /mnt/lv1 再加一块磁盘/dev/sda6 创建物理卷 [root@centos7 ~]#pvcreate /dev/sda6 Physical volume "/dev/sda6" successfully created. [root@centos7 ~]#vgextend vg0 /dev/sda6 Volume group "vg0" successfully extended 这样vg0又有空间了

再缩减逻辑卷,先取消挂载 [root@centos7 ~]#umount /mnt/lv0 然后检查文件系统的完整性 [root@centos7 ~]#e2fsck -f /dev/vg0/lv0 在进行缩减文件系统 [root@centos7 ~]#resize2fs /dev/vg0/lv0 8G 再缩减逻辑卷,缩减逻辑卷可能会破坏数据 [root@centos7 ~]#lvreduce -L 8G /dev/vg0/lv0 缩减完之后再挂上 [root@centos7 ~]#mount -a [root@centos7 ~]#df

[root@centos7 ~]#lvextend -r 0l +100%FREE /dev/vg0/lv0

总结:实验:缩减LV umount /mnt/lv0 fsck -f /dev/vg0/lv0 resize2fs /dev/vg0/lv0 8G lvreduce -L 8G /dev/vg0/lv0 mount -a

================================================================ 假设sdc的逻辑卷坏了,现在需要搬家

pvmove /dev/sdc这个就是搬家 第一步添加物理卷 [root@centos7 ~]#pvcreate /dev/sdd Physical volume "/dev/sdd" successfully created. 扩展卷组 [root@centos7 ~]#vgextend vg0 /dev/sdd Volume group "vg0" successfully extended 下面卷组就有空间了 [root@centos7 ~]#vgs VG #PV #LV #SN Attr VSize VFree vg0 5 2 0 wz--n- 59.92g 19.98g

[root@centos7 ~]#pvs /dev/sdc PV VG Fmt Attr PSize PFree /dev/sdc vg0 lvm2 a-- 19.98g 0

搬家,迁移sdc [root@centos7 ~]#pvmove /dev/sdc 在查看空余空间,空余19.98

再从卷组中移除 [root@centos7 ~]#vgreduce vg0 /dev/sdc Removed "/dev/sdc" from volume group "vg0"

再从pv中删除 [root@centos7 ~]#pvremove /dev/sdc Labels on physical volume "/dev/sdc" successfully wiped.

下面是逻辑卷迁移到另外一个服务器上 创建一个vg1的逻辑卷

[root@centos7 ~]#mkfs.ext4 /dev/vg1/lv2 [root@centos7 ~]#mkdir /mnt/lv2 [root@centos7 ~]#mount /dev/vg1/lv2 mount: can't find /dev/vg1/lv2 in /etc/fstab [root@centos7 ~]#mount /dev/vg1/lv2 /mnt/lv2 [root@centos7 ~]#cp /etc/* /mnt/lv2

第一步将卷组改名字

[root@centos7 ~]#lvrename /dev/vg100/lv2 lv100 改名字 [root@centos7 ~]#umount /mnt/lv2 将vg禁用 [root@centos7 ~]#vgchange -an vg100 0 logical volume(s) in volume group "vg100" now active 再将vg100导出 [root@centos7 ~]#vgexport vg100 Volume group "vg100" successfully exported 再将硬盘拆下来,放到一另外一个服务器上,再导出 [root@centos6 ~]#vgimport vg100 Volume group "vg100" successfully imported 在进行激活逻辑卷 [root@centos6 ~]#vgchange -ay vg100 1 logical volume(s) in volume group "vg100" now active [root@centos6 ~]#mount /dev/vg100/lv100 /mnt/lv100 [root@centos6 ~]#ls /mnt/lv100

======================

逻辑卷管理器快照

[root@centos7 ~]#lvcreate -n lv0-snapshot -L 1G -s -p r /dev/vg0/lv0 Using default stripesize 64.00 KiB. Logical volume "lv0-snapshot" created.

QQ

快照和源文件一样大 下面是快照还原,但是要先取消挂载 [root@centos7 ~]#lvconvert --merge /dev/vg0/lv0-snapshot 删除逻辑卷 [root@centos7 ~]#lvremove /dev/vg0 删除卷组 [root@centos7 ~]#vgremove vg0 Volume group "vg0" successfully removed 删除pv [root@centos7 ~]#pvremove /dev/sdc1