I'm experimenting with mounting the Raspbian .img and adding code/files and then repacking. The purpose of this is to be able to create custom distros for projects (rather than making clones of the SD cards using dd).

I have got the .img file mounting based on this.

However, I very quickly run out of space on the partition (I think there is something like 400mb of free space out of the gate). And so i'd like to resize the partition.

I have tried a few things so far:


First, adding bits to the end of the file with

dd if=/dev/zeros bs=512k count=1000 >> 2018-06-27-raspbian-stretch-lite.img

and then running:

fdisk 2018-06-27-raspbian-stretch-lite.img

delete the old partition and create a new partition from the start of the original partition to the end (the end is now longer because the .img has been extended)

When I flash an SD card with this img I get a kernel panic.


I resize the .img file directly with:

qemu-img resize 2018-06-27-raspbian-stretch-lite.img 2.8G

This seems to work but when I make an SD card and run it on a PI, it says "resizing sd-card" the first time it boots.

This isn't too bad but I think in an ideal world I would have control over this process and understand it a little better.

EDIT: So, running this actually only extends the .img file size but did not give me more space when I mounted the disk img.

In my mind the fdisk mechanism should work and/or is ideal and a better understanding of this process would be great!

  • Fiddling with fdisk will only modify the partition table - to resize the partition you need to use resize2fs
    – Milliways
    Aug 23, 2018 at 20:34
  • 1
  • @Milliways thanks! This is excellent! So, will I run these steps on the .img file or on /dev/loop0?
    – sabjorn
    Aug 25, 2018 at 3:27
  • The link was just an example of resize2fs use - it is intended to run on the Pi itself, but (with appropriate changes) could be used on an image. You need to adjust partitions first gparted is probably easier if you have an appropriate machine to run it on. I have posted examples of a script to adjust images which run from command line. fdisk is a bit difficult to script, parted may be better.
    – Milliways
    Aug 25, 2018 at 14:10
  • total success. Ran resize2fs on /dev/loop0 after running losetup. Works like a charm.
    – sabjorn
    Aug 26, 2018 at 19:05

2 Answers 2


If you like to be completely flexible with partitions you can backup a Raspbian image for example into a tar archive. Then you can setup your partitions as you like. You can restore the tar archive to partitions of any size as long as it fits into it with its disk usage. There is no problem to backup from a 32 GB SD Card and restore it on a 8 GB card. This could be interesting if you have one installation to copy to many different SD Cards. How to do this you can look at Howto prepare a SD card from a tar archive.


Use following commands:

sudo apt-get install gparted
sudo losetup /dev/loop0 /path/to/imagefile.img
sudo partprobe /dev/loop0
sudo gparted /dev/loop0

GParted will be able to resize partitions and the included filesystem in one go. Best of all, it is a graphical program. If you want a commandline program, use GNU parted which incidentially uses the same backend code as GParted.

  • any alternative to gparted? Non-graphical is a must.
    – sabjorn
    Aug 24, 2018 at 23:06
  • 1
    GNU parted is the basis of gparted and a seperate commandline program.
    – flakeshake
    Aug 25, 2018 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.