Skip to content

Instantly share code, notes, and snippets.

@phedoreanu
Last active June 23, 2018 20:05
Show Gist options
  • Save phedoreanu/2ecbe589e76d015b7b26bc33af65a375 to your computer and use it in GitHub Desktop.
Save phedoreanu/2ecbe589e76d015b7b26bc33af65a375 to your computer and use it in GitHub Desktop.
Bootstrap sdcard with ArchLinuxARM for RaspberryPi 2/3
#!/bin/bash
DISK=$1
if [[ -z $DISK ]]; then
echo "Please supply the target disk i.e. /dev/sdb"
exit 1
fi
ALARM_VERSION=$2
if [[ -z $ALARM_VERSION ]]; then
ALARM_VERSION=3
fi
echo "Installing 'ArchLinuxARM-rpi-$ALARM_VERSION' on '$DISK'"
#Zero the beginning of the SD card or eMMC module
dd if=/dev/zero of=$DISK bs=1M count=8
# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
# Start fdisk to partition the SD card
# At the fdisk prompt, create the new partitions
sed -e 's/\s*\([\+a-z0-9A-Z]*\).*/\1/' << EOF | fdisk $DISK
o # clear the in memory partition table
n # new partition
p # default p # primary partition
1 # default 1 # partition number 1
2048 # default - start at beginning of disk
+100M #
t # type W95 FAT32 (LBA).
c
n # new ext4
p
2 # default 2
# default start
# default end
w # write the partition table
EOF
sleep 1
# Create and mount the FAT filesystem
mkfs.vfat ${DISK}1
sleep 1
# Create the ext4 filesystem
yes | mkfs.ext4 ${DISK}2
sleep 1
# Create dirs
mkdir root boot
# Mount the FAT filesystem
mount ${DISK}1 boot
# Mount the filesystem
mount ${DISK}2 root
# Download and extract the root filesystem (as root, not via sudo)
curl -LO http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-${ALARM_VERSION}-latest.tar.gz
sleep 1
bsdtar -xpf ArchLinuxARM-rpi-${ALARM_VERSION}-latest.tar.gz -C root
sync
# Flash the bootloader files
mv root/boot/* boot
# Unmount the partition
umount boot root
# Clean-up
rm -rf root/ boot/ ArchLinuxARM-rpi-*.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment