Skip to content

Pve 中的 Ubuntu (LVM) 动态扩容 #261

@Bpazy

Description

@Bpazy

大体上就两个步骤:

  1. 在 PVE 扩容 Hard Disk;
  2. 在 Ubuntu 中使用该部分空间;

pve 扩容很简单:
image

主要讲一下 ubuntu 里的操作:
首先确保系统采用的是 LVM 方式进行管理的:

root@pve-ubuntu:/home/ziyuan# sudo lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 55.6M  1 loop /snap/core18/2721
loop1                       7:1    0 55.7M  1 loop /snap/core18/2745
loop2                       7:2    0 63.3M  1 loop /snap/core20/1852
loop3                       7:3    0 63.3M  1 loop /snap/core20/1879
loop4                       7:4    0 91.8M  1 loop /snap/lxd/23991
loop5                       7:5    0 91.9M  1 loop /snap/lxd/24061
loop6                       7:6    0 53.2M  1 loop /snap/snapd/18933
loop7                       7:7    0 53.2M  1 loop /snap/snapd/19122
sda                         8:0    0   64G  0 disk 
├─sda1                      8:1    0    1M  0 part 
├─sda2                      8:2    0    1G  0 part /boot
├─sda3                      8:3    0   31G  0 part 
│ └─ubuntu--vg-ubuntu--lv 253:0    0   63G  0 lvm  /
sr0                        11:0    1 1024M  0 rom  

LVM 非常灵活,所以选择将剩余空间创建为 sda4 然后添加到 ubuntu-lv 中:

root@pve-ubuntu:/home/ziyuan# fdisk /dev/sda

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

GPT PMBR size mismatch (67108863 != 134217727) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.

Command (m for help): p

Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: QEMU HARDDISK   
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: 8AE1E7AF-8CC8-4409-8215-7BED79299D55

Device       Start      End  Sectors Size Type
/dev/sda1     2048     4095     2048   1M BIOS boot
/dev/sda2     4096  2101247  2097152   1G Linux filesystem
/dev/sda3  2101248 67106815 65005568  31G Linux filesystem

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

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

Command (m for help): p
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: QEMU HARDDISK   
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: 8AE1E7AF-8CC8-4409-8215-7BED79299D55

Device        Start       End  Sectors Size Type
/dev/sda1      2048      4095     2048   1M BIOS boot
/dev/sda2      4096   2101247  2097152   1G Linux filesystem
/dev/sda3   2101248  67106815 65005568  31G Linux filesystem
/dev/sda4  67106816 134217694 67110879  32G Linux filesystem

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

再将 sda4 添加到 ubuntu-lv 中:

root@pve-ubuntu:/home/ziyuan# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 55.6M  1 loop /snap/core18/2721
loop1                       7:1    0 55.7M  1 loop /snap/core18/2745
loop2                       7:2    0 63.3M  1 loop /snap/core20/1852
loop3                       7:3    0 63.3M  1 loop /snap/core20/1879
loop4                       7:4    0 91.8M  1 loop /snap/lxd/23991
loop5                       7:5    0 91.9M  1 loop /snap/lxd/24061
loop6                       7:6    0 53.2M  1 loop /snap/snapd/18933
loop7                       7:7    0 53.2M  1 loop /snap/snapd/19122
sda                         8:0    0   64G  0 disk 
├─sda1                      8:1    0    1M  0 part 
├─sda2                      8:2    0    1G  0 part /boot
├─sda3                      8:3    0   31G  0 part 
│ └─ubuntu--vg-ubuntu--lv 253:0    0   30G  0 lvm  /
└─sda4                      8:4    0   32G  0 part 
sr0                        11:0    1 1024M  0 rom  
root@pve-ubuntu:/home/ziyuan# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.
root@pve-ubuntu:/home/ziyuan# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <31.00 GiB / not usable 0   
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              7935
  Free PE               255
  Allocated PE          7680
  PV UUID               BSt56B-JvpL-EZ8X-bUi8-1cgR-SfGP-xrr2IJ
   
  "/dev/sda4" is a new physical volume of "32.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name               
  PV Size               32.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               rHquaT-CU7z-33l0-R1RC-N2h4-qKMM-0tiV0P
   
root@pve-ubuntu:/home/ziyuan# vgdisplay
  --- 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               <31.00 GiB
  PE Size               4.00 MiB
  Total PE              7935
  Alloc PE / Size       7680 / 30.00 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               nR8EFU-9EGZ-d0mv-vSzj-cEZc-EOj5-irugrI
   
root@pve-ubuntu:/home/ziyuan# vgextend ubuntu-vg /dev/sda4
  Volume group "ubuntu-vg" successfully extended
root@pve-ubuntu:/home/ziyuan# 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               62.99 GiB
  PE Size               4.00 MiB
  Total PE              16126
  Alloc PE / Size       7680 / 30.00 GiB
  Free  PE / Size       8446 / 32.99 GiB
  VG UUID               nR8EFU-9EGZ-d0mv-vSzj-cEZc-EOj5-irugrI
   
root@pve-ubuntu:/home/ziyuan# 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               62.99 GiB
  PE Size               4.00 MiB
  Total PE              16126
  Alloc PE / Size       7680 / 30.00 GiB
  Free  PE / Size       8446 / 32.99 GiB
  VG UUID               nR8EFU-9EGZ-d0mv-vSzj-cEZc-EOj5-irugrI
   
root@pve-ubuntu:/home/ziyuan# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv 
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 30.00 GiB (7680 extents) to 62.99 GiB (16126 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@pve-ubuntu:/home/ziyuan# resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 8
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 16513024 (4k) blocks long.

扩容成功,查看结果:

root@pve-ubuntu:/home/ziyuan# df -hl
Filesystem                         Size  Used Avail Use% Mounted on
udev                               1.9G     0  1.9G   0% /dev
tmpfs                              382M  2.1M  380M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   62G   26G   33G  45% /
tmpfs                              1.9G     0  1.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2                          974M  304M  603M  34% /boot

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions