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.