在系统运维和服务器管理过程中,经常遇到服务器磁盘容量不足,需要在线扩容的情况。普通磁盘分区的管理方式在逻辑分区划好之后就无法改变其大小。而LVM可以实现Linux服务器下面磁盘空间的在线扩容和动态管理,相对于普通的磁盘分区有很大的灵活性。
一、LVM简介
LVM是 Logical VolumeManager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM将一个或多个硬盘的分区在逻辑上进行组合,做为一个大的硬盘空间来使用,当硬盘的剩余空间不够的时候,可以将其它的硬盘加入到分区当中,这样可以实现磁盘空间的动态管理。
二、LVM基本术语
物理卷(physical volume,PV):物理卷就是指硬盘分区,也可以是整个硬盘或已创建的RAID ,是LVM的基本存储设备,与普通物理存储介质的区别是该设备包含有LVM相关的管理参数。
卷组(volume group,VG):卷组是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个“LVM分区”(逻辑卷)。
逻辑卷(logical volume,LV):LVM的逻辑卷类似于非LVM系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。
物理块(physical extent,PE):物理卷以大小相等的物理块为存储的基本单位,同时也是LVM寻址的最小单元。
逻辑块(logical extent,LE):逻辑卷以大小相等的逻辑块为存储的基本单位,在同一个卷组中,LE的大小和PE是相等的,并且一一对应。
三、通过LVM分区方式安装Centos
1.选择安装类型,选择最后一个复选框,即创建自定义布局。
2.首先划分/boot分区,/boot分区建议使用物理分区。
3.将剩余空间划分为一个物理卷(PV)。
4.创建一个卷组(VG)。默认卷组名为VolGroup00,修改卷组名为vg51cto_lv。
5.在VG上面创建逻辑卷(LV)。逻辑卷默认为LogVol00,LogVol01……末两位由00开始依次递增。
6.LVM分区完成。
7.df -h 查看磁盘空间。
1
2
3
4
5
6
7
8
|
[root@hadoop01 ~]
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg51cto_lv-LogVol01
9.9G 1.2G 8.2G 13% /
tmpfs 495M 0 495M 0%
/dev/shm
/dev/sda1
194M 29M 155M 16%
/boot
/dev/mapper/vg51cto_lv-LogVol03
20G 172M 19G 1%
/data
/dev/mapper/vg51cto_lv-LogVol02
5.0G 138M 4.6G 3%
/home
/dev/mapper/vg51cto_lv-LogVol04
23G 173M 22G 1%
/usr/local
|
四、LVM管理
第一部分:卷组(volume group ,VG)相关的操作。
1.查看卷组(VG)名,卷组名为vg51cto_lv。
1
2
3
|
[root@hadoop01 ~]
# vgscan
Reading allphysical volumes. This may take awhile...
Foundvolume group
"vg51cto_lv"
using metadata
type
lvm2
|
2.查看卷组(VG)包含的PV、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
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
|
[root@hadoop01 ~]
# vgdisplay -v vg51cto_lv
Usingvolume group(s) on
command
line
Findingvolume group
"vg51cto_lv"
--- Volumegroup ---
VGName vg51cto_lv
SystemID
Format lvm2
MetadataAreas 1
MetadataSequence No 6
VGAccess
read
/write
VGStatus resizable
MAX LV 0
Cur LV 5
OpenLV 5
Max PV 0
Cur PV 1
Act PV 1
VGSize 59.80 GiB
PESize 4.00 MiB
TotalPE 15309
Alloc PE
/Size
15309 / 59.80 GiB
Free PE / Size 0 / 0
VGUUID uF4S1y-21AZ-KOfs-rNIF-lFmN-wQ75-2sX7F1
--- Logicalvolume ---
LVPath
/dev/vg51cto_lv/LogVol04
LVName LogVol04
VGName vg51cto_lv
LVUUID rvOZeO-Oz2G-wx7Z-u0b6-CvXj-FKEj-RwCLXO
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:30:51 +0800
LVStatus available
# open 1
LVSize 22.80 GiB
CurrentLE 5837
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Blockdevice 253:2
--- Logicalvolume ---
LVPath
/dev/vg51cto_lv/LogVol01
LVName LogVol01
VGName vg51cto_lv
LVUUID d6YKrd-fWMJ-x61I-cfWG-v2me-Hn8V-xdm96L
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:30:56 +0800
LVStatus available
# open 1
LVSize 10.00 GiB
CurrentLE 2560
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Blockdevice 253:0
--- Logicalvolume ---
LVPath
/dev/vg51cto_lv/LogVol00
LVName LogVol00
VGName vg51cto_lv
LVUUID hSyH2r-mvMV-th7q-geKg-sVpm-O1zJ-D6DwT8
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:30:58 +0800
LVStatus available
# open 1
LVSize 2.00 GiB
CurrentLE 512
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Blockdevice 253:1
--- Logicalvolume ---
LVPath
/dev/vg51cto_lv/LogVol03
LVName LogVol03
VGName vg51cto_lv
LVUUID gKHLfz-O5Lm-TGMq-2LcF-xcH1-ASea-QCVVNc
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:30:58 +0800
LVStatus available
# open 1
LVSize 20.00 GiB
CurrentLE 5120
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Blockdevice 253:3
--- Logicalvolume ---
LVPath
/dev/vg51cto_lv/LogVol02
LVName LogVol02
VGName vg51cto_lv
LVUUID lkqKmO-Dt0u-3CnB-UqeD-d6gc-2rmP-gWtKVp
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:31:00 +0800
LVStatus available
# open 1
LVSize 5.00 GiB
CurrentLE 1280
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Blockdevice 253:4
---Physical volumes ---
PV Name
/dev/sda2
PVUUID pKC6SA-fkfe-OAl2-scVr-tq0h-EAXD-AWpwgX
PVStatus allocatable
Total PE
/Free
PE 15309 / 0
|
3.修改卷组名,由vg51cto_lv修改成vg51cto。
1)修改卷组名的命令为vgrename,语法如下:
vgrename OldVolumeGroupNameNew VolumeGroupName
1
2
|
[root@hadoop01 ~]
# vgrename vg51cto_lv vg51cto
Volumegroup
"vg51cto_lv"
successfully renamed to
"vg51cto"
|
2)修改/etc/fstab配置文件,把vg51cto_lv修改成vg51cto。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@hadoop01 ~]
# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 5 17:33:46 2015
#
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info
#
/dev/mapper/vg51cto-LogVol01
/ ext4 defaults 1 1
UUID=9c02e519-3b85-4918-b9fd-395535cab496
/boot
ext4 defaults 1 2
/dev/mapper/vg51cto-LogVol03
/data
ext4 defaults 1 2
/dev/mapper/vg51cto-LogVol02
/home
ext4 defaults 1 2
/dev/mapper/vg51cto-LogVol04
/usr/local
ext4 defaults 1 2
/dev/mapper/vg51cto-LogVol00
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
|
3)修改grub.conf配置文件,把vg51cto_lv修改成vg51cto。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@hadoop01 ~]
# vim /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub aftermaking changes to this file
# NOTICE: You have a /boot partition. Thismeans that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/vg51cto_lv-LogVol01
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)
/grub/splash
.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root(hd0,0)
kernel
/vmlinuz-2
.6.32-431.el6.x86_64 ro root=
/dev/mapper/vg51cto-LogVol01
rd_NO_LUKSrd_LVM_LV=vg51cto
/LogVol01
rd_LVM_LV=vg51cto
/LogVol00
rd_NO_MD crashkernel=autoLANG=zh_CN.UTF-8 KEYBOARDTYPE=pcKEYTABLE=us rd_NO_DM rhgb quiet
initrd
/initramfs-2
.6.32-431.el6.x86_64.img
|
4)reboot重启后用df -h查看。
1
2
3
4
5
6
7
8
|
[root@hadoop01 ~]
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg51cto-LogVol01
9.9G 1.2G 8.2G 13% /
tmpfs 495M 0 495M 0%
/dev/shm
/dev/sda1
194M 29M 155M 16%
/boot
/dev/mapper/vg51cto-LogVol03
20G 172M 19G 1%
/data
/dev/mapper/vg51cto-LogVol02
5.0G 138M 4.6G 3%
/home
/dev/mapper/vg51cto-LogVol04
23G 173M 22G 1%
/usr/local
|
第二部分:逻辑卷(logical volume,LV)相关的操作。
1.查看逻辑卷(LV)名。
使用LVM安装系统时,默认的卷组名为VolGroup00,卷组内的逻辑卷为LogVol00,LogVol01……末两位由00开始依次递增。
1
2
3
4
5
6
|
[root@hadoop01 ~]
# lvscan
ACTIVE
'/dev/vg51cto/LogVol04'
[22.80 GiB]inherit
ACTIVE
'/dev/vg51cto/LogVol01'
[10.00 GiB]inherit
ACTIVE
'/dev/vg51cto/LogVol00'
[2.00 GiB]inherit
ACTIVE
'/dev/vg51cto/LogVol03'
[20.00 GiB]inherit
ACTIVE
'/dev/vg51cto/LogVol02'
[5.00 GiB]inherit
|
2.查看逻辑卷详细信息
lvdisplay 命令查看所有逻辑卷的详细信息
lvdisplay 逻辑卷名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@hadoop01 ~]
# lvdisplay /dev/vg51cto/LogVol00
--- Logicalvolume ---
LVPath
/dev/vg51cto/LogVol00
LVName LogVol00
VGName vg51cto
LVUUID hSyH2r-mvMV-th7q-geKg-sVpm-O1zJ-D6DwT8
LV WriteAccess
read
/write
LV Creationhost,
time
hadoop01, 2015-11-05 17:30:58 +0800
LVStatus available
# open 1
LVSize 2.00 GiB
CurrentLE 512
Segments 1
Allocation inherit
Read aheadsectors auto
- currentlyset to 256
Block device 253:1
|
3.修改逻辑卷名
1)修改逻辑卷命令为lvrename,语法如下:
1
2
3
4
5
6
7
8
9
10
11
|
lvrename OldLogicalVolumePath NewLogicalVolumePath
[root@hadoop01 ~]
# lvrename /dev/vg51cto/LogVol00 /dev/vg51cto/lv_swap
Renamed
"LogVol00"
to
"lv_swap"
in
volume group
"vg51cto"
[root@hadoop01 ~]
# lvrename /dev/vg51cto/LogVol01 /dev/vg51cto/lv_root
Renamed
"LogVol01"
to
"lv_root"
in
volume group
"vg51cto"
[root@hadoop01 ~]
# lvrename /dev/vg51cto/LogVol02 /dev/vg51cto/lv_home
Renamed
"LogVol02"
to
"lv_home"
in
volume group
"vg51cto"
[root@hadoop01 ~]
# lvrename /dev/vg51cto/LogVol03 /dev/vg51cto/lv_data
Renamed
"LogVol03"
to
"lv_data"
in
volume group
"vg51cto"
[root@hadoop01 ~]
# lvrename /dev/vg51cto/LogVol04 /dev/vg51cto/lv_usr
Renamed
"LogVol04"
to
"lv_usr"
in
volume group
"vg51cto"
|
2)修改/etc/fstab配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@hadoop01 ~]
# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 5 17:33:46 2015
#
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info
#
/dev/mapper/vg51cto-lv_root
/ ext4 defaults 1 1
UUID=9c02e519-3b85-4918-b9fd-395535cab496
/boot
ext4 defaults 1 2
/dev/mapper/vg51cto-lv_data
/data
ext4 defaults 1 2
/dev/mapper/vg51cto-lv_home
/home
ext4 defaults 1 2
/dev/mapper/vg51cto-lv_usr
/usr/local
ext4 defaults 1 2
/dev/mapper/vg51cto-lv_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
|
3)修改grub.conf配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@hadoop01 ~]
# vim /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub aftermaking changes to this file
# NOTICE: You have a /boot partition. Thismeans that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/vg51cto_lv-LogVol01
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
|