Linux逻辑卷管理LVM学习总结
LVM(Logical Volume Manager),它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,可以弹性的增加或减小分区的大小,使得磁盘分区管理更具灵活性。LVM的实现示意图:
创建LVM实例:
1、首先创建3个准备组成卷组VG的物理分区,并且把分区格式改为8e。以/dev/sdb1、/dev/sdb2、/dev/sdb3为例:
#在/dev/sdb磁盘上创建分区/dev/sdb1、/dev/sdb2、/dev/sdb3,大小分别为3G、4G、5G [root@localhost ~]#fdisk /dev/sdb Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +3G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (394-2610, default 394): Using default value 394 Last cylinder, +cylinders or +size{K,M,G} (394-2610, default 2610): +4G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (917-2610, default 917): Using default value 917 Last cylinder, +cylinders or +size{K,M,G} (917-2610, default 2610): +5G #调整分区格式为8e,并查看 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x49c5d108 Device Boot Start End Blocks Id System /dev/sdb1 1 393 3156741 8e Linux LVM /dev/sdb2 394 916 4200997+ 8e Linux LVM /dev/sdb3 917 1570 5253255 8e Linux LVM
2、创建物理卷PV
物理卷Physical Volume就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘 等)比较,却包含有与LVM相关的管理参数。pvcreate 命令将希望添加到卷组的所有分区或者磁盘创建为物理卷。
#pvcreate /dev/sdb ,将整个磁盘创建为物理卷。
#pvcreate /dev/sdb1 ,将单个分区创建为物理卷。
#创建物理卷 pvcreate /dev/sdb{1,2,3} 查看物理卷 [root@localhost ~]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sdb1 lvm2 a-- 3.01g 3.01g /dev/sdb2 lvm2 a-- 4.01g 4.01g /dev/sdb3 lvm2 a-- 5.01g 5.01g [root@localhost ~]#pvscan PV /dev/sda2 VG vg0 lvm2 [59.99 GiB / 7.99 GiB free] PV /dev/sdb1 lvm2 [3.01 GiB] PV /dev/sdb2 lvm2 [4.01 GiB] PV /dev/sdb3 lvm2 [5.01 GiB] Total: 4 [72.02 GiB] / in use: 1 [59.99 GiB] / in no VG: 3 [12.03 GiB]
3、创建卷组VG
卷组Volume Group是由多个PV整合而成的,VG最多可以有65534个PE(Physical Extent)组成,每一个物理卷被划分为称为PE的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认的PE块是 4M,默认VG最大是256G{由默认的PE块大小计算:4M*65534/(1024M/G)=256G}。
#创建卷组testvg,由sdb1、sdb2和sdb3整合而成 [root@localhost ~]# vgcreate testvg /dev/sdb{1,2,3} Volume group "testvg" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree testvg 3 0 0 wz--n- 12.02g 12.02g vg0 1 4 0 wz--n- 59.99g 7.99g #从卷组testvg中移除/dev/sdb2,查看testvg的容量减小了 [root@localhost ~]# vgreduce testvg /dev/sdb2 Removed "/dev/sdb2" from volume group "testvg" [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree testvg 2 0 0 wz--n- 8.02g 8.02g vg0 1 4 0 wz--n- 59.99g 7.99g #重新把/dev/sdb2添加到卷组testvg之中 [root@localhost ~]# vgextend testvg /dev/sdb2 Volume group "testvg" successfully extended [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree testvg 3 0 0 wz--n- 12.02g 12.02g vg0 1 4 0 wz--n- 59.99g 7.99g
4、创建逻辑卷LV
创建逻辑卷Logical Volume,就像硬盘上的分区一样,LV的大小是PE的整数倍。LV的块设备文件通常是/dev/vgname/lvname和/dev/mapper/vgname-lvname
lvcreate简单格式:lvcreate -L 容量 -n LVname VGname
e.g. lvcreate -L 5G -n testlv1 testvg
其中-L容量的指定也可以用-l选项,指定PE的个数。
#创建testlv1,指定大小为5G [root@localhost ~]# lvcreate -L 5G -n testlv1 testvg Logical volume "testlv1" created #查看是否创建成功 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert testlv1 testvg -wi-a----- 5.00g root vg0 -wi-ao---- 20.00g swap vg0 -wi-ao---- 2.00g usr vg0 -wi-ao---- 10.00g var vg0 -wi-ao---- 20.00g #查看testlv1的设备文件 [root@localhost ~]# ls -l /dev/testvg/testlv1 lrwxrwxrwx 1 root root 7 Jul 20 14:32 /dev/testvg/testlv1 -> ../dm-2 [root@localhost ~]# ls -l /dev/mapper/testvg-testlv1 lrwxrwxrwx 1 root root 7 Jul 20 14:32 /dev/mapper/testvg-testlv1 -> ../dm-2
格式化逻辑卷testlv1并挂载
[root@localhost ~]# mke2fs -t ext4 /dev/testvg/testlv1 [root@localhost ~]# mount /dev/testvg/testlv1 /mnt/lvm
扩展和缩减逻辑卷
扩展逻辑卷的步骤
1、创建新的分区,并修改分区类型为8e
2、创建物理卷PV
3、将新创建的PV添加到要扩展的逻辑卷所在的卷组
4、用命令lvextend或者lvresize来将新加入的PE添加到要扩展的逻辑卷中
5、用命令resize2fs扩展文件系统的容量
如果卷组中有多余的空间足够扩展逻辑卷,可以直接从第4步开始。
实例: 将testlv1扩展到13G
#创建1G新分区/dev/sdb5,并修改分区类型为8e,创建之后的新分区: Device Boot Start End Blocks Id System /dev/sdb1 1 393 3156741 8e Linux LVM /dev/sdb2 394 916 4200997+ 8e Linux LVM /dev/sdb3 917 1570 5253255 8e Linux LVM /dev/sdb4 1571 2610 8353800 5 Extended /dev/sdb5 1571 1702 1060258+ 8e Linux LVM [root@localhost ~]# kpartx -af /dev/sdb [root@localhost ~]# partx -a /dev/sdb #将分区/dev/sdb5 创建为物理卷 [root@localhost ~]# pvcreate /dev/sdb5 dev_is_mpath: failed to get device for 8:21 Physical volume "/dev/sdb5" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sdb1 testvg lvm2 a-- 3.01g 3.01g /dev/sdb2 testvg lvm2 a-- 4.00g 4.00g /dev/sdb3 testvg lvm2 a-- 5.01g 8.00m /dev/sdb5 lvm2 a-- 1.01g 1.01g #将/dev/sdb5添加至testvg卷组中 [root@localhost ~]# vgextend testvg /dev/sdb5 Volume group "testvg" successfully extended [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree testvg 4 1 0 wz--n- 13.03g 8.03g vg0 1 4 0 wz--n- 59.99g 7.99g #可以看到testvg的大小已经增加到13G,然后扩展逻辑卷的testlv1至13G [root@localhost ~]# lvextend -L 13G /dev/testvg/testlv1 Extending logical volume testlv1 to 13.00 GiB Logical volume testlv1 successfully resized [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert testlv1 testvg -wi-ao---- 13.00g root vg0 -wi-ao---- 20.00g swap vg0 -wi-ao---- 2.00g usr vg0 -wi-ao---- 10.00g var vg0 -wi-ao---- 20.00g [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree testvg 4 1 0 wz--n- 13.03g 28.00m vg0 1 4 0 wz--n- 59.99g 7.99g #上面可以看到逻辑卷已经扩展至13G,接下来进行文件系统扩展 [root@localhost ~]# resize2fs /dev/testvg/testlv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/testvg/testlv1 is mounted on /mnt/lvm; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/testvg/testlv1 to 3407872 (4k) blocks. The filesystem on /dev/testvg/testlv1 is now 3407872 blocks long. [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 427M 19G 3% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 30M 155M 16% /boot /dev/mapper/vg0-usr 9.9G 2.0G 7.4G 22% /usr /dev/mapper/vg0-var 20G 371M 19G 2% /var /dev/mapper/testvg-testlv1 13G 141M 13G 2% /mnt/lvm
缩减逻辑卷的步骤
缩减逻辑卷要先离线, 缩减要慎重!
1、先卸载文件系统,然后强制执行检测e2fsck -f
2、然后确定要移除的物理卷的大小,有多少个PE。然后用命令resize2fs缩减文件系统大小,然后重新挂载逻辑卷
3、用命令lvreduce或者lvresize缩减指定个数的PE, 也可以计算出相应的大小来缩减
4、LV缩减之后,通过命令pvdisplay查看,指定要移除的PV,如果要移除的PV有PE在用,用pvmove转移PE到空闲的PV。pvmove可以指定要转移的PE
pvmove /dev/sdb1:1000-1999 /dev/sdc1
or
pvmove /dev/sdb1:1000-1999 /dev/sdc1:0-999
5、用命令vgreduce缩减指定要从卷组冲移除的物理卷,然后用pvremove命令移除物理卷
实例: 缩减逻辑卷并从卷组中移除物理卷/dev/sdb1
#首先卸载文件系统 [root@localhost ~]# umount /mnt/lvm #强制检测/dev/testvg/testlv1 [root@bogon ~]# e2fsck -f /dev/testvg/testlv1 e2fsck 1.41.12 (17-May-2010) 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/testlv1: 11/851968 files (0.0% non-contiguous), 89440/3407872 blocks #用命令pvdisplay查看/dev/sdb1的大小以及PE个数 [root@localhost ~]# pvdisplay /dev/sdb1 --- Physical volume --- PV Name /dev/sdb1 VG Name testvg PV Size 3.01 GiB / not usable 2.75 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 770 Free PE 0 Allocated PE 770 PV UUID xNUWFa-1FdZ-Ctkj-rvJp-dACa-FnJw-mJf6Sc #可以看出/dev/sdb1大小为3.01G,一共770个PE。testlv1一共13G,移除/dev/sdb1后是10G,这里用9.7G,容量用小数无效,所以使用9700M,其实比9.7G更小 [root@localhost ~]# resize2fs /dev/testvg/testlv1 9.7G resize2fs 1.41.12 (17-May-2010) resize2fs: Invalid new size: 9.7G [root@localhost ~]# resize2fs /dev/testvg/testlv1 9700M resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/testvg/testlv1 to 2483200 (4k) blocks. The filesystem on /dev/testvg/testlv1 is now 2483200 blocks long. #重新挂载逻辑卷 [root@localhost ~]# mount /dev/testvg/testlv1 /mnt/lvm #用命令lvreduce通过指定PE个数或者大小来缩减逻辑卷 [root@localhost ~]# lvreduce -l -770 /dev/testvg/testlv1 WARNING: Reducing active and open logical volume to 9.99 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce testlv1? [y/n]: y Reducing logical volume testlv1 to 9.99 GiB Logical volume testlv1 successfully resized #查看缩减后的LV,缩减到了9.99G [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert testlv1 testvg -wi-ao---- 9.99g root vg0 -wi-ao---- 20.00g swap vg0 -wi-ao---- 2.00g usr vg0 -wi-ao---- 10.00g var vg0 -wi-ao---- 20.00g #用pvdisplay命令查看指定要移除的PV是否有占用的PE [root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sdb1 VG Name testvg PV Size 3.01 GiB / not usable 2.75 MiB Allocatable yes PE Size 4.00 MiB Total PE 770 Free PE 519 Allocated PE 251 PV UUID xNUWFa-1FdZ-Ctkj-rvJp-dACa-FnJw-mJf6Sc --- Physical volume --- PV Name /dev/sdb5 VG Name testvg PV Size 1.01 GiB / not usable 3.41 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 258 Allocated PE 0 PV UUID 8esNBR-k4zM-sQW6-Z71N-hKBX-0YlT-Egb1o5 #可以看出/dev/sdb1有251个PE被分配,而/dev/sdb5的258个PE都是空闲的,所以通过pvmove命令来转移被分配的PE到/dev/sdb5 [root@localhost ~]# pvmove /dev/sdb1:0-250 /dev/sdb5:0-250 /dev/sdb1: Moved: 0.0% /dev/sdb1: Moved: 31.5% /dev/sdb1: Moved: 74.5% /dev/sdb1: Moved: 100.0% #查看转移之后PE状况 [root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sdb1 VG Name testvg PV Size 3.01 GiB / not usable 2.75 MiB Allocatable yes PE Size 4.00 MiB Total PE 770 Free PE 770 Allocated PE 0 PV UUID xNUWFa-1FdZ-Ctkj-rvJp-dACa-FnJw-mJf6Sc --- Physical volume --- PV Name /dev/sdb5 VG Name testvg PV Size 1.01 GiB / not usable 3.41 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 7 Allocated PE 251 PV UUID 8esNBR-k4zM-sQW6-Z71N-hKBX-0YlT-Egb1o5 #可以看出/dev/sdb1中被分配的PE都已经转移到/dev/sdb5中了 #然后再用vgreduce来缩减VG,并用pvremove移除物理卷/dev/sdb1 [root@localhost ~]# vgreduce testvg /dev/sdb1 Removed "/dev/sdb1" from volume group "testvg" [root@localhost ~]# pvremove /dev/sdb1 Labels on physical volume "/dev/sdb1" successfully wiped [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sdb2 testvg lvm2 a-- 4.00g 0 /dev/sdb3 testvg lvm2 a-- 5.01g 0 /dev/sdb5 testvg lvm2 a-- 1.01g 28.00m
LVM快照系统
快照就是将当时的系统资讯记录下来,就好像照相记录一般! 未来若有任何数据更动了,则原始数据会被搬移到快照区,没有被更动的区域则由快照区与文件系统共享。
实例: 创建逻辑卷testlv1的快照
#lvcreate -s命令创建快照 #lvcreate -L 快照区大小 -s -n 快照名 被快照的逻辑卷 [root@bogon ~]# lvcreate -L 1G -s -n testlv-snap /dev/testvg/testlv1 Logical volume "testlv-snap" created [root@bogon ~]# lvdisplay --- Logical volume --- LV Path /dev/testvg/testlv1 LV Name testlv1 VG Name testvg LV UUID zi5txf-3qCY-n70x-pAq6-hfUv-OBo6-4fdFtg LV Write Access read/write LV Creation host, time bogon, 2014-07-20 14:32:32 +0800 LV snapshot status source of testlv-snap [active] LV Status available # open 1 LV Size 9.00 GiB Current LE 2304 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 #挂载快照 [root@bogon ~]# mount /dev/testvg/testlv-snap /mnt/snapshot
删除LVM
实例:删除之前创建的LVM
[root@localhost ~]# umount /mnt/lvm [root@localhost ~]# umount /mnt/snapshot [root@localhost ~]# lvremove /dev/testvg/testlv-snap Do you really want to remove active logical volume testlv-snap? [y/n]: y Logical volume "testlv-snap" successfully removed [root@localhost ~]# lvremove /dev/testvg/testlv1 Do you really want to remove active logical volume testlv1? [y/n]: y Logical volume "testlv1" successfully removed ####取消激活状态的卷组#### [root@localhost ~]# vgchange -a n testvg [root@localhost ~]# vgremove testvg Volume group "testvg" successfully removed ####删除PV#### [root@localhost ~]# pvremove /dev/sdb{2,3,5} Labels on physical volume "/dev/sdb2" successfully wiped Labels on physical volume "/dev/sdb3" successfully wiped Labels on physical volume "/dev/sdb5" successfully wiped [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g
最后用fdisk将各物理分区的类型8e改回83
参考资料:
http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html
http://blog.csdn.net/seteor/article/details/6708025