14

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?

dsg
  • 121
  • 6
DrAl
  • 249
  • 1
  • 2
  • 5

4 Answers4

8

This is a synthesis of the answers above and elsewhere that worked for me - back up your image in case you make a mistake:

Firstly make the image file bigger (here we're adding 1GB to the end):

truncate -s +1G ./image.img

Next map the whole image as a loop device so we can poke at the partition table

sudo losetup /dev/loop0 ./image.img

For future reference lets dump it:

sudo fdisk -l /dev/loop0

Output looks like:

Disk /dev/loop0: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c4661
  Device Boot      Start         End      Blocks   Id  System

/dev/loop0p1 8192 122879 57344 c W95 FAT32 (LBA) /dev/loop0p2 122880 5785599 2831360 83 Linux

Now we'll remake the last partition by deleting it, then recreating it at the same start location, same type but different end location. So make note of the "Start" column for loop0p2 (partition 2 - the Linux partition) - we'll use it later - its 122880 here.

sudo fdisk /dev/loop0

Enter the following - they are safe to enter - nothing permanent happens until you've read my explanation that follows:

  1. p
  2. d
  3. 2
  4. n
  5. p
  6. 2
  7. 122880
  8. just hit enter to accept the default
    
  9. p

Step 1 - print current table. Steps 2-3 - delete partition 2, Steps 4-8 - recreate partition 2 with new end point (default is end of image), Step 9 - print out the new table.

Assuming your newly printed table is identical to the original table except for the End value and Blocks value (ie the size has changed) you're ready to commit.

Enter w to commit your change, then enter q to quit.


Updating here - in 2023:

You can skip this next section on most modern Linux distributions as loop devices support partitions - eg you could simply access /dev/loop0p2 instead of having to map using an offset. eg:

sudo e2fsck -f /dev/loop0p2
sudo resize2fs /dev/loop0

Resumes original text:


You can delete that loopback device, we'll make another for the second partition. Remember the start offset you noted and used above - we'll use it again:

sudo losetup -d /dev/loop0     # delete the old loop setup
sudo losetup -o $((122880*512)) /dev/loop0 ./image.img

That will create a new mapping on /dev/loop0 pointing just at partition 2 - for reference 512 is the sector-size you can see in the first fdisk output.

Now resize the partition to fill the available space:

sudo e2fsck -f /dev/loop0
sudo resize2fs /dev/loop0

Done - now clean up:

sudo losetup -d /dev/loop0
Greg
  • 181
  • 1
  • 3
8

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!

Jivings
  • 22,656
  • 11
  • 94
  • 140
4

For the ones who have Oracle VM VirtualBox running it is quite easy.

On a Windows machine the things go as follows:

input.img is here the image which size should be reduced and output.img is the final image - in this example defined to be 7000 MB large.

ECHO "========================================================================="
ECHO "input.img => output.img"

ECHO "========================================================================="

DEL  input.vdi

DEL  output.vdi

DEL  output.img

ECHO "========================================================================="
"C:\Program Files\Oracle\VirtualBox\VBoxManage" convertfromraw input.img input.vdi -format VDI --variant Standard

ECHO =========================================================================
"C:\Program Files\Oracle\VirtualBox\VBoxManage" createhd --filename output.vdi --size 7000 --format VDI --variant Standard

ECHO =========================================================================
"C:\Program Files\Oracle\VirtualBox\VBoxManage" clonehd input.vdi output.vdi --existing

ECHO =========================================================================
"C:\Program Files\Oracle\VirtualBox\VBoxManage" clonehd output.vdi output.img --format RAW

Hope this helps a bit.

techraf
  • 4,353
  • 10
  • 32
  • 43
Markus
  • 41
  • 1
2

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/rpi.img

Next is resizing the partition and filesystem. First, get the offset of the partition;

fdisk -l /path/to/rpi.img

Results in a table like this:

Device    Boot   Start      End   Sectors   Size  Id  Type
rpi.img1          8192   122879    114688    56M   c  W95 FAT32 (LBA)
rpi.img2       1122880  8447999   8325120     4G  83  Linux

Note the start offset of the second partition

losetup --offset $((122880 * 512)) /dev/loop0 /path/to/rpi.img
e2fsck -f /dev/loop0
resize2fs -f /dev/loop0

Lastly unmount /dev/loop

losetup -d /dev/loop0

Done! Your disk should now be 5GB bigger!

JoSSte
  • 147
  • 10