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, and make it executable.


if [ x$USER = xroot ]
	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
	dd if=/dev/zero of=image/ bs=1M
	rm image/
	umount image
	mkdir -p image
	sudo -E $0 "$@"

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 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.