Skip to content

Creating RasPi SD images

myeisha edited this page Jan 28, 2014 · 1 revision

The process to create a clean image file from some SD card you have at hand (possibly an old image you booted, updated some packages, and then shut down again) is rather simple. You will need a small script, a directory to work in, and a little bit of time.

First, create a directory somewhere. All further work will happen inside of this directory. Place the following script inside of this directory, name it clean-image.sh, and make it executable.

#!/bin/bash

if [ x$USER = xroot ]
then
	mount -oloop,offset=$((122880*512)) ${1:-raspi-image.img} image
	rm -r image/etc/hexabus_msg_bridge.conf image/etc/radvd.conf image/var/log/* image/home/*/.bash_history image/root/.bash_history
	rm image/home/hexanode/*json
	rm -rf image/home/hexabus/*
	sed -e '/#EOF/,$ d' < image/etc/network/interfaces | sponge image/etc/network/interfaces
	cat > image/etc/resolv.conf <<EOF
nameserver 8.8.8.8
EOF
	dd if=/dev/zero of=image/..zero bs=1M
	rm image/..zero
	umount image
else
	mkdir -p image
	sudo -E $0 "$@"
fi

This script cleans the image of residual files that might be harmful, like contents of default user home directories, log files, and configurations for various daemons running on the system that should not exist on first boot. It also clears free space of the filesystem to make compression more efficient. What it does not do, however, is clean the apt-cache, you will have to do this by hand if you want it cleaned.

Second, copy your SD card into the directory with dd, i.e. using dd if=/dev/sdc of=raspi-image.img bs=1M, if /dev/sdc is the device node for your SD card.

Third, run clean-image.sh. If you have not named your image file raspi-image.img, you will have to give the actual name of the file to the script as an argument.

The image is now ready. For distribution, you will probably want to compress it with xz or similar tools.

Clone this wiki locally