目录

QEMU磁盘扩容

当前使用的磁盘类型为qcow2,该类型可以增加大小,但不能随意的减少大小。下面为qcow2扩容的指导:

磁盘已经没有空间能够使用,根目录挂载在逻辑卷ubuntu--vg-ubuntu--lv

lilac@vm-node2:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  8.8G  8.3G   76M 100% /
  1. 查看磁盘文件路径 首先通过配置文件确认磁盘文件路径,寻找到<disk>节点,其中<source file=''>为虚拟机使用的磁盘文件
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/data/qemu_disk/vm-node2.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </disk>
  1. 调整磁盘文件大小 增加大小:
resize [--object objectdef] [--image-opts] [-f fmt] [--preallocation=prealloc] [-q] [--shrink] filename [+ | -]size
<br/>
root@kwephis8419858:~# qemu-img resize /data/qemu_disk/vm-node2.qcow2 +10G
Image resized.

减少大小:

root@kwephis8419858:~# qemu-img resize --shrink /data/qemu_disk/vm-node2.qcow2 -5G

需要注意,--shrink选项启用”收缩“操作,前提是已经释放了磁盘中未使用的空间(例如,通过文件系统的空洞或删除不再使用的文件)。若没有释放未使用空间,则缩小操作可能会失败。

虚拟机内会使用到 gdisk 和 parted ,它们是两款常用的磁盘分区工具。其中 gdisk 只支持 GPT 分区表,而 parted 能支持 GPT 和 MBR 两种分区表格式,支持更复杂的操作。 下面介绍 gdisk 的使用:

  1. 启用磁盘分区工具
lilac@vm-node2:~$ sudo gdisk /dev/vda
GPT fdisk (gdisk) version 1.0.3
</>
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
</>
Found valid GPT with protective MBR; using GPT.
</>
Command (? for help):

此时磁盘工具已经成功开启了,在Command行中键入需要操作的命令。

  1. 打印磁盘信息 能够使用p命令来打印信息
Command (? for help): p
Disk /dev/vda: 41943040 sectors, 20.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): A0A01BC8-24A8-4105-B825-0BCAFBFECCD4
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
</>
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02
   2            4096         2101247   1024.0 MiB  8300
   3         2101248        20969471   9.0 GiB     8300

信息中表示/dev/vda的大小有 20.0 GiB,然而最大的 vd3 仅仅使用 9.0 GiB。

  1. 新增分区 使用n命令新增分区
Command (? for help): n
Partition number (4-128, default 4): 4
First sector (34-41943006, default = 20969472) or {+-}size{KMGTP}:
Last sector (20969472-41943006, default = 41943006) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

其中 Partition number 为分区号,First sector 为分区开始的扇区,Last sector 为分区结束的扇区,上述使用默认值则使用最大容量。

  1. 应用修改
Command (? for help): w
</>
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
</>
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/vda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

此时修改已经成功应用了

  1. 刷新分区 虽然分区成功创建,但 /dev/vda4 可能无法立即显示,这是由于系统未刷新分区表,我们可以重启系统,或者通过partprobe命令刷新列表
lilac@vm-node2:~$ sudo partprobe
lilac@vm-node2:~$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                        11:0    1 1024M  0 rom
vda                       252:0    0   20G  0 disk
├─vda1                    252:1    0    1M  0 part
├─vda2                    252:2    0    1G  0 part /boot
├─vda3                    252:3    0    9G  0 part
│ └─ubuntu--vg-ubuntu--lv 253:0    0    9G  0 lvm  /
└─vda4                    252:4    0   10G  0 part

使用 lsblk 命令查看时,发现 vda4 存在

  1. 物理卷初始化 查看物理卷信息,pvdisplay命令却并未列出 /dev/vda4,这说明该分区未被初始化为物理卷,还不能被 LVM 系统识别纳入卷组管理
lilac@vm-node2:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               ubuntu-vg
  PV Size               <9.00 GiB / not usable 1.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2303
  Free PE               0
  Allocated PE          2303
  PV UUID               IY1tcG-DS5x-1dRj-nSVE-wFFR-NzBe-yLUAfs
>
lilac@vm-node2:~$ sudo pvcreate /dev/vda4
  Physical volume "/dev/vda4" successfully created.

执行pvcreate命令后,得到提示物理卷成功创建

  1. 卷组查看、修改 上述操作将扩容的磁盘分配了一个新的分区,并初始化为物理卷,但是虚拟机内部仍然不能够使用这部分空间。Ubuntu 当前使用卷组、逻辑卷的方式管理我们的存储空间,故而需要针对卷组以及逻辑卷再做分配
lilac@vm-node2:~$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                fPXkkm-zMDb-0EQI-GjNY-tqVY-hLAw-YrHtS7
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2024-11-04 03:04:25 +0000
  LV Status              available
  # open                 1
  LV Size                <9.00 GiB
  Current LE             2303
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
>
lilac@vm-node2:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  7
  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               <9.00 GiB
  PE Size               4.00 MiB
  Total PE              2303
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               yrTG3S-0RaY-2F4N-OZ16-dhd8-oPUd-Vayrc6

查看逻辑卷ubuntu-lv并识别他的归属为ubuntu-vg,其中卷组的大小为 9.00 GiB,则逻辑卷不能直接扩展,我们需要将前面新建的分区vda4加入到卷组中,增加卷组的大小

lilac@vm-node2:~$ sudo vgextend ubuntu-vg /dev/vda4
  Volume group "ubuntu-vg" successfully extended
  1. 逻辑卷修改
lilac@vm-node2:~$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <9.00 GiB (2303 extents) to 18.99 GiB (4862 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
>
lilac@vm-node2:~$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 4978688 (4k) blocks long.

上述操作后,若无问题则扩容完成

lilac@vm-node2:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv   19G  8.3G  9.6G  47% /