Skip to content

Instantly share code, notes, and snippets.

@wopfel
Last active November 17, 2018 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wopfel/f0224b611c769ae4fd59f52bf1a3ac84 to your computer and use it in GitHub Desktop.
Save wopfel/f0224b611c769ae4fd59f52bf1a3ac84 to your computer and use it in GitHub Desktop.
Prepare SD card for Raspberry Pi / ArchLinuxARM
#!/bin/bash
# Call: ./prepare-sd-card.sh /dev/sdX
# Danger! May wipe the wrong data...
exit 99
# This script also copies a public key to the SSH authorized_keys file for the root user (ansible, here)
set -e
if [[ -z $1 ]] ; then
echo "Parameter (sdcard /dev/sdx) missing"
exit 1
fi
if [[ ! -b $1 ]] ; then
echo "$1 is no block device"
exit 1
fi
# Gives only sdb
BASENAME=$( basename $1 )
OUTPUT=$( lsblk -do name,tran,rm $1 | tail -n 1 )
echo $OUTPUT
REGEX="^$BASENAME usb 1$"
if [[ ! $OUTPUT =~ $REGEX ]] ; then
echo Not a usb device / removable
exit 1
fi
FDISK_CMDS='
o
p
n
p
1
+100M
t
c
n
p
2
w
q
'
echo "$FDISK_CMDS" | fdisk $1
mkfs.vfat ${1}1
yes | mkfs.ext4 ${1}2
mount ${1}1 /root/pi-boot
mount ${1}2 /root/pi-root
df -Th /root/pi-*
bsdtar -xpf ArchLinuxARM-rpi-2-latest.tar.gz -C /root/pi-root
mv /root/pi-root/boot/* /root/pi-boot
df -Th /root/pi-*
mkdir /root/pi-root/root/.ssh/ && cat ansible-ssh-pubkey >> /root/pi-root/root/.ssh/authorized_keys
sync
umount /root/pi-boot /root/pi-root
sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment