LVM 的基本概念

物理卷 Physical volume (PV)

可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的 header,其余部分被切割为一块块物理区域(physical extents)。

卷组 Volume group (VG)

将一组物理卷收集为一个管理单元。

逻辑卷 Logical volume (LV)

虚拟分区,由物理区域(physical extents)组成。

物理区域 Physical extent (PE)

硬盘可供指派给逻辑卷的最小单位(通常为 4MB)。

磁盘操作相关命令

df -h(查看挂载点)

1
2
3
4
5
6
7
8
9
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 395M 1.9M 393M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 19G 15G 2.7G 85% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/vda2 976M 214M 695M 24% /boot
/dev/vda1 511M 6.1M 505M 2% /boot/efi

vgdisplay(显示当前的 volume group)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--- Volume group ---
VG Name ubuntu-vg
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 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <18.50 GiB
PE Size 4.00 MiB
Total PE 4735
Alloc PE / Size 4735 / <18.50 GiB
Free PE / Size 0 / 0
VG UUID 85kJd0-aNqp-xkoX-HoGI-21DD-l2yL-Gn0Vzs

备注: 注意 VG SIZE,这里应该是你当前的可用空间大小,待扩容完毕,这里显示的应该是最终的大小

pvdisplay (显示当前的 physical volume)

1
2
3
4
5
6
7
8
9
10
--- Physical volume ---
PV Name /dev/vda3
VG Name ubuntu-vg
PV Size <18.50 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4735
Free PE 0
Allocated PE 4735
PV UUID VJipfU-IpDx-HOqN-ljM4-KXjJ-V7cP-yuwuzf

开始 LVM 扩容

查看 fdisk

1
fdisk -l
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
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: gpt
Disk identifier: 9D1CE45C-69DE-4DCB-9CB0-A2E3C23B3813
Device Start End Sectors Size Type
/dev/vda1 2048 1050623 1048576 512M EFI System
/dev/vda2 1050624 3147775 2097152 1G Linux filesystem
/dev/vda3 3147776 41940991 38793216 18.5G Linux filesystem
Disk /dev/vdb: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0xdab9b80a
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 83886079 83884032 40G 83 Linux
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 18.5 GiB, 19860029440 bytes, 38789120 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

查看所有连接到电脑上的储存设备

1
fdisk -l |grep '/dev'

2块磁盘效果图(新增磁盘,尚未挂载)

1
2
3
4
5
6
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors
/dev/vda1 2048 1050623 1048576 512M EFI System
/dev/vda2 1050624 3147775 2097152 1G Linux filesystem
/dev/vda3 3147776 41940991 38793216 18.5G Linux filesystem
Disk /dev/vdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 18.5 GiB, 19860029440 bytes, 38789120 sectors

创建vdb分区

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
fdisk /dev/vdb
# 按如下操作
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
The old ext4 signature will be removed by a write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xdab9b80a.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83886079, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079):
Created a new partition 1 of type 'Linux' and of size 40 GiB.
Command (m for help): w
The partition table has been altered.
Syncing disks.

格式化磁盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mkfs -t ext4 /dev/vdb1
# 输出如下:
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 10485504 4k blocks and 2621440 inodes
Filesystem UUID: 368ba4ee-2a69-4d8d-80a3-bc61c82b7fd2
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

创建 PV

1
pvcreate /dev/vdb1

查看卷组

1
2
3
4
5
6
pvscan
# 输出如下:
PV /dev/vda3 VG ubuntu-vg lvm2 [<18.50 GiB / 0 free]
PV /dev/vdb1 lvm2 [<40.00 GiB]
Total: 2 [<58.50 GiB] / in use: 1 [<18.50 GiB] / in no VG: 1 [<40.00 GiB]

扩容 VG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vgdisplay
# 输出如下:
--- Volume group ---
VG Name ubuntu-vg
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 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <18.50 GiB
PE Size 4.00 MiB
Total PE 4735
Alloc PE / Size 4735 / <18.50 GiB
Free PE / Size 0 / 0
VG UUID 85kJd0-aNqp-xkoX-HoGI-21DD-l2yL-Gn0Vzs
1
2
3
4
vgextend ubuntu-vg /dev/vdb1
# 输出如下:
Volume group "ubuntu-vg" successfully extended
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 58.49 GiB
PE Size 4.00 MiB
Total PE 14974
Alloc PE / Size 4735 / <18.50 GiB
# 此处即为可以扩容的大小
Free PE / Size 10239 / <40.00 GiB
VG UUID 85kJd0-aNqp-xkoX-HoGI-21DD-l2yL-Gn0Vzs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
lvdisplay
--- Logical volume ---
# 扩容路径
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID DYHQsn-UfPl-OOtz-O1aI-9PzP-gGb6-L2XVXz
LV Write Access read/write
LV Creation host, time ubuntu-server, 2020-09-09 23:39:29 +0800
LV Status available
# open 1
LV Size <18.50 GiB
Current LE 4735
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

扩容 LV

1
2
3
4
5
6
7
8
# 增加指定大小
lvextend -L +30G /dev/ubuntu-vg/ubuntu-lv
# 按百分比扩容
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
# 输出如下:
Size of logical volume ubuntu-vg/ubuntu-lv changed from <18.50 GiB (4735 extents) to 58.49 GiB (14974 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

刷新分区

1
resize2fs /dev/ubuntu-vg/ubuntu-lv
1
2
3
4
5
6
7
8
9
10
11
df -h
# 成功扩容:
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 395M 1.9M 393M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 58G 15G 41G 27% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/vda2 976M 214M 695M 24% /boot
/dev/vda1 511M 6.1M 505M 2% /boot/efi