forked from ilteoood/alpine-raspberry-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-image
executable file
·96 lines (70 loc) · 2.32 KB
/
make-image
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
#!/usr/bin/env bash
set -xe
mkdir -p output /output
OUTPUT_IMG="/output/$NAME.img"
rm -Rf "$OUTPUT_IMG"
truncate -s 2G "$OUTPUT_IMG"
fdisk -H 255 -S 63 "$OUTPUT_IMG" <<-EOF
o
n
p
1
+128MB
t
c
n
p
2
w
EOF
LOOP_DEV=$(losetup --partscan --show --find "${OUTPUT_IMG}")
BOOT_DEV="$LOOP_DEV"p1
ROOT_DEV="$LOOP_DEV"p2
# format partitions
mkfs.fat -F32 -n ALPINE "$BOOT_DEV"
mkfs.ext4 "$ROOT_DEV"
mkdir -p /alpine
mount --make-private "$ROOT_DEV" /alpine
mkdir -p /alpine/boot
mount --make-private "$BOOT_DEV" /alpine/boot
IMGID="$(dd if="${OUTPUT_IMG}" skip=440 bs=1 count=4 2>/dev/null | xxd -e | cut -f 2 -d' ')"
BOOT_PARTUUID="${IMGID}-01"
ROOT_PARTUUID="${IMGID}-02"
apt-get update && apt-get install -y zip unzip
curl https://raw.githubusercontent.com/alpinelinux/alpine-chroot-install/master/alpine-chroot-install | sh -s -- -k PAYLOAD_LATEST_URL
cp -R bootstrap/ /alpine/
/alpine/enter-chroot /bootstrap/system.sh
/alpine/enter-chroot /bootstrap/fusee.sh
/alpine/enter-chroot /bootstrap/services.sh
/alpine/enter-chroot /bootstrap/kernel.sh
wget https://github.com/raspberrypi/firmware/archive/refs/heads/master.zip -O /tmp/firmware.zip
unzip /tmp/firmware.zip -d /tmp/firmware
cp -R /tmp/firmware/firmware-master/boot /alpine/boot
cp -R /tmp/firmware/firmware-master/modules /alpine/lib/modules
rm -rf /tmp/firmware
rm -rf /alpine/var/lib/apt/lists/*
rm -rf /alpine/var/cache/apk/*
rm -rf /alpine/root/*
rm -rf /alpine/bootstrap/
rm /alpine/enter-chroot
rm /alpine/etc/resolv.conf
rm /alpine/env.sh
find /alpine -iname "*-" -delete
find /alpine -iname "*~" -delete
umount -lf /alpine
# shrink image
ROOT_PART_START=$(parted -ms "$OUTPUT_IMG" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')
ROOT_BLOCK_SIZE=$(tune2fs -l "$ROOT_DEV" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
ROOT_MIN_SIZE=$(resize2fs -P "$ROOT_DEV" | cut -d ':' -f 2 | tr -d ' ')
# shrink fs
e2fsck -f -p "$ROOT_DEV"
resize2fs -p "$ROOT_DEV" "$ROOT_MIN_SIZE"
# shrink partition
PART_END=$((ROOT_PART_START + (ROOT_MIN_SIZE * ROOT_BLOCK_SIZE)))
parted ---pretend-input-tty "$OUTPUT_IMG" unit B resizepart 2 $PART_END yes
losetup -d "$LOOP_DEV"
# truncate free space
FREE_START=$(parted -ms "$OUTPUT_IMG" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B')
truncate -s "$FREE_START" "$OUTPUT_IMG"
zip -j -r - "$OUTPUT_IMG" > "output/$NAME.zip"
echo "DONE."