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).