Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
set -eu
#set -x
# via http://archlinuxarm.org/platforms/armv6/raspberry-pi
DISK=/dev/sdb
PARTITION=yes
MKFS=yes
CACHE_DIR=/var/cache
TARBALL_URL=http://archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz
TARBALL_MD5_URL=http://archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz.md5
ask() {
echo -n "$1"
read answer || answer=$2
[ "$answer" ] || answer=$2
echo "$answer" | egrep -qi '^y(es)?'
}
download() {
if which curl >/dev/null; then
curl -L "$1"
elif which wget >/dev/null; then
wget -O - "$1"
else
echo "error: wget or curl is required." >&2
exit -1
fi
}
check_downloaded_files() {
tarball=$(md5sum ArchLinuxARM-rpi-latest.tar.gz | awk '{ print $1 }')
md5=$(cat ArchLinuxARM-rpi-latest.tar.gz.md5 | awk '{ print $1 }')
if [ "$tarball" != "$md5" ]; then
rm -f ArchLinuxARM-rpi-latest.tar.gz{,.md5}
echo "error: Downloaded files are broken." >&2
exit -1
fi
}
if [ "$(id -u)" -ne 0 ]; then
echo "error: You must be root." >&2
exit -1
fi
if ! ask "Will destroy data on '$DISK'. Are you sure? [y/N]: " "N"; then
echo "Canceled." >&2
exit -1
fi
if [ ! -d "$CACHE_DIR" ]; then
echo "error: $CACHE_DIR doesn't exist." >&2
exit -1
fi
CACHE_DIR=$CACHE_DIR/create-alarmpi
mkdir -p "$CACHE_DIR"
cd "$CACHE_DIR"
if [ "$PARTITION" = "yes" ]; then
cat <<EOM | fdisk ${DISK}
o
n
+100M
t
c
n
w
EOM
fi
for i in boot root; do
[ -d $i/ ] && mountpoint -q $i/ && umount $i
done
if [ "$MKFS" = 'yes' ]; then
yes | mkfs.vfat ${DISK}1
fi
mkdir -p boot
mount ${DISK}1 boot
if [ "$MKFS" = 'yes' ]; then
yes | mkfs.ext2 ${DISK}2
fi
mkdir -p root
mount ${DISK}2 root
if [ ! -f ArchLinuxARM-rpi-latest.tar.gz ]; then
download $TARBALL_URL >ArchLinuxARM-rpi-latest.tar.gz
download $TARBALL_MD5_URL >ArchLinuxARM-rpi-latest.tar.gz.md5
check_downloaded_files
else
download $TARBALL_MD5_URL >ArchLinuxARM-rpi-latest.tar.gz.md5.new
old=$(cat ArchLinuxARM-rpi-latest.tar.gz.md5 | awk '{ print $1 }')
new=$(cat ArchLinuxARM-rpi-latest.tar.gz.md5.new | awk '{ print $1 }')
if [ "$old" != "$new" ]; then
mv -f ArchLinuxARM-rpi-latest.tar.gz.md5{.new,}
download $TARBALL_URL >ArchLinuxARM-rpi-latest.tar.gz
check_downloaded_files
else
rm -f ArchLinuxARM-rpi-latest.tar.gz.md5.new
fi
fi
echo -n "Extracting the root filesystem..."
bsdtar -xpf ArchLinuxARM-rpi-latest.tar.gz -C root
mv root/boot/* boot
echo Done.
echo -n "Syncing filesystem..."
sync
echo Done.
echo -n "Unmounting filesystem..."
umount boot root
echo Done.
for i in boot root; do
rmdir $i || echo "Failed to rmdir: $i/ is not empty?"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment