Skip to content

Instantly share code, notes, and snippets.

@dnknth
Last active April 20, 2023 18:46
Show Gist options
  • Save dnknth/051a348ae69d02df935da81a90130209 to your computer and use it in GitHub Desktop.
Save dnknth/051a348ae69d02df935da81a90130209 to your computer and use it in GitHub Desktop.
Prepare an SD card with Alpine Linux for a Raspberry Pi

What's this?

A quick-and dirty way to prepare an SD card on Mac OS X with Alpine Linux for a Raspberry Pi.

Prerequisites

  • Mac OS X with GNU make and rsync installed, e.g. via homebrew.
  • An unused SD card. 2GB are sufficient.

Usage

make will format the SD card, install Alpine, and eject the card.

For finer-grained control, use a combination of targets:

  • format: Format the SD card
  • alpine: Download Alpine (see Configuration below)
  • mmc: Copy Alpine on the card, download it if needed
  • config: Include an existing rpi.apkovl.tar.gz configuration on the card. Only usefule if you have saved one previously.
  • splash: Add a boot splash. Caveat: This will disable the console. The splash screen must be named fbsplash.ppm.
  • eject: Eject the card
  • diff: Show differences between the Alpine distribution and the card contents
  • clean: Remove the downloaded Alpine distribution.

Configuration

See the Editable configuration section in the Makefile:

  • ARCH: Use armhf for any Pi, aarch64 for Pi 2 and 3.
  • MAJOR_VERSION: Alpine release, e.g. 3.9
  • MINOR_VERSION: Point release, e.g. 2 for release 3.9.2
# Prepare an SD card with Alpine Linux for a Raspberry Pi
# See: https://wiki.alpinelinux.org/wiki/Raspberry_Pi
# Editable configuration
DISTRO = alpine
# ARCH = armhf
ARCH = aarch64
MAJOR_VERSION = 3.9
MINOR_VERSION = 2
# Derived configuration
URL = http://dl-cdn.alpinelinux.org/alpine/v$(MAJOR_VERSION)/releases/$(ARCH)/alpine-rpi-$(MAJOR_VERSION).$(MINOR_VERSION)-$(ARCH).tar.gz
VOLUME = $(shell find /Volumes -maxdepth 1 -depth 1 -uid $$USER)
DISK = $(shell mount | fgrep "$(VOLUME)" | cut -f1 -d ' ' | sed -e 's/s[0-9]$$//')
SPLASH = fbsplash.ppm
# Rules
.PHONY: mmc format clean eject check-mounted clean-mmc all config splash diff
all: format mmc eject
mmc: check-mounted $(DISTRO)
rsync -av --modify-window=1 alpine/ $(VOLUME)/
splash:
install -C $(SPLASH) $(VOLUME)
check-mounted:
@ [ -n "$(VOLUME)" ] || (echo No MMC mounted ; exit 1)
$(DISTRO):
mkdir $@
wget -qO - $(URL) | tar --disable-copyfile -C $@ -xzvf -
touch $(DISTRO)/usercfg.txt
# echo "dtoverlay=pi3-disable-wifi" >> $(DISTRO)/usercfg.txt
mkdir -p $(DISTRO)/cache
clean:
rm -r $(DISTRO)
@ [ -z "$(VOLUME)" ] || diskutil eject $(VOLUME)
format: check-mounted
diskutil eraseDisk FAT32 ALPINE MBR $(DISK)
# diskutil partitionDisk $(DISK) MBR FAT32 ALPINE 256MB "Free Space" ROOT R
sleep 1 ; $(MAKE) clean-mmc
config: rpi.apkovl.tar.gz check-mounted
install -C $< $(VOLUME)
clean-mmc:
- rm -rf "$(VOLUME)/.Spotlight-V100" "$(VOLUME)/.Trashes"
- find "$(VOLUME)" -type f -name '._*' -delete
rm -rf "$(VOLUME)/.fseventsd"
rm -rf "$(VOLUME)/System Volume Information" # DOS
mkdir -p "$(VOLUME)/.fseventsd"
touch "$(VOLUME)/.fseventsd/no_log" "$(VOLUME)/.metadata_never_index" "$(VOLUME)/.Trashes"
eject: check-mounted
rm -f "$(VOLUME)"/._\*
diskutil eject "$(VOLUME)"
diff: check-mounted $(DISTRO)
rsync -avn --delete --modify-window=1 \
--exclude=cache --exclude=$(SPLASH) \
alpine/ "$(VOLUME)/"
@skaiser
Copy link

skaiser commented Apr 20, 2023

This is awesome -- thanks!

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