PiShrink an SD Card backup

I have a Windows PC ( laptop ) and needed to shrink the size of my 8Gb disk image file.

It is a known fact that not all SD cards of equal 'size' are actually the same identical size, so an img file of one card may not fit on another card.

This solution allows you to shrink the size of the primary partition in the SD card image file, and it will automatically expand itself to whatever size card you write the img file back on to.

So, in my case, the 8 Gb SD card is shrunk to 1.9 Gb.
When written to a 16 Gb SD card and loaded in the Pi, the partition automatically expands to fill the 16 Gb.
When written to a 4 Gb SD card and loaded in the Pi, the partition automatically expands to fill the 4 Gb.

The disk image file was created using Win32DiskImager on the windows pc.

I downloaded and installed VirtualBox on Windows

I downloaded the Ubuntu 17.10 iso disk image from here :
https://www.ubuntu.com/download/desktop

NOTE : do NOT use the version 16 - I tried that and wasted a day. Use the version 17 - just trust me on this.

Started VirtualBox
Add a New machine
Type : Linux
Version : Ubuntu (64 bit)
Set Memory to around 1500Mb
Create a Virtual Disk
File Location : Ubuntu1710 ( 7.10 is Ubuntu version that I downloaded )
File Size 20 Gb
VDI
Dynamically Allocated
Create
Click on Settings
System - turn off the floppy disk
Display - set video memory to around 32Mb
Storage :
IDE change 'empty' to 'choose'
set the ISO storage to the uBuntu disk image that you downloaded
Network : change NAT to Bridged -- important, otherwise you won't be able to see your shared folders to transfer the files to and from Linux.

Start the Virtual Machine

It will start and begin the installation of Linux from the iso disk image

Follow the prompts
Install Ubuntu
Continue
Install Now
Continue
Time Zone - continue
English - continue
Your name ubpi
user ubpi
pass berry
login automatically - continue

Long wait while installation happens

on my PC, I have a network shared folder : F:\MyFiles\Documents

Use Win32DiskImager to 'read' the SD card to a local file in the shared folder ( in my case, the Documents folder )

The file size form my 8Gb SD card was 7.6 Gb

Save the following script lines to a txt file and save or copy in to the shared ( Documents ) folder.

Install pishrink ( if not already installed )
----------------
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin


Run pishrink to shrink the img file :
-------------------------------------
sudo pishrink.sh 20180316_octopi_DaveOB.img

When Linux installation is complete, click on Restart

Remove the Installation medium ( need to unlink the iso installation disk image )
Devices
OpticDrive
Choose Disk Image
Cancel
Press enter to continue

Wait for the Linux desktop

Once loaded, we need to copy the txt file and the SD img file to the Linux machine :
on the left, go to Files
Other Locations
Windows Network
Workgroup
Dave_Laptop ( that's my laptops name on the network - yours would be different )
Login with your windows username and windows login password
Documents ( or whatever you named the folder that you shared in Windows )
If asked for a keyring pass, enter : berry

You should now see the txt and the img files.

Right Click on the txt file and Click on Copy.
Then change location, on the left panel, to the Linux 'Downloads' folder.
Right Click and Paste

You should now have a copy of the txt file in the Linux Downloads folder.
Do the same process for the img file

NOTE : the 8Gb image file does take quite a while to copy. watch the progress bar that appears on top of the 'files' icon on the left panel. Wait for it to complete. Mine took a good 30 minutes to copy.

Next, you need to download and install the PiShrink app ( once off operation ).

Right Click on the txt file and open it in the default text reader / notepad
Find the line that starts with 'wget', highlight the line, right click and Copy.
Click on the File icon to display the file manager again.
Right Click on the blank area below the txt and img files, and select 'Open in Terminal'
The terminal will open ( looks like a DOS prompt Cmd screen )
Right Click and Paste
The wget line will be pasted - hit Enter to execute the line.
Repeat this for each of the other 2 lines from the txt file.

Once done, you're ready to shrink the img file.

from the txt file, highlight and copy the line
sudo pishrink.sh 20180316_octopi_DaveOB.img ( change the '20180316_octopi_DaveOB' to the file name of your img file )
Press enter to execute the script.

Once it is done, close the Terminal window and refresh ( F5 ) the file manager window.
Right Click on the img file, properties, and you should see the new size of the img file.
Close the properties window.

Last thing is to copy the smaller img file back to your windows folder.
If you want to keep the original file, you can rename the smaller img file before copying it back.
Right Click on the file and Copy
On the left panel, go to the shared Documents folder, right Click and Paste.
Give it time to complete the copy process.

After you use Win32DiskImager to write the img file to your new SD card and insert in to the Pi and boot up, give it additional time for the first run, as it has to then expand the primary partition to fill the disk.

Done.

2 Likes

You could also boot your computer with something like SystemRescueCD:

http://www.system-rescue-cd.org/

One of the tools within is gparted, which I use all the time for resizing partitions. It's actually a full Linux setup that can boot from removable storage (CD, DVD, or USB flashstick). Once you've shrunk your partitions, you can use dd to image your SD cards.

SystemRescueCD doesn't install anything on your computer, so it'll leave whatever OS is installed alone.

I've been using PiShrink for a couple of years now, but I installed it on my Pi and just run it there. I can make an image of the SD card from which I currently booted my Pi, and then shrink that. I've installed it on most of my SD cards to give me the ability to shrink an image anytime I have my Pi with me. (This came in handy on several occasions where I was helping out at a local school and wanted to make backups of the kids SD cards, but never knew what size card they might restore to. The school would not let us put Linux on any of their machines.)

Install PiShrink by entering the install commands from the post above on the Pi's command line.

Create an .IMG file from the SD card the pi booted from with the following command:
sudo dd if=/dev/mmcblk0 of=pathname/mynewimage.img

This creates a new .IMG file named "mynewimage.img" at the pathname specified. "Pathname" should be a path to an external storage device, such as a USB drive. Note that the image will be an exact copy of the SD card from which you booted. If that card is 16GB, then the image file will also be 16GB. (If I recall correctly, you'll need at least double the size of your SD card in free space on your USB drive.) This process takes some time, particularly with larger cards (it's a LONG process for a 32 GB SD card, even if most of it is empty). You have to specify the pathname, since without it the Pi will try to store the new image on your boot SD card, and it will not fit.

I then navigate to the directory containing that new image and shrink it with the following command:
sudo pishrink.sh mynewimage.img

In case anyone is interested, here is the GitHub repository for PiShrink:

The readme file under the Code tab includes some brief instructions and info on options along with an example screen output of a successful run. One thing not noted in the instructions and example is that you can include a pathname for both the source and the output file.

I have Parallels running on my Mac. After a lot of work, I got Debian up and running as a virtual machine under Parallels. (Parallels instructions for installing Debian absolutely stunk at the time. I haven't looked to see if they have updated them.)

I never was able to get PiShrink to run under this setup. Has anyone else done this successfully?

I guess I'll have to test this on Ubuntu. I'm sure my Mac won't have the right stuff for this.

The Ubuntu Live USB doesn't install anything to a computer, btw. It will boot in about three minutes' time. It works on Mac computers as well.

Blockquote > The Ubuntu Live USB doesn't install anything to a computer, btw. It will boot in about three minutes' time. It works on Mac computers as well.

They would not let us do that either. I couldn't even convince them to let me put a Pi on their network, so we started with the Pi running its own hotspot. Then one of the kids managed to figure out the WiFi password, so we got on anyway.

Mac OS does not, which is why I tried it on a Debian VM under Parallels.

One of these days, I'll try the Ubuntu Live USB on my Mac, but running PiShrink on the Pi is getting it done for me for now (it's just SLOW)

I have an Ubuntu laptop and a dual-boot MacBook. But honestly, the Ubuntu Live USB is hella fast by comparison.