lvm磁盘扩容

基本概念

逻辑卷管理器(Logical Volume Manager,简称LVM)是将多个物理卷抽象成一个卷组,并在卷组的基础上进一步划分逻辑卷的一种磁盘管理方法。

LVM主要由三部分组成:物理卷(Physical Volume, PV)、卷组(Volume Group,VG)以及逻辑卷(Logical Volume,LV)。其中,物理卷可以是一个分区或者整个磁盘,是LVM的底层物理存储单元。多个物理卷合并为一个卷组,这样就创建了一个磁盘空间池。在卷组的基础上可以划分逻辑卷。

创建lvm卷

查看块情况

1
2
3
4
5
6
7
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 45.8G 0 part /
sdb 8:16 0 150G 0 disk

格式化磁盘

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
[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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x74bda18d.

Command (m for help): p

Disk /dev/sdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x74bda18d

Device Boot Start End Blocks Id System

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-314572799, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-314572799, default 314572799):
Using default value 314572799
Partition 1 of type Linux and of size 150 GiB is set

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): w
The partition table has been altered!

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

检查块信息

1
2
3
4
5
6
7
8
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 45.8G 0 part /
sdb 8:16 0 150G 0 disk
└─sdb1 8:17 0 150G 0 part

创建物理卷pv

1
2
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

创建卷组vg

1
2
[root@localhost ~]# vgcreate  vg01 /dev/sdb1
Volume group "vg01" successfully created

创建逻辑卷lv

1
2
[root@localhost ~]# lvcreate -l 100%FREE -n lv01_data  vg01
Logical volume "lv01_data" created.

格式化逻辑卷

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# mkfs.xfs /dev/vg01/lv01_data
meta-data=/dev/vg01/lv01_data isize=256 agcount=4, agsize=9830144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=39320576, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=19199, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

挂载逻辑卷

1
2
3
4
mkdir /data
mount1="/dev/vg01/lv01_data /data xfs defaults 0 0"
echo ${mount1}>>/etc/fstab
mount -a # mount /dev/vg01/lv01_data /data

挂载完成

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 46G 3.2G 43G 7% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 148K 16G 1% /dev/shm
tmpfs 16G 8.9M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 3.2G 28K 3.2G 1% /run/user/1000
tmpfs 3.2G 0 3.2G 0% /run/user/0
/dev/mapper/vg01-lv01_data 150G 33M 150G 1% /data

磁盘扩容

查看块情况

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 45.8G 0 part /
sdb 8:16 0 150G 0 disk
└─sdb1 8:17 0 150G 0 part
└─vg01-lv01_data 253:0 0 150G 0 lvm /data
sdc 8:18 0 150G 0 disk

格式化磁盘

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
[root@localhost ~]# fdisk /dev/sdc
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x74bda18d.

Command (m for help): p

Disk /dev/sdc: 161.1 GB, 161061273600 bytes, 314572800 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: 0x74bda18d

Device Boot Start End Blocks Id System

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-314572799, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-314572799, default 314572799):
Using default value 314572799
Partition 1 of type Linux and of size 150 GiB is set

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): w
The partition table has been altered!

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

检查块信息

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 45.8G 0 part /
sdb 8:16 0 150G 0 disk
└─sdb1 8:17 0 150G 0 part
└─vg01-lv01_data 253:0 0 150G 0 lvm /data
sdc 8:18 0 150G 0 disk
└─sdc1 8:19 0 150G 0 part

刷新分区表

1
partprobe /dev/sdc

查看vg组

1
2
3
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 1 1 0 wz--n- 150.00g 0

扩展逻辑卷

1
2
3
[root@localhost ~]# vgextend vg01 /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
Volume group "vg01" successfully extended

扫描逻辑卷

1
2
[root@localhost ~]# lvscan
ACTIVE '/dev/vg01/lv01_data' [<150.00 GiB] inherit

扩展逻辑卷

1
2
3
4
[root@localhost ~]# lvextend -l 100%VG /dev/vg01/lv01_data
Reducing 100%VG to remaining free space 300 GiB in VG.
Size of logical volume centos/root changed from <150 GiB (111550 extents) to 300 GiB (234349 extents).
Logical volume vg01/lv01_data successfully resized.

刷新分区表

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# xfs_growfs /dev/vg01/lv01_data
meta-data=/dev/mapper/centos-root isize=512 agcount=6, agsize=2301440 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=11827200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4495, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 11827200 to 24933376

检查扩容结果

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 46G 3.2G 43G 7% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 148K 16G 1% /dev/shm
tmpfs 16G 8.9M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 3.2G 28K 3.2G 1% /run/user/1000
tmpfs 3.2G 0 3.2G 0% /run/user/0
/dev/mapper/vg01-lv01_data 300G 33M 300G 1% /data