PancrasL的博客

虚拟机环境的Ubuntu Server20.04根空间满了(使用LVM分区),使用命令行操作对其扩充

2021-12-31

前言

磁盘扩充可以分为三步:

  • 使用虚拟机分配新的磁盘空间
  • 使用fdisk创建新的磁盘分区
  • 将新建的磁盘分区加入到lv组(LV,Logic Volume)
  • 扩充lv组

分配新的磁盘空间

扩充磁盘空间时虚拟机要处于关机状态,而且要删除所有的快照.

image-20211231174641376

创建磁盘分区

(1)查看磁盘分区

通过lsblk命令查看磁盘空间,可以看到磁盘sda从20G增长为了40G,但是新增的空间还没有被分配,因此我们接下来要使用fdisk命令创建新的磁盘分区。

image-20211231174904839

(2)创建磁盘分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 使用命令n创建一个新的分区,并将剩余磁盘空间分配给该分区,我这里是sda4
sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (41943039 != 83886079) will be corrected by write.

Command (m for help): n
Partition number (4-128, default 4): 4
First sector (41940992-83886046, default 41940992):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41940992-83886046, default 83886046):

Created a new partition 4 of type 'Linux filesystem' and of size 20 GiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

image-20211231175408914

将新创建的磁盘加入到lv组

(1)把/dev/sda4添加到vg中

1
2
3
4
5
6
7
8
9
10
11
12
# pancras @ host in ~ [9:56:18] $ sudo vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <19.00g 0

# pancras @ host in ~ [9:56:20] $ sudo vgextend ubuntu-vg /dev/sda4
Physical volume "/dev/sda4" successfully created.
Volume group "ubuntu-vg" successfully extended

# pancras @ host in ~ [9:56:38] $ sudo vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 2 1 0 wz--n- 38.99g <20.00g

image-20211231175820636

扩充lv组

(1)将vg中空闲的空间加入lv中

1
2
3
4
5
6
7
8
9
10
11
12
13

# pancras @ host in ~ [10:00:11] $ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- <19.00g

# pancras @ host in ~ [10:00:13] $ sudo lvextend -l +100%free /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to 38.99 GiB (9982 exte nts).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

# pancras @ host in ~ [10:00:36] $ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 38.99g

image-20211231180112821

(2)根分区识别空闲的磁盘空间

1
2
3
4
5
# pancras @ host in ~ [10:01:44] C:130 $ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 10221568 (4k) blocks long.

image-20211231180158274

Reference

【linux】lvm扩展根分区 - 东瑜 - 博客园 (cnblogs.com)