LVM利用Linux内核的device-mapper来实现存储系统的虚拟化(系统分区独立于底层硬件)。 通过LVM,你可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),可以更简便地扩大和缩小分区,可以增删分区时无需担心某个硬盘上没有足够的连续空间, without getting caught up in the problems of fdisking a disk that is in use (and wondering whether the kernel is using the old or new partition table) and without having to move other partition out of the way. LVM是用来方便管理的,不会提供额外的安全保证。 However, it sits nicely with the other two technologies we are using.
LVM的基本组成块(building blocks)如下:
物理卷Physical volume (PV) :可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的header,其余部分被切割为一块块物理区域(physical extents)。 Think of physical volumes as big building blocks which can be used to build your hard drive.
** 卷组Volume group (VG)** :将一组物理卷收集为一个管理单元。Group of physical volumes that are used as storage volume (as one disk). They contain logical volumes. Think of volume groups as hard drives.
** 逻辑卷Logical volume (LV)** :虚拟分区,由物理区域(physical extents)组成。A “virtual/logical partition” that resides in a volume group and is composed of physical extents. Think of logical volumes as normal partitions.
** 物理区域Physical extent (PE)** :硬盘可供指派给逻辑卷的最小单位(通常为4MB)。A small part of a disk (usually 4MB) that can be assigned to a logical Volume. Think of physical extents as parts of disks that can be allocated to any partition.
优点
比起正常的硬盘分区管理,LVM更富于弹性:
使用卷组(VG),使众多硬盘空间看起来像一个大硬盘。
使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区。
可以创建小的逻辑卷(LV),在空间不足时再动态调整它的大小。
在调整逻辑卷(LV)大小时可以不用考虑逻辑卷在硬盘上的位置,不用担心没有可用的连续空间。It does not depend on the position of the LV within VG, there is no need to ensure surrounding available space.
Command (m forhelp): n Command action e extended p primary partition (1-4) #主分区 p #分区号 Partition number (1-4): 1 #回车用默认的1 First cylinder (1-1044, default 1): #大小 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): #改变类型 Command (m forhelp): t Selected partition 1 #LVM 的分区代码 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) #保存 w #退出 q
Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 11749 93858816 8e Linux LVM
--- Physical volume --- PV Name /dev/sdb1 VG Name vg_data PV Size 200.00 GiB / not usable 1.34 MiB Allocatable yes PE Size 4.00 MiB Total PE 51199 Free PE 7679 Allocated PE 43520 PV UUID c9js6G-WP93-ug9b-9aXe-unnZ-doWQ-AkzOKf
--- Physical volume --- PV Name /dev/sda2 VG Name VolGroup PV Size 89.51 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 22914 Free PE 386 Allocated PE 22528 PV UUID bFne0f-RnL8-vut2-AcKr-fr9k-VfeG-C8OHSR
Reading all physical volumes. This may take a while... Found volume group "vg_data" using metadata type lvm2 Found volume group "VolGroup" using metadata type lvm2
vgdisplay
--- Volume group --- VG Name vg_data System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size 200.00 GiB PE Size 4.00 MiB Total PE 51199 Alloc PE / Size 43520 / 170.00 GiB Free PE / Size 7679 / 30.00 GiB VG UUID EQhiUt-Waj1-F3V2-cwej-2joz-oC3P-JMSFui
--- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 5 Open LV 5 Max PV 0 Cur PV 1 Act PV 1 VG Size 89.51 GiB PE Size 4.00 MiB Total PE 22914 Alloc PE / Size 22528 / 88.00 GiB Free PE / Size 386 / 1.51 GiB VG UUID i0qDld-uOz4-P1BS-2gN8-2vhj-5gdc-DnHfa4
ACTIVE '/dev/vg_data/lv_oracle' [20.00 GiB] inherit ACTIVE '/dev/VolGroup/lv_root' [50.00 GiB] inherit ACTIVE '/dev/VolGroup/lv_home' [10.00 GiB] inherit
lvdisplay
--- Logical volume --- LV Path /dev/vg_data/lv_oracle LV Name lv_oracle VG Name vg_data LV UUID P3jVXt-92Lp-QElD-ddl8-lW4r-okz7-n8fdXD LV Write Access read/write LV Creation host, time capdb, 2016-03-17 17:12:43 +0800 LV Status available # open 1 LV Size 20.00 GiB Current LE 5120 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:5
#挂载逻辑卷 mkdir /oracle #修改fstab vi /etc/fstab /dev/vg_data/lv_oracle /oracle ext4 dafualts 0 0 #测试lv是否可以挂载 mount -a mount /dev/vg_data/lv_oracle /oracle df -h
--- Volume group --- VG Name vg_data System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size 200.00 GiB PE Size 4.00 MiB Total PE 51199 Alloc PE / Size 43520 / 170.00 GiB Free PE / Size 7679 / 30.00 GiB VG UUID EQhiUt-Waj1-F3V2-cwej-2joz-oC3P-JMSFui
Command (m forhelp): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1044, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G
Command (m forhelp): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM)
Command (m forhelp): w The partition table has been altered!
#创建一个物理卷 /dev/sdc1 pvcreate /dev/sdc1
#增加到已存在的卷组 vgextend vg_data /dev/sdc1
#验证卷组大小 vgdisplay
删除卷组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#卸载逻辑卷 umount /oracle #删除逻辑卷 lvremove /dev/vg_data/lv_oracle #取消激活卷组 vgchange -a n vg_data #删除卷组 vgremove vg_data #删除物理卷 pvremove /dev/sdb1 pvremove /dev/hda{6,7,8} #普通磁盘类型是83 fdisk /dev/sdb t 83