Inspiration for this tutorial comes from Richard Hayler. Without it, I would have had no clue what to do. Thank you Richard!
http://richardhayler.blogspot.ca/2015/10/squeezing-raspbian-jessie-on-to-4gb-sd.html
You will need to start with three cards:
- the target 4Gig,
- the origin 8Gig card
- and an intermediate 16Gig card.
Start by burning Jessie Lite onto the 4Gig card, and regular Jessie onto both the 8gig and the 16Gig card. Make sure you choose both versions from the same date.
1. First boot
Boot the 4 Gig card
sudo raspi-config
expand filesystem
Get our target point:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 3.6G 816M 2.6G 24% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 6.2M 457M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 463M 0 463M 0% /sys/fs/cgroup
/dev/mmcblk0p1 60M 20M 41M 34% /boot
sudo fdisk -l /dev/mmcblk0
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/mmcblk0p2 131072 7774207 7643136 3.7G 83 Linux
2. Diet regimen
Boot the pi up with the 8 gig card.
This is our starting point:
pi@raspberrypi:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 3.6G 3.3G 115M 97% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 6.3M 457M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 463M 0 463M 0% /sys/fs/cgroup
/dev/mmcblk0p1 60M 20M 41M 34% /boot
tmpfs 93M 0 93M 0% /run/user/1000
and
sudo fdisk -l /dev/mmcblk0
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/mmcblk0p2 131072 7870463 7739392 3.7G 83 Linux
It’s time to remove some packages so that Jessie can go onto a 4 Gig card. We don’t need to remove that much, really. We’re removing Wolfram and LibreOffice, because they give us the biggest bang for the buck, and we know they’re not useful to us.
sudo apt-get purge wolfram-engine sudo apt-get purge libreoffice* sudo apt-get clean sudo apt-get autoremove
Now df -h will give the following: (the value in blue should have changed)
pi@raspberrypi:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 3.6G 2.4G 1010M 71% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 6.3M 457M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 463M 0 463M 0% /sys/fs/cgroup
/dev/mmcblk0p1 60M 20M 41M 34% /boot
tmpfs 93M 0 93M 0% /run/user/1000
and no change for sudo fdisk -l /dev/mmcblk0
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/mmcblk0p2 131072 7870463 7739392 3.7G 83 Linux
3. Start playing with partitions!
Boot up the pi with the 16 Gig card
sudo raspi-config
expand file system (this one is really needed, we need access to the full 16 gig)
Put the 8Gig in the card reader
sudo fdisk -l /dev/sda:
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/sda2 131072 7870463 7739392 3.7G 83 Linux
We can see that partition sda1 is the same for both, and that’s great. We’re happy with that. We now need to make the 8Gig sda2 fit onto the 4Gig sda2
sudo umount /dev/sda2
sudo e2fsck -f /dev/sda2
sudo resize2fs /dev/sda2 3500M
Now for the nerve wracking moment:
sudo fdisk /dev/sda
Enter d to delete a partition, the partition number is 2
Enter n to create a new partition, make it a primary partition (choose p), again the partition number is 2
The start sector is 131072, which is the same for both the 4gig and 8gig cards, the last sector will be 2658303, which is the end sector for the 4 Gig partition 2
Double check the work, by pressing p:
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/sda2 131072 2658303 2527232 1.2G 83 Linux
This should match what we had for the 4 Gig drive, even though we’re working on the 8 gig drive.
Press w to write all this to the 8 gig card.
The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
double check the integrity of the card by entering:
sudo e2fsck -f /dev/sda2
4. Check the madness
Boot up the Pi with the 8 gig card, just in case something went wrong. Verify that you still have a working Jessie setup.
5. Make an image of the slimmed down Jessie
First let’s get a new tool:
sudo apt-get install dcfldd
And let’s use it! This will create an image of our second partition, and save it on the 16 Gig card. That’s why we needed a big card
sudo dcfldd if=/dev/sda2 of=litejessie.img 119296 blocks (3728Mb) written. 119424+0 records in 119424+0 records out
Now swap out the 8 Gig card from the card reader and put in the 4 Gig card. Let’s do the opposite work, put the new image from the 16 Gig card onto the 4 Gig card. (the numbers should match!)
sudo dcfldd of=/dev/sda2 if=litejessie.img 119296 blocks (3728Mb) written. 119424+0 records in 119424+0 records out