-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_debian_img.sh
481 lines (401 loc) · 15.1 KB
/
make_debian_img.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
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/sh
# Copyright (C) 2024, John Clark <[email protected]>
set -e
# script exit codes:
# 1: missing utility
# 2: download failure
# 3: image mount failure
# 4: missing file
# 5: invalid file hash
# 9: superuser required
main() {
# file media is sized with the number between 'mmc_' and '.img'
# use 'm' for 1024^2 and 'g' for 1024^3
local media='mmc_2g.img' # or block device '/dev/sdX'
local deb_dist='testing' # testing/trixie
local hostname='visionfive2'
local acct_uid='debian'
local acct_pass='debian'
local extra_pkgs='bc, curl, pciutils, sudo, unzip, usbutils, wget, xxd, xz-utils, zip, zstd'
# partition table sector offsets
local sec_spl=4096
local sec_itb=8192
local sec_rfs=16384
if is_param 'clean' "$@"; then
rm -rf cache*/var
rm -f "$media"*
rm -rf "$mountpt"
rm -rf rootfs
echo '\nclean complete\n'
exit 0
fi
check_installed 'debootstrap' 'wget' 'xz-utils'
if [ -f "$media" ]; then
read -p "file $media exists, overwrite? <y/N> " yn
if ! [ "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'yes' -o "$yn" = 'Yes' ]; then
echo 'exiting...'
exit 0
fi
fi
# no compression if disabled or block media
local compress=$(is_param 'nocomp' "$@" || [ -b "$media" ] && echo false || echo true)
if $compress && [ -f "$media.xz" ]; then
read -p "file $media.xz exists, overwrite? <y/N> " yn
if ! [ "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'yes' -o "$yn" = 'Yes' ]; then
echo 'exiting...'
exit 0
fi
fi
print_hdr 'downloading files'
local cache="cache.$deb_dist"
# linux firmware
local lfw=$(download "$cache" 'https://mirrors.edge.kernel.org/pub/linux/kernel/firmware/linux-firmware-20240513.tar.xz')
local lfw_sha='9f05edb99668135d37cedc4fdd18aac2802dc9e4566e086e6c6c2e321f3ecc4e'
[ -f "$lfw" ] || { echo "unable to fetch $lfw"; exit 4; }
[ "$lfw_sha" = $(sha256sum "$lfw" | cut -c1-64) ] || { echo "invalid hash for $lfw"; exit 5; }
# u-boot
local uboot_spl=$(download "$cache" 'https://github.com/inindev/visionfive2/releases/download/v13-6.11/u-boot-spl.bin.normal.out')
[ -f "$uboot_spl" ] || { echo "unable to fetch $uboot_spl"; exit 4; }
local uboot_itb=$(download "$cache" 'https://github.com/inindev/visionfive2/releases/download/v13-6.11/u-boot.itb')
[ -f "$uboot_itb" ] || { echo "unable to fetch: $uboot_itb"; exit 4; }
# setup media
if [ ! -b "$media" ]; then
print_hdr 'creating image file'
make_image_file "$media"
fi
print_hdr 'partitioning media'
parition_media "$media" $sec_spl $sec_itb $sec_rfs
print_hdr 'formatting media'
format_media "$media" 3
print_hdr 'mounting media'
mount_media "$media" 3 $sec_rfs
print_hdr 'configuring files'
install -Dvm 644 'files/kernel-img.conf' "$mountpt/etc/kernel-img.conf"
print_hdr 'setting up fstab'
local mdev="$(findmnt -no source "$mountpt")"
local uuid="$(blkid -o value -s UUID "$mdev")"
echo "$(file_fstab $uuid)\n" > "$mountpt/etc/fstab"
print_hdr 'setting up extlinux boot'
install -Dvm 754 'files/dtb_cp' "$mountpt/etc/kernel/postinst.d/dtb_cp"
install -Dvm 754 'files/kernel_chmod' "$mountpt/etc/kernel/postinst.d/kernel_chmod"
install -Dvm 754 'files/dtb_rm' "$mountpt/etc/kernel/postrm.d/dtb_rm"
install -Dvm 754 'files/mk_extlinux' "$mountpt/boot/mk_extlinux"
ln -svf '../../../boot/mk_extlinux' "$mountpt/etc/kernel/postinst.d/update_extlinux"
ln -svf '../../../boot/mk_extlinux' "$mountpt/etc/kernel/postrm.d/update_extlinux"
print_hdr 'installing overlay files'
local dtbos="$(find "$cache/overlays" -maxdepth 1 -name '*.dtbo' 2>/dev/null | sort)"
if [ -n "$dtbos" ]; then
local dtbo dtgt="$mountpt/boot/overlay/lib"
mkdir -pv "$dtgt"
for dtbo in $dtbos; do
install -vm 644 "$dtbo" "$dtgt"
done
fi
print_hdr 'installing firmware'
mkdir -p "$mountpt/usr/lib/firmware"
local lfwn=$(basename "$lfw")
local lfwbn="${lfwn%%.*}"
tar -C "$mountpt/usr/lib/firmware" --strip-components=1 --wildcards -xavf "$lfw" \
"$lfwbn/microchip" \
"$lfwbn/r8a779x_usb3*" \
"$lfwbn/rtl_bt" \
"$lfwbn/rtl_nic" \
"$lfwbn/rtlwifi" \
"$lfwbn/rtw88" \
"$lfwbn/rtw89"
# install debian linux from deb packages (debootstrap)
print_hdr 'installing root filesystem from debian.org'
# do not write the cache to the image
mkdir -p "$cache/var/cache" "$cache/var/lib/apt/lists"
mkdir -p "$mountpt/var/cache" "$mountpt/var/lib/apt/lists"
mount -o bind "$cache/var/cache" "$mountpt/var/cache"
mount -o bind "$cache/var/lib/apt/lists" "$mountpt/var/lib/apt/lists"
local pkgs="linux-image-riscv64, dbus, dhcpcd, libpam-systemd, openssh-server, systemd-timesyncd"
debootstrap --arch 'riscv64' --include "$pkgs, $extra_pkgs" --exclude "isc-dhcp-client" "$deb_dist" "$mountpt" 'https://deb.debian.org/debian/'
umount "$mountpt/var/cache"
umount "$mountpt/var/lib/apt/lists"
# a deadlock on reboot seems to be related to agetty shutdown timing
# adding an ExecStop to the serial-getty service file appears to move the timing window
mkdir -p "$mountpt/etc/systemd/system/[email protected]"
printf '[Service]\nExecStop=-/usr/bin/echo -n\n' > "$mountpt/etc/systemd/system/[email protected]/override.conf"
# apt sources & default locale
echo "$(file_apt_sources $deb_dist)\n" > "$mountpt/etc/apt/sources.list"
echo "$(file_locale_cfg)\n" > "$mountpt/etc/default/locale"
# enable ll alias
sed -i '/alias.ll=/s/^#*\s*//' "$mountpt/etc/skel/.bashrc"
sed -i '/export.LS_OPTIONS/s/^#*\s*//' "$mountpt/root/.bashrc"
sed -i '/eval.*dircolors/s/^#*\s*//' "$mountpt/root/.bashrc"
sed -i '/alias.l.=/s/^#*\s*//' "$mountpt/root/.bashrc"
# motd (off by default)
is_param 'motd' "$@" && [ -f '../misc/motd' ] && cp -f '../misc/motd' "$mountpt/etc"
# set watchdog timeout to 300s
sed -i '/RebootWatchdogSec/s/.*/RebootWatchdogSec=5min/' "$mountpt/etc/systemd/system.conf"
# hostname
echo $hostname > "$mountpt/etc/hostname"
sed -i "s/127.0.0.1\tlocalhost/127.0.0.1\tlocalhost\n127.0.1.1\t$hostname/" "$mountpt/etc/hosts"
print_hdr 'creating user account'
chroot "$mountpt" /usr/sbin/useradd -m "$acct_uid" -s '/bin/bash'
chroot "$mountpt" /bin/sh -c "/usr/bin/echo $acct_uid:$acct_pass | /usr/sbin/chpasswd -c YESCRYPT"
chroot "$mountpt" /usr/bin/passwd -e "$acct_uid"
(umask 377 && echo "$acct_uid ALL=(ALL) NOPASSWD: ALL" > "$mountpt/etc/sudoers.d/$acct_uid")
print_hdr 'installing rootfs expansion script to /etc/rc.local'
install -Dvm 754 'files/rc.local' "$mountpt/etc/rc.local"
# disable sshd until after keys are regenerated on first boot
rm -fv "$mountpt/etc/systemd/system/sshd.service"
rm -fv "$mountpt/etc/systemd/system/multi-user.target.wants/ssh.service"
rm -fv "$mountpt/etc/ssh/ssh_host_"*
# generate machine id on first boot
truncate -s0 "$mountpt/etc/machine-id"
# reduce entropy on non-block media
[ -b "$media" ] || fstrim -v "$mountpt"
umount "$mountpt"
rm -rf "$mountpt"
print_hdr 'installing u-boot'
dd if="$uboot_spl" of="$media" bs=512 seek=$sec_spl conv=notrunc
dd if="$uboot_itb" of="$media" bs=512 seek=$sec_itb conv=notrunc,fsync
if $compress; then
print_hdr 'compressing image file'
xz -z8v "$media"
echo "\n${cya}compressed image is now ready${rst}"
echo "\n${cya}copy image to target media:${rst}"
echo " ${cya}sudo sh -c 'xzcat $media.xz > /dev/sdX && sync'${rst}"
elif [ -b "$media" ]; then
echo "\n${cya}media is now ready${rst}"
else
echo "\n${cya}image is now ready${rst}"
echo "\n${cya}copy image to media:${rst}"
echo " ${cya}sudo sh -c 'cat $media > /dev/sdX && sync'${rst}"
fi
echo
}
make_image_file() {
local filename="$1"
rm -f "$filename"*
local size="$(echo "$filename" | sed -rn 's/.*mmc_([[:digit:]]+[m|g])\.img$/\1/p')"
truncate -s "$size" "$filename"
}
parition_media() {
local media="$1"
local sec_spl="$2"
local sec_itb="$3"
local sec_rfs="$4"
# partition with gpt
cat <<-EOF | sfdisk "$media"
label: gpt
unit: sectors
sector-size: 512
part1: start=$sec_spl, size=$((sec_itb - sec_spl)), type=2e54b353-1271-4842-806f-e436d6af6985, name=spl
part2: start=$sec_itb, size=$((sec_rfs - sec_itb)), type=5b193300-fc78-40cd-8002-e86c45580b47, name=uboot
part3: start=$sec_rfs, type=0fc63daf-8483-4772-8e79-3d69d8477de4, name=rootfs
EOF
sync
}
format_media() {
local media="$1"
local partnum="${2:-3}"
# create ext4 filesystem
if [ -b "$media" ]; then
local rdn="$(basename "$media")"
local sbpn="$(echo /sys/block/${rdn}/${rdn}*${partnum})"
local part="/dev/$(basename "$sbpn")"
mkfs.ext4 -L rootfs -vO metadata_csum_seed "$part" && sync
else
local lodev="$(losetup -f)"
losetup -vP "$lodev" "$media" && sync
mkfs.ext4 -L rootfs -vO metadata_csum_seed "${lodev}p${partnum}" && sync
losetup -vd "$lodev" && sync
fi
}
mount_media() {
local media="$1"
local partnum="${2:-3}"
local sec_rfs="$3"
if [ -d "$mountpt" ]; then
mountpoint -q "$mountpt/var/cache" && umount "$mountpt/var/cache"
mountpoint -q "$mountpt/var/lib/apt/lists" && umount "$mountpt/var/lib/apt/lists"
mountpoint -q "$mountpt" && umount "$mountpt"
else
mkdir -p "$mountpt"
fi
local part
if [ -b "$media" ]; then
local rdn="$(basename "$media")"
local sbpn="$(echo /sys/block/${rdn}/${rdn}*${partnum})"
part="/dev/$(basename "$sbpn")"
mount "$part" "$mountpt"
elif [ -f "$media" ]; then
mount -no loop,offset=$(($sec_rfs * 512)) "$media" "$mountpt"
part="$(losetup -nO name -j "$media")"
else
echo "file not found: $media"
exit 4
fi
if [ ! -d "$mountpt/lost+found" ]; then
echo 'failed to mount the image file'
exit 3
fi
echo "partition ${cya}$part${rst} successfully mounted on ${cya}$mountpt${rst}"
}
check_mount_only() {
local item img flag=false
for item in "$@"; do
case "$item" in
mount) flag=true ;;
*.img) img=$item ;;
*.img.xz) img=$item ;;
esac
done
! $flag && return
if [ ! -f "$img" ]; then
if [ -z "$img" ]; then
echo "no image file specified"
else
echo "file not found: ${red}$img${rst}"
fi
exit 3
fi
if [ "$img" = *.xz ]; then
local tmp=$(basename "$img" .xz)
if [ -f "$tmp" ]; then
echo "compressed file ${bld}$img${rst} was specified but uncompressed file ${bld}$tmp${rst} exists..."
echo -n "mount ${bld}$tmp${rst}"
read -p " instead? <Y/n> " yn
if ! [ -z "$yn" -o "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'yes' -o "$yn" = 'Yes' ]; then
echo 'exiting...'
exit 0
fi
img=$tmp
else
echo -n "compressed file ${bld}$img${rst} was specified"
read -p ', decompress to mount? <Y/n>' yn
if ! [ -z "$yn" -o "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'yes' -o "$yn" = 'Yes' ]; then
echo 'exiting...'
exit 0
fi
xz -dk "$img"
img=$(basename "$img" .xz)
fi
fi
echo "mounting file ${yel}$img${rst}..."
mount_media "$img" 3 16384 # todo: remove hardcode
trap - EXIT INT QUIT ABRT TERM
echo "media mounted, use ${grn}sudo umount $mountpt${rst} to unmount"
exit 0
}
file_fstab() {
local uuid="$1"
cat <<-EOF
# if editing the device name for the root entry, it is necessary
# to regenerate the extlinux.conf file by running /boot/mk_extlinux
# <device> <mount> <type> <options> <dump> <pass>
UUID=$uuid / ext4 errors=remount-ro 0 1
EOF
}
file_apt_sources() {
local deb_dist="$1"
cat <<-EOF
# For information about how to configure apt package sources,
# see the sources.list(5) manual.
deb http://deb.debian.org/debian ${deb_dist} main contrib non-free non-free-firmware
#deb-src http://deb.debian.org/debian ${deb_dist} main contrib non-free non-free-firmware
#deb http://deb.debian.org/debian-security ${deb_dist}-security main contrib non-free non-free-firmware
#deb-src http://deb.debian.org/debian-security ${deb_dist}-security main contrib non-free non-free-firmware
#deb http://deb.debian.org/debian ${deb_dist}-updates main contrib non-free non-free-firmware
#deb-src http://deb.debian.org/debian ${deb_dist}-updates main contrib non-free non-free-firmware
EOF
}
file_wpa_supplicant_conf() {
cat <<-EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
EOF
}
file_locale_cfg() {
cat <<-EOF
LANG="C.UTF-8"
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
EOF
}
# download / return file from cache
download() {
local cache="$1"
local url="$2"
[ -d "$cache" ] || mkdir -p "$cache"
local filename="$(basename "$url")"
local filepath="$cache/$filename"
[ -f "$filepath" ] || wget "$url" -P "$cache"
[ -f "$filepath" ] || exit 2
echo "$filepath"
}
is_param() {
local item match
for item in "$@"; do
if [ -z "$match" ]; then
match="$item"
elif [ "$match" = "$item" ]; then
return 0
fi
done
return 1
}
check_installed() {
local item todo
for item in "$@"; do
dpkg -l "$item" 2>/dev/null | grep -q "ii $item" || todo="$todo $item"
done
if [ ! -z "$todo" ]; then
echo "this script requires the following packages:${bld}${yel}$todo${rst}"
echo " run: ${bld}${grn}sudo apt update && sudo apt -y install$todo${rst}\n"
exit 1
fi
}
print_hdr() {
local msg="$1"
echo "\n${h1}$msg...${rst}"
}
rst='\033[m'
bld='\033[1m'
red='\033[31m'
grn='\033[32m'
yel='\033[33m'
blu='\033[34m'
mag='\033[35m'
cya='\033[36m'
h1="${blu}==>${rst} ${bld}"
# ensure inner mount points get cleaned up
on_exit() {
if mountpoint -q "$mountpt"; then
mountpoint -q "$mountpt/var/cache" && umount "$mountpt/var/cache"
mountpoint -q "$mountpt/var/lib/apt/lists" && umount "$mountpt/var/lib/apt/lists"
read -p "$mountpt is still mounted, unmount? <Y/n> " yn
if [ -z "$yn" -o "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'yes' -o "$yn" = 'Yes' ]; then
echo "unmounting $mountpt"
umount "$mountpt"
sync
rm -rf "$mountpt"
fi
fi
}
mountpt='rootfs'
trap on_exit EXIT INT QUIT ABRT TERM
if [ 0 -ne $(id -u) ]; then
echo 'this script must be run as root'
echo " run: ${bld}${grn}sudo sh $(basename "$0")${rst}\n"
exit 9
fi
cd "$(dirname "$(realpath "$0")")"
check_mount_only "$@"
main "$@"