QEMU 中的 VM 无法识别虚拟大小,报告“设备上没有剩余空间”

QEMU 中的 VM 无法识别虚拟大小,报告“设备上没有剩余空间”

我从最新的 Debian 云映像开始,并为我的 VM 创建一份副本。

❯ cp debian-12-nocloud-arm64.qcow2 kthw-jumpbox

❯ qemu-img info kthw-jumpbox 
image: kthw-jumpbox
file format: qcow2
virtual size: 2 GiB (2147483648 bytes)
disk size: 381 MiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: kthw-jumpbox
    protocol type: file
    file length: 381 MiB (399179776 bytes)
    disk size: 381 MiB

然后调整大小为15G:

❯ qemu-img resize kthw-jumpbox 15G
Image resized.

❯ qemu-img info kthw-jumpbox 
image: kthw-jumpbox
file format: qcow2
virtual size: 15 GiB (16106127360 bytes)
disk size: 381 MiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: kthw-jumpbox
    protocol type: file
    file length: 381 MiB (399180288 bytes)
    disk size: 381 MiB

所以我们可以看到虚拟尺寸是15GiB

我做了一些设置此虚拟机的工作。但是当我复制文件时,出现错误:

# cp downloads/kubectl /usr/local/bin/
cp: error copying 'downloads/kubectl' to '/usr/local/bin/kubectl': No space left
 on device

检查后df发现,使用量为 2G,已达到 100%:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            186M     0  186M   0% /dev
tmpfs            48M  696K   47M   2% /run
/dev/vda1       1.9G  1.8G     0 100% /
tmpfs           236M     0  236M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda15      127M  294K  127M   1% /boot/efi
tmpfs            48M     0   48M   0% /run/user/0

qemu-img仍然显示虚拟大小为15G

❯ qemu-img info kthw-jumpbox 
image: kthw-jumpbox
file format: qcow2
virtual size: 15 GiB (16106127360 bytes)
disk size: 1.3 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: kthw-jumpbox
    protocol type: file
    file length: 1.3 GiB (1397948416 bytes)
    disk size: 1.3 GiB

发生了什么事?为什么虚拟机看不到虚拟大小?我该怎么做才能修复它?

答案1

growpart我明白了。我需要使用和 将文件系统扩展为虚拟分区的大小resize2fs

# apt-get install cloud-utils
...
# growpart /dev/vda 1
CHANGED: partition=1 start=262144 old: size=3930112 end=4192255 new: size=311951
03 end=31457246

# resize2fs /dev/vda1
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
[ 1374.335500] EXT4-fs (vda1): resizing filesystem from 491264 to 3899387 blocks
[ 1374.682227] EXT4-fs (vda1): resized filesystem to 3899387
The filesystem on /dev/vda1 is now 3899387 (4k) blocks long.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            186M     0  186M   0% /dev
tmpfs            48M  700K   47M   2% /run
/dev/vda1        15G  1.3G   13G   9% /
tmpfs           236M     0  236M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda15      127M  294K  127M   1% /boot/efi
tmpfs            48M     0   48M   0% /run/user/0

相关内容