Ubuntu 22.04擴容LVM空間

mrchip發表於2024-06-12

今天為了編譯ThingsBoard的原始碼,發現原來給虛擬機器分配的40個G不夠用了。於是乎在VMWare Workstation中擴容了40G的磁碟空間。但是此時lvm是不會自動擴容的,因此我們需要手動調整下卷的配置。

首先df -h檢查發現掛載的空間的確沒有變化

mrchip@ubuntu22:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              791M  2.5M  788M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   37G   35G     0 100% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  253M  1.6G  14% /boot
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e80883038e918e14fc90c4519ecc708e642d079d7482ae9a0b78b456c0502ed3/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/baca760e9d9316152da7adb633a6bb05ab76e6cbfe1267bea951f1b3a9038c05/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e8f65d1e3aa83c67c5c55ae5e2bce0b87e905477d0a7dc20ee6fc15a47d2a2eb/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/1d091959988cd36e8f71b01572a2e37e341238d263627d8eb839798c4dec86d9/shm
tmpfs                              791M  4.0K  791M   1% /run/user/1000

然後執行lsblk發現磁碟的確變大了

NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0 48.6M  1 loop /snap/cmake/1390
loop1                       7:1    0 48.6M  1 loop /snap/cmake/1391
loop2                       7:2    0 63.9M  1 loop /snap/core20/2182
loop3                       7:3    0 63.9M  1 loop /snap/core20/2318
loop4                       7:4    0 66.2M  1 loop /snap/core24/405
loop5                       7:5    0 66.2M  1 loop /snap/core24/423
loop6                       7:6    0   87M  1 loop /snap/lxd/27948
loop7                       7:7    0 17.7M  1 loop /snap/helm/414
loop8                       7:8    0   87M  1 loop /snap/lxd/28373
loop9                       7:9    0 38.7M  1 loop /snap/snapd/21465
loop10                      7:10   0 38.8M  1 loop /snap/snapd/21759
sda                         8:0    0   80G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0    2G  0 part /boot
└─sda3                      8:3    0   38G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0   37G  0 lvm  /
sr0                        11:0    1    2G  0 rom

接下來我們記錄命令操作來擴容分割槽

1. 將新的空間新建一個磁碟分割槽

mrchip@ubuntu22:~$ sudo fdisk /dev/sda
[sudo] password for mrchip:

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (83886079 != 167772159) will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


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

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

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

2. 將新建的分割槽sda4轉換為PV並擴充ubuntu-vg這個卷組

mrchip@ubuntu22:~$ sudo pvcreate -ff -y /dev/sda4
  Physical volume "/dev/sda4" successfully created.
mrchip@ubuntu22:~$ sudo vgextend ubuntu-vg /dev/sda4
  Volume group "ubuntu-vg" successfully extended
mrchip@ubuntu22:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               77.99 GiB
  PE Size               4.00 MiB
  Total PE              19966
  Alloc PE / Size       9472 / 37.00 GiB
  Free  PE / Size       10494 / 40.99 GiB
  VG UUID               aa7Nif-O9wu-0AMo-pxFA-RTsq-v48M-MZ5EUm

3. 擴容ubuntu-lv這個邏輯空間

sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 37.00 GiB (9472 extents) to 77.99 GiB (19966 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
mrchip@ubuntu22:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              791M  2.5M  788M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   37G   35G     0 100% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  253M  1.6G  14% /boot
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e80883038e918e14fc90c4519ecc708e642d079d7482ae9a0b78b456c0502ed3/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/baca760e9d9316152da7adb633a6bb05ab76e6cbfe1267bea951f1b3a9038c05/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e8f65d1e3aa83c67c5c55ae5e2bce0b87e905477d0a7dc20ee6fc15a47d2a2eb/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/1d091959988cd36e8f71b01572a2e37e341238d263627d8eb839798c4dec86d9/shm
tmpfs                              791M  4.0K  791M   1% /run/user/1000

4. 對lvm分割槽的大小進行擴充

mrchip@ubuntu22:~$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 10
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 20445184 (4k) blocks long.

mrchip@ubuntu22:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              791M  2.5M  788M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G   35G   39G  48% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  253M  1.6G  14% /boot
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e80883038e918e14fc90c4519ecc708e642d079d7482ae9a0b78b456c0502ed3/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/baca760e9d9316152da7adb633a6bb05ab76e6cbfe1267bea951f1b3a9038c05/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/e8f65d1e3aa83c67c5c55ae5e2bce0b87e905477d0a7dc20ee6fc15a47d2a2eb/shm
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/1d091959988cd36e8f71b01572a2e37e341238d263627d8eb839798c4dec86d9/shm
tmpfs                              791M  4.0K  791M   1% /run/user/1000

參考連結

  1. Linux LVM分割槽擴容
  2. 虛擬機器下ubuntu使用df命令檢視磁碟空間小於實際空間(解決Linux /dev/mapper/ubuntu--vg-ubuntu--lv 磁碟空間不足)
  3. LVM 'Can’t open /dev/sdb1 exclusively. Mounted filesystem?' Problem

相關文章