-
Notifications
You must be signed in to change notification settings - Fork 53
/
make-image
executable file
·88 lines (65 loc) · 1.91 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
#!/usr/bin/env bash
set -xe
mkdir -p dist /dist
OUTPUT_IMG="/dist/target.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
curl https://raw.githubusercontent.com/alpinelinux/alpine-chroot-install/master/alpine-chroot-install | sh
cp -R bootstrap/ /alpine/
/alpine/enter-chroot run-parts /bootstrap/
rm -rf /alpine/var/lib/apt/lists/*
rm -rf /alpine/var/cache/apk/*
rm -rf /alpine/root/*
rm -rf /alpine/bootstrap/
rm /alpine/env.sh /alpine/etc/resolv.conf /alpine/enter-chroot /alpine/destroy
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" <<EOF
unit B
resizepart 2 $PART_END
yes
quit
EOF
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"
gzip -c "$OUTPUT_IMG" > "dist/alpine-rpi-$ALPINE_BRANCH-$ARCH.img.gz"
echo "DONE."