Howto Resize QEMU qcow2 Image, Partition and Filesystem

I'm dealing with a lot of technologies and recently I get back to playing with QEMU for virtualization on my PC. I've shared about BunsenLabs Linux on my other blog. I have problem to remember everything I've done. Therefore, I'm documenting the tasks I'm doing so I can find it here later when I need it again.

This post will be a quick documentation of my process in growing a QEMU qcow2 image. Perhaps you may find it useful too when you're dealing with the same situation.

Check the image before resize,
The command:
qemu-img info bunsenlabs.img
Example output:
➜  qemu-img info bunsenlabs.img
image: bunsenlabs.img
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 5.42 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

I need to resize it to 32GB, so I added +22G like so:
The command:
qemu-img resize bunsenlabs.img +22G
Example output:
➜  qemu-img resize bunsenlabs.img +22G
Image resized.

And now, let's see the new size:
The command:
qemu-img info bunsenlabs.img
Example output:
➜  qemu-img info bunsenlabs.img
image: bunsenlabs.img
file format: qcow2
virtual size: 32 GiB (34359738368 bytes)
disk size: 5.42 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Resizing the Filesystem

We need a tool called virt-filesystems to resize the filesystem directly from our linux host. This tool is not included if you install virt-manager. I found it in libguestfs-tools package. So, we have to install this package before we are able to use the command.

Use this command on debian based linux to install the package:
sudo apt install libguestfs-tools

Now, we can use virt-filesystems command to view current partitions and filesystems. Use sudo to run this command to prevent error libguestfs: error: /usr/bin/supermin exited with error status 1..

Here is the command:
sudo virt-filesystems --long -h --all -a $(pwd)/bunsenlabs.img
Example output:
➜  sudo virt-filesystems --long -h --all -a $(pwd)/bunsenlabs.img
Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4  -      -    9.0G  -
/dev/sda5  filesystem  swap  -      -    975M  -
/dev/sda1  partition   -     -      83   9.0G  /dev/sda
/dev/sda2  partition   -     -      05   1.0K  /dev/sda
/dev/sda5  partition   -     -      82   975M  /dev/sda
/dev/sda   device      -     -      -    32G   -

As we can see from the output above, the device /dev/sda is already resized to 32G but the partition /dev/sda1 is still 9.0G.

Now we need to resize the /dev/sda1 partition and underlying filesystems using virt-resize.

Note that virt-resize CANNOT resize disk images in-place.

So we need to make a backup copy. We will use the backup copy of the qcow2 as input and use the original qcow2 image as output.

First, we make a backup copy of the disk as shown below.
cp bunsenlabs.img bunsenlabs-orig.img

Then, resize:
The command:
sudo virt-resize --expand /dev/sda1 bunsenlabs-orig.img bunsenlabs.img
Example output:
➜  sudo virt-resize --expand /dev/sda1 bunsenlabs-orig.img bunsenlabs.img
[   0.0] Examining bunsenlabs-orig.img
**********

Summary of changes:

/dev/sda1: This partition will be resized from 9.0G to 31.0G.  The 
filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’ 
method.

/dev/sda2: This partition will be left alone.

**********
[   3.0] Setting up initial partition table on bunsenlabs.img
[   4.3] Copying /dev/sda1
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[  13.0] Copying /dev/sda2
[  14.7] Expanding /dev/sda1 using the ‘resize2fs’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

We can check the image again.
➜  sudo virt-filesystems --long -h --all -a bunsenlabs.img    
Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4  -      -    31G   -
/dev/sda5  filesystem  swap  -      -    975M  -
/dev/sda1  partition   -     -      83   31G   /dev/sda
/dev/sda2  partition   -     -      05   1.0K  /dev/sda
/dev/sda5  partition   -     -      82   975M  /dev/sda
/dev/sda   device      -     -      -    32G   -

That's it. We can boot the QEMU image now.

And here is the view of my new partition size from gparted in my BunsenLabs:


Ulasan

Catatan popular daripada blog ini

Selamat Pagi Dunia Tipu-tipu

Merdeka 2021 - Merdekakah Kita?