Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 6:40 am

I would like to reduce the size of the file system on the Pi SDcard to make it possible to create a disk image which can be loaded onto a smaller SDcard and which also will take less space on my Win7 hard drive.
I have used a 32 GB SDcard to install Raspbian Wheezy (2015-05-05) and then installed Free-Pascal and Lazarus on it. The final size used on the card is about 5 GB, but the image created by Win32Diskimager is 30 GB....
So is there a way to shrink the file system on the Pi SDcard to be say 1 GB bigger than the used data size?
Bo Berglund
Sweden

User avatar
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 6:48 am

One way is to use Linux and a USB card reader to partition the new card with sufficient space, then copy the contents of each partition over (use something like rsync). That way you only copy over the used space and you can set the new file system up to use the full size of the card straight away.

Otherwise there are plenty of examples of methods that can be used - although win32diskimager always creates an image that is the size of the card, no matter how much of it is used.

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 6:55 am

Starting off with a 32gb card was probably not a good idea in hindsight. You could go through the same exercise and use say an 8gig card.
Or someone cleverer than myself could suggest a command to reduce the image............
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 7:56 am

I see this on my card now that I have installed fresh overnight. Took several hours...

Code: Select all

[email protected] ~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
rootfs          30304204 5075036  23947548  18% /
/dev/root       30304204 5075036  23947548  18% /
devtmpfs          470416       0    470416   0% /dev
tmpfs              94944     232     94712   1% /run
tmpfs               5120       0      5120   0% /run/lock
tmpfs             189880       0    189880   0% /run/shm
/dev/mmcblk0p1     57288   19400     37888  34% /boot
I assume that this command shows the same thing several times, rootfs sems to be the same as /dev/root judging by the identical size.
So out of 31121852 blocks 5094668 are used (16%)...
I guess that an 8GB card would be sufficient given that it leaves 2.6 GB space for data (source code).
But I need to do it all over a 4th time then.
I have failed totally on Jessie, then did it on Wheezy but screwed up the tightvnc installation so I redid it last night and this morning but on a reclaimed 32 GB card.
I guess that now that I know the drill I can as well redo it once more....
Bo Berglund
Sweden

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 8:00 am

You could try dowloadng a live linux iso ( lubuntu is a good choice IMHO ) , buring a cd and booting up ur pc from that ...
then you will have all the fun & and tools of linux ,
use gparted or 'disks' graphical tools to create a backup SD image & shrink the partitions as needed .

Just a thaught,
your emissions may vary .

--- edit ---
Or if you like the commandline tools should also work !

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 8:19 am

rpdom wrote:One way is to use Linux and a USB card reader to partition the new card with sufficient space, then copy the contents of each partition over (use something like rsync). That way you only copy over the used space and you can set the new file system up to use the full size of the card straight away.
Do you know if a card that sits in a USB card reader will be directly visible if inserted into USB on a Pi?
Or does one have to do something special in Raspbian to make it show up?
I am rally rather inexperienced in Linux and have to google every command all the time...
rpdom wrote:Otherwise there are plenty of examples of methods that can be used - although win32diskimager always creates an image that is the size of the card, no matter how much of it is used.
Is there a better disk imager that handles the empty space such that it does not use up space in the image file?
Bo Berglund
Sweden

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 8:44 am

If I get a USB card reader and plug it into the Pi with say an 8GB card inserted, is there then some kind of backup command inside Raspbian which can be used to create a backup of the running SD card content on the one inserted such that it can be used to boot the Pi from?
Basically create a file system on the attached SDcard but sized smaller than the original card with lots of unused space so that the end result is a card with the exact same content except in a smaller file system?
Bo Berglund
Sweden

User avatar
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 8:48 am

Bosse_B wrote:Do you know if a card that sits in a USB card reader will be directly visible if inserted into USB on a Pi?
Yes. It will usually appear as /dev/sda and any partitions on it will be /dev/sda1, /dev/sda2 etc.

The partitions may auto-mount if you are using a GUI or have an automounter installed. You probably want to unmount them before making any changes to the partition table on the new card.
I am rally rather inexperienced in Linux and have to google every command all the time...
That's a great way to learn. It's good that you are looking for things yourself :)
rpdom wrote:Otherwise there are plenty of examples of methods that can be used - although win32diskimager always creates an image that is the size of the card, no matter how much of it is used.
Is there a better disk imager that handles the empty space such that it does not use up space in the image file?[/quote]
Not that I'm aware of. I don't use windows though.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 10:38 am

Now I have bought myself a Kingston USB3/2 SDcard reader and put the SDcard of my last failed attempt into it and inserted into the Pi. It becomes visible in the File Manager below /media.
And on the command line fdisk -l reports it as /dev/sda with two partitions sda1 (FAT) and sda2 (Linux).
So the disk is visible!
I also bought myself two 8GB SDcards and one 16GB class 10.
If I insert the 16GB card into the reader and insert it into Windows USB it appear as an empty FAT32 drive with 16 GB space.
If I now move the adapter over to the Pi fdisk -l it is listed as:

Code: Select all

Disk /dev/sdb: 16.0 GB, 16003366912 bytes
255 heads, 63 sectors/track, 1945 cylinders, total 31256576 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192    31256575    15624192    c  W95 FAT32 (LBA)
How would I proceed in order to mirror the active Pi disk content onto either of the two SDcards?
Must I manually use Linux tools to:
- remove the existing partition on the disk
- create a new partition starting at 8192 and ending at 122879 (it must be the boot partition)
- create another partition starting at 122880 and ending at 15564799 (for the 8 GB card)
- format the first partition as FAT32 (LBA)
- format the second partition as Linux (what type, ext1-2-3-4?)
- copy all of the content of boot onto the first partition (dd?)
- copy all of the content of / except /boot to the second partition (dd?)

I assume that by doing the file-wise copy with dd the size of the target does not matter as long as the files will fit....
And would the so created disk be bootable in a pi?

It would be good to have someone spell out the exact commands needed for the partitioning, formatting and copy ....
Bo Berglund
Sweden

User avatar
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 11:11 am

Bosse_B wrote:Must I manually use Linux tools to:
- remove the existing partition on the disk
- create a new partition starting at 8192 and ending at 122879 (it must be the boot partition)
You don't need to set the "boot" flag. That is ignored by the Pi. Just make sure the partition type is set correctly (type 0c).
- create another partition starting at 122880 and ending at 15564799 (for the 8 GB card)
The end address will vary from card to card. Usually you can just accept the default end value that the partition utility offers.
- format the first partition as FAT32 (LBA)
Formatting isn't required if you use dd to copy the contents. dd will copy the format as well.
- format the second partition as Linux (what type, ext1-2-3-4?)
Yes, as ext4.
- copy all of the content of boot onto the first partition (dd?)
Yes, dd will work for the first partition as long as the destination one is exactly the same size.
- copy all of the content of / except /boot to the second partition (dd?)
Don't use dd for this. It will copy the whole filesystem at its current size. Make sure both source and destination filesystems are mounted and use rsync to copy the contents. If you use the -x option on rsync it will skip /boot (and /proc, /sys, /dev) automatically.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 12:56 pm

I read the man fdisk and found thta one should use cfdisk instead.
So I read man cfdisk too and then I went ahead and started it thus:

Code: Select all

sudo cfdisk /dev/sdb
I found how to delete the existing partition, but when I wanted to create the new partition it asks for the size of the partition (in MB) rather than the start and end sector. Now I am not sure what to enter here because according to my calculations the size of the FAT32 partition for the boot data is 57344x1024 bytes.
Would this be entered as 57344K (1K = 1024 bytes)
or
as 58720.256K (1K = 1000 bytes)
or
as 56M (1M = 1024x1024= 1048576 bytes)???
cfdisk asks for Mbytes, but what kind....
And how does one specify the start sector as #8092?

I made a nondestructive test and this is what I wound up with after setting the size of the first partition to 56, and taking the default size for the next partition:

Code: Select all

                             cfdisk (util-linux 2.20.1)

                                Disk Drive: /dev/sdb
                          Size: 7969177600 bytes, 7969 MB
                Heads: 221   Sectors per Track: 20   Cylinders: 3521

 Name    Flags       Part Type        FS Type         [Label] Size (MB)
-------------------------------------------------------------------------------
 sdb1    Boot         Primary         W95 FAT32 (LBA)             56.58
 sdb2                 Primary         Linux                     7912.61       *
This does not look right to me, but is it?
A bit scary this...
Bo Berglund
Sweden

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 1:05 pm

Forgot to show how the cfdisk view of my 32GB working card from yesterday looks like:

Code: Select all

This is cfdisk view of a working SDcard:

                              cfdisk (util-linux 2.20.1)

                                 Disk Drive: /dev/sdb
                           Size: 32010928128 bytes, 32.0 GB
                 Heads: 64   Sectors per Track: 32   Cylinders: 30528

 Name    Flags        Part Type        FS Type        [Label]  Size (MB)
------------------------------------------------------------------------
                       Pri/Log         Free Space                   4.20
 sdb1                  Primary         vfat           [boot]       58.73
 sdb2                  Primary         ext4                     31594.65
                       Pri/Log         Free Space                 353.38
Notice that it states vfat rather than FAT32 but fdisk shows it as FAT32.
And there is actually free space in front of the first partition (those 8096 sectors?)
How would I create this on the new card (free space-58.73M FAT32-ext4-free space)?
Or does it not matter?
Bo Berglund
Sweden

User avatar
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 1:54 pm

Just use fdisk. It gives you a bit more control on some things than cfdisk does. cfdisk is just a bit more user-friendly.

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

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 1:59 pm

Note that this conversation has been going on for as long as the Pi has been in existence. It just goes on and on, because there is no solution that meets all the needed criteria:
  1. Works effectively for low-information users.
  2. Efficient in terms of space and time.
  3. Is Windows based (see requirement 1 above)
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)

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 2:14 pm

Joe Schmoe wrote:Note that this conversation has been going on for as long as the Pi has been in existence. It just goes on and on, because there is no solution that meets all the needed criteria:
Hmm, I am positive that I have used RPis since 2012 and more frequently since Jan 2013.
And still this discussion started 2015-10-05 08:40:19 (timestamp of first post here (mine))...
Bo Berglund
Sweden

Heater
Posts: 13113
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 2:26 pm

This particular thread may be new but the discussion has been going on since the dawn of the Pi.

Here is what I would do:

1) Install gparted on my PC. It's a package you can install with "apt-get install gparted".

2) Put SD card into my PC and use gparted to resize the partition.

3) Perhaps use gparted to create new a partition or two in the resulting free space.

See here: http://gparted.org/display-doc.php%3Fna ... partitions

If you don't have Debian on your PC then get it. If you don't want to install Debian on your PC use a Linux live CD like knoppix or whatever to boot your PC and do the repair work.
Last edited by Heater on Mon Oct 05, 2015 2:33 pm, edited 2 times in total.

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

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 2:28 pm

Heater wrote:This particular thread may be new but the discussion has been going on since the dawn of the Pi.
Quite so. The actors and the characters change over time, but the message and content remains the same.
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)

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

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:05 pm

1) Install gparted on my PC. It's a package you can install with "apt-get install gparted".
This violates requirements 1 & 3.

Besides, you know perfectly well that when they say "my PC", they mean Windows.
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
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:26 pm

Joe Schmoe wrote:
1) Install gparted on my PC. It's a package you can install with "apt-get install gparted".
This violates requirements 1 & 3.

Besides, you know perfectly well that when they say "my PC", they mean Windows.
To be honest, with all the programming experience of some people on here, I'm very surprised that someone hasn't written a utility for Windows that will back up a Pi SD card (at the very least a Raspbian card, and if possible a NOOBS-with-Raspbian card) to a minimal image file or some sort of backup, and also have an option to expand that onto a new card.

I've got no experience programming for Windows, but I'm sure I could write a script that does it for Linux easily enough. It shouldn't be too hard to implement something, surely?

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:30 pm

I decided to stop wasting my and your time trying to copy the already installed disk to the smaller 8GB disk.
It is probably quicker to just do the installation again, now that I have a list of things to do.
(I do journal everything when I do these things so it can be repeated later on).
It will probably take 2-3 hours most of which is compile time for FPC and Lazarus...

I did not realize that a thing like this could be so problematic in Linux....
Sorry for asking.

And you are right, when I refer to my PC I usually mean Windows...
But I try to be clearer by saying my Win7 PC, but sometimes I forget...

Right now I have installed and tested VNC and retrieved the sources for FPC via svn and am ready to start compiling version 3.1.1 using version 2.6.4 :cool:
Will be done after dinner me thinks.
Bo Berglund
Sweden

User avatar
RaTTuS
Posts: 10415
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:42 pm

1) stuff a usb sd card adapter into the RPI
2) make 2 partitions using fdisk one vfat and one ext2
3) format them both
4) mount both partitions somewhere i.e. /mnt & /mnt/boot
4) sudo cp /boot to /mnt/boot
5) sudo rsync -ax / /mnt
6) sudo sync
7 sudo umount both partitions
8) remove sdcard and use that to boot elsewhere
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:50 pm

Steps 1-8
Violates requirements 1 & 3.
(Certainly #3. #1, perhaps debatable)
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)

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

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 3:58 pm

I've got no experience programming for Windows, but I'm sure I could write a script that does it for Linux easily enough. It shouldn't be too hard to implement something, surely?
I agree with the sentiment and that there's certainly no technical reason why this hasn't been done.

Unfortunately, there are various psychological and "social engineering" reasons why it hasn't and won't happen. Basically, what happens is that once you've achieved the level of skill necessary to do this thing, you will have achieved the level of skill necessary to do it the right way - in Linux - and will thus have lost any interest in going back and doing it in Windows. I see this pattern repeating over & over in various guises over the years.
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
RaTTuS
Posts: 10415
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 4:17 pm

it is fairly easy to script it
but there are quite a lot gotya's that will crash and burn so the script ends up being much more complicated than learning how to do it in a good way ;)
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
rpdom
Posts: 15014
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to reduce the Pi image on the SDcard?

Mon Oct 05, 2015 6:09 pm

Joe Schmoe wrote:
Steps 1-8
Violates requirements 1 & 3.
(Certainly #3. #1, perhaps debatable)
Seeing as no one really cares about your "requirements", joe, it doesn't matter :lol:

Return to “General discussion”