Monday, 5 October 2015

Squeezing Raspbian Jessie on to a 4GB SD card

The promise of sudo-free access to GPIO was just too tempting so, despite the fact that I'd only created the whole club set of fresh SD cards before the start of term, I decided to re-create them with the fab new Jessie image.

The challenge: We have a bunch of original model Bs which are still perfectly good for a lot of the circuit based activities but they all have 4GB SD cards. I could just buy a bunch of 8GB ones, but that seemed like a waste.

But the new Jessie image clocks in at over 4.3GB. Here's how I made it fit. It might look a little daunting but its actually quite straight forward. the only tricky bit I found was keeping track of the cards at the various steps. There is probably an easier way of doing it, but this worked for me and only took an hour (with most of that time taken by waiting for the cards/partitions to copy).

What you'll need:
  • A 4GB SD card (call it card F) with a working (but un-needed) Wheezy installation (card M) , a blank 8GB SD card and one at least 16GB card with a working Raspbian installation (card S) or another Linux/Mac machine with enough free space to store GBs of images.
  • An SD card reader



1. Download the Jessie image. In some circumstances, because the unzipped file is >4GB, you may encounter difficulties extracting the compressed image. On a Mac, you'll need to use a tool like the Unarchiver. Store this .img on your Pi booted with the 16GB card (S) or your Mac/Linux box.

2. Insert card M into the reader and write the jessie image to it. Then remove card M from the reader.

3. Now take your 4GB card (F) and put that in your card reader (still attached to the Pi booted with card S or using your Linux/Mac) and display the current partition information of the 4GB card (F) and note it down. 

sudo fdisk -l /dev/sda

Disk /dev/sda: 8068 MB, 8068792320 bytes
249 heads, 62 sectors/track, 1020 cylinders, total 15759360 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: 0xba2edfb9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sda2          122880     7710719     3793920   83  Linux

4. Now shutdown and boot with card M. Personalise as needed (e.g whether to boot to desktop, swap default background etc).

5. Now for the slimming down. Remove Wolfram/mathematica

sudo apt-get purge wolfram-engine

6. Remove LibreOffice

sudo apt-get purge libreoffice*
sudo apt-get purge libreoffice-base
sudo apt-get purge libreoffice-impress
sudo apt-get purge libreoffice-writer
sudo apt-get purge libreoffice-calc
sudo apt-get purge libreoffice-draw
sudo apt-get purge libreoffice-math
sudo apt-get clean
sudo apt-get autoremove

7.  Remove any empty icons from the top menu bar and the main drop-down menu. Reboot

8.  You should now find that your build uses less than 4GB. 

df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.4G  2.4G  763M  77% /
devtmpfs        182M     0  182M   0% /dev
tmpfs           186M     0  186M   0% /dev/shm
tmpfs           186M  4.5M  182M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           186M     0  186M   0% /sys/fs/cgroup
/dev/mmcblk0p1   56M   20M   37M  36% /boot
tmpfs            38M     0   38M   0% /run/user/109
tmpfs            38M     0   38M   0% /run/user/1000

Also, have a look at the partition table with 

sudo fdisk -l /dev/mmcblk0

Disk /dev/mmcblk0: 7.4 GiB, 7892631552 bytes, 15415296 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
Disklabel type: dos
Disk identifier: 0xba2edfb9

Device         Boot  Start     End Sectors Size Id Type
/dev/mmcblk0p1        8192  122879  114688  56M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      122880 8447999 8325120   4G 83 Linux

You can see that the second partition is still too big to fit on the 4GB card, even though not all the space is being used. 

9. Now Boot SD card S in your Pi and insert card M into the SD card reader (or use another Linux/Mac for this step).  These instructions assume that card M is now available as /dev/sda
Now we can shrink the 2nd disk partition:

sudo e2fsck -f /dev/sda2
sudo resize2fs /dev/sda2 3500M

10. Now we need to modify the partition table of card M to match.   Make sure you run fdisk against the correct disk! We need to edit the second partition, so run fdisk in interactive mode.

sudo fdisk /dev/sda

We can't simply resize it, we have to delete it and recreate it with a smaller size:

Press d to delete a partition, then 2 to select sda2.
Now add a new partition by pressing n, followed by p, then 2 (this creates a new, primary position, number 2)
It will ask you what sector you want this new partition to start at. Use the same starting value as we saw in step 2 (in this case, 122880), and give the end sector to match a normal 4GB layout - 7710719

Now press p to display the new layout and check that it matches the desired situation. When you're happy, press w to write the changes to the card.

11. Reboot with card M and check everything still works.

12. Now boot back into your big SD card (or other Linux/Mac box). Take a copy of the two individual partitions on SD card M using dd or dcfldd

dcfldd if=/dev/sda1 of=image4GB-p1.img
dcfldd if=/dev/sda2 of=image4GB-p2.img

13. Now swap the card in the reader (M) for card F (the 4GB one). Write these copied partitions  to your 4GB card.

dcfldd  if=image4GB-p1.img of=/dev/sda1
dcfldd  if=image4GB-p2.img of=/dev/sda2

14. Now boot from this card (M). It should be identical to the 8GB image.  If you want to make more 4GB installations of Jessie, just copy this entire card (no need to bother with individual partitions now we've got everything squeezed in).

9 comments:

  1. Hi great work!
    How much free space is left?
    Can you suggest anything else to delete with it relevant command?
    Many SD cards are less than the head line 2 or 4GB will your system cope with say 3.9GB.?

    ReplyDelete
  2. gr8! u could put swhere that 4gb image of yours for dl and testing purposes, not only 4 people who don't have atm 8gb card to spare :) greetz!

    ReplyDelete
  3. Super helpful! Thanks for the write up.

    ReplyDelete

  4. It's great to understand all the steps needed, thank you for the good work. But I have another way to shrink the SDs, once deleted the unneeded packages.

    All you have to do is to create an image from the SD, with the Windows program "Disk Imager". It will be 8 GB --or the size of the SD you are using. Then, go with that image to Linux and shrink it with the perl program "resizeimage.pl" this way:

    sudo perl resizeimage.pl /media/USB-NAME/image-unshrinked.img

    Then you'll have an image of 3,6 GB more or less. Burn it with the "Disk Imager" program on Windows on a new 4 GB SD and then expand its size to fullfill the new SD with the "sudo raspi-config" command on the Raspberry running this new Raspbian Jessie SD.

    PS: if you are interested in this method and you can't find the "resizeimage.pl" program from its author on the Internet, let me know and I will give you a link to it.

    ReplyDelete
    Replies
    1. i am interested in using your method, may i know can you share the program: "resizeimage.pl" with me?

      Delete
  5. Thanks Richard

    I also found that if you don't have a 4Gb card with Wheezy installed, you can dd the default Jessie image onto it and let it fail (as it will run out of room) - you then have a partitioned disk with the Jessie boot partition installed, and an incomplete second partition, but you can just use dd to copy the second partition from card M over the top of that incomplete partition and then re-edit the partition table on card F to match (of course, the start point of that partition is a little different to the Wheezy image, so you need to note that from step 2, but it works just fine)

    ReplyDelete
  6. Soon does anybody mind if I post my 4 gig card image ? :B

    ReplyDelete
  7. Hello everyone am ASHA from USA am using this medium to announce to the world and those in need of financial stability to look no further but contact davidbenjaminltd@outlook.com who helped me with the card in less than 1 week after being scammed of my money by others and yet no card. Are you in need of a loan? Or some sort of financial distress and need saving, SEIZE this opportunity and get rich in no time. There are no hidden charges and they deal on transparency basis. I am forever grateful to DAVID BENJAMIN for changing my life a whole new after being scammed by 2 companies who took advantage of me Contact via email on
    davidbenjaminltd@outlook.com

    ReplyDelete
  8. anyone can help me? i do until step 9:
    sudo e2fsck -f /dev/sda2

    but the system return this,
    ext2fs_check_if_mount: Can't check if filesystem is mounted due to missing mtab file while determining whether /dev/sda2 is mounted.
    e2fsck: Device or resource busy while trying to open /dev/sda2
    Filesystem mounted or opened exclusively by another program?

    ReplyDelete