This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraw-image-builder
More file actions
executable file
·114 lines (97 loc) · 3.11 KB
/
raw-image-builder
File metadata and controls
executable file
·114 lines (97 loc) · 3.11 KB
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/bash -e
# $1: partition number, $2: partition type, $3: partition mount options
function mount_partition () {
if [ "$2" ]; then
mkdir "$1"
if [ "$3" ]; then
mount -v -o "$3" "${loop_device}$1" "$1"
else
mount -v "${loop_device}$1" "$1"
fi
fi
}
function cleanup () {
echo 'Performing cleanup ...'
cd "$temp_dir" || exit 1
for i in 'p1' 'p2' 'p3' 'p4'; do
[ -d "$i" ] && umount "$i" || umount_fail=1
done
if ! ((umount_fail)); then
rm -r "${temp_dir}"
else
echo "WARNING: umount failed" >&2
fi
losetup -d "$loop_device"
}
if [ ! "$SOLUTION" ]; then
echo "Solution unspecified!" >&2
echo "Please set the SOLUTION environment variable!" >&2
exit 1
fi
if [ ! "$DEVICE_NAME" ]; then
echo "Device name unspecified!" >&2
echo "Please set the DEVICE_NAME environment variable!" >&2
exit 1
fi
if [ ! -e solutions/"$SOLUTION" ]; then
echo "Cannot find solution file!" >&2
exit 1
fi
source solutions/"$SOLUTION"
mkdir -p out aux_log
set -o pipefail
image_name="${DEVICE_NAME}_${SOLUTION}_$(date -u +%F)${IMAGE_NAME_EXTRA}"
apt-get install ${DEPENDENCIES} -y > aux_log/"${image_name}".log
dd if=/dev/zero of=out/"${image_name}".img bs=1M count="${IMAGE_TOTAL_SIZE}"
if [ "$(type -t "GDISK_COMMANDS")" = "function" ]; then
GDISK_COMMANDS "out/${image_name}.img"
else
PLAN="label:dos\nstart=2048"
# 2 partitions or more
if [ $(("$TRIPLE_PARTITION" + "$DUAL_PARTITION")) -ge 0 ]; then
# append size constraint to the first partition
PLAN+=",size=+${FIRST_PARTITION_SIZE}M"
fi
# 3 partitions
if (("$TRIPLE_PARTITION")); then
# append size constraint to the second partition
PLAN+="\nsize=+${SECOND_PARTITION_SIZE}M"
fi
# This `type=83` is actually a workaround for sfdisk to create a partition that is
# extended to the end of the disk since we don't know the start or size of the partition
PLAN+="\ntype=83\nwrite\n"
echo -e "$PLAN" | sfdisk "out/${image_name}.img"
if [ "$(type -t "EXTRA_FDISK_COMMANDS")" = "function" ]; then
EXTRA_FDISK_COMMANDS "out/${image_name}.img"
fi
fi
loop_device=$(losetup -Pfv --show out/"${image_name}".img)
sync
partprobe "${loop_device}"
if [ "$FIRST_PARTITION_TYPE" ]; then
"mkfs.${FIRST_PARTITION_TYPE}" "${loop_device}"p1
fi
if [ "$SECOND_PARTITION_TYPE" ]; then
"mkfs.${SECOND_PARTITION_TYPE}" "${loop_device}"p2
fi
if [ "$THIRD_PARTITION_TYPE" ]; then
"mkfs.${THIRD_PARTITION_TYPE}" "${loop_device}"p3
fi
if [ "$FOURTH_PARTITION_TYPE" ]; then
"mkfs.${FOURTH_PARTITION_TYPE}" "${loop_device}"p4
fi
# This environment variables are passed to do_fill_image
export IMAGE="$(readlink -e out/${image_name}.img)"
export IMAGE_DEVICE="${loop_device}"
temp_dir=$(mktemp -d)
umount_fail=0
pushd "$temp_dir"
trap 'echo "WARNING: Interrupt signal received!"; cleanup' SIGHUP SIGINT SIGQUIT SIGTERM
trap 'cleanup' EXIT
mount_partition "p1" "$FIRST_PARTITION_TYPE" "$FIRST_PARTITION_MOUNT_OPTIONS"
mount_partition "p2" "$SECOND_PARTITION_TYPE" "$SECOND_PARTITION_MOUNT_OPTIONS"
mount_partition "p3" "$THIRD_PARTITION_TYPE" "$THIRD_PARTITION_MOUNT_OPTIONS"
mount_partition "p4" "$FOURTH_PARTITION_TYPE" "$FOURTH_PARTITION_MOUNT_OPTIONS"
do_fill_image
sync && sleep 10
popd