Skip to content

Ubuntu 的根分区扩容

ESXI

在ESXI管理页增加其硬盘大小。强烈建议为虚拟机拍摄快照,增加容错空间。

观察

了解系统中各个块设备的布局和状态。可见磁盘空间已增加,而分区的大小没有增加。

shell
lsblk
log
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0   90G  0 disk 
├─sda1                      8:1    0    1G  0 part /boot/efi
├─sda2                      8:2    0    2G  0 part /boot
└─sda3                      8:3    0 60.9G  0 part 
  └─ubuntu--vg-ubuntu--lv 252:0    0 60.9G  0 lvm  /
sr0                        11:0    1 1024M  0 rom

重创分区

操作磁盘 sda3 的分区表。重新创建3号分区,以便分区能够使用新的、更大的空间。

DANGER

此步骤风险较高,建议在生产环境中谨慎使用

查看起始扇区

shell
fdisk -l
log
GPT PMBR size mismatch (134217727 != 188743679) will be corrected by write.
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 sectors
Disk model: Virtual disk    
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: 0613813F-ABE7-4621-8B36-BB9E90DD601B

Device       Start       End   Sectors  Size Type
/dev/sda1     2048   2203647   2201600    1G EFI System
/dev/sda2  2203648   6397951   4194304    2G Linux filesystem
/dev/sda3  6397952 134215679 127817728 60.9G Linux filesystem # 扇区起始号为 6397952


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 60.95 GiB, 65439531008 bytes, 127811584 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
shell
fdisk /dev/sda3 # 管理分区

删除分区 3

log
Command (m for help): d  # 删除分区
Partition number (1-3, default 3): 3  # 删除 3 号分区

Partition 3 has been deleted.

新建分区 3

log
Command (m for help): n # 新建分区
Partition number (3-128, default 3): 3 # 分区号为 3,与之前的保持一致
First sector (6397952-188743646, default 6397952): 6397952 # 输入扇区起始号,与之前的保持一致
Last sector, +/-sectors or +/-size{K,M,G,T,P} (6397952-188743646, default 188741631): # 结束扇区号,回车默认使用所有

Created a new partition 3 of type 'Linux filesystem' and of size 86.9 GiB.
Partition #3 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: n # 这里一定要选 n,否则原来的卷就会被干掉

Command (m for help): w # 保存退出

The partition table has been altered.
Syncing disks.

检查块设备的布局。可见分区/dev/sda3的空间已增加至 78G

log
root@bot1:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0   90G  0 disk 
├─sda1                      8:1    0    1G  0 part /boot/efi
├─sda2                      8:2    0    2G  0 part /boot
└─sda3                      8:3    0 86.9G  0 part 
  └─ubuntu--vg-ubuntu--lv 252:0    0 60.9G  0 lvm  /
sr0                        11:0    1 1024M  0 rom

物理卷

查看卷组信息

shell
vgdisplay # 展示卷组信息
log
  --- 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               <60.95 GiB
  PE Size               4.00 MiB
  Total PE              15602
  Alloc PE / Size       15602 / <60.95 GiB
  Free  PE / Size       0 / 0   
  VG UUID               mzekn5-1LqF-mE3Y-BmHD-E5XM-BB5Q-2skPQ9

扩大物理卷

shell
pvresize /dev/sda3 # 通知 物理卷管理器(LVM) 更新物理卷的信息,使其识别并利用新的、更大的磁盘空间。
log
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
shell
vgdisplay # 检查自由空间是否增加
log
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <86.95 GiB
  PE Size               4.00 MiB
  Total PE              22258
  Alloc PE / Size       15602 / <60.95 GiB
  Free  PE / Size       6656 / 26.00 GiB # 自由空间
  VG UUID               mzekn5-1LqF-mE3Y-BmHD-E5XM-BB5Q-2skPQ9

逻辑卷

扩容逻辑卷,占满物理卷

shell
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv  # 扩展这个逻辑卷到最大可用空间
log
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <60.95 GiB (15602 extents) to <86.95 GiB (22258 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
shell
resize2fs /dev/ubuntu-vg/ubuntu-lv  # 调整文件系统的大小
log
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 8, new_desc_blocks = 11
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 22792192 (4k) blocks long.

检验效果

看看根目录空间是否已经增加

shell
lsblk ; df -Th

扩容结束,完结撒花🎉。