lvm使用

LVM(Logic Volume Manager)逻辑卷管理,它是linux环境下对磁盘分区进行管理的一种机制。

TODO

基本术语

  • PV: Physical Volume 物理卷
  • VG: Volume Group 卷组,由一个或多个PV组成。可以在其上创建一个或多个LV
  • LV: Logical Volume 逻辑卷,在其上可以创建文件系统
  • PE: Physical Extent 物理扩展盘区,每个PV都会被划分成PE,它是可以被LVM寻址的最小单元
  • LE: Logical Extent 逻辑扩展盘区,

基本原理

1) 物理磁盘被格式化为PV,空间被划分为一个个PE
2) 不同的PV加入到同一个VG中,其对应的所有PE都进入VG的PE池中
3) LVM从PE池中选择PE创建LV,不同物理盘中的PE可能会划分到同一个LV中
4) 在LV上创建文件系统后就可以挂载使用了
5) 对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
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
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +300M
Partition 1 of type Linux and of size 300 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x233e2ebd

Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x233e2ebd

Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建pv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 300.00m 300.00m
[root@localhost ~]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 300.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 74
Free PE 74
Allocated PE 0
PV UUID 8dqEi7-haMt-ciIM-ib2e-AUxp-f7ym-bCvlfw

创建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
26
[root@localhost ~]# vgcreate vg0 /dev/sdb1
Volume group "vg0" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- 296.00m 296.00m
[root@localhost ~]# vgdisplay vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 296.00 MiB
PE Size 4.00 MiB
Total PE 74
Alloc PE / Size 0 / 0 #已分配的PE数量
Free PE / Size 74 / 296.00 MiB #剩余的PE数量
VG UUID 3FH6og-yXLt-NaL3-uehk-9lrJ-c4TD-r0z6Sa

创建lv

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
[root@localhost ~]# lvcreate -L 100m -n lv0 vg0
Logical volume "lv0" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 100.00m
[root@localhost ~]# lvdisplay /dev/vg0/lv0
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID Plb1kl-JGcg-0UfR-W3Ue-tDBc-D5Tc-tj6X0n
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-16 02:02:27 +0800
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

[root@localhost ~]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 300.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 74
Free PE 49
Allocated PE 25 # 可以看到已经有PE被分配出去了
PV UUID 8dqEi7-haMt-ciIM-ib2e-AUxp-f7ym-bCvlfw
[root@localhost ~]# vgdisplay vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 296.00 MiB
PE Size 4.00 MiB
Total PE 74
Alloc PE / Size 25 / 100.00 MiB # 可以看到已经有PE被分配出去了
Free PE / Size 49 / 196.00 MiB
VG UUID 3FH6og-yXLt-NaL3-uehk-9lrJ-c4TD-r0z6Sa

创建LV时LV的大小有以下两种方式指定

  • 通过-L参数,表示物理大小
  • 通过-l参数,使用相关VG/LV或PV大小的百分比来指定
    • 10%VG, 表示10%的VG总大小
    • 20%FREE, 表示VG中剩余空间的20%
    • 30%PVS, 表示PV集合中剩余空间的30%
    • 该参数后面跟数值(非百分比)时,表示包含多少个PE大小。
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# lvremove /dev/vg0/lv0
Do you really want to remove active logical volume vg0/lv0? [y/n]: y
Logical volume "lv0" successfully removed
[root@localhost ~]# lvcreate -l 30%VG -n lv1 vg0
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-a----- 88.00m
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <9.00g 0
vg0 1 1 0 wz--n- 296.00m 208.00m
[root@localhost ~]#

格式化文件系统

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
[root@localhost ~]# mkfs.ext3 /dev/vg0/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
22528 inodes, 90112 blocks
4505 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
11 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

[root@localhost ~]# fdisk -l /dev/vg0/lv1

Disk /dev/vg0/lv1: 92 MB, 92274688 bytes, 180224 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]# mkdir /usr1
[root@localhost ~]# mount /dev/vg0/lv1 /usr1
[root@localhost ~]# df -ha
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv1 82M 1.6M 76M 2% /usr1
[root@localhost ~]#

扩容

扩容LV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-ao---- 88.00m
[root@localhost ~]# lvextend -L +100m /dev/vg0/lv1
Size of logical volume vg0/lv1 changed from 88.00 MiB (22 extents) to 188.00 MiB (47 extents).
Logical volume vg0/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-ao---- 188.00m
[root@localhost ~]# df -ha
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv1 82M 1.6M 76M 2% /usr1 # LV已经扩容但是文件系统大小还时原来大小
[root@localhost ~]# resize2fs /dev/vg0/lv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv1 is mounted on /usr1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv1 is now 192512 blocks long.
[root@localhost ~]# df -ha
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv1 178M 1.6M 169M 1% /usr1 # 已经扩容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# lvs |grep lv1
lv1 vg0 -wi-ao---- 188.00m
[root@localhost ~]# lvextend -l +50%FREE /dev/vg0/lv1
Size of logical volume vg0/lv1 changed from 188.00 MiB (47 extents) to 244.00 MiB (61 extents).
Logical volume vg0/lv1 successfully resized.
[root@localhost ~]# lvs |grep lv1
lv1 vg0 -wi-ao---- 244.00m
[root@localhost ~]# df -ha |grep lv1
/dev/mapper/vg0-lv1 178M 1.6M 169M 1% /usr1
[root@localhost ~]# resize2fs /dev/mapper/vg0-lv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg0-lv1 is mounted on /usr1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg0-lv1 is now 249856 blocks long.
[root@localhost ~]# df -ha |grep lv1
/dev/mapper/vg0-lv1 233M 2.1M 220M 1% /usr1

lvextend 中 -L-l 参数中如果没有使用 + 号,则表示扩容到指定大小

扩容VG

当VG中没有空闲资源时需要先扩容VG才能扩容LV

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@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 296.00m 52.00m
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 296.00m 52.00m
/dev/sdc1 lvm2 --- 511.00m 511.00m
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 296.00m 52.00m
[root@localhost ~]# vgextend vg0 /dev/sdc1
Volume group "vg0" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 804.00m 560.00m
[root@localhost ~]#
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-a----- 244.00m
[root@localhost ~]# lvextend -l +100%FREE /dev/vg0/lv1
Size of logical volume vg0/lv1 changed from 244.00 MiB (61 extents) to 804.00 MiB (201 extents).
Logical volume vg0/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-a----- 804.00m

缩减容量

  • 先umount设备
  • 缩减文件系统大小
  • 缩减LV大小
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
[root@localhost ~]# umount -l /usr1
[root@localhost ~]# resize2fs /dev/mapper/vg0-lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Please run 'e2fsck -f /dev/mapper/vg0-lv1' first.
[root@localhost ~]# e2fsck -f /dev/mapper/vg0-lv1
e2fsck 1.42.9 (28-Dec-2013)
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/mapper/vg0-lv1: 2369/206848 files (6.4% non-contiguous), 61211/823296 blocks
[root@localhost ~]#
[root@localhost ~]# resize2fs /dev/mapper/vg0-lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg0-lv1 to 102400 (1k) blocks.
The filesystem on /dev/mapper/vg0-lv1 is now 102400 blocks long.
[root@localhost ~]# lvreduce -L 100M /dev/mapper/vg0-lv1
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv1? [y/n]: y
Size of logical volume vg0/lv1 changed from 804.00 MiB (201 extents) to 100.00 MiB (25 extents).
Logical volume vg0/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg0 -wi-a----- 100.00m
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 804.00m 704.00m
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 296.00m 196.00m
/dev/sdc1 vg0 lvm2 a-- 508.00m 508.00m

分区扩展

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
root@baoze:# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <28.00g 0
root@baoze:# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop1 7:1 0 62M 1 loop /snap/core20/1587
loop2 7:2 0 79.9M 1 loop /snap/lxd/22923
loop3 7:3 0 49.6M 1 loop /snap/snapd/17883
loop4 7:4 0 63.3M 1 loop /snap/core20/1778
loop5 7:5 0 103M 1 loop /snap/lxd/23541
sda 8:0 0 80G 0 disk ### 当前sda总大小80G
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 28G 0 part ### 但是sd3只有28G
└─ubuntu--vg-ubuntu--lv 253:0 0 28G 0 lvm /
sr0 11:0 1 1.4G 0 rom

#### 把sda中空闲的区域扩展到sd3中
root@baoze:# growpart /dev/sda 3
CHANGED: partition=3 start=4198400 old: size=58714112 end=62912512 new: size=163573727 end=167772127
root@baoze:# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop1 7:1 0 62M 1 loop /snap/core20/1587
loop2 7:2 0 79.9M 1 loop /snap/lxd/22923
loop3 7:3 0 49.6M 1 loop /snap/snapd/17883
loop4 7:4 0 63.3M 1 loop /snap/core20/1778
loop5 7:5 0 103M 1 loop /snap/lxd/23541
sda 8:0 0 80G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 78G 0 part ### 当前sd3中的空间已经扩展到78G
└─ubuntu--vg-ubuntu--lv 253:0 0 28G 0 lvm /
sr0 11:0 1 1.4G 0 rom
root@baoze:# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <78.00g 50.00g ### pvs中也可以看到空闲的PE了