User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Reducing SD card partition size on a live Pi

Tue Nov 05, 2013 10:07 pm

I have an 8 gig SD card with file system fully expanded. I want to use Win32DiskImager to make a copy of the SD card for use in a second Pi and have hit the issue that not all SD cards are the same size, hence I can't write a saved image from Win32DiskImager to my second card as it's very slightly smaller. So I need to reduce the size of the file system on my SD card in the hope that Win32DiskImager will then work for me.

To do this, I've followed the most excellent instructions here http://www.instructables.com/id/Raspber ... /?ALLSTEPS and at the bottom of the page her http://elinux.org/RPi_Resize_Flash_Partitions. However on re-boot I get a kernel panic. What am I doing wrong?

The picture below is what I'm doing to resize the partition (on the live Pi, 'cos the only Linux I have is Pi's), which is basically to delete and then remake mmcblkp02 as follows:

partition 2 original start sector 122,880, original end sector 15,759,359
partition 2 new start sector 122,880, new end sector 15,500,000
fdisk_small.jpg
fdisk operations [right click on picture and open in new tab to see it a sensible size]
fdisk_small.jpg (49.7 KiB) Viewed 14662 times
The picture below is the outcome on reboot, which is basically a panic because of:

EXT4-fs (mmcblk0p2): bad geometry: block count 1954560 exceeds size of device (1922140 blocks)
panic_too_small.jpg
Kernel panic at reboot [right click on picture and open in new tab to see it a sensible size]
panic_too_small.jpg (48.94 KiB) Viewed 14662 times
Can anyone tell me what I'm doing wrong, or suggest another way I might do this (that doesn't involve using another Linux PC 'cos I'm just Pi's and Windows I'm afraid)?

Rob

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Reducing SD card partition size on a live Pi

Tue Nov 05, 2013 11:49 pm

Shrink the filesystem then shrink the partition that it lives in.
If you were growing than you wold do partition then filesystem.
2160p + USB3 + Wayland: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 8:14 am

Aaah, OK, I've not come across any instructions on shrinking the file system first. Any pointers on a command to do that?
Rob Meades
http://www.meades.org

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 8:16 am

Silly me, I suppose that would be 'resize' wouldn't it? I'll go RTFM...
Rob Meades
http://www.meades.org

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 5:21 pm

OK, I have tried resize2fs but it will not resize a file system that is mounted (which is the only option I have since I only have Linux on Pi's with the one SD card in them). Is there a way to resize a mounted file system or am I b*ggered?
Rob Meades
http://www.meades.org

RobHenry
Posts: 452
Joined: Fri Sep 21, 2012 9:04 pm
Location: UK

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 6:50 pm

If you have a USB SD card reader (they can be bought at poundland) then you could achieve this while booted from your spare SD card. The 2nd partition on the card would probably show up as /dev/sda2 when attached by USB.

Edit: Another option would be to boot your PC from a linux live CD - you can get an image that boots directly into gparted which would do the job nicely.

rcblackwell
Posts: 13
Joined: Thu Oct 03, 2013 7:26 pm
Location: Pickering, Ontario, Canada

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 7:27 pm

Rob Meades wrote:OK, I have tried resize2fs but it will not resize a file system that is mounted (which is the only option I have since I only have Linux on Pi's with the one SD card in them). Is there a way to resize a mounted file system or am I b*ggered?
You could try the script in this post http://www.raspberrypi.org/phpBB3/viewt ... 69#p444354. it worked great for me.
-------------------------
Robert Blackwell
Pickering, Ontario
Canada

www.blackwellgallery.com
www.afticarr.com

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Reducing SD card partition size on a live Pi

Wed Nov 06, 2013 11:03 pm

I'm sure there is a way to do this but it's not simple...
http://sourcepole.ch/howto-shrink-a-rem ... n-wheezy-3
only there is no grub on the rpi so that complicates it... or if you were using Btrfs or zfs on root...
2160p + USB3 + Wayland: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Thu Nov 07, 2013 10:00 pm

Sorry about the silence guys, have been trying to help myself but not yet made it. I, too, realised that I could get an Ubuntu distribution (12.10) on a bootable USB stick and so I did a resize2fs there:

resize2fs -f /dev/sde1 15377120s

...where 15377120 sectors is the right size to fit both partitions in such that the last sector is 15500000, which seems like a sensible number under the ~8 GB SD card size I'm working with. I followed this with the fdisk steps to reduce partition 2 to the same size and so I now have an image which works fine in my Pi (so I haven't rubbished any files in the file system in doing so, at least none that I've run yet...).

However, Win32DiskImage plainly doesn't understand the partition information and so still wants to copy the entire SD card and hence that hasn't helped me. Since I have a bootable Linux environment I decided that dd would be my answer. Inspired by http://www.linuxweblog.com/dd-image I did this in Ubuntu:

sudo dd if=/dev/sde2 conv=sync,noerror bs=512 count=15500000 of=mmcblk0.img

...to create an image file of just the right number of sectors. This appeared to work, reporting:

15377121+0 records in
15377121+0 records out
7873085952 bytes (7.9 GB) copied, 467.486 s, 16.8 MB/s


However, when I re-write that image, to either of the SD cards, with:

sudo dd if=mmcblk0.img of=/dev/sde1 conv=sync,noerror bs=512

...the SD card is not recognised by anything at all. I note that under Ubuntu I can't use fdisk on either SD card (including the one that is known to work in the Pi), it can't find the partition tables. So I have an SD card formatted to the right size but I'm now unable to make a copy from it.

Can anyone suggest a better mechanism for making copies of SD cards reliably, ones that understand FAT32?

@Robert/@elatllat: did you manage to copy between SD cards after resizing (if that is what you were trying to do)?

@RobHenry: if dd on Ubuntu is now my problem I think I have a USB card reader somewhere so I might give that a go and do all this on the Pi.

Rob
Rob Meades
http://www.meades.org

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Fri Nov 08, 2013 12:11 am

OK, I was being stupid - I was only dd ing partition 2 not the whole of the SD card, no wonder it wouldn't boot in the Pi, trying again...
Rob Meades
http://www.meades.org

User avatar
Rob Meades
Posts: 17
Joined: Tue Nov 05, 2013 9:12 pm
Location: Saffron Walden, UK
Contact: Website

Re: Reducing SD card partition size on a live Pi

Sat Nov 09, 2013 12:08 am

All working now, so, for the record, here is what I did. It is based on the article here:

http://www.instructables.com/id/Raspber ... /?ALLSTEPS

…but I can’t find a way of replying with a formatted post on that site hence I’m posting it here.

You may have got here, like me, because you wanted to shrink the used space on your Pi SD card. This can be useful if you want to copy one Pi SD card to another, either for backup or for using in another Pi. SD cards vary slightly in size (the sizes given are approximate as SD cards have bad sectors like any other disk). Since the Linux file system expansion one does at the end of the Pi setup process uses every physical sector available on the SD card you happen to be using at the time, copying this to another SD card that happens to be slightly smaller later will not work. Of course, resizing a file system that is in use is also a very dangerous thing to do as you cannot know where the OS has stored files in the space you are about to reduce and I don't know whether resize2fs stops a resize that would corrupt existing files. However, as I had a lot of free space on my SD card, having only recently expanded the Pi file system, I was happy to take that chance.

The process is similar to the one at the link above but with two important differences:
  • You need to do the Linux file system change with resize2fs first, then do the fdisk FAT32 file system reduction. If you don't do it in this order the file system will appear corrupted to Linux.
  • You cannot run resize2fs on a mounted drive, which the SD card in your Pi will be. Hence you need to do that step on another Linux machine that has an SD card slot. If you don't happen to have one around, you can download versions of Ubuntu and other Linux distributions which boot very happily from a CD/DVD or USB stick without installing anything on your PC or otherwise affecting it in any way whatsoever. This desktop machine can then be used to do the resize2fs step. There's another advantage to this, which is that Win32DiskImager, the Windows utility commonly used to copy Pi SD cards, always copies the entire SD card, irrespective of the size you have formatted it to. Hence a copy to a slightly smaller SD card will still fail. If you have a Linux desktop available you can use the finer control of dd to resolve this.
Always make a backup copy of your SD card first, e.g. by using Win32DiskImager to create yourself an .img which you can always write back to that SD card.

I should point out that I am no Linux expert, I am simply describing here a procedure that worked for me. It may not be at all optimal but it did the job. Do keep that backup. :-)

So, in outline, the procedure I used to reduce the image size of a Pi SD card is as follows:

First, you need to determine the maximum size of file system that makes sense for your choice of SD card capacity. With the SD card in your Pi, type:

df -h

Determine what device the SD card is as described in the link above, in my case /dev/mmcblk0.

Now type:

sudo fdisk -u /dev/mmcblk0

At the prompt press p followed by <enter>.

This will display the starting and ending sectors of the partitions. Note the start and end sectors of partition 2 (mmcblk0p2 in my case). Decide how large a last sector you can bear. I decided on 15499999 (as opposed to 15733211) for my 8 gigabyte SD card. This means that the maximum size of partition 2 is now 15499999 - <start sector>. In my case this was 15499999 - 122880 = 15377119.

Shutdown your Pi and remove the SD card. Put it into your Linux desktop (or Windows desktop booted with an Ubuntu live CD/DVD/USB).

There, determine the device name of the SD card. I did this by typing df -h and then looking for the one device of the right size. An 8 gigabyte Pi SD card has a 56 Mbyte boot partition and a 7.3 gigabyte primary partiion, so it might appear as:

/dev/sde2 7.3G 1.6G 5.4G 24% /media/ubuntu/62ba9ec9-47d9-4421-aaee-71dd6c0f3707
/dev/sde1 56M 19M 38M 33% /media/ubuntu/C522-EA52


So the device name in this case is /dev/sde.

Type:

sudo -i
umount /dev/sde1
umount /dev/sde2
resize2fs -f /dev/sde1 15377119s


...replacing 15377119 with the number you determined for yourself above. Note the s on the end, making the units sectors.

Let this complete. I did the next few steps with the SD card in the Pi as I wanted to be sure everything was still working there but I imagine that you could just as easily do them on the Linux desktop.

Determine the device name of the SD card once more (in my case mmcblk0). Now resize the FAT32 file system with fdisk as described in the link at the top of this post. I typed:

sudo -i
fdisk -u /dev/mmcblk0


...then at the command prompt:

p <enter>
d <enter>
2 <enter>
n <enter>
p <enter>
2 <enter>

Here set the <start sector> to be the same as the partition 2 you just deleted, in my case 122880, and the <last sector> to the value you chose above, in my case 15499999.

Type w and press <enter>, then reboot the Pi and all should be good. If not, write your backed-up image back to the SD card, work out what you did wrong and try again.

This leaves you with the final problem of making a copy of this reduced size SD card. For this I took inspiration from:

http://www.linuxweblog.com/dd-image

...though note that I found that gzipping the image as part of the dd operation didn't work for me (I ended up with an image about half the size it should be inside the .gz file). Since I had enough disk space to do the copy without zipping this wasn't an issue.

Put the SD card from the Pi back into the Linux desktop PC and used dd. After determining the device name for your SD card, the following dd command will take a copy of the bits we now care about:

sudo dd if=/dev/sde conv=sync,noerror bs=512 count=15500000 of=mmcblk0.img

This copies all of /dev/sde, partitions 1 and 2, if a bad block is read then something will be written to the image (keeping everything in sync), uses a block size of 512 bytes (probably overkill as this will take a long time but I wanted to be careful), only copies the FAT32 sectors we care about according to the calculations above from the SD card and creates an image file called mmcblk0.img. When the copy is complete it should report something like:

15500000+0 records in
15500000+0 records out
7936000000 bytes (7.9 GB) copied, 1131.21 s, 7.0 MB/s


Make sure the drive where you are writing the image file has enough room for an 8 gigabyte file.

You can then insert your other, potentially very slightly smaller, SD card into the same desktop and, again, determine the device name it has been given. Assuming it is /dev/sde, writing the file back out to this SD card runs as follows:

sudo -i
umount /dev/sde1
umount /dev/sde2
dd if=mmcblk0.img of=/dev/sde conv=sync,noerror bs=512


Again, the copy will take a long time. It should result in a report something like the following:

15500000+0 records in
15500000+0 records out
7936000000 bytes (7.9 GB) copied, 3886.65 s, 2.0 MB/s


If you prefer, the mmcblk0.img file can, in fact, be loaded into Win32DiskImager and written to the SD card from there. This will be quite a lot faster, useful if you've booted your desktop into a Linux machine from a USB stick and can simply remove the USB stick and reboot to return to Windows.

Insert the SD card into your Pi and check that it boots and operates correctly.

Alternative Method using external USB SD card reader with Pi

It was pointed out to me that this could be done on the Pi, no desktop required, if you happen to have a USB SD card reader. I have since done this and, once you have performed the resize2fs and fdisk steps, the copy command reduces to something like:

sudo dd if=/dev/mmcblk0 conv=sync,noerror bs=512 count=15500000 of=/dev/sdc

…reporting:

15500000+0 records in
15500000+0 records out
7936000000 bytes (7.9 GB) copied, 1892.41 s, 4.2 MB/s


As you can see, for me the copy was considerably faster this way, but of course you don't end up with a useful (if large) copy of the image on your desktop afterwards.
Rob Meades
http://www.meades.org

Return to “Troubleshooting”

Who is online

Users browsing this forum: No registered users and 60 guests