How to make Raspbian Jessie Lite even smaller (lighter)

Set the keyboard language

# I’m using a US English wireless keyboard, so to switch from the default UK British English keyboard to US English keyboard perform the following steps: pi@raspberrypi:~ $ sudo dpkg-reconfigure keyboard-configuration

  • Select “Generic 105-key (Intl) PC”
  • then “Other”
  • then “English (US)”
  • then “English (US)”
  • then “Right Alt (AltGr)”
  • then “No compose key”.

# Reboot so that these changes are used. pi@raspberrypi:~ $ sudo reboot

WIFI

# If you do not have ethernet access, configure your WIFI network.

Update Raspbian software

(As of 7th Feb 2016, nothing new is installed, only updates)

pi@raspberrypi:~ $ sudo apt-get update

pi@raspberrypi:~ $ sudo apt-get upgrade

pi@raspberrypi:~ $ sudo apt-get autoremove –purge

Files System Compression (optional)

Install BTRFS

pi@raspberrypi:~ $ sudo apt-get install btrfs-tools

Add the line “btrfs” to the end of the “/etc/initramfs-tools/modules” file to make sure that the BTRFS filesystem module is loaded by the kernel at boot time so that the root files system can be BTRFS format.
pi@raspberrypi:~ $ sudo nano /etc/initramfs-tools/modules

Reboot to make sure everything still works:
pi@raspberrypi:~ $ sudo reboot

Update the PI’s firmware (On console)

pi@raspberrypi:~ $ sudo apt-get install rpi-update 
pi@raspberrypi:~ $ sudo rpi-update 
pi@raspberrypi:~ $ sudo reboot

pi@raspberrypi:~ $ sudo update-initramfs -c -k `uname -r`
You should see a line like:
PI ZERO: update-initramfs: Genrating /boot/initrd.img-4.1.18+
RPI2: update-initramfs: Genrating /boot/initrd.img-4.1.18-v7+
Make a note of the filename as you’ll need this in a minute.

Edit “/boot/config.txt” so that it uses the new image.
pi@raspberrypi:~ $ sudo nano /boot/config.txt
Add the following line to the bottom of the configuration file:
initramfs initrd.img-4.1.18-v7+
where the filename is the one you just noted.

Reboot to make sure everything still works:
pi@raspberrypi:~ $ sudo reboot

See notes later on about installing updates and update-initramfs issues.

Create a new partition:

pi@raspberrypi:~ $ sudo fdisk -l /dev/mmcblk0

This should list all the partitions on the SD card. Note the “End” sector for the second (last) parition /dev/mmcblk0p2. It should be the second number from the left on the line. My end sector was 2848767.

We need to create a new partition starting at the next available block.
2848767 + 1 = 2848768

Lets create the new partition:
pi@raspberrypi:~ $ sudo fdisk /dev/mmcblk0
Type “n” for a new partition.
Type “p” for a primary partition.
Go with the default “3” for partition 3.
Type “2848768” for the First sector

Type “+2G” to create a 2GB partition.
If you now type “p” you should see the partitions displayed including the new partition 3.  This partition should already be of the correct type “Linux”
Type “w” to write/save the new partition table.

You may get a warning that you need to reboot before the new partition table is used.
pi@raspberrypi:~ $ sudo reboot

Format the new partition as BTRFS.
pi@raspberrypi:~ $ sudo mkfs.btrfs -f /dev/mmcblk0p3

Make a new directory and mount the new partition:
pi@raspberrypi:~ $ sudo mkdir /zzz

Edit /etc/fstab so that the 3rd partition is automatically mounted as /part3 in compressed mode:
pi@raspberrypi:~ $ sudo nano /etc/fstab

and add the following line to /etc/fstab:

/dev/mmcblk0p3    /zzz    btrfs    compress,noatime    0    1

Reboot to mount the 3rd partition automatically in compressed mode.
pi@raspberrypi:~ $ sudo reboot

List all the top level directories:

pi@raspberrypi:~ $ sudo ls -la /

Copy the files over to the new partition (NOTE:I tend to run the command twice for each source directory to check it worked):

pi@raspberrypi:~ $ sudo apt-get install rsync

pi@raspberrypi:~ $ sudo rsync -aAXHv –exclude={“/part3/*”,”/dev/*”,”/proc/*”,”/sys/*”,”/tmp/*”,”/run/*”,”/mnt/*”,”/media/*”,”/lost+found”} / /zzz/

Boot from BTRFS partition

Create a new mount point for partition 2 (the current / partition) which will be used when partition 3 becomes the / partition:

pi@raspberrypi:~ $ sudo mkdir /zzz/zzz2

Edit /etc/fstab on partition3 so that partition 3 becomes “/” and partition 2 becomes “/part2”:

pi@raspberrypi:~ $ pi@raspberrypi:~ $ sudo nano /zzz/etc/fstab

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /zzz2          ext4    defaults,noatime  0       1
/dev/mmcblk0p3  /               btrfs    compress,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

Edit the boot cmdline file so that it uses partition 3 to boot from:

pi@raspberrypi:~ $ sudo nano /boot/cmdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p3 rootfstype=btrfs elevator=deadline fsck.repair=yes rootwait

 

 

pi@raspberrypi:~ $ sudo update-initramfs -c -t -k `uname -r`

Replace the original partition with a 900MB BTRS partition

 

Copy content of / partition to this new BTRFS partition

pi@raspberrypi:~ $ sudo mkdir /part4

Edit /etc/fstab on partition3 so that partition 4 becomes “/part4” and partition 2 isn’t mounted:

pi@raspberrypi:~ $ pi@raspberrypi:~ $ sudo nano /part3/etc/fstab

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
#/dev/mmcblk0p2  /part2          ext4    defaults,noatime  0       1
/dev/mmcblk0p3  /               btrfs    compress,noatime  0       1

/dev/mmcblk0p4  /part4               btrfs    compress,noatime  0       1
# a swapfile is not a swap partition, no line here

#   use  dphys-swapfile swap[on|off]  for that

Reboot to mount partition 4 to /part4 in compressed mode:
pi@raspberrypi:~ $ sudo reboot

rsync the OS files from / to /part4:

pi@raspberrypi:~ $ cd /

pi@raspberrypi:~ $ sudo rsync -aAXHv –exclude={“/part2/*”,/part4/*”,“/dev/*”,”/proc/*”,”/sys/*”,”/tmp/*”,”/run/*”,”/mnt/*”,”/media/*”,”/lost+found”} / /part4/

Edit /etc/fstab on partition 4 so that partition 4 becomes “/” and partition 3 isn’t mounted:

pi@raspberrypi:~ $ pi@raspberrypi:~ $ sudo nano /part4/etc/fstab

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
#/dev/mmcblk0p2  /part2          ext4    defaults,noatime  0       1
#/dev/mmcblk0p3  /               btrfs    compress,noatime  0       1

/dev/mmcblk0p4  /               btrfs    compress,noatime  0       1
# a swapfile is not a swap partition, no line here

#   use  dphys-swapfile swap[on|off]  for that

Edit the boot cmdline file so that it uses partition 3 to boot from:

pi@raspberrypi:~ $ sudo nano /boot/cmdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p4 rootfstype=btrfs elevator=deadline fsck.repair=yes rootwait

Reboot to mount partition 4 to / in compressed mode:

pi@raspberrypi:~ $ sudo reboot

Remove unnecessary partitions and mount points:

pi@raspberrypi:~ $ sudo fdisk -l /dev/mmcblk0

d 3 w

pi@raspberrypi:~ $ sudo rmdir /part2

pi@raspberrypi:~ $ sudo rmdir /part4

Create an image of this SD card (only the partitions used and not non partition space):

 

Write image to a new SD card

Expand / partition and filesystem

Use the “parted” command to increase partition 4’s size until the last but 1 sector (-1s).

pi@raspberrypi:~ $ sudo parted /dev/mmcblk0

resizepart 4 -1s

Run the following command repeatedly, to increase the / partition size by 1GB each time, until it reports “Error: unable to resize ‘/’ – File too large”:

pi@raspberrypi:~ $ sudo btrfs filesystem resize +1024M /

Then run the following command repeatedly, to increase the / partition size by 100MB each time, until it reports “Error: unable to resize ‘/’ – File too large”:

pi@raspberrypi:~ $ sudo btrfs filesystem resize +100M /

Please feel free to do the same for 10M and 1M if you want to use up all the available space.

 

Original Raspbian Jessie Lite starting point (/=912 MB)

Notes:

  • Performed on a Pi Zero, but should work equally well on a Raspberry Pi Model B v2
  • Using an USB 802.11n WIFI dongle (Optional)

Boot a clean install of Raspbian Jessie Lite and login as user “pi”, password “raspberry”
The current default size of Raspbian Jessie Lite:
pi@raspberrypi:~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.3G  912M  281M  77% /
/dev/mmcblk0p1   60M   20M   41M  34% /boot
/boot – is the boot partition (FAT32/vfat format) that the Raspberry PI boots from – 20,328 KB /  – is the root partition (Linux ext4 format) that contains the actual Raspbian operating system – 933,572 KB

Initial clean up, only turning off swap, but leaving Raspbian still intact (/=714 MB)

  • # First clean up the APT cached information ( / = 833,424 KB or 814 MB – Saving 100,148 KB or 98 MB)
  • pi@raspberrypi:~ $ sudo apt-get clean
  • # Increase the life of the SD card by turning off SWAP, removing the package that automatically creates it and turns it back on at boot up, and delete the swap file itself. ( / = 730,884 KB – Saving 102,540 KB or 100 MB )
    • pi@raspberrypi:~ $ sudo swapoff –all
    • pi@raspberrypi:~ $ sudo apt-get remove –purge dphys-swapfile
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
    • pi@raspberrypi:~ $ sudo rm /var/swap
  • # REMOVE UNWANTED PACKAGES
    • # List all installed packages
      • pi@raspberrypi:~ $ dpkg-query -l
  • # Remove development libraries (Saves 86 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge dpkg-dev libc-dev-bin libgcc-4.9-dev libpng12-dev libraspberrypi-dev libstdc++-4.9-dev manpages-dev
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove c / cpp / gcc / g++ / gdb / etc (Saves 42 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge cpp cpp-4.9 gcc-4.6-base gcc-4.7-base gcc-4.8-base gdb
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
    • pi@raspberrypi:~ $ sudo dpkg-reconfigure locales
    • # Deselect “en_GB.UTF-8 UTF-8”, then select “en_US.UTF-8 UTF-8”, then OK
      • # On next screen select “C.UTF-8”, then OK
    • # Reboot so that these changes take hold.
      • pi@raspberrypi:~ $ sudo reboot
    • # Remove Internationalisation locales related packages (Saves 50 MB)
      • pi@raspberrypi:~ $ sudo apt-get remove –purge debconf-i18n  libicu52 locales
      • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
    • # Remove web / network utils (Saves 6 MB)
      • pi@raspberrypi:~ $ sudo apt-get remove –purge curl geoip-database wget dmidecode
      • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove perl (Saves 33 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge perl perl-modules
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove python (Saves 47 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge python
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove compression related packages (Saves 1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge bzip2 unzip xz-utils
  • # Remove BSD related packages (Saves 0.5 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge bsdmainutils
  • # Remove man pages related packages (Saves 41 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge groff-base manpages libraspberrypi-doc
  • # Remove all editors apart from nano (Saves 1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge ed vim-tiny
  • # Remove filesystem related packages (Saves 0.33 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge dosfstools dphys-swapfile hardlink
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove Network File System (NFS) related packages (Saves 2.5 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge nfs-common ucf
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge

# LEAVE FOR NOW!!!!

  • # Remove BASH tab completion related packages (Saves 1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge bash-completion
  • # Remove Networking related packages (Saves 1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge iptables iputils-ping traceroute
  • # Remove Logging related packages (Saves 2 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge liblogging-stdlog0 liblognorm1 rsyslog
  • # PERHAPS REMOVE THE LOGS AND LEAVE THE DIRS INTACT???? (Saves 2.1 MB)
    • pi@raspberrypi:/tmp/mnt/purge $ sudo rm -rf /var/log/*
  • # Remove audio related packages (Saves 6 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge lua5.1 libluajit-5.1-common
    • pi@raspberrypi:~ $ sudo apt-get autoremove –purge
  • # Remove Raspberry Pi related packages (Saves 0.1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge raspi-copies-and-fills
  • # Remove package installer related packages (Saves 14.1 MB)
    • pi@raspberrypi:~ $ sudo apt-get remove –purge aptitude aptitude-common
  • # REMOVE REDUNDANT LIBRARIES
  • # REMOVE UNNECESSARY FIRMWARE
  • # CLEAN UP pi@raspberrypi:~ $ sudo apt-get clean
  • # Even though we have removed packages with the “–purge” option, occasionally packages are removed and still leave configuration files behind.
  • # Search for redundant package configuration (plus example output): pi@raspberrypi:~ $ dpkg –list | grep ‘^rc ‘ rc libtalloc2:armhf 2.1.1-2 armhf hierarchical pool based memory allocator rc libwbclient0:armhf 2:4.1.17+dfsg-2+deb8u1 armhf Samba winbind client library rc samba-common 2:4.1.17+dfsg-2+deb8u1 all common files used by both the Samba server and client
  • # Remove redundant package configuration pi@raspberrypi:~ $ sudo dpkg –list | grep ‘^rc ‘ | awk ‘{ print $2 }’ | sudo xargs dpkg -P
  • # UPDATE PACKAGES BECAUSE THIS MAY BE THE LAST TIME YOU CAN
  • # Update Raspbian software (As of 7th Feb 2016, nothing new is installed, only updates) pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get upgrade
# Display the current storage usage now:
 pi@raspberrypi:~ $ df -h
 Filesystem Size Used Avail Use% Mounted on
 /dev/root 1.3G 936M 257M 79% /
 /dev/mmcblk0p1 60M 20M 41M 34% /boot 

# Finally, clean up APT pi@raspberrypi:~ $ sudo apt-get autoremove –purge pi@raspberrypi:~ $ sudo apt-get clean