一、LVM是什么
LVM是 Logical Volume Manager(逻辑卷管理)的简写,简单的说LVM可以跨分区存储数据,并且LVM可以在无需停机的状态下动态调整分区的大小。
二、基本术语
*物理存储介质(PhysicalStorageMedia)
指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元。
*物理卷(Physical Volume,PV)
指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
*卷组(Volume Group,VG)
类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。
*逻辑卷(Logical Volume,LV)
类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。
*物理块(Physical Extent,PE)
每一个物理卷PV被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷(PV)由大小等同的基本单元PE组成。
*逻辑块(Logical Extent,LE)
每一个物理卷PV被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷(PV)由大小等同的基本单元PE组成。
逻辑卷LV也被划分为可被寻址的基本单位,称为LE。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。如图(1)
三、创建管理
常用管理命令如下:
管理lv:
pv: pvcreate, pvdisplay, pvs, pvscan, pvremove, pvmove
vg: vgcreate, vgdisplay, vgs, vgscan, vgremove, vgextend, vgreduce
lv: lvcreate, lvdisplay, lvs, lvresize, lvextend, lvreduce, lvremove
1、PV相关操作
创建pv
1.1、如图(2) 使用fdisk创建物理磁盘分区
1
2
3
4
5
6
7
8
9
10
11
|
[root@www ~]# fdisk -l /dev/sdb 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: 0xd9180e6e Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 8e Linux LVM /dev/sdb2 263 655 3156772+ 8e Linux LVM [root@www ~]# |
创建物理卷
pvcreate
1
2
3
|
[root@www ~]# pvcreate /dev/sdb1 /dev/sdb2 //创建PV
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
|
查看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
|
[root@www ~]# pvs //查看PV
PV VG Fmt Attr PSize PFree
/dev/sda2 vg0 lvm2 a-- 59.99g 7.99g
/dev/sdb1 lvm2 --- 2.01g 2.01g
/dev/sdb2 lvm2 --- 3.01g 3.01g
[root@www ~]# pvdisplay /dev/sdb[0-9] //可以看到详细信息
--- Physical volume ---
PV Name /dev/sdb1
VG Name myvg
PV Size 2.01 GiB / not usable 3.16 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 513
Free PE 513
Allocated PE 0
PV UUID rA5Vdy-j2rr-b9Vf-zo8I-sjFD-1z23-VhHAgI
--- Physical volume ---
PV Name /dev/sdb2
VG Name myvg
PV Size 3.01 GiB / not usable 2.79 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 770
Free PE 770
Allocated PE 0
PV UUID GVtLyK-Ugd1-DKHo-3wHS-oia1-lbpz-PA4oma
[root@www ~]# |
删除pv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@www ~]# pvcreate /dev/sdb3 //创建pv
Physical volume "/dev/sdb3" successfully created
[root@www ~]# pvs //查看pv
PV VG Fmt Attr PSize PFree
/dev/sda2 vg0 lvm2 a-- 59.99g 7.99g
/dev/sdb1 myvg lvm2 a-- 2.00g 0
/dev/sdb2 myvg lvm2 a-- 3.01g 2.01g
/dev/sdb3 lvm2 --- 3.01g 3.01g
[root@www ~]# pvremove /dev/sdb3 //删除pv /dev/sdb3
Labels on physical volume "/dev/sdb3" successfully wiped
[root@www ~]# pvs PV VG Fmt Attr PSize PFree
/dev/sda2 vg0 lvm2 a-- 59.99g 7.99g
/dev/sdb1 myvg lvm2 a-- 2.00g 0
/dev/sdb2 myvg lvm2 a-- 3.01g 2.01g
|
2、VG相关操作
创建VG
vgcreate
# vgcreate VGNAME /dev/PV1...
[-s|--physicalextentsize PhysicalExtentSize[bBsSkKmMgGtTpPeE]] //指定PE大小
[-t|--test] //测试,不真正创建
[-v|--verbose]//显示创建过程
1
2
|
[root@www ~]# vgcreate myvg /dev/sdb{1,2} Volume group "myvg" successfully created
|
查看VG
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
|
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 5.01g 5.01g
vg0 1 4 0 wz--n- 59.99g 7.99g
[root@www ~]# vgdisplay --- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 5.01 GiB
PE Size 4.00 MiB //PE 块大小
Total PE 1283
Alloc PE / Size 0 / 0
Free PE / Size 1283 / 5.01 GiB
VG UUID llSGpO-Bk51-KVBB-llAZ-AA9E-aBYq-sUrnPL
|
删除vg
# vgremove VGNAME...
1
2
3
4
5
6
7
8
9
10
|
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 5.01g 5.01g
vg0 1 4 0 wz--n- 59.99g 7.99g
[root@www ~]# vgremove myvg //删除vg
Volume group "myvg" successfully removed
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
vg0 1 4 0 wz--n- 59.99g 7.99g
[root@www ~]# |
缩减VG:通过从vg中移除pv实现
注意:计划移除的pv不能包含已经存储数据的pe;如果有需要先使用pvmove移动到同vg内其它pv上;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@www ~]# vgcreate myvg /dev/sdb{1,2} Volume group "myvg" successfully created
[root@www ~]# vgdisplay --- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 5.01 GiB //大小为5G
PE Size 4.00 MiB
Total PE 1283
Alloc PE / Size 0 / 0
Free PE / Size 1283 / 5.01 GiB
VG UUID Iz9bc0-Sw38-H916-TAg3-abNh-iUJW-hjuRoB
|
------------------------------------------------------------------------------
我们要在myvg中移除/dev/sdb2 【缩减vg要保证缩减后的vg大小能够存储原来的数据】
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
|
[root@www ~]# pvmove /dev/sdb2 // 将/sdb2中的数据移动到其他pv上
No data to move for myvg
[root@www ~]# vgreduce myvg /dev/sdb2 //移除/dev/sdb2
Removed "/dev/sdb2" from volume group "myvg"
[root@www ~]# vgdisplay myvg --- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 2.00 GiB // 大小变为2G
PE Size 4.00 MiB
Total PE 513
Alloc PE / Size 0 / 0
Free PE / Size 513 / 2.00 GiB
VG UUID Iz9bc0-Sw38-H916-TAg3-abNh-iUJW-hjuRoB
|
-----------------------
扩展VG: 向VG中添加更多的PV来实现
vgextend
1
2
3
4
5
6
7
8
9
10
|
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- 2.00g 2.00g //扩展前大小为2G
vg0 1 4 0 wz--n- 59.99g 7.99g
[root@www ~]# vgextend myvg /dev/sdb2 //向myvg中添加/dev/sdb2
Volume group "myvg" successfully extended
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 5.01g 5.01g //大小变为5G
vg0 1 4 0 wz--n- 59.99g 7.99g
|
3、LV相关操作
创建LV Logical Volume
创建lv:
lvcreate -L #[bBsSkKmMgGtTpPeE] [-n|--name LogicalVolume{Name|Path}] VG_NAME
LV设备路径:
(1) /dev/VGNAME/LVNAME
(2) /dev/mapper/VGNAME-LVNAME
实际路径:/dev/dm-#
注意:lv的大小必须为pe的整数倍
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
|
[root@www ~]# lvcreate -L 4G -n mylv myvg //创建大小为4G的LV 名字为mylv
Logical volume "mylv" created
[root@www ~]# lvs //查看LV
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 4.00g //大小为4G
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@www ~]# lvdisplay --- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID i3tDAL-GdB4-TVIf-Efun-vVmg-Ig00-cSCdKk
LV Write Access read/write
LV Creation host, time www.w1.com, 2015-08-16 05:05:40 +0800
LV Status available
# open 0
LV Size 4.00 GiB
Current LE 1024
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
|
移除LV
lvremove LV_PATH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@www ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 4.00g //查看lv
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@www ~]# lvremove /dev/myvg/mylv //移除mylv
Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed
[root@www ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root vg0 -wi-ao---- 20.00g //已经不存在mylv
swap vg0 -wi-ao---- 2.00g
usr vg0 -wi-ao---- 10.00g
var vg0 -wi-ao---- 20.00g
[root@www ~]# |
扩展LV
存储设备有:物理边界
文件系统有:逻辑边界
扩展:先扩展物理边界,而后拉伸逻辑边界
lvextend, lvresize
resize2fs
扩展LV步骤
1、先确定目标大小:要确保此lv所在vg中有足够的空闲可用;
2G --> 4G
4G
+2G
2、扩展物理边界
lvextend -L [+]#[bBsSkKmMgGtTpPeE] /dev/VGNAME/LVNAME
3、拉伸逻辑边界
resize2fs /dev/VGNAME/LVNAME
例:先创建大小为2G的LV然后在扩展为4G
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@www ~]# lvcreate -L 2G -n mylv myvg Logical volume "mylv" created
[root@www ~]# lvdisplay /dev/myvg/mylv --- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID yZE2SK-G9J9-dFdH-uJvK-Lln6-AS2G-UqHMjU
LV Write Access read/write
LV Creation host, time www.w1.com, 2015-08-16 05:12:04 +0800
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
|
格式化/myvg/mylv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@www ~]# mke2fs -t ext4 /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user
First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@www ~]# mkdir /nyist //创建挂载点
[root@www ~]# mount /dev/myvg/mylv /nyist //挂载
[root@www ~]# df -h //查看大小
Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 18G 908M 96% / tmpfs 565M 0 565M 0% /dev/shm /dev/sda1 190M 33M 148M 18% /boot /dev/mapper/vg0-usr 9.8G 2.4G 6.9G 26% /usr /dev/mapper/vg0- var 20G 294M 19G 2% / var
/dev/sda3 59G 4.7G 52G 9% /usr/soc /dev/mapper/myvg-mylv 2.0G 3.0M 1.9G 1% /nyist
|
1
|
[root@www ~]# cp /etc/fstab /nyist |
查看文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@www ~]# cat /nyist/fstab # # /etc/fstab # Created by anaconda on Thu Mar 26 20:01:38 2015 # # Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and / or blkid(8) for more info
# /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=0d0698ff-1645-4ff8-af27-fc6e73573941 /boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0- var / var ext4 defaults 1 2
/dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sda3 /usr/soc ext4 defaults 0 0 [root@www ~]# |
查看VG剩余空间:
1
2
3
4
|
[root@www ~]# vgs VG #PV #LV #SN Attr VSize VFree
myvg 2 1 0 wz--n- 5.01g 3.01g //还有3G 我们要扩展到4G 容量足够
vg0 1 4 0 wz--n- 59.99g 7.99g
|
扩展物理边界
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
|
[root@www ~]# lvs /dev/myvg/mylv LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 2.00g //目前大小为2G
[root@www ~]# lvextend -L 4G /dev/myvg/mylv //扩展LV 也可以写为: lvextend -L +2G /vev/myvg/mylv Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
Logical volume mylv successfully resized
[root@www ~]# lvs /dev/myvg/mylv查看mylv大小 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 4.00g //扩展为4G
[root@www ~]# [root@www ~]# lvdisplay /dev/myvg/mylv --- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID yZE2SK-G9J9-dFdH-uJvK-Lln6-AS2G-UqHMjU
LV Write Access read/write
LV Creation host, time www.w1.com, 2015-08-16 05:12:04 +0800
LV Status available
# open 0
LV Size 4.00 GiB //已经扩展为4G
Current LE 1024
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
|
查看逻辑空间大小
1
2
3
4
5
6
7
|
[root@www ~]# df -h Filesystem Size Used Avail Use% Mounted on ...... /dev/sda3 59G 4.7G 52G 9% /usr/soc /dev/mapper/myvg-mylv 2.0G 3.1M 1.9G 1% /nyist //依然为2G
[root@www ~]# |
拉伸逻辑边界
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@www ~]# resize2fs /dev/myvg/mylv //扩展逻辑边界
resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /nyist; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv to 1048576 (4k) blocks. The filesystem on /dev/myvg/mylv is now 1048576 blocks long. [root@www ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 18G 908M 96% / tmpfs 565M 0 565M 0% /dev/shm /dev/sda1 190M 33M 148M 18% /boot /dev/mapper/vg0-usr 9.8G 2.4G 6.9G 26% /usr /dev/mapper/vg0- var 20G 294M 19G 2% / var
/dev/sda3 59G 4.7G 52G 9% /usr/soc /dev/mapper/myvg-mylv 3.9G 4.0M 3.7G 1% /nyist //大小已经为3.9G 成功扩展
[root@www ~]# |
============================================================
缩减LV
注意:
1、缩减LV必须要离线
2、必须先缩减文件系统,然后再缩减LV大小
过程:
1、先确定缩减后的目标大小:要确保缩减后的大小能够容纳现在的所有数据;
2、卸载逻辑卷,并强制做文件系统一致性检查;
e2fsck -f /dev/VGNAME/LVNAME
3、缩减逻辑边界
resize2fs /dev/VGNAME/LVNAME #[bBsSkKmMgGtTpPeE]
// resize2fs /dev/myvg/mylv 3G
4、缩减物理边界
lvreduce -L [-]#[bBsSkKmMgGtTpPeE] /dev/VGNAME/LVNAME
//lvreduce -L 3G /dev/myvg/mylv //缩减到3G
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@www ~]# umount /nyist [root@www ~]# e2fsck -f /dev/myvg/mylv 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/myvg/mylv: 12/262144 files (0.0% non-contiguous), 33871/1048576 blocks [root@www ~]# resize2fs /dev/myvg/mylv 3G //缩减逻辑边界
resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 786432 (4k) blocks. The filesystem on /dev/myvg/mylv is now 786432 blocks long. [root@www ~]# lvreduce -L 3G /dev/myvg/mylv //缩减物理边界
WARNING: Reducing active logical volume to 3.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y Size of logical volume myvg/mylv changed from 4.00 GiB (1024 extents) to 3.00 GiB (768 extents).
Logical volume mylv successfully resized
[root@www ~]# |
验证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@www ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 3.00g //大小3G
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@www ~]# mount /dev/myvg/mylv /nyist [root@www ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 18G 908M 96% / tmpfs 565M 0 565M 0% /dev/shm /dev/sda1 190M 33M 148M 18% /boot /dev/mapper/vg0-usr 9.8G 2.4G 6.9G 26% /usr /dev/mapper/vg0- var 20G 294M 19G 2% / var
/dev/sda3 59G 4.7G 52G 9% /usr/soc /dev/mapper/myvg-mylv 2.9G 3.1M 2.8G 1% /nyist //大小2.9G
[root@www ~]# |