Ubuntu 22.04扩容LVM空间

今天为了编译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

热门相关:你是我生命的四分之三   秦吏   一世倾心:误惹腹黑师弟   妖魔哪里走   带着仙门混北欧