-
Notifications
You must be signed in to change notification settings - Fork 12
/
mkimage.sh
executable file
·179 lines (138 loc) · 3.51 KB
/
mkimage.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
#
# Chimera Linux device image creation tool
#
# This is juts a wrapper around the more advanced device image tools which
# primarily exists to create device images for release. All additional
# arguments are passed to mkpart.sh.
#
# Copyright 2023 q66 <[email protected]>
#
# License: BSD-2-Clause
#
umask 022
readonly PROGNAME=$(basename "$0")
do_cleanup() {
if [ -n "$ROOT_DIR" -a -d "$ROOT_DIR" ]; then
umount -fR "$ROOT_DIR" > /dev/null 2>&1
sync
rmdir "$ROOT_DIR"
fi
if [ -n "$LOOP_DEV" ]; then
losetup -d "$LOOP_DEV"
fi
}
error_sig() {
do_cleanup
exit ${1:=0}
}
trap 'error_sig $? $LINENO' INT TERM 0
msg() {
printf "\033[1m$@\n\033[m"
}
die() {
msg "ERROR: $@"
error_sig 1 $LINENO
}
if [ "$(id -u)" != "0" ]; then
die "must be run as root"
fi
usage() {
cat <<EOF
Usage: $PROGNAME [opts] tarballs -- [mkpart_args]
The platform name is inferred from the last input tarball name.
If multiple tarballs are specified, they are to be separated with
semicolons.
Compression methods: none, gz
Options:
-o FILE Output file name (default: chimera-linux-<arch>-IMAGE-<date>-<platform>.img)
-s SIZE The image size (default: 2G)
-c TYPE Compress the image with TYPE (default: gz)
-h Print this message.
EOF
exit ${1:=1}
}
if ! command -v losetup > /dev/null 2>&1; then
die "losetup is required"
fi
if ! command -v truncate > /dev/null 2>&1; then
die "truncate is required"
fi
IMAGE_SIZE=2G
OUT_FILE=
PLATFORM=
LOOP_DEV=
ARCH=
COMP=gz
while getopts "o:s:c:h" opt; do
case "$opt" in
o) OUT_FILE="$OPTARG" ;;
s) IMAGE_SIZE="$OPTARG";;
c) COMP="$OPTARG";;
h) usage 0 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
IN_FILES="$1"
shift
case "$COMP" in
gz|none) ;;
*) die "invalid compression method: $COMP" ;;
esac
if [ -z "$IN_FILES" ]; then
die "input file(s) not given"
fi
OLD_IFS=$IFS
IFS=;
LAST_FILE=
for tfile in $IN_FILES; do
if [ ! -r "$tfile" ]; then
die "could not read input file: $tfile"
fi
LAST_FILE=$tfile
done
IFS=$OLD_IFS
ROOT_DIR=$(mktemp -d)
if [ $? -ne 0 ]; then
die "failed to create root directory"
fi
PLATFORM="${LAST_FILE#*ROOTFS-}"
PLATFORM="${PLATFORM#*DROOTFS-}"
PLATFORM="${PLATFORM#*-}"
PLATFORM="${PLATFORM%%.*}"
ARCH="${LAST_FILE#chimera-linux-}"
ARCH="${ARCH%-ROOTFS*}"
ARCH="${ARCH%-DROOTFS*}"
[ -n "$PLATFORM" -a -n "$ARCH" ] || die "invalid input filename"
if [ ! -r "sfdisk/$PLATFORM" ]; then
die "unknown platform: $PLATFORM"
fi
if [ -z "$OUT_FILE" ]; then
OUT_FILE="chimera-linux-${ARCH}-IMAGE-$(date '+%Y%m%d')-${PLATFORM}.img"
fi
mkdir -p "${ROOT_DIR}" || die "failed to create directories"
msg "Creating image..."
truncate -s "$IMAGE_SIZE" "$OUT_FILE" > /dev/null 2>&1 || \
die "failed to create image"
LOOP_DEV=$(losetup --show -fP "$OUT_FILE")
if [ $? -ne 0 ]; then
LOOP_DEV=
die "failed to attach loop device"
fi
msg "Creating and mounting partitions..."
./mkpart.sh -j "$@" "$LOOP_DEV" "$PLATFORM" "$ROOT_DIR" || \
die "could not set up target image"
./unrootfs.sh "$IN_FILES" "$ROOT_DIR" "$LOOP_DEV" || \
die "could not install Chimera"
msg "Cleaning up..."
umount -R "$ROOT_DIR" || die "failed to unmount image"
rmdir "$ROOT_DIR" || die "root directory not emoty"
ROOT_DIR=
losetup -d "$LOOP_DEV" || die "failed to detach loop device"
LOOP_DEV=
if [ "$COMP" != "none" ]; then
msg "Compressing image..."
gzip -9 "$OUT_FILE"
fi
msg "Successfully generated image (${OUT_FILE}.gz)."