虚拟机Linux磁盘扩容

image-20240731213324426

场景

esxi下直接调整虚拟磁盘大小,guest系统内新增容量不生效,需要手动调整硬盘分区表、执行文件系统扩容。

步骤

1. 调整虚拟磁盘大小

img

2. 确认guest系统已识别到新的磁盘容量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@gitlab ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00099ab9

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM

Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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 /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 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 /dev/sda: 107.4 GB

3. 扩展磁盘分区

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
# 此处磁盘号按实际填写,如果只有一个盘一般是sda
[root@gitlab ~]# fdisk /dev/sda
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 (2 primary, 0 extended, 2 free)
e extended

# 新建主分区
Select (default p): p

# 分区号设置
Partition number (3,4, default 3):

# 分区起始块
First sector (33554432-209715199, default 33554432):
Using default value 33554432
Last sector, +sectors or +size{K,M,G} (33554432-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 84 GiB is set

# 修改分区文件系统
Command (m for help): t

# 指定刚刚新建的分区号码
Partition number (1-3, default 3):

# 列出所有文件系统编码表
Hex code (type L to list all codes): L

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix

# 将分区3文件系统调整为 Linux LVM
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.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

4. 使新分区表生效

1
[root@gitlab ~]# partprobe

partprobe命令用于重读分区表,将磁盘分区表变化信息通知内核,请求操作系统重新加载分区表。

5. 查看新的分区结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@gitlab ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00099ab9

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM
/dev/sda3 33554432 209715199 88080384 8e Linux LVM

Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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 /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 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

可以看见新创建的/dev/sda3分区已列出。

6. LVM扩展 VG & LV

PV(Physical Volume)
物理卷,处于LVM最底层,可以是物理硬盘或者分区。

VG(Volume Group)
卷组,建立在PV之上,可以含有一个到多个PV。

LV(Logical Volume)
逻辑卷,建立在VG之上,相当于分区的概念。不过大小可以动态改变。

查看已有的VG,可见原有的一个VG对应1个PV两个LV:

1
2
3
[root@gitlab ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <15.00g 0

查看LV列表,可见原有的root分区和swap分区挂载在centos这个VG上:

1
2
3
4
[root@gitlab ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 13.39g
swap centos -wi-ao---- 1.60g

新建PV:

1
2
3
# 此处pvcreate引用上面创建的物理磁盘分区
[root@gitlab ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.

扩展VG:

此处使用PV/dev/sda3 扩展VG centos

1
2
[root@gitlab ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended

再次查看VG,此时可见centos已由两个PV组成:

1
2
3
[root@gitlab ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 98.99g <84.00g

扩展LV:

1
2
3
4
# 此处的路径请根据实际情况填写
[root@gitlab ~]# lvextend /dev/mapper/centos-root /dev/sda3
Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 97.39 GiB (24932 extents).
Logical volume centos/root successfully resized.

再次查看LV信息,可见LV容量已经增长:

1
2
3
4
5
# 此处的路径请根据实际情况填写
[root@gitlab ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 97.39g
swap centos -wi-ao---- 1.60g

7. XFS扩容

使用df -h查看文件系统使用情况,容量仍未增长:

1
2
3
4
5
6
7
8
9
[root@gitlab ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.9M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root 98G 1.4G 97G 2% /
/dev/sda1 1014M 194M 821M 20% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/0

是因为LVM是物理磁盘和文件系统的中间层,需要在实际的文件系统上执行扩容,才可最终生效:

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

再次使用df -h查看文件系统使用情况,可见新增容量已生效:

1
2
3
4
5
6
7
8
9
[root@gitlab ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.9M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root 98G 1.4G 97G 2% /
/dev/sda1 1014M 194M 821M 20% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/0