Skip to content

Commit

Permalink
Add general image builder
Browse files Browse the repository at this point in the history
  • Loading branch information
longsleep committed Dec 18, 2016
1 parent 7d582dc commit 272461a
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions build-pine64-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/sh
#
# This scripts takes a simpleimage and a kernel tarball, resizes the
# secondary partition and creates a rootfs inside it. Then extracts the
# Kernel tarball on top of it, resulting in a full Pine64 disk image.
#
# Latest stuff can be found at the following locations:
# - https://www.stdin.xyz/downloads/people/longsleep/pine64-images/simpleimage-pine64-latest.img.xz
# - https://www.stdin.xyz/downloads/people/longsleep/pine64-images/linux/linux-pine64-latest.tar.xz"

SIMPLEIMAGE="$1"
KERNELTAR="$2"
DISTRO="$3"
COUNT="$4"

if [ -z "$SIMPLEIMAGE" -o -z "$KERNELTAR" ]; then
echo "Usage: $0 <simpleimage.img.xz> <kernel.tar.xz> [distro] [count]"
exit 1
fi

if [ "$(id -u)" -ne "0" ]; then
echo "This script requires root."
exit 1
fi

if [ -z "$DISTRO" ]; then
DISTRO="xenial"
fi

if [ -z "$COUNT" ]; then
COUNT=1
fi

SIMPLEIMAGE=$(readlink -f "$SIMPLEIMAGE")
KERNELTAR=$(readlink -f "$KERNELTAR")

SIZE=3650 # MiB
DATE=$(date +%Y%m%H)

PWD=$(readlink -f .)
TEMP=$(mktemp -p $PWD -d -t "pine64-build-XXXXXXXXXX")
IMAGE="$DISTRO-pine64-bspkernel-$DATE-$COUNT.img"
echo "> Building in $TEMP ..."

cleanup() {
local arg=$?
echo "> Cleaning up ..."
umount "$TEMP/boot" || true
umount "$TEMP/rootfs" || true
kpartx -sd "$TEMP/$IMAGE" || true
rmdir "$TEMP/boot"
rmdir "$TEMP/rootfs"
rm "$TEMP/*" || true
rmdir "$TEMP"
exit $arg
}
trap cleanup EXIT

set -x

# Unpack
unxz -k --stdout "$SIMPLEIMAGE" > "$TEMP/$IMAGE"
# Enlarge
dd if=/dev/zero bs=1M count=$SIZE >> "$TEMP/$IMAGE"
# Resize
echo ", +" | sfdisk -N 2 "$TEMP/$IMAGE"

# Device
mkdir "$TEMP/boot"
mkdir "$TEMP/rootfs"
DEVICE=$(losetup --show --find "$TEMP/$IMAGE")
DEVICENAME=$(basename $DEVICE)
echo "> Device is $DEVICE ..."
kpartx -avs $DEVICE

# Resize filesystem
resize2fs /dev/mapper/${DEVICENAME}p2 || true

# Mount
mount /dev/mapper/${DEVICENAME}p1 "$TEMP/boot"
mount /dev/mapper/${DEVICENAME}p2 "$TEMP/rootfs"

sleep 2
(cd simpleimage && ./make_rootfs.sh "$TEMP/rootfs" "$KERNELTAR" "$DISTRO" "$TEMP/boot")

mv -v "$TEMP/$IMAGE" .

0 comments on commit 272461a

Please sign in to comment.