Skip to content

Instantly share code, notes, and snippets.

@ellmetha
Last active March 15, 2017 18:44
Show Gist options
  • Save ellmetha/5367985 to your computer and use it in GitHub Desktop.
Save ellmetha/5367985 to your computer and use it in GitHub Desktop.
A script to prepare and install a Raspbian system on a SD Card.
#!/bin/bash
# A script to prepare and install a Raspbian system on a SD Card.
#
# ellmetha - 04/2013
# Distributed under the GPL version 3 license
#
VERSION="1.0"
INIT_DIR=$PWD
# Debootstrap params
#--------------------------------------------------------------------------------
MIRROR="http://archive.raspbian.org/raspbian"
ARCH="armhf"
SUITE="wheezy"
# SD card params
#--------------------------------------------------------------------------------
BOOTP_SIZE="64M"
BUILDENV="/mnt/rootfs--"`date +%Y%m%d`
# System params
#--------------------------------------------------------------------------------
ROOT_PASSWD="rpi"
APT_SOURCES="deb http://archive.raspbian.org/raspbian wheezy main"
CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
HOSTNAME_RPI="rpi"
FSTAB="proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 0"
NETWORKING="auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp"
THIRD_STAGE="#!/bin/bash
apt-get update
# Install packages
DEBIAN_FRONTEND=noninteractive apt-get -y --force-yes install git-core binutils ca-certificates locales ntpdate console-data keyboard-configuration sudo make
# Configure locale
sed -i \"s/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/g\" /etc/locale.gen && locale-gen
# Configure timezone
ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime
# Configure keyboard
sed -i 's/XKBLAYOUT=\"us\"/XKBLAYOUT=\"fr\"/g' /etc/default/keyboard
# Prepare rpi-update
wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update
mkdir -p /lib/modules/3.1.9+
touch /boot/start.elf
chmod +x /usr/bin/rpi-update
rpi-update
# Set root password
echo -e \"${ROOT_PASSWD}\n${ROOT_PASSWD}\" | passwd root
# Final operations
rm -f /etc/udev/rules.d/70-persistent-net.rules
rm -f third-stage
"
# ~STEP1: Verifications & preparation
#--------------------------------------------------------------------------------
# The script must be run as root
if [ $EUID -ne 0 ]; then
echo "This tool must be run as root: # sudo $0" 1>&2
exit 1
fi
# The targetted device must be a block device
target_device=$1
if ! [ -b $target_device ]; then
echo "$target_device is not a block device"
exit 1
elif [ -z $target_device ]; then
echo "A block device must be specified"
exit 1
fi
# For safety, wait for user confirmation before process install
echo
echo " .~~. .~~."
echo " '. \ ' ' / .'"
echo " .~ .~~~..~."
echo " : .~.'~'.~. : The system will be installed on:"
echo " ~ ( ) ( ) ~ $target_device"
echo "( : '~'.~.'~' : )"
echo " ~ .~ ( ) ~. ~ Sure? Then, press [Enter]"
echo " ( : '~' : )"
echo " '~ .~~~. ~'"
echo -n " '~' "
read
echo
# ~STEP2: Prepare SD Card and create the required filesystems
#--------------------------------------------------------------------------------
# Delete MBR & partition table from the SD card
dd if=/dev/zero of=$target_device bs=512 count=1
# Create the required partitions
fdisk $target_device << EOF
n
p
1
+$BOOTP_SIZE
t
c
n
p
2
w
EOF
# Verify that the partition has been created
if ! [ -b ${target_device}1 && -b ${target_device}2 ]; then
echo "The required partitions do not seem to have been created"
exit 1
else
bootp=${target_device}1
rootp=${target_device}2
fi
# Partitions formatting
mkfs.vfat $bootp
mkfs.ext4 $rootp
# Mounting root partition
mkdir $BUILDENV
mount $rootp $BUILDENV
# ~STEP3: Installation & configuration
#--------------------------------------------------------------------------------
# Debootstrap the system
cd $BUILDENV
debootstrap --foreign --arch armhf $SUITE $BUILDENV $MIRROR
# Copy qemu-arm-static to the build directory to be able to run ARM binaries
cp $(which qemu-arm-static) usr/bin
# Chroot to the build directory and finalize the debootstrap process
LC_ALL="C" chroot $BUILDENV /debootstrap/debootstrap --second-stage
# Mount the boot partition
mount $bootp ${BUILDENV}/boot
# Fill /etc/apt/sources.list
echo "$APT_SOURCES" > etc/apt/sources.list
# Fill /boot/cmdline.txt
echo "$CMDLINE" > boot/cmdline.txt
# Fill /etc/fstab
echo "$FSTAB" > etc/fstab
# Fill /etc/hostname
echo "$HOSTNAME_RPI" > etc/hostname
# Fill /etc/network/interfaces
echo "$NETWORKING" > etc/network/interfaces
# Exec thir-stage
echo "$THIRD_STAGE" > third-stage
chmod +x third-stage
mount -t proc proc ${BUILDENV}/proc
mount -t sysfs sys ${BUILDENV}/sys
mount -o bind /dev ${BUILDENV}/dev
LC_ALL="C" chroot $BUILDENV /third-stage
umount ${BUILDENV}/proc
umount ${BUILDENV}/sys
umount ${BUILDENV}/dev
# ~STEP4: This is the end
#--------------------------------------------------------------------------------
# Goto initial dir and umount root partition and boot partition
cd $INIT_DIR
umount ${BUILDENV}/boot
umount ${BUILDENV}
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment