Tell me more ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I've been trying to create a new Raspbian image with a different kernel (with CAN support) and with Python 3.3 installed. Rather than doing all of this on the Raspberry Pi, I thought it would be interesting to do it on the computer instead (following these instructions). However, once I'd got the image mounted and ready to go, I quickly ran out of disk space. I presume this is because the image is sized to be only just big enough with the expectation that the user will resize the file-system once it has been written to an SD card.

Is it possible to resize the image and file-system before writing it to the SD card so that I can do more customisation without running out of space?

share|improve this question
This question will probably help you. – Jivings Feb 12 at 8:32
@Jivings: all of these answers describe how to resize the image after it has been written to the SD card. I know how to do that; I'm interested in how to resize the image before writing it to the SD card. – DrAl Feb 12 at 8:41

1 Answer

This is how to resize a raw image file. I know it sounds stupidly simplistic, but it will work.

Create a blank image, of the size you want to expand the original (in my example I use 5GB);

dd if=/dev/zero of=/path/to/temp_image bs=1 count=1 seek=5G

Append this to the original image:

cat /path/to/temp_image >> /path/to/rasperrypi.img

Resize the filesystem that lives at the end of the disk (usually this is the one you want bigger):

resize2fs -f /path/to/rasperrypi.img

Done! Your disk should now be 5GB bigger!

share|improve this answer
That sounds perfect, thank you: I'll test this tonight. – DrAl Feb 12 at 11:46
I've just tried this and when I get to the resize2fs stage (after adding 1GB to the file), I get "Bad magic number in super-block while trying to open 2012-12-16-wheezy-raspbian.img Couldn't find valid filesystem superblock." Any suggestions as to what I'm doing wrong? – DrAl Feb 12 at 19:04
It looks to me like it's an issue with the fact I'm trying to resize2fs the whole image (the equivalent of /dev/sda) instead of the partition (the equivalent of /dev/sda2). However, I could be wrong and I've tried a few other things without success (like resizing the partition with fdisk, which seems to work but doesn't help me resize the file system). – DrAl Feb 12 at 19:11
Okay, I've found the answer to the last bit with a bit of googling: I needed to create a loop device pointing to the partition in the image. I did this by getting the start sector of the second partition from fdisk -l file.img (it was 122880) and then losetup --offset $((122880 * 512)) /dev/loop0 file.img. I could then use resize2fs on /dev/loop0 and tidy up with losetup -d /dev/loop0. If you update your answer to include this, I'll mark it as the accepted answer. Thanks for pointing me in the right direction. – DrAl Feb 12 at 19:38
I got the additional help I needed from here. – DrAl Feb 12 at 19:42
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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