2
延展系統硬碟
Jeffery edited this page 2026-06-11 10:03:47 +08:00

簡介

當虛擬機磁碟容量在外部(例如 PVE)擴充後,Ubuntu 內部不會自動使用新空間。本文記錄將新增空間一路套用到 LVM 邏輯磁區的流程:調整分割區 → 擴充 PV → 擴充 LV → 擴充檔案系統。

注意:fdisk 刪除並重建分割區具風險,操作前請先備份重要資料,並確認起始磁區(First sector)維持原值。

1. 確認磁碟大小

先用 lsblk 查看目前磁碟與分割狀況。

lsblk

以下範例中,sda 已擴充為 64G,但分割區 sda3 與其上的 LVM 仍只有 30G。

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

2. 調整硬碟大小

fdisk 刪除舊的 sda3 並以相同起始磁區重建為較大的分割區。刪除分割區時務必選擇保留(N)LVM2 簽章,否則會破壞磁區。

sudo fdisk /dev/sda

操作流程如下(p 檢視、d 刪除、n 新增、w 寫入):

Command (m for help): p
Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  4198399  4194304    2G Linux filesystem
/dev/sda3  4198400 67108830 62910431   30G Linux filesystem

Command (m for help): d
Partition number (1-3, default 3):
Partition 3 has been deleted.

Command (m for help): n
Partition number (3-128, default 3): 
First sector (3719168-46137310, default 3719168): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3719168-46137310, default 46135295):
Created a new partition 3 of type 'Linux filesystem' and of size 62 GiB.
Partition #3 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p
Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   4198399   4194304    2G Linux filesystem
/dev/sda3  4198400 124217694 130019295   62G Linux filesystem

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

3. 調整 PV 大小

讓 LVM 的實體磁區(Physical Volume)認得擴大後的分割區。

sudo pvresize /dev/sda3

4. 調整 LV 大小

將邏輯磁區(Logical Volume)擴充到吃下所有剩餘空間,再擴充其上的檔案系統。

sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv