-
Notifications
You must be signed in to change notification settings - Fork 9
/
create_image.sh
executable file
·397 lines (300 loc) · 9.23 KB
/
create_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/bin/bash
function round_sectors() {
SECTORS="$1"
ROUNDED=$(((($SECTORS / 8) + 1) * 8))
echo $ROUNDED
}
CWD=$(pwd)
SOURCES_PATH="$CWD/sources"
TOOLS_PATH="$CWD/tools"
# Script to create the multitool image for rk322x boards
USERID=$(id -u)
if [ "$USERID" != "0" ]; then
echo "This script can only work with root permissions"
exit 26
fi
TARGET_CONF="$1"
if [ -z "$TARGET_CONF" ]; then
echo "Please specify a target configuration"
exit 40
fi
if [ ! -f "${SOURCES_PATH}/${TARGET_CONF}.conf" ]; then
echo "Could not find ${TARGET_CONF}.conf target configuration file"
exit 42
fi
. "${SOURCES_PATH}/${TARGET_CONF}.conf"
if [ $? -ne 0 ]; then
echo "Could not source ${TARGET_CONF}.conf"
exit 41
fi
# Target-specific sources path
TS_SOURCES_PATH="$CWD/sources/${TARGET_CONF}"
# Destination path and image
DIST_PATH="${CWD}/dist-${TARGET_CONF}"
DEST_IMAGE="${DIST_PATH}/multitool.img"
mkdir -p "$DIST_PATH"
if [ ! -f "$DIST_PATH/root.img" ]; then
echo -n "Creating debian base rootfs. This will take a while..."
cd "${SOURCES_PATH}/multistrap"
multistrap -f multistrap.conf > /tmp/multistrap.log 2>&1
if [ $? -ne 0 ]; then
echo -e "\nfailed:"
tail /tmp/multistrap.log
echo -e "\nFull log at /tmp/multistrap.log"
exit 25
fi
echo "done!"
echo -n "Creating squashfs from rootfs..."
mksquashfs rootfs "$DIST_PATH/root.img" -noappend -all-root > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\nfailed"
exit 26
fi
echo "done"
fi
ROOTFS_SIZE=$(du "$DIST_PATH/root.img" | cut -f 1)
ROOTFS_SECTORS=$(($ROOTFS_SIZE * 2))
ROOTFS_SECTORS=$(round_sectors $ROOTFS_SECTORS)
if [ $? -ne 0 ]; then
echo -e "\ncould not determine size of squashfs root filesystem"
exit 27
fi
cd "$CWD"
echo "-> rootfs size: ${ROOTFS_SIZE}kb"
echo "Creating empty image in $DEST_IMAGE"
#dd if=/dev/zero of="$DEST_IMAGE" bs=1M count=1024 conv=sync,fsync >/dev/null 2>&1
fallocate -l 512M "$DEST_IMAGE" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error while creating $DEST_IMAGE empty file"
exit 1
fi
echo "Mounting as loop device"
LOOP_DEVICE=$(losetup -f --show "$DEST_IMAGE")
if [ $? -ne 0 ]; then
echo "Could not loop mount $DEST_IMAGE"
exit 2
fi
echo "Creating partition table and partitions"
parted -s -- "$LOOP_DEVICE" mktable msdos >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not create partitions table"
exit 3
fi
START_ROOTFS=$BEGIN_USER_PARTITIONS
END_ROOTFS=$(($START_ROOTFS + $ROOTFS_SECTORS - 1))
START_FAT=$(round_sectors $END_ROOTFS)
END_FAT=$(($START_FAT + 131072 - 1)) # 131072 sectors = 64Mb
START_NTFS=$(round_sectors $END_FAT)
parted -s -- "$LOOP_DEVICE" unit s mkpart primary ntfs $START_NTFS -1s >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not create ntfs partition"
exit 3
fi
parted -s -- "$LOOP_DEVICE" unit s mkpart primary fat32 $START_FAT $END_FAT >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not create fat partition"
exit 3
fi
parted -s -- "$LOOP_DEVICE" unit s mkpart primary $START_ROOTFS $END_ROOTFS >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not create rootfs partition"
exit 3
fi
parted -s -- "$LOOP_DEVICE" set 1 boot off set 2 boot on set 3 boot off >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not set partition flags"
exit 28
fi
sync
sleep 1
# First check: in containers, it may happen that loop device partitions
# spawns as soon as they are created. We check their presence. If they already
# are there, we don't remount the device
SQUASHFS_PARTITION="${LOOP_DEVICE}p3"
NTFS_PARTITION="${LOOP_DEVICE}p1"
FAT_PARTITION="${LOOP_DEVICE}p2"
echo "squashfs partition: $SQUASHFS_PARTITION"
echo "fat partition: $FAT_PARTITION"
echo "ntfs partition: $NTFS_PARTITION"
if [ ! -b "$SQUASHFS_PARTITION" -o ! -b "$FAT_PARTITION" -o ! -b "$NTFS_PARTITION" ]; then
echo "Remounting loop device with partitions"
losetup -d "$LOOP_DEVICE" >/dev/null 2>&1
sleep 1
if [ $? -ne 0 ]; then
echo "Could not umount loop device $LOOP_DEVICE"
exit 4
fi
LOOP_DEVICE=$(losetup -f --show -P "$DEST_IMAGE")
if [ $? -ne 0 ]; then
echo "Could not remount loop device $LOOP_DEVICE"
exit 5
fi
SQUASHFS_PARTITION="${LOOP_DEVICE}p3"
NTFS_PARTITION="${LOOP_DEVICE}p1"
FAT_PARTITION="${LOOP_DEVICE}p2"
echo "squashfs partition after remount: $SQUASHFS_PARTITION"
echo "fat partition: after remount $FAT_PARTITION"
echo "ntfs partition: after remount $NTFS_PARTITION"
sleep 1
fi
if [ ! -b "$SQUASHFS_PARTITION" ]; then
echo "Could not find expected partition $SQUASHFS_PARTITION"
exit 26
fi
if [ ! -b "$FAT_PARTITION" ]; then
echo "Could not find expected partition $FAT_PARTITION"
exit 6
fi
if [ ! -b "$NTFS_PARTITION" ]; then
echo "Could not find expected partition $NTFS_PARTITION"
exit 6
fi
echo "Copying squashfs rootfilesystem"
dd if="${DIST_PATH}/root.img" of="$SQUASHFS_PARTITION" bs=4k conv=sync,fsync >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not install squashfs filesystem"
exit 27
fi
# ---- boot install -----
source "${TS_SOURCES_PATH}/boot_install"
echo "Formatting FAT32 partition"
mkfs.vfat -s 16 -n "BOOTSTRAP" "$FAT_PARTITION" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not format FAT32 partition"
exit 7
fi
echo "Formatting NTFS partition"
mkfs.ntfs -f -L "MULTITOOL" -p $START_NTFS -H 65535 -S 65535 -c 16384 "$NTFS_PARTITION" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not format NTFS partition"
exit 7
fi
TEMP_DIR=$(mktemp -d)
echo "Mounting NTFS partition"
mount "$NTFS_PARTITION" "$TEMP_DIR" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not mount $NTFS_PARTITION to $TEMP_DIR"
exit 9
fi
echo "Populating partition"
cp "${CWD}/LICENSE" "${TEMP_DIR}/LICENSE"
if [ $? -ne 0 ]; then
echo "Could not copy LICENSE to partition"
exit 28
fi
git log --no-merges --pretty="%as: %s" > "${TEMP_DIR}/CHANGELOG"
if [ $? -ne 0 ]; then
echo "Could not store CHANGELOG to partition"
fi
git log -1 --pretty="%h - %aD" > "${TEMP_DIR}/ISSUE"
if [ $? -ne 0 ]; then
echo "Could not store ISSUE to paritition"
fi
echo "${TARGET_CONF}" > "${TEMP_DIR}/TARGET"
if [ $? -ne 0 ]; then
echo "Could not store TARGET to partition"
fi
mkdir -p "${TEMP_DIR}/backups"
if [ $? -ne 0 ]; then
echo "Could not create backup directory"
exit 28
fi
mkdir -p "${TEMP_DIR}/images"
if [ $? -ne 0 ]; then
echo "Could not create images directory"
exit 29
fi
mkdir -p "${TEMP_DIR}/bsp"
if [ $? -ne 0 ]; then
echo "Could not create bsp directory"
exit 30
fi
echo "Copying board support package blobs into bsp directory"
cp "${DIST_PATH}/uboot.img" "${TEMP_DIR}/bsp/uboot.img"
[[ -f "${DIST_PATH}/trustos.img" ]] && cp "${DIST_PATH}/trustos.img" "${TEMP_DIR}/bsp/trustos.img"
[[ -f "${DIST_PATH}/legacy-uboot.img" ]] && cp "${DIST_PATH}/legacy-uboot.img" "${TEMP_DIR}/bsp/legacy-uboot.img"
echo "Unmount NTFS partition"
umount "$NTFS_PARTITION" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not umount $NTFS_PARTITION"
exit 17
fi
echo "Mounting FAT32 partition"
if [ $? -ne 0 ]; then
echo "Could not create temporary directory"
exit 8
fi
mount "$FAT_PARTITION" "$TEMP_DIR" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not mount $FAT_PARTITION to $TEMP_DIR"
exit 9
fi
echo "Populating partition"
cp "${TS_SOURCES_PATH}/${KERNEL_IMAGE}" "${TEMP_DIR}/kernel.img" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not copy kernel"
exit 10
fi
cp "${TS_SOURCES_PATH}/${DEVICE_TREE}" "${TEMP_DIR}/${DEVICE_TREE}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not copy device tree"
exit 12
fi
mkdir -p "${TEMP_DIR}/extlinux"
if [ $? -ne 0 ]; then
echo "Could not create extlinux directory"
exit 13
fi
cp "${TS_SOURCES_PATH}/extlinux.conf" "${TEMP_DIR}/extlinux/extlinux.conf" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not copy extlinux.conf"
exit 14
fi
# Gather the PARTUUID of the squashfs partition loop device
# blkid is friendlier in case of containers, so we use it here in place of lsblk
SQUASHFS_PARTITION_UUID=$(blkid -o value -s PARTUUID $SQUASHFS_PARTITION)
if [ $? -ne 0 ]; then
echo "Could not get SQUASHFS PARTUUID"
exit 15
fi
[[ -z $SQUASHFS_PARTITION_UUID ]] && echo "--- warning: empty squashfs partition UUID ---"
echo "squashfs partition uuid: $SQUASHFS_PARTITION_UUID"
# Gather the UUID of the FAT partition of the loop device
# blkid is friendlier in case of containers, so we use it here in place of lsblk
FAT_PARTITION_UUID=$(blkid -o value -s UUID $FAT_PARTITION)
if [ $? -ne 0 ]; then
echo "Could not get FAT PARTUUID"
exit 15
fi
[[ -z $FAT_PARTITION_UUID ]] && echo "--- warning: empty FAT boot partition UUID ---"
echo "fat partition uuid: $FAT_PARTITION_UUID"
sed -i "s/#SQUASHFS_PARTUUID#/$SQUASHFS_PARTITION_UUID/g" "${TEMP_DIR}/extlinux/extlinux.conf"
if [ $? -ne 0 ]; then
echo "Could not substitute SQUASHFS PARTUUID in extlinux.conf"
exit 16
fi
sed -i "s/#FAT_PARTUUID#/$FAT_PARTITION_UUID/g" "${TEMP_DIR}/extlinux/extlinux.conf"
if [ $? -ne 0 ]; then
echo "Could not substitute FAT PARTUUID in extlinux.conf"
exit 16
fi
echo "Unmount FAT32 partition"
umount "$FAT_PARTITION" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not umount $FAT_PARTITION"
exit 17
fi
rmdir "$TEMP_DIR"
if [ $? -ne 0 ]; then
echo "Could not remove temporary directory $TEMP_DIR"
exit 24
fi
echo "Unmounting loop device"
losetup -d "$LOOP_DEVICE" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Could not unmount $LOOP_DEVICE"
exit 23
fi
#truncate -s 140M "$DEST_IMAGE"
sync
echo "Done! Available image in $DEST_IMAGE"