This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
forked from ayufan-pine64/linux-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-pine64-image.sh
executable file
·114 lines (94 loc) · 2.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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"
OUT_IMAGE="$1"
SIMPLEIMAGE="$2"
KERNELTAR="$3"
PACKAGEDEB="$4"
DISTRO="$5"
MODEL="$6"
VARIANT="$7"
SIZE="${8:-3650}"
if [[ -z "$MODEL" ]]; then
MODEL="pine64"
fi
export MODEL
if [ -z "$SIMPLEIMAGE" -o -z "$KERNELTAR" ]; then
echo "Usage: $0 <result.img> <simpleimage.img.xz> <kernel.tar.xz> <package.deb> [distro] [model] [variant: mate, i3, empty] [size (MiB)]"
exit 1
fi
if [ "$(id -u)" -ne "0" ]; then
echo "This script requires root."
exit 1
fi
if [ -z "$DISTRO" ]; then
DISTRO="xenial"
fi
case "$VARIANT" in
minimal)
SIZE=1024
;;
i3)
SIZE=2048
;;
mate)
SIZE=5120
;;
openmediavault)
SIZE=2048
;;
*)
echo "Unknown VARIANT: $VARIANT"
exit 1
;;
esac
SIMPLEIMAGE=$(readlink -f "$SIMPLEIMAGE")
KERNELTAR=$(readlink -f "$KERNELTAR")
PWD=$(readlink -f .)
TEMP=$(mktemp -p $PWD -d -t "$MODEL-build-XXXXXXXXXX")
IMAGE="$(basename "$OUT_IMAGE")"
OUT_IMAGE="$(readlink -f "$IMAGE")"
echo "> Building in $TEMP ..."
cleanup() {
local arg=$?
echo "> Cleaning up ..."
umount "$TEMP/boot" || true
umount $TEMP/rootfs/* || true
umount "$TEMP/rootfs" || true
kpartx -sd "$TEMP/$IMAGE" || true
kpartx -sd "$OUT_IMAGE" || true
rmdir "$TEMP/boot"
rmdir "$TEMP/rootfs"
rm -r "$TEMP"
exit $arg
}
trap cleanup EXIT
set -ex
# Unpack
unxz -k --stdout "$SIMPLEIMAGE" > "$TEMP/$IMAGE"
# Enlarge
dd if=/dev/zero bs=1M seek=$(($SIZE-1)) count=1 of="$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 && sh ./make_rootfs.sh "$TEMP/rootfs" "$KERNELTAR" "$PACKAGEDEB" "$DISTRO" "$TEMP/boot" "$MODEL" "$VARIANT")
mv -v "$TEMP/$IMAGE" "$OUT_IMAGE"
fstrim "$TEMP/rootfs"