Software

Linux 重新擴充 LVM 空間

事情是這樣發生的,因為在安裝 ubuntu 的時候在劃分 LVM 並沒有完全使用到,一開始沒什麼問題,但是使用容量越來越大的時候出現了空間不足的問題,LVM 管理方有很多,也可以增加 PV 或是 VG 放進去 LV 裡面,但是這邊遇到的是在同一個 PV 裡面直接做 resize

sudo pvdisplay

--- Physical volume ---
   PV Name               /dev/sda3
   VG Name               ubuntu-vg
   PV Size               <31.00 GiB / not usable 0
   Allocatable           yes (but full)
   PE Size               4.00 MiB
   Total PE              7935
   Free PE               0
   Allocated PE          7935
   PV UUID               5AOQdS-hOZ8-lQ39-0iiI-b5oW-teRi-iYVpNa

使用 pvdisplay 看之後發現目前的 PV 只有 31 GB 但是我確定這顆硬碟應該是 128GB 的,所以接下來我們要使用 resizepart 來把 PV 的空間釋放出來

sudo parted

#使用 print 來確認目前的分割
 (parted) print
 Model: VMware Virtual disk (scsi)
 Disk /dev/sda: 137GB <=== 應該是原有的大小
 Sector size (logical/physical): 512B/512B
 Partition Table: gpt
 Disk Flags:
 Number  Start   End     Size    File system  Name  Flags
  1      1049kB  2097kB  1049kB                     bios_grub
  2      2097kB  1076MB  1074MB  ext4
  3      1076MB  34.4GB  33.3GB
# 使用  resizepart 
 (parted) resizepart
 Partition number? 3
 End?  [34.4GB]? 137GB
# 在執行一次 print 確認有沒有 resizepart
(parted) print
 Model: VMware Virtual disk (scsi)
 Disk /dev/sda: 137GB
 Sector size (logical/physical): 512B/512B
 Partition Table: gpt
 Disk Flags:
 Number  Start   End     Size    File system  Name  Flags
  1      1049kB  2097kB  1049kB                     bios_grub
  2      2097kB  1076MB  1074MB  ext4
  3      1076MB  137GB   136GB <=== 已經確認空間擴充進去了
 # 退出 parted
(parted) quit 

將 PV 空間擴充

sudo pvdisplay

--- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <31.00 GiB / not usable 0
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7935
  Free PE               0
  Allocated PE          7935
  PV UUID               5AOQdS-hOZ8-lQ39-0iiI-b5oW-teRi-iYVpNa

使用 pvdisplay 時發現雖然 partition 擴充了但是 PV 還沒擴充,所以我們要開始對 PV 做 resize

sudo pvresize /dev/sda3

Physical volume "/dev/sda3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

sudo pvdisplay

--- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <126.59 GiB / not usable 2.38 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              32406
  Free PE               24471
  Allocated PE          7935
  PV UUID               5AOQdS-hOZ8-lQ39-0iiI-b5oW-teRi-iYVpNa

再使用一次 pvdisplay 看到已經 resize 好了,接下來我們要開始擴充 LV

sudo lvdisplay

--- Logical volume ---
   LV Path                /dev/ubuntu-vg/ubuntu-lv
   LV Name                ubuntu-lv
   VG Name                ubuntu-vg
   LV UUID                h2alaJ-CYHS-pi1q-h71A-HYoo-dRVj-Cgg24D
   LV Write Access        read/write
   LV Creation host, time ubuntu-server, 2021-08-04 01:27:57 +0000
   LV Status              available
   # open                 1
   LV Size                <31.00 GiB
   Current LE             7935
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
 currently set to     256
 Block device           253:0 

目前的 LV 還沒擴充,所以我們要將 LV 擴充

sudo lvextend -L 126G /dev/ubuntu-vg/ubuntu-lv

Size of logical volume ubuntu-vg/ubuntu-lv changed from <31.00 GiB (7935 extents) to 126.00 GiB (32256 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

sudo lvdisplay

--- Logical volume ---
   LV Path                /dev/ubuntu-vg/ubuntu-lv
   LV Name                ubuntu-lv
   VG Name                ubuntu-vg
   LV UUID                h2alaJ-CYHS-pi1q-h71A-HYoo-dRVj-Cgg24D
   LV Write Access        read/write
   LV Creation host, time ubuntu-server, 2021-08-04 01:27:57 +0000
   LV Status              available
   # open                 1
   LV Size                126.00 GiB
   Current LE             32256
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   currently set to       256
   Block device           253:0 

重新再執行一次 lvdisplay 後看到空間已經擴充進去 LV 了,但是還有最後一步要做執行 resize2fs 才可以把空間將進 Filesystem 裡面

resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

到這邊後可以在下一次 df 做確認空間有沒有擴充進去,而這個方式我有做過測試,如果是 VM 原硬碟擴充也適用,但如果是要增加一個 PV 然後加進 VG 就不能用這個方式了