How do I resize a disk image (Raspbian SD/USB image)?

To resize the last partition in a disk image, for example a Raspberry Pi SD card image first increase the size of the file on disk using truncate, here we're going to increase it by 500MB.

truncate 2019-06-20-raspbian-buster-full.img --size=+500M

Once the file size has been increased the size of the second partition need to be updated to use all of the remaining space.

parted --script ./2019-06-20-raspbian-buster-full.img resizepart 2 100%

And then the filesystem will need to be extended to the size of the new partition, first enable access to the partitions in the disk image.

losetup -fP --show 2019-06-20-raspbian-buster-full.img

This will return the loop device name, in this example we're going to assume it's "/dev/loop1". 

Then check the filesystem.

e2fsck -f /dev/loop1p2

Now we can do the actual filesystem resize.

resize2fs /dev/loop1p2

If you see no errors then we're done and we can remove the loop interface.

losetup -d /dev/loop1

The size of the disk image has now been extended and the second partition has been increased to use all of the space.

You cannot comment on this entry