Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shell script that creates a Arch Linux image for the Raspberry Pi 2. Downloads the latest distribution from and creates the flash filesystems including boot partition. Partitions are aligned for typical SD cards and ext filesystem tuned accordingly.
#!/bin/sh -ex
losetup /dev/loop0 && exit 1 || true
image=arch-linux-$(date +%Y%m%d).img
wget -q -N
truncate -s 1G $image
losetup /dev/loop0 $image
parted -s /dev/loop0 mklabel msdos
parted -s /dev/loop0 unit s mkpart primary fat32 -- 1 65535
parted -s /dev/loop0 set 1 boot on
parted -s /dev/loop0 unit s mkpart primary ext2 -- 65536 -1
parted -s /dev/loop0 print
mkfs.vfat -I -n SYSTEM /dev/loop0p1
mkfs.ext4 -F -L root -b 4096 -E stride=4,stripe_width=1024 /dev/loop0p2
mkdir -p root
mount /dev/loop0p2 root
bsdtar xfz ArchLinuxARM-rpi-2-latest.tar.gz -C root
mv root/boot root/boot-temp
mkdir -p root/boot
mount /dev/loop0p1 root/boot
mv root/boot-temp/* root/boot/
rm -rf root/boot-temp
sed -i "s/ defaults / defaults,noatime /" root/etc/fstab
umount root/boot root
losetup -d /dev/loop0
#!/bin/sh -exu
dd if=arch-linux-*.img of=/dev/<blockdevice> status=progress
Copy link

bcomnes commented Jan 28, 2019

Great script! I'm trying to get this to run in and running into issues on the bsdtar step. It seems the mounted loop0p2 at root isn't writable for some reason. Any ideas

EDIT: image was too small! I just had to increase the size of the image and it works great.

Copy link

bcomnes commented Jan 31, 2019

I turned this into a CI/CD workflow: with images releasing to the releases page:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment