-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrun_qemu.sh
executable file
·1662 lines (1473 loc) · 43.5 KB
/
run_qemu.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash -Ee
# SPDX-License-Identifier: CC0-1.0
# Copyright (C) 2021 Intel Corporation. All rights reserved.
# default config
: "${builddir:=./qbuild}"
rootpw="root"
rootfssize="10G"
espsize="512M"
nvme_size="1G"
efi_mem_size="2" #in GiB
legacy_pmem_size="2" #in GiB
pmem_size="16384" #in MiB
pmem_label_size=2 #in MiB
pmem_final_size="$((pmem_size + pmem_label_size))"
: "${qemu:=qemu-system-x86_64}"
: "${gdb:=gdb}"
: "${ndctl:=$(readlink -e ~/git/ndctl)}"
selftests_home=root/built-selftests
mkosi_bin="mkosi"
mkosi_opts=("-i" "-f")
console="ttyS0"
accel="kvm"
# some canned hmat defaults - make configurable as/when needed
# terminology:
# local = attached directly to the socket in question
# far = memory controller is on 'this' socket, but distinct numa node/pxm domain
# cross = memory controller across sockets
# mem = memory node and pmem = NVDIMM node, as before
# Units: lat(ency) - nanoseconds, bw - MB/s
local_mem_lat=5
local_mem_bw=2000
far_mem_lat=10
far_mem_bw=1500
cross_mem_lat=20
cross_mem_bw=1000
# local_pmem is not a thing. In these configs we always give pmems their own node
far_pmem_lat=30
far_pmem_bw=1000
cross_pmem_lat=40
cross_pmem_bw=500
# similarly, some canned SLIT defaults
local_mem_dist=10
far_mem_dist=12
cross_mem_dist=21
far_pmem_dist=17
cross_pmem_dist=28
# CXL device params
cxl_addr="0x4c00000000"
cxl_backend_size="512M"
cxl_t3_size="256M"
cxl_label_size="128K"
num_build_cpus="$(($(getconf _NPROCESSORS_ONLN) + 1))"
rsync_opts=("--delete" "--exclude=.git/" "--exclude=build/" "-L" "-r")
qemu_dir=$(dirname "$(dirname "$qemu")")
if [[ $qemu_dir != . ]]; then
qemu_img="$qemu_dir/qemu-img"
if [ ! -f "$qemu_img" ]; then
qemu_img="$qemu_dir/build/qemu-img"
fi
qmp="$qemu_dir/scripts/qmp/qmp-shell"
else
qemu_img="qemu-img"
# Allows to not find the command qmp-shell
set +Ee
qmp=$(command -v qmp-shell)
# Do not leave qmp variable empty
if [ -z "$qmp" ]; then
qmp="qmp"
fi
# Back to the original setup
set -Ee
fi
fail()
{
# shellcheck disable=SC2059
printf "$@"
printf '\n'
exit 1
}
script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
parser_generator="${script_dir}/parser_generator.m4"
parser_lib="${script_dir}/run_qemu_parser.sh"
if [ ! -e "$parser_lib" ] || [ "$parser_generator" -nt "$parser_lib" ]; then
if command -V argbash > /dev/null; then
argbash --strip user-content "$parser_generator" -o "$parser_lib"
else
fail "error: please install argbash"
fi
fi
# shellcheck source=run_qemu_parser.sh
. "${script_dir}/run_qemu_parser.sh" || fail "Couldn't find $parser_lib"
if [ ${_arg_working_dir:0:1} == "-" ]; then
printf "Invalid option '%s'\n" "$_arg_working_dir"
printf "Try 'run_qemu.sh --help' for more information.\n"
exit 1
fi
cxl_test_script="$script_dir/scripts/rq_cxl_tests.sh"
cxl_results_script="$script_dir/scripts/rq_cxl_results.sh"
nfit_test_script="$script_dir/scripts/rq_nfit_tests.sh"
nfit_results_script="$script_dir/scripts/rq_nfit_results.sh"
# /etc/os-release is what mkosi "detect_distribution()" uses too.
get_os()
{
awk -F= 'BEGIN { notfound=1 }
/^ID=/ { print $2; notfound=0; exit }
END { exit notfound }' /etc/os-release
}
# distro: if any, user input passed to mkosi configuration line: "Distribution=$distro"
# rev: if any, user input passed to mkosi configuration line: "Release=$rev"
# _distro: variable private to this script. Defaults to build OS like mkosi does.
if [ -n "$distro" ]; then
_distro=${distro}
distribution_def="Distribution=$distro"
else
_distro=$(get_os)
distribution_def=''
fi
if [ -n "$rev" ]; then
release_def="Release=$rev"
else
release_def=''
fi
# distro specific variables
distro_vars="${script_dir}/${_distro}_vars.sh"
# shellcheck source=fedora_vars.sh
# shellcheck source=arch_vars.sh
# shellcheck source=ubuntu_vars.sh
[ -f "$distro_vars" ] && source "$distro_vars"
pushd "$_arg_working_dir" > /dev/null || fail "couldn't cd to $_arg_working_dir"
set_valid_mkosi_ver()
{
"$mkosi_bin" --version
mkosi_ver="$("$mkosi_bin" --version | awk '/mkosi/{ print $2 }')"
# drop the "~devel" suffix present in __version__ when running from
# pre-v25 versions
mkosi_ver="${mkosi_ver%%~*}"
# only look at the major version
mkosi_ver="${mkosi_ver%%.*}"
# Make sure we got a number
test "$mkosi_ver" -eq "$mkosi_ver" ||
fail 'mkosi version %s is not a number' "$mkosi_ver"
}
set_valid_mkosi_ver
# [Packages] was renamed to [Content] in v11, see mkosi/NEWS.md
test "$mkosi_ver" -ge 11 ||
fail 'mkosi version 11 or above is required, found %s' "$mkosi_ver"
kill_guest()
{
# sometimes this can be inadvertently re-entrant
sleep 1
if [ -x "$qmp" ] && [ -e "$qmp_sock" ]; then
"$qmp" "$qmp_sock" <<< "quit" > /dev/null
if (( _arg_quiet < 3 )); then
echo "run_qemu: Killed guest via QMP"
fi
fi
}
guest_alive()
{
if [ -e "$qmp_sock" ]; then
return 0
fi
return 1
}
loop_teardown()
{
loopdev="$(sudo losetup --list | grep "$_arg_rootfs" | awk '{ print $1 }')"
if [ -b "$loopdev" ]; then
sudo umount "${loopdev}p1" || true
sudo umount "${loopdev}p2" || true
sudo losetup -d "$loopdev"
fi
}
cleanup()
{
kill_guest
loop_teardown
set +x
}
# In POSIX theory, the shell automatically saves for us the $? of the
# "last command before the trap"; see special built-in 'exit' in
# "2. Shell Command Language" on opengroup.org. However this is
# too subtle (two concurrent '$?' now?) and difficult to trace,
# especially when set -e interferes! So, just save that $? ourselves
# and show it clearly in --debug / set -x mode.
trap 'exit_handler $?' EXIT
exit_handler()
{
# 42 "breadcrumb" if forgotten and missing
local err="${1-42}"
# "set -e" can trigger _twice_ and abort the EXIT handler again!
# https://unix.stackexchange.com/questions/667368/bash-change-exit-status-in-trap
# So, don't let some cleanup failure hide the real $err:
cleanup || true
exit "$err"
}
set_topology()
{
case "$1" in
1S|tiny)
num_nvmes=0
num_nodes=1
num_mems=0
num_pmems=0
num_efi_mems=0
num_legacy_pmems=0
;;
2S0|small0)
num_nvmes=0
num_nodes=2
num_mems=0
num_pmems=2
num_efi_mems=0
num_legacy_pmems=0
;;
2S|small)
num_nvmes=0
num_nodes=2
num_mems=2
num_pmems=2
num_efi_mems=1
num_legacy_pmems=1
;;
2S4|med*)
num_nvmes=0
num_nodes=2
num_mems=4
num_pmems=4
num_efi_mems=1
num_legacy_pmems=2
;;
4S|large)
num_nvmes=0
num_nodes=4
num_mems=4
num_pmems=4
num_efi_mems=2
num_legacy_pmems=2
;;
8S|huge)
num_nvmes=0
num_nodes=8
num_mems=8
num_pmems=8
num_efi_mems=2
num_legacy_pmems=2
;;
16S|insane)
num_nvmes=0
num_nodes=16
num_mems=0
num_pmems=16
num_efi_mems=2
num_legacy_pmems=2
;;
16Sb|broken)
num_nvmes=0
num_nodes=16
num_mems=0
num_pmems=32
num_efi_mems=2
num_legacy_pmems=2
;;
gcp)
num_nvmes=0
num_nodes=1
num_mems=0
num_pmems=0
num_efi_mems=0
num_legacy_pmems=0
;;
*)
printf "error: invalid preset: %s\n" "$1"
exit 1
;;
esac
# After presets override individuals
if [[ "$_arg_nvmes" ]]; then
num_nvmes="$_arg_nvmes"
fi
if [[ "$_arg_nodes" ]]; then
num_nodes="$_arg_nodes"
fi
if [[ "$_arg_mems" ]]; then
num_mems="$_arg_mems"
fi
if [[ "$_arg_pmems" ]]; then
num_pmems="$_arg_pmems"
fi
if [[ "$_arg_efi_mems" ]]; then
num_efi_mems="$_arg_efi_mems"
fi
if [[ "$_arg_legacy_pmems" ]]; then
num_legacy_pmems="$_arg_legacy_pmems"
fi
}
process_options_logic()
{
if [[ $_arg_debug == "on" ]]; then
set -x
fi
if [[ $_arg_cxl_test_run == "on" ]]; then
_arg_cxl="on"
_arg_cxl_debug="on"
_arg_cxl_test="on"
if [[ ! $_arg_autorun ]]; then
_arg_autorun="$cxl_test_script"
fi
if [[ ! $_arg_post_script ]]; then
_arg_post_script="$cxl_results_script"
fi
if [[ ! $_arg_log ]]; then
_arg_log="/tmp/rq_${_arg_instance}.log"
fi
if [[ $_arg_timeout == "0" ]]; then
_arg_timeout="15"
fi
fi
if [[ $_arg_nfit_test_run == "on" ]]; then
_arg_nfit_test="on"
set_topology "med"
if [[ ! $_arg_autorun ]]; then
_arg_autorun="$nfit_test_script"
fi
if [[ ! $_arg_post_script ]]; then
_arg_post_script="$nfit_results_script"
fi
if [[ ! $_arg_log ]]; then
_arg_log="/tmp/rq_${_arg_instance}.log"
fi
if [[ $_arg_timeout == "0" ]]; then
_arg_timeout="20"
fi
fi
if [[ $_arg_git_qemu == "on" ]]; then
qemu=~/git/qemu/x86_64-softmmu/qemu-system-x86_64
qemu_img=~/git/qemu/qemu-img
qmp=~/git/qemu/scripts/qmp/qmp-shell
# upstream changed where binaries go recently
if [ ! -f "$qemu_img" ]; then
qemu=~/git/qemu/build/qemu-system-x86_64
qemu_img=~/git/qemu/build/qemu-img
fi
if [ ! -x "$qemu" ]; then
fail "expected to find $qemu"
fi
fi
if [[ $_arg_curses == "on" ]]; then
dispmode="-curses"
else
dispmode="-nographic"
fi
set_topology "$_arg_preset"
if [[ $_arg_nfit_test == "on" ]]; then
if (( _arg_quiet < 3 )); then
printf "setting preset to 'med' for nfit_test\n"
fi
set_topology "med"
fi
if [[ "$_arg_mirror" ]]; then
mkosi_opts+=(-m "$_arg_mirror")
fi
if [[ $_arg_kcmd_replace && ! -f "$_arg_kcmd_replace" ]]; then
fail "File not found: $_arg_kcmd_replace"
fi
if [[ $_arg_kcmd_append && ! -f "$_arg_kcmd_append" ]]; then
fail "File not found: $_arg_kcmd_append"
fi
if [[ $_arg_gdb_qemu == "on" ]] && [[ $gdb == "gdb" ]]; then
gdb_extra=("-ex" "handle SIGUSR1 noprint nostop")
fi
if [[ $_arg_timeout -gt 0 ]]; then
if [[ ! -x "$qmp" ]]; then
fail " --timeout requires 'qmp'. $qmp not found"
fi
_arg_qmp="on"
fi
if [[ $_arg_qmp == "on" ]]; then
qmp_sock="/tmp/run_qemu_qmp_$_arg_instance"
fi
# canonicalize _arg_log here so that relative paths don't end up in
# builddir which can be surprising.
if [[ $_arg_log ]]; then
_arg_log=$(realpath "$_arg_log")
fi
if [[ $_arg_gcp == "on" ]]; then
# GCP wants 1GiB aligned images.
# 10G - ~256M (ESP) to make the resulting image exactly 10GiB
rootfssize=10468941824
rootpw="$(openssl rand -base64 12)"
console="ttyS0,38400n8d"
set_topology "gcp"
fi
num_cxl_pmems="$_arg_cxl_pmems"
if (( $num_cxl_pmems > 4 )); then
echo "error: a maximum of 4 CXL memdevs allowed"
exit 1
fi
num_cxl_vmems="$((4 - $num_cxl_pmems))"
if [[ $_arg_kvm = "off" ]]; then
accel="tcg"
fi
check_ndctl_dir
}
make_install_kernel()
{
local inst_path="$1"
test -n "$kver" || { # can't use fail() when inlined with declare -f
>&2 printf 'ERROR: Undefined $kver in make_install_kernel()\n'
exit 1
}
cat arch/x86_64/boot/bzImage > "$inst_path"/vmlinuz-"$kver"
cp System.map "$inst_path"/System.map-"$kver"
ln -fs vmlinuz-"$kver" "$inst_path"/vmlinuz
ln -fs System.map-"$kver" "$inst_path"/System.map
}
install_build_initrd()
{
inst_prefix="$builddir/mkosi.extra"
inst_path="$builddir/mkosi.extra/boot"
make INSTALL_HDR_PATH="$inst_prefix/usr" headers_install
make_install_kernel "$inst_path"
# Much of the script relies on a kernel named vmlinuz-$kver. This is
# distro specific as the default from Linux is simply "vmlinuz". Adjust
# that here.
[ ! -f "$inst_path/vmlinuz-$kver" ] && cp "$inst_path/vmlinuz" "$inst_path/vmlinuz-$kver"
# mkosi 13 onwards uses 'kernel-install add <uname> to install the kernel,
# and it expects a /lib/modules/$kver/vmlinuz
cp "$inst_path/vmlinuz-$kver" "$inst_prefix/lib/modules/$kver/vmlinuz"
dracut --force --verbose \
--no-hostonly \
--show-modules \
--kver="$kver" \
--kmoddir "$inst_prefix/lib/modules/$kver/" \
--kernel-image "./vmlinux" \
--add "bash systemd kernel-modules fs-lib" \
--omit "iscsi fcoe fcoe-uefi" \
--omit-drivers "nfit libnvdimm nd_pmem" \
"$inst_path/initramfs-$kver.img"
}
__build_kernel()
{
inst_prefix="$builddir/mkosi.extra"
inst_path="$builddir/mkosi.extra/boot"
mod_inst_param="INSTALL_MOD_PATH=$(readlink -f "$inst_prefix")"
quiet=""
if (( _arg_quiet >= 1 )); then
quiet="--quiet"
fi
mkdir -p "$inst_path"
# /lib -> /usr/lib
mkdir -p "${inst_prefix}/usr/lib"
ln -sf usr/lib "${inst_prefix}/lib"
if [[ $_arg_defconfig == "on" ]]; then
make $quiet olddefconfig
make $quiet prepare
fi
kver=$(make -s kernelrelease)
test -n "$kver"
make $quiet -j"$num_build_cpus"
# Install Modules Strip = ims
local ims=""
if [[ $_arg_strip_modules == "on" ]]; then
ims="INSTALL_MOD_STRIP=1"
fi
make $quiet -j"$num_build_cpus" "$mod_inst_param" $ims modules_install
if [[ $_arg_nfit_test == "on" ]]; then
test_path="tools/testing/nvdimm"
make $quiet -j"$num_build_cpus" M="$test_path"
make $quiet "$mod_inst_param" M="$test_path" $ims modules_install
fi
if [[ $_arg_cxl_test == "on" ]]; then
test_path="tools/testing/cxl"
make $quiet -j"$num_build_cpus" M="$test_path"
make $quiet "$mod_inst_param" M="$test_path" $ims modules_install
fi
if [[ $_arg_kern_selftests == "on" ]]; then
selftests_dir=$(readlink -f "$inst_prefix")/$selftests_home
make $quiet -j"$num_build_cpus" -C tools/testing/selftests install INSTALL_PATH="$selftests_dir"
fi
if [[ $_arg_gdb == "on" ]]; then
make $quiet scripts_gdb
fi
if (( _arg_quiet >= 1 )); then
install_build_initrd > /dev/null
else
install_build_initrd
fi
initrd="mkosi.extra/boot/initramfs-$kver.img"
}
build_kernel()
{
if (( _arg_quiet >= 2 )); then
__build_kernel > /dev/null
else
__build_kernel
fi
}
setup_autorun()
{
local prefix="$1"
local bin_dir="/usr/local/bin"
local systemd_dir="/etc/systemd/system/"
local systemd_unit="$systemd_dir/rq_autorun.service"
local systemd_linkdir="$systemd_dir/rq-custom.target.wants"
if [[ ! $_arg_autorun ]]; then
autorun_file="$prefix/$systemd_unit"
rm -f "${autorun_file:?}"
return
fi
mkdir -p "$prefix/$bin_dir"
mkdir -p "$prefix/$systemd_dir"
mkdir -p "$prefix/$systemd_linkdir"
cp -L "$_arg_autorun" "$prefix/$bin_dir"
chmod +x "$prefix/$bin_dir/${_arg_autorun##*/}"
cat <<- EOF > "$prefix/$systemd_dir/rq-custom.target"
[Unit]
Description=run_qemu Custom Target
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes
EOF
cat <<- EOF > "$prefix/$systemd_unit"
[Unit]
Description=run_qemu autorun script
After=multi-user.target
[Service]
Type=simple
User=root
Group=root
PAMName=login
KeyringMode=shared
ExecStart=$bin_dir/${_arg_autorun##*/}
[Install]
WantedBy=rq-custom.target
EOF
ln -sfr "$prefix/$systemd_unit" "$prefix/$systemd_linkdir/${systemd_unit##*/}"
ln -sfr "$prefix/$systemd_dir/rq-custom.target" "$prefix/$systemd_dir/default.target"
}
get_loopdev()
{
local loopdev num_loopdev
loopdev="$(sudo losetup --list | grep "$_arg_rootfs" | awk '{ print $1 }')"
num_loopdev="$(wc -l <<< "$loopdev")"
if (( num_loopdev != 1 )); then
{ lsblk -f || true
sudo losetup --list
echo "Expected 1 loopdev for $_arg_rootfs, found $num_loopdev."
echo "Try 'sudo losetup -D' to remove any stale loopdevs"
} >&2
exit 1
fi
test -b "$loopdev" || fail "%s is not a block device" "$loopdev"
printf '%s' "$loopdev"
}
# mount_rootfs <partnum>
mount_rootfs()
{
partnum="$1"
mp="mnt"
pushd "$builddir" > /dev/null || exit 1
test -s "$_arg_rootfs"
mkdir -p "$mp"
sudo losetup -Pf "$_arg_rootfs"
# The -P scan is performed in the background (this can be observed with a simple
# 'ls -l /dev/disk/by-loop-ref/')
sleep 2
loopdev=$(get_loopdev)
looppart="${loopdev}p${partnum}"
sleep 1
sudo mount "$looppart" "$mp"
popd > /dev/null || exit 1 # back to kernel tree
}
# umount_rootfs <partnum>
umount_rootfs()
{
partnum="$1"
mp="mnt"
loopdev=$(get_loopdev "$partnum")
looppart="${loopdev}p${partnum}"
sync
sudo umount "$looppart"
sudo rm -rf "$mp"
sudo losetup -d "$loopdev"
}
declare -a kcmd
build_kernel_cmdline()
{
root="$1"
# standard options
kcmd=(
"selinux=0"
"audit=0"
"console=tty0"
"console=$console"
"root=$root"
"ignore_loglevel"
"rw"
"initcall_debug"
"log_buf_len=20M"
"memory_hotplug.memmap_on_memory=force"
)
if [[ $_arg_gdb == "on" ]]; then
kcmd+=(
"nokaslr"
)
fi
if [[ $_arg_cxl_debug == "on" ]]; then
kcmd+=(
"cxl_acpi.dyndbg=+fplm"
"cxl_pci.dyndbg=+fplm"
"cxl_core.dyndbg=+fplm"
"cxl_mem.dyndbg=+fplm"
"cxl_pmem.dyndbg=+fplm"
"cxl_port.dyndbg=+fplm"
"cxl_region.dyndbg=+fplm"
"cxl_test.dyndbg=+fplm"
"cxl_mock.dyndbg=+fplm"
"cxl_mock_mem.dyndbg=+fplm"
)
fi
if [[ $_arg_dax_debug == "on" ]]; then
kcmd+=(
"dax.dyndbg=+fplm"
"dax_cxl.dyndbg=+fplm"
"device_dax.dyndbg=+fplm"
)
fi
if [[ $_arg_nfit_debug == "on" ]]; then
kcmd+=(
"libnvdimm.dyndbg=+fplm"
"nfit.dyndbg=+fplm"
"nfit_test.dyndbg=+fplm"
"nd_pmem.dyndbg=+fplm"
"nd_btt.dyndbg=+fplm"
"dax.dyndbg=+fplm"
"dax_hmem.dyndbg=+fplm"
"nfit_test_iomap.dyndbg=+fplm"
)
fi
if [[ $_arg_nfit_test == "on" ]]; then
num_efi_mems=0
num_legacy_pmems=0
kcmd+=(
"memmap=3G!6G,1G!9G"
"efi_fake_mem=2G@10G:0x40000"
)
fi
tot_mem="$(((_arg_mem_size / 1024) * (num_mems + num_nodes)))" # in GiB
if (( num_legacy_pmems > 0 )); then
reserve_efi="$((num_efi_mems * efi_mem_size))" # in GiB
reserve_pmem="$((num_legacy_pmems * legacy_pmem_size))" # in GiB
start="$((tot_mem - reserve_efi - reserve_pmem))" #in GiB
declare -a legacy_pmems
cur="$start"
for (( i = 0; i < num_legacy_pmems; i++ )); do
cur=$((cur + (i * legacy_pmem_size)))
legacy_pmems[$i]="${legacy_pmem_size}G!${cur}G"
done
pmems_str="$(printf "%s," "${legacy_pmems[@]}")"
pmems_str="${pmems_str%,}"
kcmd+=( "memmap=$pmems_str" )
fi
if (( num_efi_mems > 0 )); then
reserve="$((num_efi_mems * efi_mem_size))" # in GiB
start="$((tot_mem - reserve))" #in GiB
declare -a efi_mems
cur="$start"
for (( i = 0; i < num_efi_mems; i++ )); do
cur=$((cur + (i * efi_mem_size)))
efi_mems[$i]="${efi_mem_size}G@${cur}G:0x40000"
done
efi_mems_str="$(printf "%s," "${efi_mems[@]}")"
efi_mems_str="${efi_mems_str%,}"
kcmd+=( "efi_fake_mem=$efi_mems_str" )
fi
# process kcmd replacement
if [[ $_arg_kcmd_replace ]]; then
mapfile -t kcmd < <(options_from_file "$_arg_kcmd_replace")
fi
# process kcmd appends
if [[ $_arg_kcmd_append ]]; then
mapfile -t kcmd_extra < <(options_from_file "$_arg_kcmd_append")
fi
# construct the final array
kcmd+=( "${kcmd_extra[@]}" )
}
update_rootfs_boot_kernel()
{
if [[ ! $kver ]]; then
fail "Error: kver not set in update_rootfs_boot_kernel"
fi
mount_rootfs 1 # EFI system partition
conffile="$builddir/mnt/loader/entries/run-qemu-kernel-$kver.conf"
sudo sfdisk -l "${loopdev}" || sudo parted "${loopdev}" print || true
root_partuuid="$(sudo blkid "${loopdev}p2" -o export | awk -F'=' '/^PARTUUID/{ print $2 }')"
if [[ ! $root_partuuid ]]; then
sudo losetup --list
ls -l /dev/disk/by-loop-ref/ || true
sudo blkid "${loopdev}p2" -o export || true
fail "Unable to determine root partition UUID, is the mkosi image 'Bootable'?"
fi
# Note there is no initrd when booting this way, root filesystem must be built-in.
build_kernel_cmdline "PARTUUID=$root_partuuid"
sudo tee "$conffile" > /dev/null <<- EOF
title run-qemu-$_distro ($kver)
version $kver
linux run-qemu-kernel/$kver/vmlinuz
options ${kcmd[*]}
EOF
sudo mkdir -p "$builddir/mnt/run-qemu-kernel/$kver"
sudo cp "$builddir/mkosi.extra/boot/vmlinuz-$kver" "$builddir/mnt/run-qemu-kernel/$kver/vmlinuz"
defconf="$builddir/mnt/loader/loader.conf"
if [ -f "$defconf" ]; then
sudo sed -i -e 's/^#.*timeout.*/timeout 4/' "$defconf"
sudo sed -i -e '/default.*/d' "$defconf"
else
echo "timeout 4" | sudo tee "$defconf"
fi
echo "default run-qemu-kernel-$kver.conf" | sudo tee -a "$defconf"
# Fedora
sudo cp "$ovmf_path"/Shell.efi "$builddir"/mnt/shellx64.efi ||
# Arch Linux
sudo cp /usr/share/edk2-shell/x64/Shell_Full.efi "$builddir"/mnt/shellx64.efi ||
true
umount_rootfs 1
mount_rootfs 2 # Linux root partition
sudo ln -sf "../efi/run-qemu-kernel" "$builddir/mnt/boot/run-qemu-kernel"
umount_rootfs 2
}
setup_depmod()
{
prefix="$1"
depmod_dir="$prefix/etc/depmod.d"
depmod_conf="$depmod_dir/nfit_test.conf"
depmod_cxl_conf="$depmod_dir/cxl_test.conf"
depmod_load_dir="$prefix/etc/modules-load.d"
depmod_load_cxl_conf="$depmod_load_dir/cxl_test.conf"
if [[ $_arg_nfit_test == "on" ]]; then
mkdir -p "$depmod_dir"
cat <<- EOF > "$depmod_conf"
override nfit * extra
override device_dax * extra
override dax_pmem * extra
override dax_pmem_core * extra
override dax_pmem_compat * extra
override libnvdimm * extra
override nd_blk * extra
override nd_btt * extra
override nd_e820 * extra
override nd_pmem * extra
EOF
else
rm -f "$depmod_conf"
fi
if [[ $_arg_cxl_test == "on" ]]; then
mkdir -p "$depmod_dir"
cat <<- EOF > "$depmod_cxl_conf"
override cxl_acpi * extra
override cxl_core * extra
override cxl_pmem * extra
override cxl_mem * extra
override cxl_port * extra
EOF
mkdir -p "$depmod_load_dir"
cat <<- EOF > "$depmod_load_cxl_conf"
cxl_test
EOF
else
rm -f "$depmod_cxl_conf"
rm -f "$depmod_load_cxl_conf"
fi
system_map="$prefix/boot/System.map"
if [ ! -f "$system_map" ]; then
system_map="$prefix/boot/System.map-$kver"
fi
if [ ! -f "$system_map" ]; then
echo "not found: $system_map. Try rebuilding with '-r img'"
return 1
fi
: Warning: symlinks created by this depmod do not survive the move
: to the virtual machine
sudo depmod -b "$prefix" -F "$system_map" -C "$depmod_dir" "$kver"
}
__update_existing_rootfs()
{
inst_prefix="$builddir/mnt"
inst_path="$inst_prefix/boot"
mod_inst_param="INSTALL_MOD_PATH=$(readlink -f "$inst_prefix")"
# Install Modules Strip = ims
local ims=""
if [[ $_arg_strip_modules == "on" ]]; then
ims="INSTALL_MOD_STRIP=1"
fi
mount_rootfs 2 # Linux root partition
if [[ $_arg_nfit_test == "on" ]]; then
test_path="tools/testing/nvdimm"
make -j"$num_build_cpus" M="$test_path"
sudo make "$mod_inst_param" M="$test_path" $ims modules_install
else
sudo rm -rf "$test_path"/*.ko
fi
if [[ $_arg_cxl_test == "on" ]]; then
test_path="tools/testing/cxl"
make -j"$num_build_cpus" M="$test_path"
sudo make "$mod_inst_param" M="$test_path" $ims modules_install
else
sudo rm -rf "$test_path"/*.ko
fi
sudo make "$mod_inst_param" $ims modules_install
sudo make INSTALL_HDR_PATH="$inst_prefix/usr" headers_install
if [[ $_arg_debug == 'on' ]]; then
local _trace_sh='-x'
fi
sudo -E bash $_trace_sh -e -c "$(declare -f make_install_kernel); kver=$kver make_install_kernel $inst_path"
if [[ $_arg_cxl_test == "off" ]]; then
sudo rm -f "$inst_prefix"/usr/lib/modules/"$kver"/extra/cxl_*.ko
fi
if [[ $_arg_ndctl_build == "on" ]]; then
if [ -n "$ndctl" ] && [ -f "$ndctl/meson.build" ]; then
>&2 printf '\n%s\n\n' \
'WARNING: --ndctl-build ignored when updating existing image! Outdated ndctl in the image. Try adding "-r img"'
fi
fi
selftests_dir=$(readlink -f "$inst_prefix")/$selftests_home
if [[ $_arg_kern_selftests == "on" ]]; then
sudo make -j"$num_build_cpus" -C tools/testing/selftests install INSTALL_PATH="$selftests_dir"
else
sudo rm -rf "$selftests_dir"
fi
sudo -E bash $_trace_sh -c "$(declare -f setup_depmod); _arg_nfit_test=$_arg_nfit_test; _arg_cxl_test=$_arg_cxl_test; kver=$kver; setup_depmod $inst_prefix"
sudo -E bash $_trace_sh -c "$(declare -f setup_autorun); _arg_autorun=$_arg_autorun; setup_autorun $inst_prefix"
umount_rootfs 2
}
update_existing_rootfs()
{
if (( _arg_quiet >= 2 )); then
__update_existing_rootfs > /dev/null
else
__update_existing_rootfs
fi
}
enable_systemd_service()
{
servicename="$1"
if [ ! -d "mkosi.extra" ]; then
fail "couldn't find mkosi.extra, are we in an unexpected CWD?"
fi
mkdir -p mkosi.extra/etc/systemd/system/multi-user.target.wants
ln -sf "/usr/lib/systemd/system/${servicename}.service" \
"mkosi.extra/etc/systemd/system/multi-user.target.wants/${servicename}.service"
}
setup_network()
{
mkdir -p mkosi.extra/etc/systemd/network
cat <<- EOF > mkosi.extra/etc/systemd/network/20-wired.network
[Match]
Name=en*
[Network]
DHCP=yes
EOF
}
check_ndctl_dir()
{
if [ -n "$ndctl" ]; then
[ -f "$ndctl/meson.build" ] ||
fail 'ndctl="%s" is not a valid source directory\n' "$ndctl"
fi
}
prepare_ndctl_build()
{
cp "${script_dir}"/mkosi/extra/root/ndctl/reinstall.sh \
mkosi.extra/root/ndctl/
cat <<- 'EOF' > mkosi.postinst
# v14: 'systemd-nspawn"; v15: "mkosi"
printf 'container=%s\n' "$container"
# .postinst and others moved outside container in mkosi v15, see
# https://github.com/systemd/mkosi/commit/9b626c647037bc8a
if [ -n "$container" ]; then
/root/ndctl/reinstall.sh
else
# The magic, short-lived $SCRIPT variable is already deprecated
# and we don't need it.
mkosi-chroot /root/ndctl/reinstall.sh
fi
EOF
chmod +x mkosi.postinst
}
setup_gcp_tweaks()
{
mkdir -p mkosi.extra/etc/ssh/sshd_config.d/
cat <<- EOF > mkosi.extra/etc/ssh/sshd_config
Include /etc/ssh/sshd_config.d/*.conf