Page 1 of 1

Raspberry Pi: Shrinking image to fit on a smaller card

Posted: Tue Mar 10, 2015 11:09 pm
by Floob
This was (is?!) a pain. I wanted to shrink my image to fit on a smaller, or even same size card.
When I use Win32DiskImager to make an image it wouldnt write the same image back to an SD card of the same size.

The upshot is that you have to shrink the filesize of the image slightly.

So....

Get your SD card

1) Use gparted to create some unallocated space:
http://gparted.org/download.php

This is a Linux "live" CD, so when you write the ISO to a CD and boot from it, it gives you a virtual linux environment to run gparted in.
I ran it in safe mode as it had a wobbly with drivers otherwise for me.

When in gparted, make sure you select the device that is the SD card (should be easy to tell by overall size shown).

Resize your main (largest) partition by clicking on it and dragging the bar left to free up some space.
This smaller size is what we will resize the image to. Remember to click Apply to make the change to the card.

If this produces an error, try reducing the partition less (so leave less in the unpartitioned area).

Image

1.5) Create image file of your SD card with Win32DiskImager
Write new image file from newly reduced sd card.

You could do this next part in Linux (see end of post for details), but because I use windows I do this:

2) Download Cygwin
https://www.cygwin.com/

Copy your SD image (created with Win32DiskImager) across to cygwin home directory
Probably something like C:\cygwin64\home\{Username}

In a cygwin terminal type (this may be in /sbin at the root)

Code: Select all

/usr/sbin/fdisk -lu {Image filename}
This will output the structure and size of the image.
An example is

Code: Select all

Disk RPi1-2.6.0.16GB-shrunk.img: 14.7 GiB, 15811477504 bytes, 30881792 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
Disklabel type: dos
Disk identifier: 0xb847d94e

Device                      Boot  Start      End  Sectors  Size Id Type
RPi1-2.6.0.16GB-shrunk.img1 *      8192   124927   116736   57M  e W95 FAT16 (LBA)
RPi1-2.6.0.16GB-shrunk.img2      124928 29835263 29710336 14.2G 83 Linux
3) Then we resize the image with "truncate"

The calculation for your resize is (based on my numbers above)
End Sector of last (largest) device = 29835263
Add 1, because sectors start at 0
29835263 + 1 = 29835264
Multiply this by the sector size (512) = 29835264 * 512 = 15275655168

So my truncate command would be:

Code: Select all

truncate --size=15275655168 RPi1-2.6.0.16GB-shrunk.img
This then produces a smaller image filesize without overwriting a live partition.

Then when you write that image back to an SD card and boot it, make sure to run raspi-config and extend the filesystem again.


If you want to do this in Linux there is an auto script that does it for you here:
http://sirlagz.net/2013/03/10/script-au ... downsizer/

Some useful links:
http://raspberrypi.stackexchange.com/a/7192/20719
https://github.com/Drewsif/PiShrink - Very popular
https://github.com/dweeber/rpiwiggle
http://softwarebakery.com/shrinking-images-on-linux
http://sirlagz.net/2013/03/04/how-to-re ... le-part-2/
https://cygwin.com/faq/faq.html#faq.using.not-found

Re: Raspberry Pi: Shrinking image to fit on a smaller card

Posted: Sun Jan 29, 2017 8:45 pm
by evilalex
Hi Floob,

i have used your manual to resize the image from my 32GB sd card.
only after the truncate when i have written the new image to an other card it didnt work(the rpi didnt boot).
stil working on finding out what i did wrong.

but i found an mutch faster way to make a image.
since symantec has resurrected ghost, i used one of the latest builds (found online) and this worked just fine.
it compresses the backup automaticaly and when writing back to any sdcard it automaticaly resizes the partitions.
i even used this to copy the ext partition to an external hdd to let my rpi run from it.(still needs the fat32 on the sdcard to boot)
ghost runs just as a program on the computer and works on any windows system.

Re: Raspberry Pi: Shrinking image to fit on a smaller card

Posted: Fri Sep 15, 2017 5:33 pm
by Clax
evilalex wrote:Hi Floob,

but i found an mutch faster way to make a image.
since symantec has resurrected ghost, i used one of the latest builds (found online) and this worked just fine.
it compresses the backup automaticaly and when writing back to any sdcard it automaticaly resizes the partitions.
i even used this to copy the ext partition to an external hdd to let my rpi run from it.(still needs the fat32 on the sdcard to boot)
ghost runs just as a program on the computer and works on any windows system.
Can you be more specific here? Is it just called Ghost, or in a suite of some kind? Do you know the version number? What's the file extension of the images it makes, or does it give you options on that?

TIA!