PVE虚拟机磁盘扩容

由于刚开始磁盘空间设置太小,后服务器用来做云存储导致磁盘空间不足,在次环境下需要对磁盘进行扩容操作。以下主要记录在PVE环境下, Linux虚拟机磁盘扩容及系统根目录大小调整步骤。


一、PVE磁盘映像扩容

首先需要在PVE管理WEB页对虚拟机的磁盘映像进行扩容, 类似于物理机上对存储的物理磁盘进行扩容。具体步骤为:

  • 选中“虚拟机 -> 硬件 -> 磁盘”
  • Disk Action -> Resize
  • 在弹出对话框中输入要增加的大小
  • 点击弹出窗“调整磁盘大小”按钮确定

PVE管理页

磁盘大小调整弹窗

二、虚拟机扩容

通过;命令行工具登陆Linux服务器

1. 查看磁盘

(1) 执行 “sudo fdisk -l” 命令查看磁盘大小是否改变

Disk /dev/sda: 256 GiB, 274877906944 bytes, 536870912 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: E34826F9-50DB-4B45-9C97-A0AED4594306

Device       Start       End   Sectors Size Type
/dev/sda1     2048      4095      2048   1M BIOS boot
/dev/sda2     4096   4198399   4194304   2G Linux filesystem
/dev/sda3  4198400 125827071 121628672  58G Linux filesystem

可以看到磁盘已经扩容到256G, 但系统分区还是未改变。

2.扩容分区

(1) 执行 “parted /dev/sda” 命令, 此处的 “/dev/sda” 是上边查询到的磁盘路径, 需要根据具体磁盘路径修改。

Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

(2) 执行 “print” 命令, 打印磁盘分区

Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 275GB
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  2150MB  2147MB  ext4
 3      2150MB  64.4GB  62.3GB

(3) 执行 “resizepart 3 100%”, 此处的 “3” 代表的是上边打印磁盘分区之后需要扩容的分区编号。

(4) 执行 “quit” 退出命令。

(5) 执行 “pvresize /dev/sda3” 命令更新磁盘大小。

(6) 执行 “sudo fdisk -l” 命令查看扩容结果。

Disk /dev/loop0: 47.99 MiB, 50323456 bytes, 98288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 63.22 MiB, 66293760 bytes, 129480 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop2: 47.98 MiB, 50315264 bytes, 98272 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop3: 102.98 MiB, 107986944 bytes, 210912 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop4: 61.96 MiB, 64966656 bytes, 126888 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop5: 79.95 MiB, 83832832 bytes, 163736 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 256 GiB, 274877906944 bytes, 536870912 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: E34826F9-50DB-4B45-9C97-A0AED4594306

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   4198399   4194304    2G Linux filesystem
/dev/sda3  4198400 536870878 532672479  254G Linux filesystem

可以看到, /dev/sda3 的大小已经变更, 但是执行 “df -h” 会发现, 磁盘根目录大小并没有变化。

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              393M  1.1M  392M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   57G   11G   44G  20% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  247M  1.6G  14% /boot
tmpfs                              393M  4.0K  393M   1% /run/user/1000

3.扩容逻辑卷

(1) 执行 “sudo vgdisplay” 命令, 查看逻辑卷信息

  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  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               <254.00 GiB
  PE Size               4.00 MiB
  Total PE              65023
  Alloc PE / Size       65023 / <254.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               cxllzg-nf3c-QmNe-mtP3-sA8G-eIuZ-Tvp1jm

(2) 执行 “df -h” 查看挂载路径

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              393M  1.1M  392M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   57G   11G   44G  20% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  247M  1.6G  14% /boot
tmpfs                              393M  4.0K  393M   1% /run/user/1000

(3) 执行 “sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv” 划分全部空闲分区给逻辑分区

New size (9727 extents) matches existing size (9727 extents).

(4) 执行 “sudo resize2fs -p /dev/mapper/ubuntu--vg-ubuntu--lv” 扩容根目录挂载逻辑分区。

resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 8, new_desc_blocks = 32
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 66583552 (4k) blocks long.

(5) 此时再执行 “df -h” 命令可以看到, 根目录大小已经变更。

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              393M  1.1M  392M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  250G   11G  229G   5% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  247M  1.6G  14% /boot
tmpfs                              393M  4.0K  393M   1% /run/user/1000