Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Reducing Raspbian File System Image Size

Mon Jan 20, 2014 6:15 pm

geneworld wrote:I actually tried that too and was getting the same errors :(
The previous poster was wrong - it is never wrong to use quotes (assuming you are issuing the command from a bash shell prompt). It may not be necessary, but it can't be wrong.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Reducing Raspbian File System Image Size

Tue Jan 21, 2014 2:07 am

geneworld wrote: I then tried:

Code: Select all

cd /home/pi
mkdir image
sudo perl ./resizeimage.pl '/home/pi/image/test.img'
And I still got:

Code: Select all

/home/pi/image/test.img does not exist.
You can't just create a directory and expect a file to be there. Here you created the 'image' directory and then ran then command. Of course it can't file the file, it doesn't exist there as you just created it and put nothing in there.

Have you create a .img file?!?
We 'idiot proofed' the world, and now it's full of idiots!

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Reducing Raspbian File System Image Size

Tue Jan 21, 2014 2:31 am

It was my understanding that this created the file from the os that is running. How would I create an image file? Seems a little counterintuitive to me but I am new to this.

What steps should I do?
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Reducing Raspbian File System Image Size

Wed Jan 22, 2014 4:04 am

geneworld wrote:It was my understanding that this created the file from the os that is running. How would I create an image file? Seems a little counterintuitive to me but I am new to this.

What steps should I do?
Your understanding was wrong. This script only works on an existing .img file. Which is why you were getting the following error:

Code: Select all

/home/pi/image/test.img does not exist.
You need to create an .img file with with Win32DiskImager. You can do it on your RPi as well, with the dd command, but you have to have the image file saved to a different location (flash drive, nfs).

http://sourceforge.net/projects/win32diskimager/

Joe Schmoe wrote:
geneworld wrote:I actually tried that too and was getting the same errors :(
The previous poster was wrong - it is never wrong to use quotes (assuming you are issuing the command from a bash shell prompt). It may not be necessary, but it can't be wrong.
I just took a quick guess as to why it wasn't working, and I realized I never tested the script with quotes. Turns out you're right, it doesn't matter.
We 'idiot proofed' the world, and now it's full of idiots!

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Reducing Raspbian File System Image Size

Wed Jan 22, 2014 4:37 am

Oh alright I think i got it...so the path that i would choose is where the image is? And then it will basically overwrite it and shrink it as much as possible?
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Reducing Raspbian File System Image Size

Wed Jan 22, 2014 4:49 am

geneworld wrote:Oh alright I think i got it...so the path that i would choose is where the image is? And then it will basically overwrite it and shrink it as much as possible?
Yes
We 'idiot proofed' the world, and now it's full of idiots!

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Reducing Raspbian File System Image Size

Wed Jan 22, 2014 12:53 pm

Great thanks! I'll have to try it when I get home from work :D
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

craiglindley
Posts: 1
Joined: Fri Jan 24, 2014 4:52 pm

Re: Reducing Raspbian File System Image Size

Fri Jan 24, 2014 4:59 pm

I've used dd to copy an SD image to my MacBook. I tried to copy this using dd but the resultant SD card fails to boot. I then tried using Win32DiskImager for the same process and when I attempt to write out the image, it fails because the new SD card is slightly smaller than what is needed.

Is it possible to run the resize image perl script on my Mac?

Thanks

BillStephenson
Posts: 254
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: Reducing FS image size

Sun May 04, 2014 6:26 pm

tvjon wrote:Yes, I agree it's not a script problem, just wondered where those characters were introduced in the chain.

I didn't mention a specific browser as they all produce the same result, so pick any.
tvjon, I think this may be a Mac issue. It's been happening to me since the introduction of "Mavericks", and it's intermittent, so it's hard to nail down. A potential work around is, and I know this sounds strange, empty the trash on your Mac and restart it.

After that you should be fine, but it may appear again after a bit, so just repeat the steps above if it does.

BillStephenson
Posts: 254
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: Reducing Raspbian File System Image Size

Mon May 05, 2014 1:16 am

DeanC, I want to thank your for posting your script, and all the others here who've helped with supporting it. It's exactly what I needed.

DiBosco
Posts: 28
Joined: Wed Feb 13, 2013 12:00 am

Re: Reducing Raspbian File System Image Size

Tue Jul 29, 2014 8:33 am

I've tried cloning my card by running:

dd if=/dev/sdc of=/home/robertw/pi_image/sd-card.bin

Then:

Code: Select all

dd if=/home/robertw/pi_image/sd-card.bin of =/dev/sdc
Which gave me a superblock error - even though the card I used to copy to is identical to the one I did the original install on. A Google search pointed me to this thread. However, when I run:

Code: Select all

perl ./resizeimage.pl /home/robertw/pi_image/sd-card.bin
I get:

Code: Select all

sd-card.bin:
============
Old size - 3216 MB (3.14 GB)

/dev/loop0: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
        (i.e., without -a or -p options)
There was an error in the file system that can't be automatically fixed... aborting.
Is the image I produced simply unusable? I'm not sure what to do about running fsck as as far as I understand, /dev/loop0 is a temporary thing created by the perl script. Plus I tried doing that when I booted into the pi originally using this cloned card.

Many thanks!

TylerD75
Posts: 2
Joined: Fri Aug 22, 2014 1:39 pm

Re: Reducing Raspbian File System Image Size

Fri Aug 22, 2014 1:55 pm

Thank you!

This worked like a charm for me too.
Mounted a shared Windows folder on Raspbian, and ran the script on an OpenElec image:
Shrunk from 8GB to 3.6GB!

User avatar
ilovemipi
Posts: 152
Joined: Mon Dec 02, 2013 11:52 am
Location: UK + Cyprus
Contact: Website

Re: Reducing Raspbian File System Image Size

Sat Aug 23, 2014 10:17 am

TylerD75 wrote:Thank you!

This worked like a charm for me too.
Mounted a shared Windows folder on Raspbian, and ran the script on an OpenElec image:
Shrunk from 8GB to 3.6GB!
I didn't think this was possible with OpenElec images.
Can you please provide a few more details of how you 'mounted a shared Windows folder on Raspbian'.

User avatar
ilovemipi
Posts: 152
Joined: Mon Dec 02, 2013 11:52 am
Location: UK + Cyprus
Contact: Website

Re: Reducing Raspbian File System Image Size

Sun Aug 24, 2014 1:17 am

TylerD75 wrote:Thank you!

This worked like a charm for me too.
Mounted a shared Windows folder on Raspbian, and ran the script on an OpenElec image:
Shrunk from 8GB to 3.6GB!
You are right it does work for OpenElec images mounted on a shared folder with Raspbian.

Code: Select all

pi@raspberrypi ~/sambashare/img $ sudo perl ./resizeimage.pl ~/sambashare/img/kp.img

kp.img:
=======
Old size - 3767 MB (3.68 GB)
New size - 983 MB (0.96 GB)
Image file was reduced by 2784 MB (2.72 GB)
I think I have used http://forum.stmlabs.com/showthread.php?tid=10484 to fill the SD card for OpenElec once you have transferred the smaller image to a smaller SD card. The thread refers to Raspbmc but it works for OpenElec too as it doesn't have raspi-config available to fill the SD card.

mikerr
Posts: 2825
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Reducing Raspbian File System Image Size

Fri Nov 28, 2014 4:39 pm

I have got this script running ok only on a freshly downloaded raspbian image
(before its been in a pi)

On all my (raspbian) images that have been in a pi and had weeks of config I get errors
(these images work fine in a pi, and I frequently image them back to SD cards)

Code: Select all

sudo ./autosizer.sh pi-tft.img
losetup: invalid offset 'pi-tft.img' specified
found the problem is this line:

Code: Select all

sudo parted -m pi-tft.img  print
Error: Can't have a partition outside the disk!
But it appears ok ?

Code: Select all

 sudo fdisk -l -u  pi-tft.img

Disk pi-tft.img: 7948 MB, 7948205568 bytes
255 heads, 63 sectors/track, 966 cylinders, total 15523839 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: 0x000b03b7

     Device Boot      Start         End      Blocks   Id  System
pi-tft.img1            8192      122879       57344    c  W95 FAT32 (LBA)
pi-tft.img2          122880    15523839     7700480   83  Linux

any ideas ?
Android app - Raspi Card Imager - download and image SD cards - No PC required !

georgewolferm
Posts: 1
Joined: Mon Dec 15, 2014 7:55 pm

Re: Reducing Raspbian File System Image Size

Mon Dec 15, 2014 8:06 pm

I love you! It took me a week to figure this out. Shouldn't be so hard to figure out. Suggestion: include instructions to write file path and name after

Code: Select all

 sudo perl ./resizeimage.pl
like

Code: Select all

 sudo perl ./resizeimage.pl  [replace these brackets with filepath/name where your .img file is starting with /]  
you have to keep it very simple for us noobs to be able to do it.

kubislav
Posts: 1
Joined: Tue Apr 21, 2015 8:48 pm

Re: Reducing Raspbian File System Image Size

Tue Apr 21, 2015 8:53 pm

mikerr wrote:I have got this script running ok only on a freshly downloaded raspbian image
(before its been in a pi)

On all my (raspbian) images that have been in a pi and had weeks of config I get errors
(these images work fine in a pi, and I frequently image them back to SD cards)

any ideas ?
mikerr, I had the same problem using this script as you, but seems that I have found the solution. When making img file via win32diskmanager, you must write the .img suffix. I never did and it was giving me the corruption errors. Now when I have tried making a new one with .img suffix, it is working.

So just to be clear, before you press read, write mynewimage.img, not just mynewimage ...

I hope it will make your problem go away. And thanks to OP for such a ingenious script.

dave a walker
Posts: 2
Joined: Wed Jul 22, 2015 6:52 am

Re: Reducing Raspbian File System Image Size

Mon Aug 17, 2015 11:32 am

I have 16GB sdcard- i did not expand it with Raspi-config
I used dd on a linux box to retrieve the image
when i run the perl scrip resizer it says the image is only an 8GB .
ls -la says it is 16GB.
when I send the resized img to a new card it does not boot up...
I normally use HDD raw copy on windows to make sd cards...
My problem stems from my 16GB sd cards varying in size from 16Gb down to 15.5GB..

I still have not been able to write a new usable 16GB sd card...

timg11
Posts: 63
Joined: Sat May 31, 2014 11:14 pm

Re: Reducing Raspbian File System Image Size

Tue Aug 18, 2015 9:23 pm

I understand that the script works on Raspbian Images. Are there any requirements on the environment where it is run? I.E. can I run this on a Ubuntu system?
Or does it have to be run on the Raspberry Pi?
DeanC wrote:This script will take a Raspbian image (.img) file and reduce it to it's smallest size. Once the reduce image is copied to a new SD card, make sure you expand the file system.

I have added a test in the script. It will stop the script from executing if an error is encountered while checking the file system that e2fsck cannot automatically fix. There is no reason to continue the script if the image is corrupt.

*** THIS SCRIPT ONLY WORKS ON RASPBIAN IMAGES. ***

To use this script, copy the code to a file called 'resizeimage.pl'. Then issue 'sudo perl ./resizeimage.pl'.

After burning, always remember to run 'sudo raspi-config' first thing, and expand the file system before use.

VishalSanu
Posts: 1
Joined: Mon Feb 01, 2016 11:10 am

Re: Reducing Raspbian File System Image Size

Mon Feb 01, 2016 11:37 am

Hi ,

I have used resizeimage.pl script to reduces the size of my backup image. I have then burn new reduced image to a SD card. It did not worked for me. Pi is not booting up. I have used following steps. Please let me know if i am missing some thing.

I wanted to take backup of my 16gb SD card and burn it a 8 GB SD Card.

1. Took the backup of 16gb SD card using "sudo dd bs=4M if=/dev/sdb of=/home/champ/share-host/raspi-bkp/raspibkp1.img"
2. Used resizeimage.pl script to re size it. "sudo perl ./resizeimage.pl ~/share-host/raspi-bkp/raspibkp1.img "
3. Burn the image to new 8 gb SD Card. "sudo dd bs=4M if=~/share-host/raspi-bkp/raspibkp1.img of=/dev/sdb"

Thanks and Regards,
Vishal

PaulViscovich
Posts: 1
Joined: Mon May 23, 2016 7:07 pm

Re: Reducing Raspbian File System Image Size

Mon May 23, 2016 7:53 pm

First Thank you,

Second I know this is an old thread.

I was happy to com across this thread as the script as is worked like a charm. thanks to all that came before me to help get it tweaked into working as it does today.

(Smoke Cleared)

I just wanted to let folks know that if you're dealing with large images.. (128gig) this can take a very long time to complete its tasks, i started a run at 5pm on a friday and it ran over the weekend finally finishing early monday morning. the only negative is the lack of screen output letting you know its still running. I just took the wait and see approach and got through it finally,

I started with a 128 gig image and now have a 32 gig image just a little too large to to write back to a sd card but write back to a 64 gig just fine. . if i were to remove a few more items i might be able to write it back to a 32 gig card..

Again thanks for the working script.

PaulV.

Ertong
Posts: 1
Joined: Sat Jan 14, 2017 9:35 pm

Re: Reducing Raspbian File System Image Size

Sat Jan 14, 2017 9:44 pm

It looks like hardcoded

Code: Select all

$size = $size + 58720257;
worth to be replaced with

Code: Select all

$size = $size + $start;

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: Reducing Raspbian File System Image Size

Sat Apr 29, 2017 11:51 am

i made an image of my raspbian install using win32DiskImager - 16gb micro sd card
and trying to shrink it down to fit on an 8gb card

put the file on an 32gb NTFS usb stick put into the pi and errors

Code: Select all

name.img
=========
old size 15193 MB (14.84 GB)
e2fsck: operation not permitted while trying to open /dev/loop0
There was an error in the file system that can't be automatically fixed.... aborting.
copied to a Crashbang linux i am running in vmware
put the file on the os
ran the script and get

i get an error

Code: Select all

 There was an error in the file system that can't be automatically fixed.... aborting.
put the file on an 32gb ext4 usb stick but errors trying to mount it

Code: Select all

error mounting: mount wrong fs type, bad option, bad superblock on /dev/sda5
missing code page pr helper program or other error in some cases useful info is found in syslog - try dmesg|tail or so
what am i doing wrong?

Return to “Beginners”