Skip to content

Instantly share code, notes, and snippets.

@hoshsadiq
Created June 24, 2019 20:39
Show Gist options
  • Save hoshsadiq/888482befed42a649edf5e868a25cf5b to your computer and use it in GitHub Desktop.
Save hoshsadiq/888482befed42a649edf5e868a25cf5b to your computer and use it in GitHub Desktop.
creting-alpine-rpi-image
#!/usr/bin/env bash
set -euxo pipefail
umount /tmp/mnt || true
losetup --detach /dev/loop0 || true
rm -rf root.img || true
# create an empty image
dd if=/dev/zero of=root.img seek=256M bs=1 count=0
{
echo o # Create a new empty DOS partition table
# set up partition 1
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition 1
echo # use default for first sector
echo +64M # Last sector
echo t # set partition type
echo c # set partition type to W95 FAT32 (LBA)
echo a # set partition 1 as bootable/active
# now we create partition 2
echo n # new partition
echo p # primary partition
echo 2 # partion number 2
echo # default, start immediately after preceding partition
echo # default, extend partition to end of disk
# print the in-memory partition table
echo p
# save and quit
echo w
echo q
} | fdisk root.img
losetup --show -P /dev/loop0 root.img
mkfs.vfat /dev/loop0p1
mkfs.ext4 /dev/loop0p2
losetup --detach /dev/loop0
mkdir -p /tmp/mnt
startSector="$(sfdisk -J ./root.img | jq '.partitiontable.partitions[] | select(.type == "c") | .start')"
offset="$(( $startSector * 512 ))"
mount -t vfat -o loop -o "offset=$offset" root.img /tmp/mnt
curl -fsSL http://dl-cdn.alpinelinux.org/alpine/v3.9/releases/aarch64/alpine-rpi-3.9.4-aarch64.tar.gz -o- | tar -xzv --no-same-owner -C /tmp/mnt --file -
umount /tmp/mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment