一、LVM概念
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logical volumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。当系统添加了新的磁盘,通过LVM管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可。
二、LVM结构
物理存储介质(The physical media)
这里指系统的存储设备:硬盘,如:/dev/hda、/dev/sda等等,是存储系统最低层的存储单元。
物理卷PV(physical volume)
物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组VG(Volume Group)
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷PV组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷PV组成。
物理块PE(physical extent)
每一个物理卷PV被划分为被称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
逻辑块LE(logical extent)
逻辑卷也被划分为被称为逻辑块LE的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
逻辑卷LV(logical volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在VG上面割取PE就组成了LV,是一个标准的块设备,可以在其上建立文件系统(比如/home或者/usr等)。
Tips:从一块硬盘到能使用LV文件系统的步骤:硬盘→分区→(PV→VG→LV)→格式化(LV为ext文件系统)→挂载
三、创建LV
创建LV前需要创建VG,而创建VG前需要创建PV,所以我们要先创建PV再创建VG最后再创建LV,这里利用/dev/sdb盘来创建
创建PV:pvcreate /dev/sdbx
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
[root@justin ~]
# fdisk -l
Disk
/dev/sda
: 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: 0x000661de
Device Boot Start End Blocks Id System
/dev/sda1
* 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
26 1332 10485760 83 Linux
/dev/sda3
1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4
1593 2611 8182784 5 Extended
/dev/sda5
1593 2611 8181760 83 Linux
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 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: 0x00000000
[root@justin ~]
# fdisk /dev/sdb
Command (m
for
help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652
Command (m
for
help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +3G
Command (m
for
help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (394-652, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-652, default 652):
Using default value 652
Command (m
for
help): p
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 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: 0xd58eff7b
Device Boot Start End Blocks Id System
/dev/sdb4
1 652 5237158+ 5 Extended
/dev/sdb5
1 393 3156709+ 83 Linux
/dev/sdb6
394 652 2080386 83 Linux
Command (m
for
help): t
Partition number (1-6): 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-6): 6
Hex code (
type
L to list codes): 8e
Changed system
type
of partition 6 to 8e (Linux LVM)
Command (m
for
help): p
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 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: 0xd58eff7b
Device Boot Start End Blocks Id System
/dev/sdb4
1 652 5237158+ 5 Extended
/dev/sdb5
1 393 3156709+ 8e Linux LVM
/dev/sdb6
394 652 2080386 8e Linux LVM
Command (m
for
help): w
The partition table has been altered!
Calling ioctl() to re-
read
partition table.
Syncing disks.
[root@justin ~]
# pvcreate /dev/sdb5 /dev/sdb6
Writing physical volume data to disk
"/dev/sdb5"
Physical volume
"/dev/sdb5"
successfully created
Writing physical volume data to disk
"/dev/sdb6"
Physical volume
"/dev/sdb6"
successfully created
[root@justin ~]
# partprobe /dev/sdb ;使用工具partprobe让kernel读取分区信息
[root@justin ~]
# fdisk -l
Disk
/dev/sda
: 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: 0x000661de
Device Boot Start End Blocks Id System
/dev/sda1
* 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
26 1332 10485760 83 Linux
/dev/sda3
1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4
1593 2611 8182784 5 Extended
/dev/sda5
1593 2611 8181760 83 Linux
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 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: 0xd58eff7b
Device Boot Start End Blocks Id System
/dev/sdb4
1 652 5237158+ 5 Extended
/dev/sdb5
1 393 3156709+ 8e Linux LVM
/dev/sdb6
394 652 2080386 8e Linux LVM
[root@justin ~]
# pvdisplay ;查看PV信息
"/dev/sdb5"
is a new physical volume of
"3.01 GiB"
--- NEW Physical volume ---
PV Name
/dev/sdb5
VG Name
PV Size 3.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Pz14Rv-2Ngu-lbdD-5dgF-CLU1-cQ9p-bDAf52
"/dev/sdb6"
is a new physical volume of
"1.98 GiB"
--- NEW Physical volume ---
PV Name
/dev/sdb6
VG Name
PV Size 1.98 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 9sIqz9-iXNv-kloo-p55V-ZzMT-eGD0-Tf2iEn
[root@justin ~]
#
|
VG创建:vgcreate 卷组名 物理卷
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@justin ~]
# vgcreate VGtest /dev/sdb5 /dev/sdb6
Volume group
"VGtest"
successfully created
[root@justin ~]
# vgdisplay
--- Volume group ---
VG Name VGtest
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 4.99 GiB
PE Size 4.00 MiB
Total PE 1277
Alloc PE / Size 0 / 0
Free PE / Size 1277 / 4.99 GiB
VG UUID TmBe8X-wSlx-uJDy-1f0W-m4yj-31E7-zPtyqS
[root@justin ~]
#
|
创建LV:lvcreate -L 大小 卷组名 -n 逻辑卷名称
-L:指定逻辑卷大小 -n:指定逻辑卷名称
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
|
[root@justin ~]
# lvcreate -L 4G VGtest -n LVtest
Logical volume
"LVtest"
created
[root@justin ~]
# lvdisplay
--- Logical volume ---
LV Path
/dev/VGtest/LVtest
LV Name LVtest
VG Name VGtest
LV UUID nBIB0X-1ZON-hFhv-oDF3-8Dfb-61Vt-AeiGUM
LV Write Access
read
/write
LV Creation host,
time
justin, 2013-11-15 14:22:00 +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:0
[root@justin ~]
# lvcreate -L 1G VGtest -n LVtest1
Volume group
"VGtest"
has insufficient
free
space (253 extents): 256 required.
[root@justin ~]
# lvcreate -L VGtest -n LVtest1
Invalid argument
for
--size: VGtest
Error during parsing of
command
line.
[root@justin ~]
# lvcreate -L 256M VGtest -n LVtest1
Logical volume
"LVtest1"
created
[root@justin ~]
# lvdisplay
--- Logical volume ---
LV Path
/dev/VGtest/LVtest
LV Name LVtest
VG Name VGtest
LV UUID nBIB0X-1ZON-hFhv-oDF3-8Dfb-61Vt-AeiGUM
LV Write Access
read
/write
LV Creation host,
time
justin, 2013-11-15 14:22:00 +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:0
--- Logical volume ---
LV Path
/dev/VGtest/LVtest1
LV Name LVtest1
VG Name VGtest
LV UUID 6AggK6-XjPH-Jgt0-A4pI-2Fnw-cHKf-hM5mba
LV Write Access
read
/write
LV Creation host,
time
justin, 2013-11-15 14:23:00 +0800
LV Status available
# open 0
LV Size 256.00 MiB
Current LE 64
Segments 1
Allocation inherit
Read ahead sectors auto
- currently
set
to 256
Block device 253:1
[root@justin ~]
#
|
格式化和挂载:mkfs [-t 系统类型] 分区 、mkfs.extn 分区 mount [-t 系统类型] 要挂载的分区 挂载到的分区
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
|
[root@justin ~]
# mkfs.ext4 /dev/VGtest/LVtest ;对应LV Path名称
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
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved
for
the super user
First data block=0
Maximum filesystem blocks=1073741824
|