blob: 5b12672d25febe4cb6cfa4037e6af0e0b6183bac (
plain)
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
#!/bin/bash
# vim:sw=8:noet
# Except TEGRA
prepare_image() {
print_message "Creating image $IMAGE_OUT..."
dd if=/dev/zero of="$IMAGE_OUT" bs="$MB" count="$IMAGE_SIZE" ||
fatal_error "failed to create image $IMAGE_OUT"
sync
print_done
losetup "$MEDIA" "$IMAGE_OUT"
}
prepare_media() {
umount ${MEDIA}*
}
clean_disklabel() {
print_message "Clean old partitions table at $MEDIA..."
# Clear the first block (two 512-byte sectors for
# MBR and header, and 16KiB primary GPT Table):
dd if=/dev/zero of=$MEDIA bs=512 count=128 &&
# Clear secondary GPT Table
dd if=/dev/zero of=$MEDIA bs=512 count=34 seek=$(($(blockdev --getsz $MEDIA) - 34)) ||
fatal_error "failed clean old partitions table at $MEDIA!!!"
print_done
}
clear_riscv64_bootloader_partition() {
print_message "Find and clear old partitions with BBL and FSBL..."
[ -n "$BBL" ] && [ -n "$FSBL" ] ||
fatal_error "BBL and FSBL variables is not defined"
let 'START_SECTOR_RAW = START_SECTOR * 2048' # 1 sector = 512 Byte
local PART_NUMS=$(ls "$MEDIA"* |sed "s:$MEDIA::" |sed 's/^p//')
[ -n "$PART_NUMS" ] || fatal_error "$MEDIA not contain partitions"
local PART_NUM=
for PART_NUM in $PART_NUMS; do
FIND_PART_CODE=$(sgdisk -i "$PART_NUM" "$MEDIA" |
grep 'Partition GUID code:' | cut -d ' ' -f 4)
case "$FIND_PART_CODE" in
"$BBL"|"$FSBL")
sgdisk -d "$PART_NUM" "$MEDIA"
continue
;;
esac
FIND_PART_START=$(sgdisk -i "$PART_NUM" "$MEDIA" |
grep 'First sector: ' | cut -d ' ' -f 3)
[ "$FIND_PART_START" -ge "$START_SECTOR_RAW" ] ||
fatal_error "Partition $PART_NUM is in the bootloader zone"
done
}
create_disklabel() {
clean_disklabel
print_message "Creating disklabel $MEDIALABEL at $MEDIA..."
parted -s "$MEDIA" mklabel "$MEDIATABLE" ||
fatal_error "failed to create partitions table $MEDIATABLE"
print_done
}
format_partition() {
if [ -n "$FIRMPART" ]; then
print_message "Formating firmware partition to FAT32..."
echo 'y' | mkfs.fat -F 32 $FIRMPART ||
fatal_error "failed to format firmware partition"
sync
print_done
fi
if [ -n "$BOOTPART" ]; then
print_message "Formating "$BOOTPART" boot partition to ext4..."
echo 'y' | mkfs.ext4 "$BOOTPART" ||
fatal_error "failed to format BOOT partition"
sync
print_done
fi
if [ -n "${LUKS-}" ]; then
cryptsetup_luks_format
cryptsetup_luks_open
fi
print_message "Formating "$ROOTPART" root partition to $ROOT_FSTYPE..."
echo 'y' | mkfs.$ROOT_FSTYPE $ROOT_FS_OPT "$ROOTPART" ||
fatal_error "failed to format ROOT partition"
sync
print_done
}
set_bootflag() {
if [ "$MEDIATABLE" = msdos ] && [ -n "$FIRMPART_NUM" ]; then
if [ -z "$EFI" ]; then
if [ -z "$BOOTPART_NUM" ]; then
print_message "Setting boot flag at root partition..."
parted -s "$MEDIA" set "$ROOTPART_NUM" boot on ||
fatal_error "failed to set bootflag on root partition"
print_done
else
print_message "Setting boot flag at boot partition..."
parted -s "$MEDIA" set "$BOOTPART_NUM" boot on ||
fatal_error "failed to set bootflag on boot partition"
print_done
fi
else
print_message "Unsetting boot flag at root partition..."
parted -s "$MEDIA" set "$ROOTPART_NUM" boot off ||
fatal_error "failed to unset boot flag on root partition"
print_done
fi
fi
if [ "$MEDIATABLE" = gpt ] && [ -n "$FIRMPART_NUM" ]; then
if [ -n "$EFI" ]; then
print_message "Setting esp flag on EFI partition..."
parted -s "$MEDIA" set "$FIRMPART_NUM" esp on ||
fatal_error "failed to set esp flag on EFI partition"
print_done
print_message "Unsetting bootflag on root partition..."
parted -s "$MEDIA" set "$ROOTPART_NUM" legacy_boot off ||
fatal_error "failed to unset boot flag on root partition"
print_done
else
print_message "Unsetting esp flag on EFI partition..."
parted -s "$MEDIA" set "$FIRMPART_NUM" esp off ||
fatal_error "failed to unset esp flag on EFI partition"
print_done
print_message "Setting bootflag on root partition..."
parted -s "$MEDIA" set "$ROOTPART_NUM" legacy_boot on ||
fatal_error "failed to set boot flag on root partition"
print_done
fi
fi
}
create_partition() {
[ -n "${START_SECTOR}" ] || START_SECTOR=2
if [ -n "$FIRMPART" ]; then
print_message "Creating firmware partition..."
let NEXT_START="$START_SECTOR+$FIRMPART_SIZE"
echo 'y' | parted -s -a optimal "$MEDIA" \
mkpart primary fat32 ${START_SECTOR}MiB ${NEXT_START}MiB ||
fatal_error "failed to create firmware partition"
sync
print_done
fi
if [ -n "$BOOTPART" ]; then
print_message "Creating boot partition..."
[ -n "$NEXT_START" ] && START_BOOT_SECTOR=$NEXT_START ||
START_BOOT_SECTOR="$START_SECTOR"
let NEXT_START="$START_BOOT_SECTOR+$BOOTPART_SIZE"
echo 'y' | parted -s -a optimal "$MEDIA" \
mkpart primary ext4 ${START_BOOT_SECTOR}MiB ${NEXT_START}MiB ||
fatal_error "failed to create BOOT partition"
sync
print_done
fi
print_message "Creating root partition..."
[ -n "$END_SECTOR" ] || END_SECTOR=100%
[ -n "$NEXT_START" ] || NEXT_START="$START_SECTOR"
echo 'y' | parted -s -a optimal "$MEDIA" \
mkpart primary "$NEXT_START"MiB "$END_SECTOR" ||
fatal_error "failed to create ROOT partition"
sync
print_done
[ -z "${IMAGE_OUT-}" ] || finish_partitioning
}
cryptsetup_luks_format() {
print_message "Formatting luks..."
local PREV_LOG_MODE=$LOG_MODE
LOG_MODE="con"
local LUKS_DEFAULT_OPTIONS="--batch-mode --verify-passphrase"
cryptsetup luksFormat $LUKS_DEFAULT_OPTIONS $LUKS_OPT $ROOTPART ||
fatal_error "failed to encrypt root partition"
LOG_MODE=PREV_LOG_MODE
print_done
}
cryptsetup_luks_open() {
print_message "Try to decrypt root partition..."
local UNIQUE_MAPPING_NAME="luks_$(basename $ROOTPART)"
cryptsetup open $ROOTPART $UNIQUE_MAPPING_NAME
ROOTPART="/dev/mapper/$UNIQUE_MAPPING_NAME"
LUKS_DEVICE_IS_OPEN="1"
}
cryptsetup_luks_close() {
cryptsetup close $ROOTPART
LUKS_DEVICE_IS_OPEN=
}
finish_partitioning() {
# Create device maps from partition tables
if [ -n "${IMAGE_OUT-}" ]; then
kpartx -a -s "$MEDIA"
ROOTPART="/dev/mapper/$(basename "$MEDIA")p$ROOTPART_NUM"
[ -n "$FIRMPART_NUM" ] &&
FIRMPART="/dev/mapper/$(basename "$MEDIA")p$FIRMPART_NUM"
[ -n "$BOOTPART_NUM" ] &&
BOOTPART="/dev/mapper/$(basename "$MEDIA")p$BOOTPART_NUM"
[ -n "$BBLPART_NUM" ] &&
BBLPART="/dev/mapper/$(basename "$MEDIA")p$BBLPART_NUM"
fi
}
# This function REQUIRES a path to the device and
# prints the path to root partition, if there is one.
#
# The presence of /etc/fstab has been taken as the
# main characteristic of the root partition.
#
find_root_partition() {
local TMPDEVROOT DEVNAME=$1
TMPDEVROOT=$(mktemp -d --tmpdir devroot.XXXXXXXX)
[ -d "$TMPDEVROOT" ] ||
fatal_error "Cannot create a temporary directory"
for d in "${DEVNAME}"?*; do
local TDIR=$(findmnt --noheading --output TARGET "$d")
if [ -z "$TDIR" ]; then
TDIR="${TMPDEVROOT}/${d##*/}"
mkdir "$TDIR"
mount -o ro $d "$TDIR" ||
continue
fi
if [ -f "$TDIR/etc/fstab" ]; then
echo "$d"
break
fi
done
[ `ls "$TMPDEVROOT" | wc -l` -gt 0 ] &&
umount "$TMPDEVROOT"/*
rm -rf "$TMPDEVROOT"
}
# Takes a part number from the path to partition and prints it.
#
partnum() {
local PART=$1
echo ${PART##${MEDIA}${partsuffix}}
}
# Prints the path of device which specified by LABEL/UUID/path.
#
dev_by_spec() {
blkid --output device -t $1
}
# This function sets partitions' numbers and paths
# that are using everywhere in alt-rootfs-installer.
#
# Call it when precise partitions number are required.
#
find_partitions() {
ROOTPART=$(find_root_partition "$MEDIA")
ROOTPART_NUM=$(partnum "$ROOTPART")
local TDIR=$(findmnt --noheading --output TARGET "$ROOTPART")
local MOUNTED=0
if [ ! -d "$TDIR" ]; then
TDIR=$(mktemp -d --tmpdir 'find_fstab.XXXXXXXX')
mount -o ro "$ROOTPART" "$TDIR"
MOUNTED=1
fi
local FSTAB="$TDIR"/etc/fstab
local BOOTSPEC=$(grep '/boot\s' "$FSTAB" | awk '{print $1}')
BOOTPART=$(dev_by_spec "$BOOTSPEC")
BOOTPART_NUM=$(partnum $BOOTPART)
local FIRMSPEC=$(grep '/boot/efi\s' "$FSTAB" | awk '{print $1}')
FIRMPART=$(dev_by_spec "$FIRMSPEC")
FIRMPART_NUM=$(partnum $FIRMPART)
if [ $MOUNTED = 1 ]; then
umount "$TDIR"
rm -rf "$TDIR"
fi
}
change_partition() {
[ -n "$FIRMPART_NUM" ] && FIRMPART="${MEDIA}${partsuffix}${FIRMPART_NUM}" ||
FIRMPART=
[ -n "$BOOTPART_NUM" ] && BOOTPART="${MEDIA}${partsuffix}${BOOTPART_NUM}" ||
BOOTPART=
[ -n "$ROOTPART_NUM" ] && ROOTPART="${MEDIA}${partsuffix}${ROOTPART_NUM}"
}
mount_partition() {
[ -n "${ROOTFS-}" ] || [ -z "${LUKS-}" ] || cryptsetup_luks_open
# Get UUID
ROOTPART_UID="$(blkid $ROOTPART | sed -E 's;.*\sUUID="([^"]*)".*;\1;')"
TMPROOT="$(mktemp -d --tmpdir 'rootpart.XXXXXXXX')"
[ -d "${TMPROOT-}" ] ||
fatal_error "Can't create a temporary directory for rootfs"
print_message "Mounting root partition to the temporary directory..."
mount "$ROOTPART" "$TMPROOT" ||
fatal_error "failed to mount $ROOTPART to $TMPROOT"
print_done
if [ -n "$BOOTPART" ]; then
BOOTPART_UID="$(blkid $BOOTPART |sed -E 's;.*\sUUID="([^"]*)".*;\1;')"
print_message "Mounting boot partition to the temporary directory..."
TMPBOOT="$TMPROOT/boot"
mkdir -p "$TMPBOOT"
[ -d "${TMPBOOT-}" ] ||
fatal_error "Can't create a temporary directory for boot"
mount "$BOOTPART" "$TMPBOOT" ||
fatal_error "failed to mount $BOOTPART to $TMPBOOT"
print_done
fi
if [ -n "$FIRMPART" ]; then
FIRMPART_UID="$(blkid $FIRMPART |sed -E 's;.*\sUUID="([^"]*)".*;\1;')"
print_message "Mounting firmware partition to the temporary directory..."
TMPFIRM="$TMPROOT/boot/efi"
mkdir -p "$TMPFIRM"
[ -d "${TMPFIRM-}" ] ||
fatal_error "Can't create a temporary directory for firmware"
mount "$FIRMPART" "$TMPFIRM" ||
fatal_error "failed to mount $FIRMPART to $TMPFIRM"
print_done
fi
}
unmount_partition() {
if [ -n "$TMPFIRM" ] && [ -d "$TMPFIRM" ]; then
umount "$TMPFIRM"
fi
if [ -n "$TMPBOOT" ] && [ -d "$TMPBOOT" ]; then
umount "$TMPBOOT"
rmdir "$TMPBOOT"
fi
if [ -n "$TMPROOT" ] && [ -d "$TMPROOT" ]; then
umount "$TMPROOT"
rmdir "$TMPROOT"
fi
if [ -n "$LUKS_DEVICE_IS_OPEN" ]; then
cryptsetup_luks_close
fi
}
write_rootfs() {
if [ -n "${IMAGE_OUT-}" ]; then
print_message "Writing $ROOTFS rootfs to $IMAGE_OUT..."
else
print_message "Writing $ROOTFS rootfs to $MEDIA..."
fi
echo
$TAR "$ROOTFS" -C "$TMPROOT" ||
fatal_error "failed to write rootfs"
sync
print_done
}
setup_fstab() {
if [ -n "$ROOTPART_UID" ]; then
print_message "Updating fstab and extlinux.conf..."
grep -e '[[:space:]]/[[:space:]]' $TMPROOT/etc/fstab &&
sed -i "s/LABEL=ROOT/UUID=$ROOTPART_UID/" "$TMPROOT"/etc/fstab ||
echo "UUID=$ROOTPART_UID / $ROOT_FSTYPE relatime 1 1" >> "$TMPROOT/etc/fstab"
if [ -f "$TMPROOT/boot/extlinux/extlinux.conf" ]; then
sed -i "s/LABEL=ROOT/UUID=$ROOTPART_UID/" "$TMPROOT/boot/extlinux/extlinux.conf"
fi
if [ -n "$BOOTPART_UID" ]; then
mkdir -p "$TMPROOT/boot"
echo "UUID=$BOOTPART_UID /boot ext4 nodev,nosuid,noexec,relatime 1 2" >> "$TMPROOT/etc/fstab"
fi
if [ -n "$FIRMPART_UID" ]; then
mkdir -p "$TMPROOT/boot/efi"
echo "UUID=$FIRMPART_UID /boot/efi vfat umask=0,quiet,showexec,iocharset=utf8,codepage=866 1 2" >> "$TMPROOT/etc/fstab"
fi
print_done
fi
}
sync_partitions() {
print_message "Informing kernel about partition table changes..."
partprobe "$MEDIA" || fatal_error "failed to partprobe $MEDIA"
sync
sleep 3
print_done
}
update_cmdline_txt() {
[ -f "$TMPROOT/usr/share/u-boot/rpi_4/cmdline.txt" ] &&
RPI4_UBOOT=$TMPROOT/usr/share/u-boot/rpi_4/cmdline.txt
[ -f "$TMPROOT/usr/share/u-boot/rpi_4_32b/cmdline.txt" ] &&
RPI4_UBOOT=$TMPROOT/usr/share/u-boot/rpi_4_32b/cmdline.txt
[ -f "$TMPROOT/boot/efi/cmdline.txt" ] &&
RPI4_UBOOT=$TMPROOT/boot/efi/cmdline.txt
[ -n "${RPI4_UBOOT-}" ] &&
sed -i "s/LABEL=ROOT/UUID=$ROOTPART_UID/" "$RPI4_UBOOT"
}
image_to_media() {
print_message "Writing $IMAGE to $MEDIA..."
"$CAT" "$IMAGE" |
log_errtty dd of=$MEDIA bs=4M iflag=fullblock oflag=direct status=progress &&
print_done || print_fail
sync
unset MEDIATABLE
yes | parted "${MEDIA}" print | grep gpt && MEDIATABLE="gpt"
yes | parted "${MEDIA}" print | grep msdos && MEDIATABLE="msdos"
partprobe "$MEDIA"
if [ "$MEDIATABLE" = "gpt" ]; then
sgdisk -g "$MEDIA"
fi
find_partitions
if [ "$RESIZE" = 1 ]; then
print_message "Resizing root partition $ROOTPART..."
parted -s "$MEDIA" resizepart "$ROOTPART_NUM" 100% &&
resize2fs -f $ROOTPART ||
fatal_error "root partition $ROOTPART resize failed!!!"
e2fsck -f $ROOTPART
print_done
fi
partprobe "$MEDIA"
}
set_path_to_uboot() {
[ -n "$UBOOT" ] || UBOOT="${TMPROOT-}/usr/share/u-boot/$TARGET"
}
mount_chroot() {
mount --bind /dev "${TMPROOT-}/dev"
mount --bind /dev/pts "${TMPROOT-}/dev/pts"
mount --bind /sys "${TMPROOT-}/sys"
mount --bind /proc "${TMPROOT-}/proc"
mount tmpfs -t tmpfs -o mode=755 "${TMPROOT-}/tmp"
chroot "${TMPROOT-}" /bin/true ||
fatal_error "test chroot to rootfs failed!!!"
}
unmount_chroot() {
! mountpoint -q "${TMPROOT-}/tmp" || umount "${TMPROOT-}/tmp"
! mountpoint -q "${TMPROOT-}/proc" || umount "${TMPROOT-}/proc"
! mountpoint -q "${TMPROOT-}/sys" || umount "${TMPROOT-}/sys"
! mountpoint -q "${TMPROOT-}/dev/pts" || umount "${TMPROOT-}/dev/pts"
! mountpoint -q "${TMPROOT-}/dev" || umount "${TMPROOT-}/dev"
}
exec_chroot() {
chroot "${TMPROOT-}" "$@"
}
rpm_install() {
mkdir -p "${TMPROOT-}"/tmp/rpms &&
cp "$1"/*.rpm "${TMPROOT-}"/tmp/rpms/ &&
exec_chroot rpm -Uhv $(exec_chroot find /tmp/rpms -name '*.rpm')
}
add_dtb() {
while read dtbdir; do
if basename "$dtbdir" | grep -qe "^$DTB_KVER"; then
print_message "Adding dtb to $(basename $dtbdir)..."
mkdir -p "$dtbdir"/"$DTB_VENDOR"/
cp "$DTB_FILE" "$dtbdir"/"$DTB_VENDOR"/
! mountpoint -q "${TMPROOT-}/boot/efi" ||
cp "$DTB_FILE" "${TMPROOT-}/boot/efi/"
found="yes"
fi
done < <(find "${TMPROOT-}"/boot/devicetree -maxdepth 1 -mindepth 1 -type d)
[ -z "$found" ] && fatal_error "Could not find any devicetree dirs for $DTB_KVER kernels"
unset found
print_done
}
write_partition_blob() {
local name="$1"
local file="$2"
local of="$3"
local bs="${4-4M}"
print_message "Writing $name for ${TARGET-}..."
local blob
blob="$(readlink -e "$file")"
[ -f "${blob-}" ] || fatal_error "${blob-} was not found"
[ -b "${of-}" ] || fatal_error "${of-} is not a block device or does not exist"
dd if="$blob" of="$of" bs="$bs" status=none conv=nocreat || \
fatal_error "failed to write $name"
sync
print_done
}
|