-
Notifications
You must be signed in to change notification settings - Fork 126
/
build-pine64-image.sh
executable file
·86 lines (70 loc) · 2.03 KB
/
build-pine64-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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%d_%H%M%S_%Z)
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 ",+,L" | sfdisk -N 2 -L -uS --force "$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" .