summaryrefslogtreecommitdiff
path: root/scripts/check-qemu-install
blob: 0929ea02702b31ad6e0951754b4ddfc5a6ac7db9 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# File: check-qemu-install
# Purpose:
#  This script installs a system on a emulated qemu host to verify
#  that the iso produced is installable and boots.
#  after the iso is booted from disk it also tries to execute the
#  vyos-smoketest script to verify checks there.
#
#  For now it will not fail on failed smoketest but will fail on
#  install and boot errors.
#  Arguments:
#    iso            iso image to install
#    [disk]         disk filename to use, if none is provided it
#                   is autogenerated
#    [--keep]       Keep the disk image after completion
#    [--logfile]    name of logfile to save, defaulting to stdout
#    [--silent]     only print on errors
#    [--debug]      print all communication with the device

import sys
import os
import time
import argparse
import subprocess
import random
import traceback
import logging
import re
import shutil
import json
import platform

from io import BytesIO
from datetime import datetime
from glob import glob

import tomli
import pexpect

EXCEPTION = 0
DISK_IMAGE_EXTENSION = '.raw'

# Nested installer-ISO QEMU test: mkisofs payload (see --nested-installer-iso-test)
NESTED_ISO_DATA_DIR = 'nested_iso_data'
NESTED_INSTALLER_PAYLOAD_ISO = 'nested_installer_payload.iso'
NESTED_INNER_ISO_NAME = 'vyos-installer.iso'

# Local-only HTTP nested-ISO upgrade test: default VRF (main RIB) then named VRF purple
NESTED_HTTP_SERV_PORT = 18088
NESTED_HTTP_IMAGE_NAME = 'test-image-update'
NESTED_HTTP_VRF_NAME = 'purple'
NESTED_HTTP_VRF_DUMMY = 'dum8000'
NESTED_HTTP_VRF_TABLE = '42042'
NESTED_HTTP_VRF_ADDR = '198.51.100.99'  # RFC 5737 TEST-NET-3: bind address for http.server inside VRF
NESTED_HTTP_MOUNT_POINT = '/mnt/nested_installer_iso'

# Cloud-Init data
CI_CONFIG_ARGS = {
    'hostname': 'vyos-CLOUD-INIT',
    'eth0_ipv4': '192.0.2.1/25',
    'eth0_ipv6': '2001:db8::1/64',
    'default_nexthop' : '192.0.2.126',
    'default_nexthop6' : '2001:db8::ffff',
    }

tpm_folder = '/tmp/vyos_tpm_test'
tpm_sock = f'{tpm_folder}/swtpm-sock'
qemu_name = 'VyOS-QEMU'

test_timeout = 5 *3600 # 5 hours (in seconds) to complete individual testcases

op_mode_prompt = r'vyos@vyos:~\$'
cfg_mode_prompt = r'vyos@vyos#'
default_user = 'vyos'
default_password = 'vyos'

# getch.py
KEY_F2 = chr(27) + chr(91) + chr(49) + chr(50) + chr(126)
KEY_F10 = chr(27) + chr(91) + chr(50) + chr(49) + chr(126)
KEY_UP = chr(27) + chr(91) + chr(65)
KEY_DOWN = chr(27) + chr(91) + chr(66)
KEY_SPACE = chr(32)
KEY_RETURN = chr(13)
KEY_ESC = chr(27)
KEY_Y = chr(121)

mok_password = '1234'

# Map QEMU arch
QEMU_CONFIG = {
    'amd64': {
        'bin' : 'qemu-system-x86_64',
        'machine' : 'pc',
        'bios' : '/usr/share/OVMF/OVMF_CODE.fd',
    },
    "arm64": {
        'bin': 'qemu-system-aarch64',
        'machine': 'virt,highmem=on',
        'bios': '/usr/share/qemu-efi-aarch64/QEMU_EFI.fd',
    },
}

parser = argparse.ArgumentParser(description='Install and start a test VyOS vm.')
parser.add_argument('--iso', help='ISO file to install')
parser.add_argument('--disk', help='name of disk image file', nargs='?')
parser.add_argument('--keep', help='Do not remove disk-image after installation',
                              action='store_true', default=False)
parser.add_argument('--silent', help='Do not show output on stdout unless an error has occurred',
                              action='store_true', default=False)
parser.add_argument('--debug', help='Send all debug output to stdout',
                               action='store_true', default=False)
parser.add_argument('--logfile', help='Log to file')
parser.add_argument('--match', help='Smoketests to run')
parser.add_argument('--uefi', help='Boot using UEFI', action='store_true', default=False)
parser.add_argument('--vnc', help='Enable VNC', action='store_true', default=False)
parser.add_argument('--raid', help='Perform a RAID-1 install', action='store_true', default=False)
parser.add_argument('--no-interfaces', help='Execute testsuite without interface tests to save time',
                action='store_true', default=False)
parser.add_argument('--smoketest', help='Execute script based CLI smoketests',
                action='store_true', default=False)
parser.add_argument('--configtest', help='Execute load/commit config tests',
				action='store_true', default=False)
parser.add_argument('--tpmtest', help='Execute TPM encrypted config tests',
                action='store_true', default=False)
parser.add_argument('--sbtest', help='Execute Secure Boot tests',
                action='store_true', default=False)
parser.add_argument('--cloud-init', help='Execute cloud-init tests',
                action='store_true', default=False)
parser.add_argument('--test-image-update', help='Test ISO image update in default VRF and custom VRF',
                action='store_true', default=False)
parser.add_argument('--qemu-cmd', help='Only generate QEMU launch command',
                action='store_true', default=False)
parser.add_argument('--cpu', help='Set QEMU CPU', type=int, default=2)
parser.add_argument('--cpu-type', help='Set QEMU CPU type', type=str, default='host')
parser.add_argument('--memory', help='Set QEMU memory', type=int, default=4)
parser.add_argument('--vyconf', help='Execute testsuite with vyconfd', action='store_true',
                    default=False)
parser.add_argument('--no-vpp', help='Execute testsuite without VPP tests',
                action='store_true', default=False)
parser.add_argument('--huge-page-size', help='Huge page size (e.g., 2M, 1G)', type=str)
parser.add_argument('--huge-page-count', help='Number of huge pages to allocate', type=int)
parser.add_argument('--isolate-cpus', help='CPU cores to isolate (e.g., 1,3-4', type=str)

args = parser.parse_args()

if os.geteuid() != 0:
    exit('You need to have root privileges to run this script.')

if args.cloud_init:
    hostname = CI_CONFIG_ARGS['hostname']
    op_mode_prompt = rf'vyos@{hostname}:~\$'
    cfg_mode_prompt = rf'vyos@{hostname}#'

# This is what we requested the build to contain
with open('data/defaults.toml', 'rb') as f:
    vyos_defaults = tomli.load(f)

# This is what we got from the build
manifest_file = 'build/manifest.json'
if os.path.isfile(manifest_file):
    with open('build/manifest.json', 'rb') as f:
        manifest = json.load(f)

    vyos_version = manifest['build_config']['version']
    vyos_codename = manifest['build_config']['release_train']

class StreamToLogger(object):
    """
    Fake file-like stream object that redirects writes to a logger instance.
    """
    def __init__(self, logger, log_level=logging.INFO):
        self.logger = logger
        self.log_level = log_level
        self.linebuf = b''
        self.ansi_escape = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]')

    def write(self, buf):
        self.linebuf += buf
        while b'\n' in self.linebuf:
            f = self.linebuf.split(b'\n', 1)
            if len(f) == 2:
                self.logger.debug(self.ansi_escape.sub('', f[0].decode(errors="replace").rstrip()))
                self.linebuf = f[1]

    def flush(self):
        pass

class EarlyExit(Exception):
    pass

def _kvm_exists():
    return os.path.exists("/dev/kvm")

def get_qemu_cmd(name, enable_uefi, disk_img, raid=None, iso_img=None, tpm=False,
                 vnc_enabled=False, secure_boot=False, nested_cdrom_iso=None):
    uefi = ""
    uuid = "f48b60b2-e6ad-49ef-9d09-4245d0585e52"
    accel = ',accel=kvm' if _kvm_exists() else ''

    if platform.machine() in ['amd64', 'x86_64']:
        architecture = 'amd64'
    elif platform.machine() in ['arm64', 'aarch64']:
        architecture = 'arm64'
    else:
        raise ValueError('Unsupported architecture!')

    qemu_arch_config = QEMU_CONFIG[architecture]
    qemu_bin = qemu_arch_config['bin']
    bios = qemu_arch_config['bios']

    cpu_type = args.cpu_type
    enable_kvm = '-enable-kvm' if _kvm_exists() else ''
    machine = qemu_arch_config['machine']
    vga = '-vga none'
    vnc = ''
    if vnc_enabled:
        vga = '-vga virtio'
        vnc = '-vnc :0'

    if enable_uefi:
        uefi = f'-bios {bios}'
        name = f'{name}-UEFI'

        if secure_boot:
            name = f'{name}-SECURE-BOOT'
            machine = 'q35,smm=on'

            uefi = f'-drive "if=pflash,unit=0,format=raw,readonly=on,file={OVMF_CODE}" ' \
                   f'-drive "if=pflash,unit=1,format=raw,file={OVMF_VARS_TMP}"'
            # Changing UEFI settings require a display
            vga = '-vga virtio'

    cdrom = ""
    nested_cdrom = ""
    if iso_img:
        bootindex = '10'
        cdrom = f' -drive file={iso_img},format=raw,if=none,media=cdrom,id=drive-cd1,readonly=on'
        if architecture == 'arm64':
            cdrom = f' {cdrom} -device scsi-cd,bus=scsi0.0,drive=drive-cd1,id=cd1,bootindex={bootindex}'
        else:
            cdrom = f' {cdrom} -device ahci,id=achi0 -device ide-cd,bus=achi0.0,drive=drive-cd1,id=cd1,bootindex={bootindex}'

    if nested_cdrom_iso:
        drive_settings = 'drive=drive-cd2,id=cd2,bootindex=11'
        nested_cdrom = f' -drive file={nested_cdrom_iso},format=raw,if=none,media=cdrom,id=drive-cd2,readonly=on'
        if architecture == 'arm64':
            nested_cdrom = f' {nested_cdrom} -device scsi-cd,bus=scsi0.0,{drive_settings}'
        else:
            if not iso_img:
                # Second IDE CD on its own AHCI controller (no installer CD in this command)
                nested_cdrom = f'{nested_cdrom} -device ahci,id=nestedahci' \
                               f' -device ide-cd,bus=nestedahci.0,{drive_settings}'
            else:
                nested_cdrom = f'{nested_cdrom} -device ide-cd,bus=achi0.1,{drive_settings}'

    # RFC7042 section 2.1.2 MAC addresses used for documentation
    macbase = '00:00:5E:00:53'

    # Set QEmu disk image format - this differs if VyOS was installed via smoketest
    # or we use an already ewxisting image
    disk_format = 'qcow2' if args.disk.endswith('.qcow2') else 'raw'
    cmd = f'{qemu_bin} \
        -name "{name}" \
        -smp {args.cpu},sockets=1,cores={args.cpu},threads=1 \
        -cpu {cpu_type} \
        -machine {machine}{accel} \
        {uefi} \
        -m {args.memory}G \
        -nographic \
        {vga} {vnc}\
        -uuid {uuid} \
        {enable_kvm} \
        -monitor unix:/tmp/qemu-monitor-socket-{disk_img},server,nowait \
        -netdev user,id=n0,net=192.0.2.0/24,dhcpstart=192.0.2.101,dns=192.0.2.10 -device virtio-net-pci,netdev=n0,mac={macbase}:00,romfile="",host_mtu=1500 \
        -netdev user,id=n1 -device virtio-net-pci,netdev=n1,mac={macbase}:01,romfile="",host_mtu=1500 \
        -netdev user,id=n2 -device virtio-net-pci,netdev=n2,mac={macbase}:02,romfile="",host_mtu=1500 \
        -netdev user,id=n3 -device virtio-net-pci,netdev=n3,mac={macbase}:03,romfile="",host_mtu=1500 \
        -netdev user,id=n4 -device virtio-net-pci,netdev=n4,mac={macbase}:04,romfile="" \
        -netdev user,id=n5 -device virtio-net-pci,netdev=n5,mac={macbase}:05,romfile="" \
        -netdev user,id=n6 -device virtio-net-pci,netdev=n6,mac={macbase}:06,romfile="" \
        -netdev user,id=n7 -device virtio-net-pci,netdev=n7,mac={macbase}:07,romfile="" \
        -device virtio-scsi-pci,id=scsi0 \
        {cdrom}{nested_cdrom} \
        -drive format={disk_format},file={disk_img},if=none,media=disk,id=drive-hd1,readonly=off \
        -device scsi-hd,bus=scsi0.0,drive=drive-hd1,id=hd1,bootindex=1'

    if raid:
        cmd += f' -drive format={disk_format},file={raid},if=none,media=disk,id=drive-hd2,readonly=off' \
               f' -device scsi-hd,bus=scsi0.0,drive=drive-hd2,id=hd2,bootindex=2'

    if tpm:
        cmd += f' -chardev socket,id=chrtpm,path={tpm_sock}' \
               ' -tpmdev emulator,id=tpm0,chardev=chrtpm' \
               ' -device tpm-tis,tpmdev=tpm0'

    return cmd

def shutdownVM(c, log, message=''):
    #################################################
    # Powering off system
    #################################################
    if message:
        log.info(message)

    c.sendline('poweroff now')
    log.info('Shutting down virtual machine')
    for i in range(30):
        log.info('Waiting for shutdown...')
        # Shutdown in qemu doesnt work first time
        # Use this workaround
        # https://vyos.dev/T5024
        c.sendline('poweroff now')
        if not c.isalive():
            log.info('VM is shut down!')
            break
        time.sleep(10)
    else:
        tmp = 'VM Did not shut down after 300sec'
        log.error(tmp)
        raise Exception(tmp)
    c.close()

def waitForLogin(child, log, timeout=20) -> None:
    try:
        child.expect('The highlighted entry will be executed automatically in',
                     timeout=timeout)
        child.sendline('')
    except pexpect.TIMEOUT:
        log.warning('Did not find GRUB countdown window, ignoring')

def loginVM(c, log):
    log.info('Waiting for login prompt')
    c.expect('[Ll]ogin:', timeout=600)
    c.sendline(default_user)
    c.expect('[Pp]assword:')
    c.sendline(default_password)
    c.expect(op_mode_prompt)
    log.info('Logged in!')
    c.sendline('set terminal width 160')
    c.expect(op_mode_prompt)
    c.sendline('set terminal length 60')
    c.expect(op_mode_prompt)

def clearTPM():
    for f in glob(f'{tpm_folder}/*'):
        os.remove(f)

# Setting up logger
log = logging.getLogger()
log.setLevel(logging.DEBUG)

stl = StreamToLogger(log)
formatter = logging.Formatter('%(levelname)5s - %(message)s')

handler = logging.StreamHandler(sys.stdout)
if args.silent:
    handler.setLevel(logging.ERROR)
elif args.debug:
    handler.setLevel(logging.DEBUG)
else:
    handler.setLevel(logging.INFO)

handler.setFormatter(formatter)
log.addHandler(handler)

# Mutually exclusive primary testcase selectors (--no-interfaces is a
# smoketest modifier).
_primary_modes = []
if args.cloud_init:
    _primary_modes.append('--cloud-init')
if args.test_image_update:
    _primary_modes.append('--test-image-update')
if args.tpmtest:
    _primary_modes.append('--tpmtest')
if args.raid:
    _primary_modes.append('--raid')
if args.smoketest:
    _primary_modes.append('--smoketest')
if args.configtest:
    _primary_modes.append('--configtest')
if args.sbtest:
    _primary_modes.append('--sbtest')
if len(_primary_modes) > 1:
    log.error('Incompatible combination of testcase flags (%s): only one of '
        '--cloud-init, --test-image-update, --tpmtest, --raid, --smoketest, '
        '--configtest, --sbtest may be set.', ', '.join(_primary_modes))
    sys.exit(1)

if args.no_interfaces and not args.smoketest:
    log.error('--no-interfaces requires --smoketest')
    sys.exit(1)

if args.sbtest and not args.uefi:
    log.error('--sbtest requires --uefi')
    sys.exit(1)

if args.logfile:
    filehandler = logging.FileHandler(args.logfile)
    filehandler.setLevel(logging.DEBUG)
    filehandler.setFormatter(formatter)
    log.addHandler(filehandler)

if args.silent:
    output = BytesIO()
else:
    output = sys.stdout.buffer

if not os.path.exists('/dev/kvm'):
    log.error('KVM not enabled on host, proceeding with software emulation')

if not args.iso and not args.disk:
    log.error('Neither ISO not QCOW2 disk image supplied - error!')
    sys.exit(1)

if not args.disk:
    tmp_disk_time = datetime.now().strftime('%Y%m%d-%H%M%S')
    tmp_disk_random = "%04x" % random.randint(0,65535)
    args.disk = f'testinstall-{tmp_disk_time}-{tmp_disk_random}{DISK_IMAGE_EXTENSION}'

if args.iso and not os.path.isfile(args.iso):
    log.error('Unable to find VyOS ISO image needed by testcases!')
    sys.exit(1)

if args.test_image_update and not args.iso:
    log.error('--test-image-update requires --iso file!')
    sys.exit(1)

OVMF_CODE = '/usr/share/OVMF/OVMF_CODE_4M.secboot.fd'
OVMF_VARS_TMP = args.disk.replace(DISK_IMAGE_EXTENSION, '.efivars')
if args.sbtest:
    shutil.copy('/usr/share/OVMF/OVMF_VARS_4M.ms.fd', OVMF_VARS_TMP)

# Creating diskimage!!
diskname_raid = None
def gen_disk(name):
    if not os.path.isfile(name):
        log.info(f'Creating Disk image {name}')
        c = subprocess.check_output(['qemu-img', 'create', name, '5G'])
        log.debug(c.decode())
    else:
        log.info(f'Re-using already existing disk image "{name}".')

if args.raid:
    filename, ext = os.path.splitext(args.disk)
    diskname_raid = f'{filename}_disk1{ext}'
    # change primary diskname, too
    args.disk = f'{filename}_disk0{ext}'
    gen_disk(diskname_raid)

# must be called after the raid disk as args.disk name is altered in the RAID path
gen_disk(args.disk)

nested_payload_iso_path = None
if args.test_image_update:
    if os.path.isdir(NESTED_ISO_DATA_DIR):
        shutil.rmtree(NESTED_ISO_DATA_DIR)
    os.makedirs(NESTED_ISO_DATA_DIR)
    shutil.copy2(args.iso, os.path.join(NESTED_ISO_DATA_DIR, NESTED_INNER_ISO_NAME))
    if os.path.isfile(NESTED_INSTALLER_PAYLOAD_ISO):
        os.unlink(NESTED_INSTALLER_PAYLOAD_ISO)
    log.info('Assembling nested installer payload ISO (second CD-ROM, drive-cd2)')
    subprocess.check_call([
        'mkisofs', '-joliet', '-rock', '-volid', 'VYOSNESTED',
        '-output', NESTED_INSTALLER_PAYLOAD_ISO, NESTED_ISO_DATA_DIR,
    ])
    nested_payload_iso_path = os.path.abspath(NESTED_INSTALLER_PAYLOAD_ISO)
    log.info('Removing nested ISO staging directory %s', NESTED_ISO_DATA_DIR)
    shutil.rmtree(NESTED_ISO_DATA_DIR)

# Create software emulated TPM - clear existing TPM data first - might be a
# leftover from a previous run
clearTPM()

def start_swtpm():
    if not os.path.exists(tpm_folder):
        os.mkdir(tpm_folder)

    def swtpm_thread():
        subprocess.check_output([
            'swtpm', 'socket', '--tpmstate', f'dir={tpm_folder}',
            '--ctrl', f'type=unixio,path={tpm_sock}', '--tpm2', '--log', 'level=1'
        ])

    from multiprocessing import Process
    tpm_process = Process(target=swtpm_thread)
    tpm_process.start()
    return tpm_process

def toggleUEFISecureBoot(c):
    def UEFIKeyPress(c, key):
        UEFI_SLEEP = 1
        c.send(key)
        time.sleep(UEFI_SLEEP)

    # Enter UEFI
    for ii in range(1, 10):
        c.send(KEY_F2)
        time.sleep(0.250)

    time.sleep(10)

    # Device Manager
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_RETURN)

    # Secure Boot Configuration
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_RETURN)

    # Attempt Secure Boot Toggle
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_RETURN)
    UEFIKeyPress(c, KEY_RETURN)

    # Save Secure Boot
    UEFIKeyPress(c, KEY_F10)
    UEFIKeyPress(c, KEY_Y)

    # Go Back to Menu
    UEFIKeyPress(c, KEY_ESC)
    UEFIKeyPress(c, KEY_ESC)

    # Go Down for reset
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_DOWN)
    UEFIKeyPress(c, KEY_RETURN)

def BOOTLOADERchooseSerialConsole(child, live: bool) -> None:
    """ Select GRUB boot entry that uses the serial console. This differs
        between a LIVE ISO image and an already installed system. """
    BOOTLOADER_TMO = 40
    BOOTLOADER_SLEEP = 1.5
    BOOTLOADER_LOAD_TMO = 5 # let GRUB screen load

    UEFI_STRING = r'BdsDxe.*'
    GRUB_STRING = r'GNU GRUB.*'
    ISOLINUX_STRING = r'ISOLINUX.*'

    # XXX: UEFI systems directly load GRUB, BIOS systems fallback to ISOLINUX
    if live and args.uefi:
        # Let UEFI start before GRUB
        child.expect(UEFI_STRING, timeout=BOOTLOADER_TMO)
        # Wait for GRUB
        child.expect(GRUB_STRING, timeout=BOOTLOADER_TMO)
        time.sleep(BOOTLOADER_LOAD_TMO)

        # Select GRUB serial console
        # Key DOWN -> fail-safe mode
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        # Key DOWN -> Serial Console boot
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        # Boot
        child.send(KEY_RETURN)
    elif live and not args.uefi:
        # Wait for ISOLINUX
        child.expect(ISOLINUX_STRING, timeout=BOOTLOADER_TMO)
        time.sleep(BOOTLOADER_LOAD_TMO)

        # Boot Menu starts at Live system (vyos) - KVM console
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        # Live system (vyos fail-safe mode)
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        # Live system (vyos) - Serial console - boot it up
        child.send(KEY_RETURN)
    else:
        # Let UEFI start before GRUB
        if args.uefi:
            child.expect(UEFI_STRING, timeout=BOOTLOADER_TMO)

        # Wait for GRUB
        child.expect(GRUB_STRING, timeout=BOOTLOADER_TMO)
        time.sleep(BOOTLOADER_LOAD_TMO)

        # Select GRUB serial console
        # Boot options
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        child.send(KEY_RETURN)
        time.sleep(BOOTLOADER_SLEEP)
        # Select console type
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        child.send(KEY_RETURN)
        time.sleep(BOOTLOADER_SLEEP)
        # *ttyS (serial)
        child.send(KEY_DOWN)
        time.sleep(BOOTLOADER_SLEEP)
        child.send(KEY_RETURN)
        time.sleep(BOOTLOADER_SLEEP)
        # Boot
        child.send(KEY_RETURN)

    return None

def basic_cli_tests(c):
    # Repeating / shared basic CLI test between installed images and the
    # cloud-init test-case
    c.sendline('configure')
    c.expect(cfg_mode_prompt)
    c.sendline('exit')
    c.expect(op_mode_prompt)
    c.sendline('show version')
    c.expect(op_mode_prompt)
    c.sendline('show version kernel')
    c.expect(f'{vyos_defaults["kernel_version"]}-{vyos_defaults["kernel_flavor"]}')
    c.expect(op_mode_prompt)
    c.sendline('show version frr')
    c.expect(op_mode_prompt)
    c.sendline('show interfaces')
    c.expect(op_mode_prompt)
    # c.sendline('systemd-detect-virt')
    # c.expect('kvm')
    # c.expect(op_mode_prompt)
    c.sendline('show system cpu')
    c.expect(op_mode_prompt)
    c.sendline('show system memory')
    c.expect(op_mode_prompt)
    c.sendline('show system memory detail | no-more')
    c.expect(op_mode_prompt)
    c.sendline('show configuration commands | match "option kernel"')
    c.expect(op_mode_prompt)
    c.sendline('cat /proc/cmdline')
    c.expect(op_mode_prompt)
    c.sendline('show version all | grep -e "vpp" -e "vyos-1x"')
    c.expect(op_mode_prompt)

    # Get serial console interface name from flavor definition
    flavor_json = '/usr/share/vyos/flavor.json'
    c.sendline(f'jq -r ".console_type" {flavor_json}')
    c.expect(op_mode_prompt)
    lines = [l.strip() for l in c.before.splitlines() if l.strip()]
    console_type = lines[-1].decode('utf-8').strip() # e.g. ttyS

    # We do only care for ttyS or ttyAMA console types, exit early
    # for VGA consoles
    if console_type == 'tty':
        return

    c.sendline(f'jq -r ".console_num" {flavor_json}')
    c.expect(op_mode_prompt)
    lines = [l.strip() for l in c.before.splitlines() if l.strip()]
    console_num = lines[-1].decode('utf-8').strip() # e.g. 0

    # Get serial console speed from flavor definition
    c.sendline(f'jq -r ".console_speed" {flavor_json}')
    c.expect(op_mode_prompt)
    lines = [l.strip() for l in c.before.splitlines() if l.strip()]
    console_speed = lines[-1].decode('utf-8').strip() # e.g. 115200

    # Ensure serial console with kernel option is set in CLI, done automatically by image installer
    c.sendline('show configuration commands | match "system console"')
    c.expect(rf'set system console device {console_type}{console_num} kernel\r\r\nset system console device {console_type}{console_num} speed \'{console_speed}\'')
    c.expect(op_mode_prompt)

    # Validate GRUB has the right console_type defined
    c.sendline('cat /boot/grub/grub.cfg.d/20-vyos-defaults-autoload.cfg | grep "set console_type"')
    c.expect(f'set console_type="{console_type}"')
    c.expect(op_mode_prompt)

def _image_update_cli_sequence(c, log, new_image_name, server_bind_host='127.0.0.1', use_vrf=False):
    """One add-system-image/delete cycle for nested ISO over HTTP (optional Linux VRF + VyOS vrf arg)."""
    url = f'http://{server_bind_host}:{NESTED_HTTP_SERV_PORT}/{NESTED_INNER_ISO_NAME}'

    vrf_exec = ''
    if use_vrf:
        log.info(f'Configure VRF ({NESTED_HTTP_VRF_NAME}) for HTTP server - used for image update ')
        vrf_exec = f'ip vrf exec {NESTED_HTTP_VRF_NAME} '
        c.sendline('configure')
        c.expect(cfg_mode_prompt)
        c.sendline(f'set vrf name {NESTED_HTTP_VRF_NAME} table {NESTED_HTTP_VRF_TABLE}')
        c.expect(cfg_mode_prompt)
        c.sendline(f'set interfaces dummy {NESTED_HTTP_VRF_DUMMY} address {server_bind_host}/32')
        c.expect(cfg_mode_prompt)
        c.sendline(f'set interfaces dummy {NESTED_HTTP_VRF_DUMMY} vrf {NESTED_HTTP_VRF_NAME}')
        c.expect(cfg_mode_prompt)
        c.sendline('commit')
        c.expect(cfg_mode_prompt)
        c.sendline('exit')
        c.expect(op_mode_prompt)

    c.sendline(
        f'sudo bash -c \'{vrf_exec}python3 -m http.server {NESTED_HTTP_SERV_PORT} --bind {server_bind_host} '
        f'--directory {NESTED_HTTP_MOUNT_POINT} </dev/null >/tmp/nested_http.log 2>&1 &\''
    )
    c.expect(op_mode_prompt)

    time.sleep(5) # Wait for HTTP server to start

    update_cmd_cli = f'TERM=dumb add system image {url}'
    if use_vrf:
        update_cmd_cli = f'{update_cmd_cli} vrf {NESTED_HTTP_VRF_NAME}'
    c.sendline(update_cmd_cli)

    timeout = 600
    deadline = time.time() + timeout
    while True:
        if time.time() > deadline:
            raise Exception(f'add system image timed out after {timeout}s')
        i = c.expect([
            'What would you like to name this image',
            'Would you like to set the new image as the default one for boot',
            'An active configuration was found. Would you like to copy it to the new image',
            'Would you like to copy SSH host keys',
            'Would you like to save the SSH known hosts (fingerprints)',
            'Signature is not available. Do you want to continue with installation',
            'There are unsaved changes to the configuration',
            'Would you like to continue',
            'Unable to',
            'Error:',
            op_mode_prompt,
        ], timeout=300)
        if i == 0:
            c.sendline(new_image_name)
        elif i == 1:
            c.sendline('n')
        elif i == 2:
            c.sendline('y')
        elif i == 3:
            c.sendline('y')
        elif i == 4:
            c.sendline('y')
        elif i == 5:
            c.sendline('y')
        elif i == 6:
            c.sendline('y')
        elif i == 7:
            c.sendline('y')
        elif i == 8 or i == 9:
            raise Exception('add system image reported an error')
        elif i == 10:
            log.info('add system image completed')
            break

    c.sendline('show system image')
    c.expect(new_image_name)
    c.expect(op_mode_prompt)

    c.sendline(f'TERM=dumb delete system image {new_image_name}')
    deadline = time.time() + timeout
    while time.time() < deadline:
        j = c.expect([
            'Are you sure you want to delete',
            'Do you really want to delete the image',
            'Cannot ',
            'Error:',
            'Unable to ',
            op_mode_prompt,
        ], timeout=300)
        if j == 0 or j == 1:
            c.sendline('y')
        elif j == 2 or j == 3 or j == 4:
            raise Exception('delete system image failed')
        elif j == 5:
            break
    else:
        raise Exception('delete system image timed out')

    c.sendline('show system image')
    c.expect(op_mode_prompt)
    if new_image_name in c.before.decode(errors='replace'):
        raise Exception(f'Image {new_image_name!r} still listed after delete')

    c.sendline(f'sudo fuser -k {NESTED_HTTP_SERV_PORT}/tcp 2>/dev/null || true')
    c.expect(op_mode_prompt)

if args.qemu_cmd:
    tmp = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid,
                       iso_img=args.iso, vnc_enabled=args.vnc, secure_boot=args.sbtest,
                       nested_cdrom_iso=nested_payload_iso_path)
    os.system(tmp)
    exit(0)

if args.cloud_init:
    # Build cloud-init seed iso
    CI_ISO = 'ci_seed.iso'
    CI_DATA_DIR = 'ci_data'
    # Insert cloud-init ISO into CD-ROM
    args.iso = CI_ISO

    if not os.path.exists(CI_DATA_DIR):
        os.makedirs(CI_DATA_DIR)

    # create "empty" meta-data file
    open(f'{CI_DATA_DIR}/meta-data', mode='w').close()

    network_config = """version: 2
ethernets:
  eth0:
    dhcp4: false
    dhcp6: false
"""

    user_data = """#cloud-config
vyos_config_commands:
  - set system host-name '{hostname}'
  - set service ntp server 1.pool.ntp.org
  - set service ntp server 2.pool.ntp.org
  - delete interfaces ethernet eth0 address 'dhcp'
  - set interfaces ethernet eth0 address '{eth0_ipv4}'
  - set interfaces ethernet eth0 address '{eth0_ipv6}'
  - set protocols static route 0.0.0.0/0 next-hop '{default_nexthop}'
  - set protocols static route6 ::/0 next-hop '{default_nexthop6}'
""".format(**CI_CONFIG_ARGS)

    with open(f'{CI_DATA_DIR}/network-config', mode='w') as f:
        f.writelines(network_config)

    with open(f'{CI_DATA_DIR}/user-data', mode='w') as f:
        f.writelines(user_data)

    # Assemble cloud-init ISO file
    if os.path.exists(CI_ISO):
        os.unlink(CI_ISO)
    os.system(f'mkisofs -joliet -rock -volid "cidata" -output {CI_ISO} {CI_DATA_DIR}')

tpm_process = None
try:
    # Start TPM emulator
    if args.tpmtest:
        tpm_process = start_swtpm()

    #################################################
    # Installing image to disk
    #################################################
    log.info('Installing system')
    cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid,
                       tpm=args.tpmtest, iso_img=args.iso, vnc_enabled=args.vnc,
                       secure_boot=args.sbtest, nested_cdrom_iso=nested_payload_iso_path)
    log.debug(f'Executing command: {cmd}')
    c = pexpect.spawn(cmd, logfile=stl, timeout=60)

    #################################################
    # Logging into VyOS system
    #################################################
    if args.sbtest:
        log.info('Disable UEFI Secure Boot for initial installation')
        toggleUEFISecureBoot(c)

    BOOTLOADERchooseSerialConsole(c, live=(not args.cloud_init))
    loginVM(c, log)

    #################################################
    # Cloud-Init comes with a pre-assembled ISO - just boot and test it
    #################################################
    if args.cloud_init:
        log.info('Perform basic CLI configuration mode tests from cloud-init...')
        basic_cli_tests(c)

        # Valida assigned IPv4 and IPv6 addresses
        c.sendline('ip --json addr show dev eth0 | jq -r \'.[0].addr_info[] | select(.family=="inet") | "\(.local)/\(.prefixlen)"\'')
        c.expect(CI_CONFIG_ARGS['eth0_ipv4'])
        c.expect(op_mode_prompt)

        c.sendline('ip --json addr show dev eth0 | jq -r \'.[0].addr_info[] | select(.family=="inet6" and .scope=="global" and (.temporary|not)) | "\(.local)/\(.prefixlen)"\'')
        c.expect(CI_CONFIG_ARGS['eth0_ipv6'])
        c.expect(op_mode_prompt)

        # Valida default route nexthops
        c.sendline('ip -4 --json route show default | jq -r ".[0].gateway"')
        c.expect(CI_CONFIG_ARGS['default_nexthop'])
        c.expect(op_mode_prompt)

        c.sendline('ip -6 --json route show default | jq -r ".[0].gateway"')
        c.expect(CI_CONFIG_ARGS['default_nexthop6'])
        c.expect(op_mode_prompt)

        shutdownVM(c, log, 'Powering off system')
        c.close()
        raise EarlyExit

    #################################################
    # Check for no private key contents within the image
    #################################################
    msg = 'Found private key - bailing out'
    c.sendline(f'if sudo grep -rq "BEGIN PRIVATE KEY" /var/lib/shim-signed/mok; then echo {msg}; exit 1; fi')
    tmp = c.expect([f'\n{msg}', op_mode_prompt])
    if tmp == 0:
        log.error(msg)
        exit(1)

    #################################################
    # Configure boot options if required
    #################################################
    if args.huge_page_size and args.huge_page_count:
        c.sendline('configure')
        c.expect(cfg_mode_prompt)
        c.sendline(f'set system option kernel memory hugepage-size {args.huge_page_size} hugepage-count {args.huge_page_count}')
        c.expect(cfg_mode_prompt)
        if args.isolate_cpus:
            c.sendline(f'set system option kernel cpu isolate-cpus {args.isolate_cpus}')
            c.expect(cfg_mode_prompt)
        c.sendline('set system option kernel disable-mitigations')
        c.expect(cfg_mode_prompt)
        c.sendline('commit')
        c.expect(cfg_mode_prompt)
        c.sendline('save')
        c.expect(cfg_mode_prompt)
        c.sendline('exit')
        c.expect(op_mode_prompt)

    #################################################
    # Installing into VyOS system
    #################################################
    log.info('Starting installer')
    c.sendline('install image')
    c.expect('\nWould you like to continue?.*')
    c.sendline('y')
    c.expect('\nWhat would you like to name this image?.*')
    c.sendline('')
    c.expect(f'\nPlease enter a password for the "{default_user}" user.*')
    c.sendline('vyos')
    c.expect(f'\nPlease confirm password for the "{default_user}" user.*')
    c.sendline('vyos')
    c.expect('\nWhat console should be used by default?.*')
    c.sendline('S')

    if args.raid:
        c.expect('\nWould you like to configure RAID-1 mirroring??.*')
        c.sendline('y')
        c.expect('\nWould you like to configure RAID-1 mirroring on them?.*')
        c.sendline('y')
        c.expect('\nInstallation will delete all data on both drives. Continue?.*')
        c.sendline('y')
        c.expect('\nWhich file would you like as boot config?.*')
        c.sendline('')
    else:
        c.expect('\nWhich one should be used for installation?.*')
        c.sendline('')
        c.expect('\nInstallation will delete all data on the drive. Continue?.*')
        c.sendline('y')
        c.expect('\nWould you like to use all the free space on the drive?.*')
        c.sendline('y')
        c.expect('\nWhich file would you like as boot config?.*')
        c.sendline('')

    c.expect(op_mode_prompt)

    if args.sbtest:
        c.sendline('install mok')
        c.expect('input password:.*')
        c.sendline(mok_password)
        c.expect('input password again:.*')
        c.sendline(mok_password)
        c.expect(op_mode_prompt)

    log.info('system installed, rebooting')
    c.sendline('reboot now')

    #################################################
    # SHIM Mok Manager
    #################################################
    if args.sbtest:
        log.info('Install Secure Boot Machine Owner Key')
        MOK_SLEEP = 0.5
        c.expect('BdsDxe: starting Boot00.*')
        time.sleep(3)
        # press any key
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

        # Enroll MOK
        c.send(KEY_DOWN)
        time.sleep(MOK_SLEEP)
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

        # Continue
        c.send(KEY_DOWN)
        time.sleep(MOK_SLEEP)
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

        # Enroll Keys
        c.send(KEY_DOWN)
        time.sleep(MOK_SLEEP)
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

        c.sendline(mok_password)
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

        # Reboot
        c.send(KEY_RETURN)
        time.sleep(MOK_SLEEP)

    #################################################
    # Re-Enable Secure Boot
    #################################################
    if args.sbtest:
        log.info('Enable UEFI Secure Boot for initial installation')
        toggleUEFISecureBoot(c)

    #################################################
    # Removing CD installation media
    #################################################
    time.sleep(2)
    log.info('eject installation media')
    os.system(f'echo "eject -f drive-cd1" | socat - unix-connect:/tmp/qemu-monitor-socket-{args.disk}')

    #################################################
    # Booting installed system
    #################################################
    log.info('Booting installed system')
    BOOTLOADERchooseSerialConsole(c, live=False)

    #################################################
    # Logging into VyOS system
    #################################################
    waitForLogin(c, log)
    loginVM(c, log)

    #################################################
    # Boot options require a reboot
    #################################################
    if args.huge_page_size and args.huge_page_count:
        log.info('Rebooting to apply kernel boot options')
        c.sendline('reboot now')
        loginVM(c, log)

    ################################################
    # Always load the WiFi simulation module
    ################################################
    c.sendline('sudo modprobe mac80211_hwsim')
    c.expect(op_mode_prompt)

    # Inform smoketest about this environment
    c.sendline('touch /tmp/vyos.smoketests.hint')
    c.expect(op_mode_prompt)

    #################################################
    # Basic Configmode/Opmode switch
    #################################################
    log.info('Basic CLI configuration mode test')
    basic_cli_tests(c)

    #################################################
    # Verify /etc/os-release via lsb_release
    #################################################
    c.sendline('lsb_release --short --id 2>/dev/null')
    c.expect('VyOS')
    if os.path.isfile(manifest_file):
        c.sendline('lsb_release --short --release 2>/dev/null')
        c.expect(vyos_version)
        c.sendline('lsb_release --short --codename 2>/dev/null')
        c.expect(vyos_codename)

    # Ensure ephemeral key is loaded
    vyos_kernel_key = 'VyOS build time autogenerated kernel key'
    c.sendline(f'show log kernel | match "{vyos_kernel_key}"')
    c.expect(f'.*{vyos_kernel_key}.*')
    c.expect(op_mode_prompt)

    #################################################
    # Executing test-suite
    #################################################
    if args.tpmtest:
        def verify_mount():
            # Verify encrypted and mounted
            c.sendline('mount | grep vyos_config')
            c.expect('/dev/mapper/vyos_config on /opt/vyatta/etc/config.*')
            c.expect(op_mode_prompt)

        def verify_config():
            # Verify encrypted config is loaded
            c.sendline('show config commands | cat')
            c.expect('set system option performance \'network-latency\'')
            c.expect('set system option reboot-on-panic')
            c.expect(op_mode_prompt)

        log.info('Running TPM encrypted config tests')
        tpm_timeout = 600 # Give it 10 mins to encrypt

        # Verify TPM is loaded
        c.sendline('find /dev -name tpm0')
        c.expect('/dev/tpm0')
        c.expect(op_mode_prompt)

        # Create recovery key
        import string
        from random import choices
        test_recovery_key = ''.join(choices(string.ascii_uppercase + string.digits, k=32))

        log.info('Encrypting config to TPM')
        c.sendline('encryption enable')
        c.expect('Automatically generate a recovery key\?.*')
        c.sendline('n')
        c.expect('Enter recovery key: ')
        c.sendline(test_recovery_key)
        c.expect('Enter size of encrypted config partition.*', timeout=30)
        c.sendline('32')
        c.expect('Encrypted config volume has been enabled', timeout=tpm_timeout)
        c.expect('Backup the recovery key in a safe place!')
        c.expect(f'Recovery key: {test_recovery_key}')
        c.expect(op_mode_prompt)

        verify_mount()

        # Add some non-default config nodes this encrypted config
        log.info('Adding nodes for encrypted config test')
        c.sendline('configure')
        c.expect(cfg_mode_prompt)
        c.sendline('set system option performance network-latency')
        c.expect(cfg_mode_prompt)
        c.sendline('set system option reboot-on-panic')
        c.expect(cfg_mode_prompt)
        c.sendline('commit')
        c.expect(cfg_mode_prompt)
        c.sendline('save')
        c.expect(cfg_mode_prompt)
        c.sendline('exit')
        c.expect(op_mode_prompt)

        log.info('system installed, rebooting')
        c.sendline('reboot now')

        waitForLogin(c, log)

        # Check for vyos-router message
        c.expect('.*Mounted encrypted config volume', timeout=120)

        loginVM(c, log)

        verify_mount()
        verify_config()

        # Shutdown VM
        shutdownVM(c, log, 'Shutdown VM for TPM fail test')

        # Clear swtpm
        clearTPM()

        # Shutdown kills swtpm
        tpm_process.join()
        tpm_process.close()

        # Start emulator again
        tpm_process = start_swtpm()

        # Booting back into VM
        log.info('Booting system with cleared TPM')
        cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid, tpm=args.tpmtest, vnc_enabled=args.vnc)
        log.debug(f'Executing command: {cmd}')
        c = pexpect.spawn(cmd, logfile=stl)

        waitForLogin(c, log)

        c.expect('.*Encrypted config volume has not been mounted', timeout=120)

        loginVM(c, log)

        # Test loading config with recovery key
        c.sendline('encryption load')
        c.expect('Enter recovery key: ')
        c.sendline(test_recovery_key)
        c.expect('Encrypted config volume has been mounted', timeout=120)
        c.expect(op_mode_prompt)

        verify_mount()

        log.info('Loading encrypted config.boot')
        c.sendline('configure')
        c.expect(cfg_mode_prompt)
        c.sendline('load /opt/vyatta/etc/config/config.boot')
        c.expect(cfg_mode_prompt)
        c.sendline('commit')
        c.expect(cfg_mode_prompt)
        c.sendline('exit')
        c.expect(op_mode_prompt)

        verify_config()

        # Shutdown VM
        shutdownVM(c, log, 'Shutdown VM for non-TPM backed test')
        clearTPM()

        # Shutdown kills swtpm
        tpm_process.join()
        tpm_process.close()
        tpm_process = None

        # Booting back into VM
        log.info('Booting system without TPM')
        cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid, tpm=False, vnc_enabled=args.vnc)
        log.debug(f'Executing command: {cmd}')
        c = pexpect.spawn(cmd, logfile=stl)

        waitForLogin(c, log)
        loginVM(c, log)

        # New recovery key
        test_recovery_key = ''.join(choices(string.ascii_uppercase + string.digits, k=32))

        log.info('Encrypting config')
        c.sendline('encryption enable')
        c.expect('Are you sure you want to proceed\?.*')
        c.sendline('y')
        c.expect('Enter key: ')
        c.sendline(test_recovery_key)
        c.expect('Confirm key:')
        c.sendline(test_recovery_key)
        c.expect('Enter size of encrypted config partition.*', timeout=30)
        c.sendline('32')
        c.expect('Encrypted config volume has been enabled', timeout=tpm_timeout)
        c.expect('Backup the key in a safe place!')
        c.expect(f'Key: {test_recovery_key}')
        c.expect(op_mode_prompt)

        verify_mount()

        shutdownVM(c, log, 'Shutdown VM for non-TPM config load')

        # Booting back into VM
        log.info('Booting system without TPM')
        cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid, tpm=False, vnc_enabled=args.vnc)
        log.debug(f'Executing command: {cmd}')
        c = pexpect.spawn(cmd, logfile=stl)

        waitForLogin(c, log)

        c.expect('.*encrypted config volume will not be automatically mounted', timeout=120)

        loginVM(c, log)

        # Test loading config with recovery key
        c.sendline('encryption load')
        c.expect('Enter key: ')
        c.sendline(test_recovery_key)
        c.expect('Encrypted config volume has been mounted', timeout=120)
        c.expect(op_mode_prompt)

        verify_mount()

        log.info('Loading encrypted config.boot')
        c.sendline('configure')
        c.expect(cfg_mode_prompt)
        c.sendline('load /opt/vyatta/etc/config/config.boot')
        c.expect(cfg_mode_prompt)
        c.sendline('commit')
        c.expect(cfg_mode_prompt)
        c.sendline('exit')
        c.expect(op_mode_prompt)

    elif args.raid:
        # Verify RAID subsystem - by deleting a disk and re-create the array
        # from scratch
        c.sendline('cat /proc/mdstat')
        c.expect(op_mode_prompt)

        shutdownVM(c, log, f'Shutdown VM and start with empty RAID member "{args.disk}"')

        if os.path.exists(args.disk):
            os.unlink(args.disk)

        gen_disk(args.disk)

        #################################################
        # Booting RAID-1 system with one missing disk
        #################################################
        log.info('Booting RAID-1 system')
        cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid, vnc_enabled=args.vnc)

        # We need to swap boot indexes to boot from second harddisk so we can
        # recreate the RAID on the first disk
        cmd = cmd.replace('bootindex=1', 'bootindex=X')
        cmd = cmd.replace('bootindex=2', 'bootindex=1')
        cmd = cmd.replace('bootindex=X', 'bootindex=2')

        log.debug(f'Executing command: {cmd}')
        c = pexpect.spawn(cmd, logfile=stl)

        #################################################
        # Logging into VyOS system
        #################################################
        waitForLogin(c, log)
        loginVM(c, log)

        c.sendline('cat /proc/mdstat')
        c.expect(op_mode_prompt)

        log.info('Re-format new RAID member')
        c.sendline('format by-id disk drive-hd1 like drive-hd2')
        c.sendline('yes')
        c.expect(op_mode_prompt)

        log.info('Add member to RAID1 (md0)')
        c.sendline('add raid md0 by-id member drive-hd1-part3')
        c.expect(op_mode_prompt)

        log.info('Now we need to wait for re-sync to complete')

        start_time = time.time()
        timeout = 60
        while True:
            if (start_time + timeout) < time.time():
                break
            c.sendline('cat /proc/mdstat')
            c.expect(op_mode_prompt)
            time.sleep(20)

        # Reboot system with new primary RAID1 disk
        shutdownVM(c, log, f'Shutdown VM and start from recovered RAID member "{args.disk}"')

        log.info('Booting RAID-1 system')
        cmd = get_qemu_cmd(qemu_name, args.uefi, args.disk, raid=diskname_raid, vnc_enabled=args.vnc)
        log.debug(f'Executing command: {cmd}')
        c = pexpect.spawn(cmd, logfile=stl)

        loginVM(c, log)

        c.sendline('cat /proc/mdstat')
        c.expect(op_mode_prompt)

    elif args.smoketest:
        # run default smoketest suite
        smoketests_path = '/usr/libexec/vyos/tests/smoke'
        if args.match:
            # Remove tests that we don't want to run
            match_str = '-o '.join([f'-name "test_*{name}*.py" ' for name in args.match.split("|")]).strip()
            c.sendline(f'sudo find {smoketests_path} -maxdepth 2 -type f -name test_* ! \( {match_str} \) -delete')
            c.expect(op_mode_prompt)
        if args.no_interfaces:
            # remove interface tests as they consume a lot of time
            c.sendline(f'sudo rm -f {smoketests_path}/cli/test_interfaces_*')
            c.expect(op_mode_prompt)
        if args.no_vpp:
            # remove VPP tests
            c.sendline(f'sudo rm -f {smoketests_path}/cli/test_vpp*')
            c.expect(op_mode_prompt)

        if args.vyconf:
            c.sendline('sudo /usr/libexec/vyos/set_vyconf_backend.py --no-prompt &> /dev/null')
            c.expect(op_mode_prompt)
            log.info('Smoketests will be run using vyconfd/vyos-commitd')

        log.info('Executing VyOS smoketests')
        c.sendline('/usr/bin/vyos-smoketest')
        i = c.expect(['\n +Invalid command:', '\n +Set failed',
                      'No such file or directory', r'\n\S+@\S+[$#]'], timeout=test_timeout)

        if i == 0:
            raise Exception('Invalid command detected')
        if i == 1:
            raise Exception('Set syntax failed :/')
        if i == 2:
            tmp = '(W)hy (T)he (F)ace? VyOS smoketest not found!'
            log.error(tmp)
            raise Exception(tmp)

        c.sendline('echo EXITCODE:$\x16?')
        i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'])
        if i == 0:
            log.info('Smoketest finished successfully!')
            pass
        elif i == 1:
            log.error('Smoketest failed :/')
            raise Exception("Smoketest-failed, please look into debug output")

    # else, run configtest suite
    elif args.configtest:
        # Remove config-tests that we don't want to run
        if args.match:
            configs_path = '/usr/libexec/vyos/tests/configs'
            if args.match.startswith("!"):
                # Exclude mode — delete only the matched names
                names = args.match[1:].split("|")
                match_str = '-o '.join([f'-name "{name}"' for name in names])
                cleanup_config_dir_cmd = f'sudo find {configs_path} -mindepth 1 -maxdepth 1 -type f \\( {match_str} \\) -exec rm -rf {{}} +'
                cleanup_config_tests_dir_cmd = f'sudo find {configs_path}/assert -mindepth 1 -maxdepth 1 -type f \\( {match_str} \\) -exec rm -rf {{}} +'
            else:
                # Include mode — keep only the matched names, delete the rest
                names = args.match.split("|")
                match_str = '-o '.join([f'-name "{name}"' for name in names])
                cleanup_config_dir_cmd = f'sudo find {configs_path} -mindepth 1 -maxdepth 1 -type f ! \\( {match_str} \\) -exec rm -rf {{}} +'
                cleanup_config_tests_dir_cmd = f'sudo find {configs_path}/assert -mindepth 1 -maxdepth 1 -type f ! \\( {match_str} \\) -exec rm -rf {{}} +'

            c.sendline(cleanup_config_dir_cmd)
            c.expect(op_mode_prompt)
            c.sendline(cleanup_config_tests_dir_cmd)
            c.expect(op_mode_prompt)


        log.info('Adding a legacy WireGuard default keypair for migrations')
        c.sendline('sudo mkdir -p /config/auth/wireguard/default')
        c.expect(op_mode_prompt)
        c.sendline('echo "aGx+fvW916Ej7QRnBbW3QMoldhNv1u95/WHz45zDmF0=" | sudo tee /config/auth/wireguard/default/private.key')
        c.expect(op_mode_prompt)
        c.sendline('echo "x39C77eavJNpvYbNzPSG3n1D68rHYei6q3AEBEyL1z8=" | sudo tee /config/auth/wireguard/default/public.key')
        c.expect(op_mode_prompt)

        log.info('Generating PKI objects')
        c.sendline('/usr/bin/vyos-configtest-pki')
        c.expect(op_mode_prompt, timeout=900)

        script_file = '/config/scripts/vyos-foo-update.script'
        c.sendline(f'echo "#!/bin/sh" > {script_file}; chmod 775 {script_file}')
        c.expect(op_mode_prompt)

        log.info('Executing load config tests')
        c.sendline('/usr/bin/vyos-configtest')
        i = c.expect(['\n +Invalid command:', 'No such file or directory',
                     r'\n\S+@\S+[$#]'], timeout=test_timeout)

        if i==0:
            raise Exception('Invalid command detected')
        elif i==1:
            tmp = 'VyOS smoketest not found!'
            log.error(tmp)
            raise Exception(tmp)

        c.sendline('echo EXITCODE:$\x16?')
        i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'])
        if i == 0:
            log.info('Configtest finished successfully!')
            pass
        elif i == 1:
            tmp = 'Configtest failed :/ - check debug output'
            log.error(tmp)
            raise Exception(tmp)
    elif args.test_image_update:
        log.info(f'Nested installer ISO: mount second CD-ROM ({NESTED_INNER_ISO_NAME})')
        c.sendline(f'sudo mkdir -p {NESTED_HTTP_MOUNT_POINT}')
        c.expect(op_mode_prompt)

        # After drive-cd1 eject the first SCSI CD node is often an empty tray; real
        # media may appear on sr1+. Try every optical device until one mounts.
        c.sendline(
            f'for d in /dev/sr*; do sudo mount -o ro "$d" {NESTED_HTTP_MOUNT_POINT} 2>/dev/null '
            '&& echo NESTED_ISO_MOUNT_OK && break; done; '
            f'mountpoint -q {NESTED_HTTP_MOUNT_POINT} || echo NESTED_ISO_MOUNT_FAIL'
        )
        if c.expect(['NESTED_ISO_MOUNT_OK', 'NESTED_ISO_MOUNT_FAIL']) != 0:
            raise Exception('Failed to mount nested installer payload ISO (no usable /dev/sr* media)')
        c.expect(op_mode_prompt)

        _image_update_cli_sequence(c, log, NESTED_HTTP_IMAGE_NAME)
        _image_update_cli_sequence(c, log, f'{NESTED_HTTP_IMAGE_NAME}-{NESTED_HTTP_VRF_NAME}', NESTED_HTTP_VRF_ADDR, use_vrf=True)

        log.info('Unmount nested installer ISO')
        c.sendline(f'sudo umount {NESTED_HTTP_MOUNT_POINT}')
        c.expect(op_mode_prompt)
        log.info('Nested installer ISO testcase complete')
    elif args.sbtest:
        c.sendline('show secure-boot')
        c.expect('SecureBoot enabled')
        c.expect(op_mode_prompt)
    else:
        log.info('No testcase selected!')

    shutdownVM(c, log, 'Powering off system')
    c.close()

except EarlyExit:
    pass

except pexpect.exceptions.TIMEOUT:
    log.error('Timeout waiting for VyOS system')
    log.error(traceback.format_exc())
    EXCEPTION = 1

except pexpect.exceptions.ExceptionPexpect:
    log.error('Exception while executing QEMU')
    log.error('Is qemu working on this system?')
    log.error(traceback.format_exc())
    EXCEPTION = 1

except Exception:
    log.error('Unknown error occurred!')
    traceback.print_exc()
    EXCEPTION = 1

#################################################
# Cleaning up
#################################################
log.info("Cleaning up")

if tpm_process:
    tpm_process.terminate()
    tpm_process.join()

if args.test_image_update:
    try:
        if os.path.isdir(NESTED_ISO_DATA_DIR):
            shutil.rmtree(NESTED_ISO_DATA_DIR)
        if nested_payload_iso_path and os.path.isfile(nested_payload_iso_path) \
                and not args.keep:
            os.remove(nested_payload_iso_path)
    except OSError:
        log.warning('Could not remove nested installer ISO artefacts', exc_info=True)

if not args.keep:
    log.info(f'Removing disk file: {args.disk}')
    try:
        os.remove(args.disk)
        if diskname_raid:
            os.remove(diskname_raid)
        if args.sbtest:
            os.remove(OVMF_VARS_TMP)
    except Exception:
        log.error('Exception while removing diskimage!')
        log.error(traceback.format_exc())
        EXCEPTION = 1

if EXCEPTION:
    log.error('Hmm... system got an exception while processing.')
    log.error('The ISO image is not considered usable!')
    sys.exit(1)

sys.exit(0)