ElEscalador
Posts: 839
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 8:51 pm

I have a Pi running on a 16gb card and I like to back up entire images to Dropbox... This is what I seen in Gparted - what are the correct things to click so this would fit on an 8GB sd card? I get a warning if I try to shrink the big partition root0. (Ideally it'd be a bit smaller than 8GB- I learned the hard way that an image of an 8GB sd card won't always fit on other cards. I'd like to not have to worry about that.)
Image
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 9:13 pm

Side-stepping your question a little, if you are running a recent version of Raspbian Jessie you have access to an SD Card Copier (PiClone) which I think is designed to make a backup that is as large as needed to capture all your data, but no bigger.

ElEscalador
Posts: 839
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 9:19 pm

B.Goode wrote:Side-stepping your question a little, if you are running a recent version of Raspbian Jessie you have access to an SD Card Copier (PiClone) which I think is designed to make a backup that is as large as needed to capture all your data, but no bigger.
Hmmm...does it? In this case I'm running an older version anyway, but even if I were - I've used the copier tool included with the Pixel releases and have not seen an option to create an .IMG file that I could then upload to dropbox for a rainy day - only the ability to make another actual card (which is great, but not what I'm hoping to do here). Have I missed something terribly simple and obvious? Wouldn't be the first time, lol.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 9:30 pm

My thinking was that if you can clone to a backup that is smaller than your 8G limit then you could easily make an image of that backup. If the clone is bigger than 8G chances are there is no scope for shrinking an image of it to make it smaller...

W. H. Heydt
Posts: 12645
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 9:36 pm

As regards the initial problem...gparted will change the partition sizes, but the usual backup tools (win32diskimager and dd) copy the entire contents of the SD card, whether or not those blocks are part of a file system or not. It's the difference between a logical copy and a physical copy. You need a backup process that recognizes and uses partition allocation information.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 9:55 pm

W. H. Heydt wrote:As regards the initial problem...gparted will change the partition sizes, but the usual backup tools (win32diskimager and dd) copy the entire contents of the SD card, whether or not those blocks are part of a file system or not. It's the difference between a logical copy and a physical copy. You need a backup process that recognizes and uses partition allocation information.
And of course that limitation in the behaviour of physical backups is the reason why my suggested way forward will not work...

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 10:34 pm

Hi.
to make my image files as small as possible for backing up I use a script run on a ubuntu desktop pc , I actually run ubuntu server 12 on an old pc as it does not require much in the way of resources and I can just ssh in to it and use the command line.

so I copy my img file to the pc then run this script

Code: Select all

#!/bin/bash

IMG="$1"

if [[ -e $IMG ]]; then
	P_START=$( fdisk -lu $IMG | grep Linux | awk '{print $2}' ) # Start of 2nd partition in 512 byte sectors
	P_SIZE=$(( $( fdisk -lu $IMG | grep Linux | awk '{print $3}' ) * 1024 )) # Partition size in bytes
	losetup /dev/loop2 $IMG -o $(($P_START * 512)) --sizelimit $P_SIZE
	fsck -f /dev/loop2
	resize2fs -M /dev/loop2 # Make the filesystem as small as possible
	fsck -f /dev/loop2
	P_NEWSIZE=$( dumpe2fs /dev/loop2 2>/dev/null | grep '^Block count:' | awk '{print $3}' ) # In 4k blocks
	P_NEWEND=$(( $P_START + ($P_NEWSIZE * 8) + 1 )) # in 512 byte sectors
	losetup -d /dev/loop2
	echo -e "p\nd\n2\nn\np\n2\n$P_START\n$P_NEWEND\np\nW\n" | fdisk $IMG
	I_SIZE=$((($P_NEWEND + 1) * 512)) # New image size in bytes
	truncate -s $I_SIZE $IMG 
else
	echo "Usage: $0 filename"
fi
it shrinks the image file to a absolute smallest it can allowing just enough space for the system to boot.
once you copy the file to a new sd card and its booted the first thing you do is run raspi-config and expand the file system.

you run the script like this assuming script save with file name shrink-img-file.sh

Code: Select all

sudo ./shrink-img-file.sh name of file to shrink goes here.img
now this is not my work I got the script from a post on this very forum some time ago.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Mon Jan 02, 2017 10:35 pm

Are you trying to shrink a partition that is in use? I mean you appear to be using gparted on pi itself.

If so, you can get around this by not using the same pi. Shut it down and take out the card. Then use a linux laptop, another pi (or the same pi with another raspian card) or live dvd/usb boot a Windows laptop temporarily with gparted live disk. Insert the raspbian card. Then the partitions will be offline and unmounted. Shrink said partition and copy-paste all of them on a smaller card or whereever you want. Gparted allows you to copy paste entire partitions. Then full physical back up that smaller card to your cloud as an image file that you can write with windows. Or even simpler, just copy the partitions and later use gparted again to copy paste them back without having to create img file

ElEscalador
Posts: 839
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Tue Jan 03, 2017 5:24 pm

tpylkko wrote:Are you trying to shrink a partition that is in use? I mean you appear to be using gparted on pi itself.

If so, you can get around this by not using the same pi. Shut it down and take out the card. Then use a linux laptop, another pi (or the same pi with another raspian card) or live dvd/usb boot a Windows laptop temporarily with gparted live disk. Insert the raspbian card. Then the partitions will be offline and unmounted. Shrink said partition and copy-paste all of them on a smaller card or whereever you want. Gparted allows you to copy paste entire partitions. Then full physical back up that smaller card to your cloud as an image file that you can write with windows. Or even simpler, just copy the partitions and later use gparted again to copy paste them back without having to create img file
Ahh..may give that a shot. (But nope - you were looking at a shot of a card plugged into USB - operating off of another Pi.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: Help Using Gparted to Shrink Resulting .IMG File Size

Tue Jan 03, 2017 6:05 pm

Ok. My bad. What error is it that you get then?

Return to “General discussion”