install Alpine Linux on Olimex TERES-I notebook

2022-04-26

this script is just template, please tweak/adapt it for particular use case.

Tools needed are linux machine with

parted f2fs-tools dosfstools e2fsprogs wget tar and usual linux tools  

for other linux distros apk-tool-static could be downloaded from https://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/ and unpacked with tar

set proper block device to mmc card on which Alpine will be installed, for example /dev/mmcblk1
when script finishes there will be all needed on mmc card to boot newly installed alpine
when machine boots there will be iwd (Internet Wireless Daemon) running and device can connect to wifi network with iwctl iwd utility
Packages installed by this script are minimally needed, rest can installed when the machine boots
usual disclaimer: no one is responsible for any damage/loss by using this

note: current u-boot stable release for TERES-I have a bug that the keyboard doesn't work in u-boot prompt and as result in grub so the officiall u-boot is commented in script and one from https://dev.alpinelinux.org/~mps/sunxi could be downloaded and used

#!/bin/sh

# apk add blkid parted dosfstools e2fsprogs wget tar
set -eu

: "${DISK:=/dev/sdX}"
if [ $DISK = "/dev/sdX" ]; then
  echo "DISK is not set correctly"
  exit
fi

readonly SCRIPT="${0##*/}"
readonly TMPDIR="$(mktemp -dt "${SCRIPT%.*}".XXXXXX)"
readonly BOOTDEV="$DISK"1
readonly ROOTDEV="$DISK"2
readonly APK_KEY="alpine-devel@lists.alpinelinux.org-60ac2099.rsa.pub"
readonly MIRROR="http://dl-cdn.alpinelinux.org/alpine"

cleanup() {
	set +e
	mountpoint -q "$TMPDIR"/boot/efi && umount "$TMPDIR"/boot/efi
	mountpoint -q "$TMPDIR" && umount "$TMPDIR"
	rm -rf "$TMPDIR"
}

trap cleanup EXIT INT
kernel='linux-sunxi-5.17.4-r0.apk'
if [ ! -f $kernel ]; then
  wget https://dev.alpinelinux.org/~mps/sunxi/"$kernel"
fi

echo "Paritioning disk"
# fat32 512, ext4 8GB
parted -s "$DISK" mktable msdos > /dev/null
parted -s "$DISK" unit s -- mkpart primary ext4 8192 1056767 > /dev/null
parted -s "$DISK" unit s -- mkpart primary ext4 1056768 17833983 > /dev/null
parted -s "$DISK" -- set 1 esp on > /dev/null

echo "Creating dirs and mounting"
mkfs.fat -F 32 -n ESP "$BOOTDEV"
#mkfs.ext4 -qL root "$ROOTDEV"
#mount -t ext4 "$ROOTDEV" "$TMPDIR"
mkfs.f2fs -f -l root "$ROOTDEV"
mount -t f2fs "$ROOTDEV" "$TMPDIR"
mkdir -p "$TMPDIR"/boot
mkdir -p "$TMPDIR"/boot/efi "$TMPDIR"/etc/apk/keys
mount -t vfat "$BOOTDEV" "$TMPDIR"/boot/efi

echo "Setup apk keys and repositories"
wget -qP "$TMPDIR"/etc/apk/keys https://alpinelinux.org/keys/"$APK_KEY"
printf "$MIRROR/edge/%s\n" main community testing > "$TMPDIR"/etc/apk/repositories

echo "installing base packages"
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --initdb add \
	alpine-base alpine-baselayout alpine-conf kmod openrc \
	dbus util-linux blkid chrony u-boot-sunxi \
	sysfsutils ssl_client ca-certificates-bundle alpine-keys \
	ethtool e2fsprogs libudev-zero libudev-zero-helper \
	iwd linux-firmware-none efibootmgr ttf-dejavu \
	agetty terminus-font openresolv tar wget
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --no-script add $kernel
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --no-script add grub-efi grub-mkfont

#dd if="$TMPDIR"/usr/share/u-boot/teres_i/u-boot-sunxi-with-spl.bin of=$DISK bs=8k seek=1
dd if=u-boot-sunxi-with-spl.bin of=$DISK bs=8k seek=1
echo "Setting up services and inittab"
for rc in boot/bootmisc boot/hostname boot/modules boot/sysctl boot/urandom boot/networking \
	sysinit/devfs sysinit/hwdrivers sysinit/mdev sysinit/modules \
	shutdown/mount-ro shutdown/killprocs \
	default/dbus default/chronyd default/iwd; do
	ln -s /etc/init.d/"${rc##*/}" "$TMPDIR"/etc/runlevels/"$rc"
done

echo "preparing scripts for setup"

cat > "$TMPDIR"/root/grub-setup.sh<<EOF
#!/bin/sh
grub-install --target=arm64-efi --efi-directory=/boot/efi --removable
grub-mkfont -s 18 -o /boot/grub/fonts/dejavu.pf2 /usr/share/fonts/ttf-dejavu/DejaVuSansMono.ttf
grub-mkconfig  > /boot/grub/grub.cfg
exit
EOF

echo 'GRUB_DISABLE_LINUX_PARTUUID=false' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_CMDLINE_LINUX="rootwait console=tty1"' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_TIMEOUT=10' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_FONT="/boot/grub/fonts/dejavu.pf2"' >> "$TMPDIR"/etc/default/grub
chmod 0744 "$TMPDIR"/root/grub-setup.sh

echo "Settting grub boot loader"
mount -t proc none $TMPDIR/proc
mount -o bind /sys $TMPDIR/sys
mount -o bind /dev $TMPDIR/dev
echo "Run this two commands and after that exit"
echo "grub-install --target=arm64-efi --efi-directory=/boot/efi --removable"
echo "grub-mkconfig  > /boot/grub/grub.cfg"
chroot $TMPDIR /root/grub-setup.sh
#chroot $TMPDIR /bin/ash -l
umount $TMPDIR/dev
umount $TMPDIR/sys
umount $TMPDIR/proc

sed -i 's/^tty1.*/tty1::respawn:\/sbin\/agetty -L 115200 tty1 linux --login-pause --autologin root --noclear/' $TMPDIR/etc/inittab

echo "Finished, cleaning up"