NOTE - Official Raspbian Lite Distribution Now Available!: As of November 2015, the official Raspbian image is now maintained in a full GUI and 'lite' GUI-less flavor. This project has now been deprecated as of the 2.0.0 ('Jessie') release, and you should switch your servers over to the official Lite image instead.
Trim the fat from the default Raspbian image.
Many minimal Raspbian images are based on very old versions of Raspbian and aren't built in an open/simple way.
Diet Raspbian uses Ansible to take a system built with the official Raspbian image, and strip it of extraneous bits like default IDEs, languages, Wolfram, a window manager, etc. Why? If you're running a Raspberry Pi as a small headless server (e.g. for home automation, a fun robot project, or in a clustered configuration), there's no need for all the extra cruft.
You can skip all of these directions and download a pre-generated Diet Raspbian image directly from the Midwestern Mac Files site (under the 'Raspberry Pi Images' section).
Everything should be done on your local host machine—nothing needs to be done on the Raspberry Pi itself!
- Install Ansible.
- Build a microSD card with the official Raspbian image, and boot your Pi.
- Copy your public key for passwordless SSH login (e.g.
ssh-copy-id pi@[IP-ADDRESS]
), and make sure you can login to the Pi without a password (e.g.ssh pi@[IP-ADDRESS]
).- If you want, you can also SSH into the Pi and run
passwd
to change thepi
account password from the default,raspberry
. - Don't run
raspi-config
at this time. - If you boot the Pi the first time while connected to a monitor, follow the instructions under 'Initial setup via GUI/X' before completing this step.
- If you want, you can also SSH into the Pi and run
- Edit the
inventory
file and set the IP address to the address of your running Pi. - Run the following command:
$ ansible-playbook -i inventory diet.yml
.
After 10-20 minutes, the space consumed by Raspbian should go from ~2.5 GB to ~700 MB (or lower, depending on how far along this project has come!). If you'd like to create a new image for cloning purposes, run the command ansible all -i inventory -a "shutdown -h now" -s
to shut down your Pi, then follow the steps under 'Creating a new Diet Raspbian disk image'.
IMPORTANT: The
diet.yml
playbook is meant to be run prior to any other Raspberry Pi configuration; it changes locale settings, general configuration, etc. (seevars/main.yml
). This is meant to be run on a freshly-imaged Raspbian microSD/SD card.
If you want to do the first couple setup steps using the GUI instead of just connecting to the Pi via SSH headlessly, you can do so using the steps below; then go to step 3 in the above directions from your local host machine.
- Boot the Pi with the fresh Rasbpian install; the Pi will boot straight into X (the GUI).
- If you have WiFi, connect to the WiFi network. If you have Ethernet, connect the network cable to your Pi.
- Open a Terminal window on the Pi and type
ifconfig
, to get your Pi's IP address. - Open Menu > Preferences > Raspberry Pi Configuration, and change the 'Boot' option to 'To CLI' and uncheck "Login as user 'pi'".
- Click OK and reboot the Pi.
Once you've run the diet.yml
playbook on your Pi, you can create a new diet-raspbian.img.gz
compressed disk image that you can use to clone (or re-clone) to your microSD cards, so you don't have to run the diet.yml
playbook in the future, or if you want to quickly rebuild your existing Pi's OS.
- With your Raspberry Pi powered off, remove the microSD card and put it into your Mac's card reader.
- Resize the ext4 partition (OPTIONAL, but will save a couple GB of space and ~10 minutes per cloned microSD card):
1. Boot up an Ubuntu VM using VirtualBox, VMWare Fusion, or Parallels Desktop.
2. Attach the USB device that has the microSD card attached to the VM.
3. Make sure gparted is installed:
$ sudo apt-get install -y gparted
4. Start the gparted GUI:$ sudo gparted
5. Select the microSD card (e.g./dev/sdb
) from 'Devices' in the GParted menu. 6. Right click on theext4
(should be ~4 GB) andboot
volumes and unmount them. 7. Right click on the theext4
volume and resize it to a smaller value (e.g.1024 MB
). 8. Click the 'Apply' button (green checkbox) to apply the changes (this will take ~10 minutes). 9. Eject the card from the Ubuntu VM so you can use it from the Mac again. - Locate the card:
$ diskutil list
(should be something like/dev/disk2
) - Make a compressed image of the card using
dd
: 1. Withpv
:$ sudo dd if=/dev/disk2 bs=1m count=1536 | pv | gzip > ~/Desktop/diet-raspbian.img.gz
2. Withoutpv
:$ sudo dd if=/dev/disk2 bs=1m count=1536 | gzip > ~/Desktop/diet-raspbian.img.gz
WARNING: Double-check that you're using the right
if
disk andof
orgzip
destinations; these values will be different on your system.
The
count=1536
above will create an image that is 1.5 GB. If it needs to be larger to contain all the partitions on the microSD card, you'll need to increase the size here.
At this point, you should have a disk image you can write to new SD cards, or use to overwrite your existing SD card.
- Put the new card into your Mac's card reader.
- Locate the card:
$ diskutil list
(should be something like/dev/disk2
) - Unmount any mounted partitions on the card:
$ diskutil unmountDisk /dev/disk2
- Write the image to the new card:
1. With
pv
:$ gzip -dc ~/Desktop/diet-raspbian-2.0.0.img.gz | pv | sudo dd of=/dev/disk2 bs=1m
2. Withoutpv
:$ gzip -dc ~/Desktop/diet-raspbian-2.0.0.img.gz | sudo dd of=/dev/disk2 bs=1m
WARNING: Double-check that you're using the right
if
disk andof
orgzip
destinations; these values will be different on your system.
- Put the freshly-minted card into your Pi and boot the Pi.
- Log in (either locally or via SSH).
- Disable swap and remove the swap file temporarily:
sudo swapoff -a && sudo rm -f /var/swap
- Run
$ sudo raspi-config
, and select the first option ('Expand Filesystem'). - Reboot the Raspberry Pi.
Created in 2015 by Jeff Geerling, author of Ansible for DevOps.