Skip to content

Instantly share code, notes, and snippets.

@abulte
Created October 23, 2012 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abulte/3941653 to your computer and use it in GitHub Desktop.
Save abulte/3941653 to your computer and use it in GitHub Desktop.
Build a custom Raspian image #2
# originally copied from http://michael.gorven.za.net/blog/2012/10/22/building-raspbian-images-raspberry-pi
# Copyright (C) 2012 Michael Gorven <http://michael.gorven.za.net/>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#!/bin/sh
# Copyright (C) 2012 Michael Gorven <http://michael.gorven.za.net/>
# Copyright (C) 2012 Alexandre Bulté <alexandre[at]bulte[dot]net>
set -ev
NAME="${1?Please specify output image name.}"
IMAGE_SIZE=1000
BOOT_SIZE=64
ALIGN_SECTORS=8192
ALIGN_FSBLOCKS="$(($ALIGN_SECTORS*512/4096))"
#LOCAL_MIRROR="file:/root/chroot/mirrors/debian"
LOCAL_MIRROR=$MIRROR
MIRROR="http://mirrordirector.raspbian.org/raspbian"
BASE_PACKAGES="openssh-server sudo ntp fake-hwclock raspi-copies-and-fills raspi-config"
RPI_USER=alexandre
RPI_PASSWD=alexandre
RPI_HOSTNAME=rpialb
GPU_MEM=16
##START_ELF=arm240_start.elf
#BUILDROOT="$(mktemp -d $PWD/raspbian.XXXXXX)/root/"
BUILDROOT="/root/make_raspbian_git/raspbian.8hvUou/root/"
export LANG=fr_FR.UTF-8
export LC_ALL=fr_FR.UTF-8
export LANGUAGE=fr_FR
check_host() {
if [ $(id -u) != 0 ]; then
echo "This script must be run as root."
exit 2
fi
for BINARY in wget gpg qemu-debootstrap rsync kpartx sfdisk mkfs.ext4 mkfs.vfat tune2fs tar; do
if ! which $BINARY > /dev/null; then
echo "Unable to find $BINARY binary, please install it"
exit 1
fi
done
echo "check_host OK"
}
bootstrap() {
if [ ! "$BUILDROOT" ]; then
echo "BUILDROOT is empty, aborting."
exit 1
fi
#KEYRING="${TMPDIR:-/tmp}/raspbian.gpg"
#if [ ! -s "$KEYRING" ]; then
# wget -O- http://archive.raspbian.org/raspbian.public.key | gpg --no-default-keyring --keyring "$KEYRING" --import
#fi
qemu-debootstrap --arch=armhf --no-check-gpg wheezy "$BUILDROOT" "$LOCAL_MIRROR"
echo "bootstrap OK"
}
configure_base() {
if [ ! "$BUILDROOT" ]; then
echo "BUILDROOT is empty, aborting."
exit 1
fi
cat > "$BUILDROOT/usr/sbin/policy-rc.d" <<-EOF
#!/bin/sh
exit 101
EOF
chmod +x "$BUILDROOT/usr/sbin/policy-rc.d"
cat > "$BUILDROOT/etc/apt/sources.list" <<-EOF
deb $MIRROR wheezy main contrib non-free rpi
EOF
cat > "$BUILDROOT/etc/apt/sources.list.d/raspi.list" <<-EOF
deb http://archive.raspberrypi.org/debian/ wheezy main
EOF
if [ "$http_proxy" ]; then
cat > "$BUILDROOT/etc/apt/apt.conf.d/30proxy" <<-EOF
Acquire::http::proxy "$http_proxy";
EOF
fi
wget -O- http://archive.raspberrypi.org/debian/raspberrypi.gpg.key | chroot "$BUILDROOT" apt-key add -
export DEBIAN_FRONTEND=noninteractive
chroot "$BUILDROOT" apt-get --yes update
chroot "$BUILDROOT" apt-get --yes dist-upgrade
chroot "$BUILDROOT" apt-get --yes install raspberrypi-bootloader libraspberrypi-bin $BASE_PACKAGES
cat > "$BUILDROOT/boot/config.txt" <<-EOF
gpu_mem=$GPU_MEM
disable_overscan=1
EOF
cat > "$BUILDROOT/boot/cmdline.txt" <<-EOF
dwc_otg.lpm_enable=0 dwc_otg.fiq_fix_enable=1 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=noop rootwait
EOF
chroot "$BUILDROOT" adduser --disabled-password --gecos 'Raspberry Pi User,,,,' pi
echo "$RPI_USER:$RPI_PASSWD" | chroot "$BUILDROOT" chpasswd
chroot "$BUILDROOT" adduser $RPI_USER sudo
cat >> "$BUILDROOT/etc/network/interfaces" <<-EOF
auto eth0
iface eth0 inet dhcp
EOF
echo "$RPI_HOSTNAME" > "$BUILDROOT/etc/hostname"
cat >> "$BUILDROOT/etc/hosts" <<-EOF
127.0.0.1 $RPI_HOSTNAME
EOF
cat > "$BUILDROOT/etc/fstab" <<-EOF
LABEL=boot /boot vfat defaults 0 2
LABEL=root / ext4 errors=remount-ro,noatime,nodiratime 0 1
EOF
#TODO?
#from http://www.raspberrypi.org/phpBB3/viewtopic.php?t=17788
#proc /proc proc defaults 0 0
#/dev/mmcblk0p1 /boot vfat defaults 0 2
#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
cat >> "$BUILDROOT/etc/modules" <<-EOF
vchiq
snd_bcm2835
EOF
cat > "$BUILDROOT/etc/sysctl.d/raspi.conf" <<-EOF
vm.min_free_kbytes = 8192
EOF
# no longer needed ?
#cp "$BUILDROOT/boot/$START_ELF" "$BUILDROOT/boot/start.elf"
echo "configure_base OK"
}
cleanup() {
chroot "$BUILDROOT" apt-get --yes clean
find "$BUILDROOT/var/lib/apt/lists/" -type f -delete
rm -f "$BUILDROOT/etc/apt/apt.conf.d/30proxy" "$BUILDROOT/usr/sbin/policy-rc.d"
echo "cleanup OK"
}
mkimage() {
IMAGE="$NAME.img"
TAR="$NAME.tar.gz"
dd if=/dev/zero of="$IMAGE" bs=1MB count=0 seek="$IMAGE_SIZE"
BOOT_SECTORS="$(($BOOT_SIZE*2048/$ALIGN_SECTORS*$ALIGN_SECTORS))"
cat | sfdisk --quiet --unit S --force "$IMAGE" <<-EOF
$ALIGN_SECTORS,$(($BOOT_SECTORS)),c,*
$(($ALIGN_SECTORS+$BOOT_SECTORS)),,L
EOF
LOOP="$(basename $(losetup -f))"
kpartx -a "$IMAGE"
BOOT="/dev/mapper/${LOOP}p1"
ROOT="/dev/mapper/${LOOP}p2"
mkfs.vfat -F 32 -n boot "$BOOT"
mkfs.ext4 -b 4096 -E stride=$ALIGN_FSBLOCKS,stripe-width=$ALIGN_FSBLOCKS -m 1 -L root "$ROOT"
tune2fs -i 0 -c 0 "$ROOT"
MNT="$(mktemp -d --tmpdir raspbian.XXXXXX)"
mount "$ROOT" "$MNT"
mkdir "$MNT/boot"
mount "$BOOT" "$MNT/boot"
rsync --archive --devices --specials --hard-links --acls --xattrs --sparse --verbose "$BUILDROOT" "$MNT"
umount "$MNT/boot"
umount "$MNT"
kpartx -d "$IMAGE"
tar -cvfz "$TAR" "$IMAGE"
echo "mkimage OK"
}
#check_host
#bootstrap
configure_base
#cleanup
#mkimage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment