Raspbian on 4GB card

Posted at 2015-11-04.

I have an old raspi I'd like to boot up once again and get testing. Most suitable card I have laying around unused was a 4 GB microsd card. I have piles of adapters to fit that to an SD slot.

I decided to grab some notes. Make of them what you will.

Get the image

From Raspbian, I downloaded the zipped (well, it does save space and even Windos should cope with it) Raspbian jessie image (2015-09-24-raspbian-jessie.img). Problem is, it is too big to fit on my card!

(Journal:) sd 11:0:0:3: [sdg] 7774208 512-byte logical blocks: (3.98 GB/3.70 GiB)
$ du -k 2015-09-24-raspbian-jessie.img
4219932 2015-09-24-raspbian-jessie.img
$ dc
7774208
2
/
p
3887104

(Yes, there's going to be dc sessions in the output. Enjoy.)

Make room! Make room!

I already went and poked around and blew out a few pieces of apparent bloatware. Would have been worth logging, and would have been interesting if I could have started a qemu in there and cleaned the place with apt instead. Maybe another time.

But then it went something like this (sans typos and assorted bits): Start with the magic bit that guides rarely show you. Using losetup and forcing partition table scan, you can easily get direct access to partitions inside disk images.

$ sudo losetup -f 2015-09-24-raspbian-jessie.img -P
$ sudo mount /dev/loop0p2 /mnt/test/
$ cd /mnt/test
$ du -ck . | sort -n | tail -n 100
... (hunt for waste of space)

Having blown out a few biggest dirs from /opt, well under 3G remained. I can probably get along well without the data for those. They will be cleaned up as soon as I get the system going.

$ df -h /mnt/test
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0p2    3,9G  2,4G  1,3G  66% /mnt/test
$ sudo umount /mnt/test

Resize the fs to minimum.

$ sudo e2fsck -f /dev/loop0p2 
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop0p2: 110133/260608 files (0.2% non-contiguous), 654360/1040640 blocks
$ sudo resize2fs -M /dev/loop0p2 
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop0p2 to 719517 (4k) blocks.
The filesystem on /dev/loop0p2 is now 719517 (4k) blocks long.

Now, shrink the partition. I just went with 3G. Funnily, the first partition starts at 8192, and fdisk wants to make a new one below that. I just gave the start sector from the old partition instead.

$ sudo fdisk /dev/loop0

Welcome to fdisk (util-linux 2.27).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/loop0: 4 GiB, 4325376000 bytes, 8448000 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/loop0p1        8192  122879  114688  56M  c W95 FAT32 (LBA)
/dev/loop0p2      122880 8447999 8325120   4G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-8447999, default 2048): 122880
Last sector, +sectors or +size{K,M,G,T,P} (122880-8447999, default 8447999): +3G

Created a new partition 2 of type 'Linux' and of size 3 GiB.

Command (m for help): p
Disk /dev/loop0: 4 GiB, 4325376000 bytes, 8448000 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/loop0p1        8192  122879  114688  56M  c W95 FAT32 (LBA)
/dev/loop0p2      122880 6414335 6291456   3G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Ok, blow the fs back up to whole partition so there's free space.

$ sudo resize2fs /dev/loop0p2 
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop0p2 to 786432 (4k) blocks.
The filesystem on /dev/loop0p2 is now 786432 (4k) blocks long.

$ sudo mount /dev/loop0p2 /mnt/test/
$ df -hT /mnt/test
Filesystem                  Type      Size  Used Avail Use% Mounted on
/dev/loop0p2                ext4      2,9G  2,4G  365M  87% /mnt/test
$ sudo umount /mnt/test 
$ sudo losetup -d /dev/loop0 
$ sync

The image file is as big as ever, of course. Only the tail end should be blank. I considered truncating it, but I thought I'd try something less destructive and just leave the write a bit short.

$ /sbin/fdisk -l 20*img
Disk 2015-09-24-raspbian-jessie.img: 4 GiB, 4325376000 bytes, 8448000 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
2015-09-24-raspbian-jessie.img1        8192  122879  114688  56M  c W95 FAT32 (LBA)
2015-09-24-raspbian-jessie.img2      122880 6414335 6291456   3G 83 Linux

Figure out the size I can fit in, the size I need to fit in, and a nice round number in between that I'll write. Supposedly dd likes 4M chunks, so I'll want a count of those.

blades@bastet:~/Downloads$ dc
3284139520
1024
/
1024
/
p
3131
4
/
p
782
790
4
1024
1024
*
*
*
p
3313500160
7774208
512
*
p
3980394496

So 790 should be more than enough blocks to cover all partitions without hitting the card edge. Make it so.

$ sudo dd bs=4M if=2015-09-24-raspbian-jessie.img of=/dev/sdg count=790
790+0 records in
790+0 records out
3313500160 bytes (3,3 GB) copied, 958,967 s, 3,5 MB/s
$ sync
$ eject sdg

Test the card

$ ping raspberrypi.local
...
^C
--- raspberrypi.local ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.419/0.638/0.857/0.219 ms
$ nmap raspberrypi.local

Starting Nmap 6.47 ( http://nmap.org ) at 2015-11-04 23:04 EET
Nmap scan report for raspberrypi.local (...)
Host is up (0.017s latency).
rDNS record for <...>
Not shown: 999 closed ports
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.29 seconds
$ ssh pi@raspberrypi.local
...
Last login: Wed Nov  4 21:02:07 2015
pi@raspberrypi ~ $ w
 21:04:53 up 3 min,  3 users,  load average: 1.53, 1.38, 0.60
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
pi       tty1                      21:02   41days  1.36s  1.22s -bash
pi       :0       :0               21:02   ?xdm?  37.35s  0.82s /usr/bin/lxsession -s LXDE-pi -e LXDE
pi       pts/0    bastet.blades.cx 21:04    3.00s  1.08s  0.05s w
pi@raspberrypi ~ $ sudo apt-get update
...
pi@raspberrypi ~ $ sudo apt-get install --no-install-recommends debfoster deborphan debian-goodies
...

Better get to work. :)

After cleanup

$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/root      ext4      2,9G  1,3G  1,5G  47% /
...

That might squeeze onto a 2GB card. I removed most graphical things but left a lot of pi-specific and pythonic stuff in there. Now, back up and start trying to make it do something.

Links