LVM(逻辑卷管理)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM(逻辑卷管理)相关的知识,希望对你有一定的参考价值。
一、简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是基于内核的一种逻辑卷管理器,LVM适合于管理大存储设备,并允许用户动态调整文件系统大小。此外,LVM的快照功能可以帮助我们快速备份数据。LVM为我们提供了逻辑概念上的磁盘,使得文件系统不在关心底层物理磁盘的概念。并且它是Linux环境下对磁盘分区进行管理的一种机制,使用lvm用户可以在无需停机的情况下方便地调整各个分区大小。
看图识LVM
二、LVM基本概念
1、Physical Volume(PV),物理卷:创建分区时,标记分区类型为8e后,就可以将该分区做成物理卷供LVM使用了。可以用任意块设备做成物理卷,单个分区、整块硬盘、RAID设备都可以。
2、Volume Group(VG),卷组:顾名思义,就是将一个或多个PV组合为一个卷组,以PE为存储单位。一个VG至少包含一个PV,VG可以创建多个。
3、Physical Extent(PE),物理区域:PE是整个LVM中可分配的最小存储单元,大小在创建卷组时指定且确定后不能更改;同一卷组的PE大小需一致。新的PV加入卷组后,PE的大小会自动更改为原卷组PE的大小。
4、Logical Volume(LV),逻辑卷:建立在卷组的基础上,卷组中空闲的PE可分配给LV使用,创建LV后可通过增加或减少PE来动态扩展和缩小空间大小。LV可以直接创建文件系统并挂载使用。两种写入模式:线性模式(linear)、交错模式(triped)。
三、LVM常用管理工具
准备工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # fdisk /dev/sda #创建分区并调整分区格式(lvm格式为8e) WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode ( command ‘c‘ ) and change display units to sectors ( command ‘u‘ ). Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First cylinder (1632-26108, default 1632): Using default value 1632 Last cylinder, +cylinders or +size{K,M,G} (1632-26108, default 26108): Using default value 26108 Command (m for help): n First cylinder (1632-26108, default 1632): Using default value 1632 Last cylinder, +cylinders or +size{K,M,G} (1632-26108, default 26108): +5G Command (m for help): n First cylinder (2286-26108, default 2286): Using default value 2286 Last cylinder, +cylinders or +size{K,M,G} (2286-26108, default 26108): +5G Command (m for help): n First cylinder (2940-26108, default 2940): Using default value 2940 Last cylinder, +cylinders or +size{K,M,G} (2940-26108, default 26108): +5G Command (m for help): n First cylinder (3594-26108, default 3594): +5G Value out of range. First cylinder (3594-26108, default 3594): Using default value 3594 Last cylinder, +cylinders or +size{K,M,G} (3594-26108, default 26108): +5G Command (m for help): t Partition number (1-8): 5 Hex code ( type L to list codes): 8e Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 6 Hex code ( type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 7 Hex code ( type L to list codes): 8e Changed system type of partition 7 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 8 Hex code ( type L to list codes): 8e Changed system type of partition 8 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re- read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. # kpartx -af /dev/sda # partx -a /dev/sda (centos6.7需要这两步才能读取硬盘) # cat /proc/partitions (查看磁盘情况) major minor #blocks name 8 0 209715200 sda 8 1 819200 sda1 8 2 10240000 sda2 8 3 2048000 sda3 8 4 31 sda4 8 5 5246007 sda5 8 6 5253223 sda6 8 7 5253223 sda7 8 8 5253223 sda8 # fdisk -l Disk /dev/sda : 214.7 GB, 214748364800 bytes 255 heads, 63 sectors /track , 26108 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: 0x000711d9 Device Boot Start End Blocks Id System /dev/sda1 * 1 103 819200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 103 1377 10240000 83 Linux /dev/sda3 1377 1632 2048000 82 Linux swap / Solaris /dev/sda4 1632 26108 196604286 5 Extended /dev/sda5 1632 2285 5246007 8e Linux LVM /dev/sda6 2286 2939 5253223+ 8e Linux LVM /dev/sda7 2940 3593 5253223+ 8e Linux LVM /dev/sda8 3594 4247 5253223+ 8e Linux LVM |
1、物理卷(PV)的常用管理命令
1 2 3 4 5 6 7 8 | pvcreate #创建一个物理卷 pvchange #修改物理卷的属性 pvresize #调整一个PV的大小 pvremove #删除一个PV pvdisplay #显示PV的属性信息 pvmove #移动PE到指定物理卷 pvscan #搜索所有磁盘上的物理卷 pvs #显示PV的简要信息 |
示例1:创建4个PV
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # pvcreate /dev/sda{5..8} Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created Physical volume "/dev/sda7" successfully created Physical volume "/dev/sda8" successfully created # pvs #查看磁盘PV情况 PV VG Fmt Attr PSize PFree /dev/sda5 lvm2 --- 5.00g 5.00g /dev/sda6 lvm2 --- 5.01g 5.01g /dev/sda7 lvm2 --- 5.01g 5.01g /dev/sda8 lvm2 --- 5.01g 5.01g # pvdisplay #查看PV详细信息 "/dev/sda5" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- #新PV PV Name /dev/sda5 #物理卷名称 VG Name PV Size 5.00 GiB #物理卷大小 Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID F4waZ4-gQtM-lrZm-RN7O-1HFX-axqe-M5IdE0 "/dev/sda6" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda6 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID fykqC3-4PPg-lgfR-iWQO-WzPK-goRT-W4S7uy "/dev/sda7" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda7 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID xakBcc-U6lL-vLC7-WIc1-X2Td-tetj-GKBlOu "/dev/sda8" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda8 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID OOr5Dh-i7ZW-UjsC-rTxW-MrnP-USMI-XQcmOO |
示例2:删除PV
1 2 3 4 5 6 7 8 | # pvmove /dev/sda5 /dev/sda6 #注意:两个PV必须在同一卷组内 # pvremove /dev/sda5 #删除PV前要确保该PV没有在任何卷组中 Labels on physical volume "/dev/sda5" successfully wiped # pvs PV VG Fmt Attr PSize PFree /dev/sda6 lvm2 --- 5.01g 5.01g /dev/sda7 lvm2 --- 5.01g 5.01g /dev/sda8 lvm2 --- 5.01g 5.01g |
2、卷组(VG)的常用管理命令
1 2 3 4 5 6 7 8 9 10 11 12 | vgcreate #创建卷组 -l:设置可容纳的LV最大数,默认为255 -p:设置包含PV的最大数,默认为255 -s:设置PE大小,默认为4M vgextend #扩展VG大小 vgdisplay #显示VG属性信息 vgchange #修改VG属性信息 vgreduce #缩减VG大小 vgremove #删除VG vgscan #搜索所有磁盘上的卷组 vgs #显示VG简要信息 vgck #检查VG元数据 |
示例1:创建一个卷组,要求PE大小为8M,名称为bjwf,最大支持LV不能超过10个,包含的PV不能超过2个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # vgcreate -s 8M -l 10 -p 2 bjwf /dev/sda{7,8} Volume group "bjwf" successfully created # vgdisplay bjwf #查看VG属性信息验证结果 --- Volume group --- VG Name bjwf #VG名称 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read /write VG Status resizable MAX LV 10 #可以看到,支持最大LV数为10个 Cur LV 0 Open LV 0 Max PV 2 #支持最大PV数为2个 Cur PV 2 Act PV 2 VG Size 10.02 GiB #卷组大小 PE Size 8.00 MiB #PE的大小 Total PE 1282 Alloc PE / Size 0 / 0 Free PE / Size 1282 / 10.02 GiB #剩余容量 VG UUID 7q5Lka-cnTN-qqlh-eIg1-Fpr2-2IPY-yrHA1S |
示例2:扩展bjwf的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # vgextend bjwf /dev/sda6 #这块由于此前定义了VG的最大PV数,所以无法扩展 No space for ‘/dev/sda6‘ - volume group ‘bjwf‘ holds max 2 physical volume(s). Unable to add physical volume ‘/dev/sda6‘ to volume group ‘bjwf‘ . # vgchange -p 3 bjwf #修改VG属性 Volume group "bjwf" successfully changed # vgextend bjwf /dev/sda6 #扩展VG Volume group "bjwf" successfully extended #显示成功 # vgdisplay bjwf #查看新扩展的VG --- Volume group --- VG Name bjwf System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 VG Access read /write VG Status resizable MAX LV 10 Cur LV 0 Open LV 0 Max PV 3 #最大PV数已经改为3 |