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:
fdisk
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.
qemu-img
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!
fdisk
will only modify the partition table - to resize the partition you need to useresize2fs
– Milliways Aug 23 '18 at 20:34.img
file or on/dev/loop0
? – sabjorn Aug 25 '18 at 03:27resize2fs
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 firstgparted
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 '18 at 14:10resize2fs
on/dev/loop0
after runninglosetup
. Works like a charm. – sabjorn Aug 26 '18 at 19:05