Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Bpazy opened this issue May 11, 2023 · 1 comment
Open

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

Bpazy opened this issue May 11, 2023 · 1 comment

Comments

@Bpazy
Copy link
Owner

Bpazy commented May 11, 2023

大体上就两个步骤:

  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
@Bpazy
Copy link
Owner Author

Bpazy commented Aug 28, 2023

growpart

这个命令就很简单了,这里以扩容 非LVM 的 vda2 分区为例,先查看 vda 磁盘情况:

ziyuan@nhan-ubuntu:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0   104M  1 loop /snap/core/16928
loop1    7:1    0 104.2M  1 loop /snap/core/17200
sr0     11:0    1  1024M  0 rom  
vda    252:0    0    30G  0 disk 
├─vda1 252:1    0     1M  0 part 
└─vda2 252:2    0    20G  0 part /
vdb    252:16   0    20G  0 disk 

可以看到 vda 容量被我扩容到 30G 了,但是 vda2 还是只有 20G,我们可以利用 growpart 命令快速扩容:

ziyuan@nhan-ubuntu:~$ sudo growpart /dev/vda 2
CHANGED: partition=2 start=4096 old: size=41936896 end=41940992 new: size=62910431,end=62914527
ziyuan@nhan-ubuntu:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0   104M  1 loop /snap/core/16928
loop1    7:1    0 104.2M  1 loop /snap/core/17200
sr0     11:0    1  1024M  0 rom  
vda    252:0    0    30G  0 disk 
├─vda1 252:1    0     1M  0 part 
└─vda2 252:2    0    30G  0 part /
vdb    252:16   0    20G  0 disk 

分区 vda2 成功变更为 30G 了,然后还需要调整文件系统大小:

ziyuan@nhan-ubuntu:~$ sudo resize2fs /dev/vda2
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/vda2 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/vda2 is now 7863803 (4k) blocks long.

最终 du 看下文件系统大小:

ziyuan@nhan-ubuntu:~$ df -hl
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           395M  3.2M  392M   1% /run
/dev/vda2        30G   17G   12G  60% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/loop0      104M  104M     0 100% /snap/core/16928
/dev/loop1      105M  105M     0 100% /snap/core/17200
shm              64M     0   64M   0% 

/dev/vda2 文件系统变为 30G 了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant