[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Resizing a QEMU raw image with an NTFS filesystem

This is a quick guide to increasing the disk space available to your Windows virtual machine with an NTFS file system. The example is based on increasing a partition from 5GB to 6GB.

Backup your original file first in case something goes wrong.

Use qemu-img to resize the QEMU raw disk image

This command increases the size of the disk image in the file Windows_XP_Professional_SP_3.img by 1GB.

qemu-img resize Windows_XP_Professional_SP_3.img +1G

After this command, if you boot your virtual machine, you will see that there is an additional 1GB of free disk space available.

Find the offset into the image

Loop mount the image.

losetup /dev/loop0 Windows_XP_Professional_SP_3.img

Inspect the partition table (here parted is used but fdisk or cfdisk can also be used).

parted /dev/loop0

Within parted, set the units to sectors, then print the current partition table.

(parted) unit s                                                           
(parted) print 

The output will look something like this:

Model: Loopback device (loopback)
Disk /dev/loop0: 12582912s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End        Size       Type     File system  Flags
 1      63s    11718798s  11718736s  primary  ntfs         boot

Note the Sector size and Start sector numbers in the output.

Now quit parted.

(parted) quit

Delete the loop device.

losetup -d /dev/loop0

Use ntfsresize to resize the NTFS partition

Loop mount the NTFS partition to be resized, using an offset calculated from the sector size and start sector.

losetup -o$((512*63)) /dev/loop0 Windows_XP_Professional_SP_3.img

First do a dry run.

 ntfsresize -n -s 6G /dev/loop0

If all is OK, do it for real.

ntfsresize -s 6G /dev/loop0

Delete the loop device.

losetup -d /dev/loop0

Update the partition table

Loop mount the image.

losetup  /dev/loop0 Windows_XP_Professional_SP_3.img

Update the partition table using parted (both fdisk and cfdisk appear to fail here).

parted /dev/loop0

This seems like a backward step, but now use parted to remove the existing partition.

(parted) rm 1

Use the parted rescue command to find the partition again, with the END option set to the size of the partition in MB.

(parted) rescue 1 6000                                                    
Information: A ntfs primary partition was found at 32.3kB -> 6000MB.  Do you want to add it to the partition table?
Yes/No/Cancel? Yes

Set the boot flag on the rescued partition.

(parted) set 1 boot on

The partition table is written when you quit parted.

(parted) quit

Delete the loop device.

losetup -d /dev/loop0

Finish up

Boot the virtual machine and allow the Windows chkdsk program to run.

Sources

 howtos:general_admin:resize_a_qemu_raw_image_with_an_ntfs_filesytem ()