前言
Logical Volume Manager (LVM),逻辑卷管理LVM是一个多才多艺的硬盘系统工具,无论在Linux或者其他类似的系统,都是非常的好用。传统分区使用固定大小分区,重新调整大小十分麻烦,但是LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘,可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷,LVM并不需要重启就可以让内核知道分区的存在。文章详细记录在PV/VG/LV中3个阶段的创建/添加/扩展/减小/删除等实战操作步骤,方便自己回顾一些基础用法,也希望能够帮助大家更好的理解LVM的原理。
LVM是一个非常给力的工具,用来创建和管理可变大小的分区
LVM基本组成
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.
-
可以在线(online)对逻辑卷(LV)和卷组(VG)进行创建、删除、调整大小等操作。LVM上的文件系统也需要重新调整大小,某些文件系统也支持这样的在线操作。
-
无需重新启动服务,就可以将服务中用到的逻辑卷(LV)在线(online)/动态(live)迁移至别的硬盘上。
-
允许创建快照,可以保存文件系统的备份,同时使服务的下线时间(downtime)降低到最小。
这些优点使得LVM对服务器的管理非常有用,对于桌面系统管理的帮助则没有那么显著,你需要根据实际情况进行取舍。
LVM
#新建
fdisk /dev/sdb
Command (m for help): 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 for help): 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
#查看创建完后的分区
fdisk -l
Disk /dev/sda: 96.6 GB, 96636764160 bytes
255 heads, 63 sectors/track, 11748 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: 0x0008cbaf
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
Disk /dev/sdb: 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: 0x16396101
Device Boot Start End Blocks Id System
/dev/sdb1 1 26108 209712478+ 8e Linux LVM
#创建物理卷(PV)
pvcreate /dev/sdb1
pvscan
PV /dev/sdb1 VG vg_data lvm2 [200.00 GiB / 30.00 GiB free]
PV /dev/sda2 VG VolGroup lvm2 [89.51 GiB / 1.51 GiB free]
Total: 2 [289.50 GiB] / in use: 2 [289.50 GiB] / in no VG: 0 [0 ]
pvdisplay
--- 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
#删除物理卷
pvremove /dev/sdb1
#创建卷组(VG)
vgcreate vg_data /dev/sdb1
vgcreate vg_data /dev/hda{6,7,8}
#扩展VG
vgextend vg_data /dev/hda9
#查看VG信息
vgscan
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
#删除VG
vgremove vg_data
#创建逻辑卷(LV)
lvcreate -L 20g -n lv_Oracle vg_data
lvcreate -l 100%FREE -n lv_oracle vg_data
#查看LV
lvscan
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
#格式化逻辑卷
mkfs -t ext4 /dev/vg_data/lv_oracle
mkfs.ext4 /dev/vg_data/lv_oracle
#挂载逻辑卷
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
#删除逻辑卷
fuser -m /oracle
umount /oracle
lvremove /dev/vg_data/lv_oracle
LVM扩展
LVM特点就是支持在线动态扩容,如果为了稳妥也可以先umount
#新建
fdisk /dev/sdb
Command (m for help): 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 for help): 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
#查看创建完后的分区
fdisk -l
Disk /dev/sda: 96.6 GB, 96636764160 bytes
255 heads, 63 sectors/track, 11748 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: 0x0008cbaf
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
Disk /dev/sdb: 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: 0x16396101
Device Boot Start End Blocks Id System
/dev/sdb1 1 26108 209712478+ 8e Linux LVM
#创建物理卷(PV)
pvcreate /dev/sdb1
pvscan
PV /dev/sdb1 VG vg_data lvm2 [200.00 GiB / 30.00 GiB free]
PV /dev/sda2 VG VolGroup lvm2 [89.51 GiB / 1.51 GiB free]
Total: 2 [289.50 GiB] / in use: 2 [289.50 GiB] / in no VG: 0 [0 ]
pvdisplay
--- 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
#删除物理卷
pvremove /dev/sdb1
#创建卷组(VG)
vgcreate vg_data /dev/sdb1
vgcreate vg_data /dev/hda{6,7,8}
#扩展VG
vgextend vg_data /dev/hda9
#查看VG信息
vgscan
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
#删除VG
vgremove vg_data
#创建逻辑卷(LV)
lvcreate -L 20g -n lv_Oracle vg_data
lvcreate -l 100%FREE -n lv_oracle vg_data
#查看LV
lvscan
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
#格式化逻辑卷
mkfs -t ext4 /dev/vg_data/lv_oracle
mkfs.ext4 /dev/vg_data/lv_oracle
#挂载逻辑卷
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
#删除逻辑卷
fuser -m /oracle
umount /oracle
lvremove /dev/vg_data/lv_oracle
LVM特点就是支持在线动态扩容,如果为了稳妥也可以先umount
#查看Free PE / Size可用空间
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
#设置逻辑卷的大小
lvresize -L 40g /dev/vg_data/lv_oracle
lvresize -L +20g /dev/vg_data/lv_oracle
#检查磁盘错误,等待时间较长可以考虑跳过
e2fsck -f /dev/vg_data/lv_oracle
#更新文件系统信息
resize2fs /dev/vg_data/lv_oracle
#检查逻辑卷状态
lvdisplay vg_data
LVM缩减
注意减少的大小,实施前umount逻辑卷
注意减少的大小,实施前umount逻辑卷
#卸载LV
umout /oracle
#检查磁盘错误
e2fsck -f /dev/vg_data/lv_oracle
#缩小文件系统,更新文件系统信息
resize2fs /dev/vg_data/lv_oracle 10g
#减少LV大小
lvresize -L 10g /dev/vg_data/lv_oracle
lvresize -L -30g /dev/vg_data/lv_oracle
#检查逻辑卷大小
lvdisplay
df -h
扩展卷组
步骤和创建VG类似
步骤和创建VG类似
#创建一个2GB分区sdc1,类型为LVM(8e)
fdisk /dev/sdc
Command (m for help): 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 for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
#创建一个物理卷 /dev/sdc1
pvcreate /dev/sdc1
#增加到已存在的卷组
vgextend vg_data /dev/sdc1
#验证卷组大小
vgdisplay
删除卷组
#卸载逻辑卷
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
lvchange
在做HA共享存储时需要配合lvchange激活lv
在做HA共享存储时需要配合lvchange激活lv
lvscan
lvchange -a y /dev/vg_data/lv_oradata
命令总结
任务 | PV 阶段 | VG 阶段 | LV 阶段 |
---|---|---|---|
搜寻 (scan) | pvscan | vgscan | lvscan |
创建 (create) | pvcreate | vgcreate | lvcreate |
列出 (display) | pvdisplay | vgdisplay | lvdisplay |
扩展 (extend) | vgextend | lvextend (lvresize) | |
减少 (reduce) | vgreduce | lvreduce (lvresize) | |
删除 (remove) | pvremove | vgremove | lvremove |
改变容量 (resize) | lvresize | ||
改变属性 (attribute) | pvchange | vgchange | lvchange |
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138373.htm