xref: /qemu/tests/qtest/cdrom-test.c (revision c726fa701c14f088241e2f3f89b0c25ffabeac3c)
142f45505SThomas Huth /*
242f45505SThomas Huth  * Various tests for emulated CD-ROM drives.
342f45505SThomas Huth  *
442f45505SThomas Huth  * Copyright (c) 2018 Red Hat Inc.
542f45505SThomas Huth  *
642f45505SThomas Huth  * Author:
742f45505SThomas Huth  *    Thomas Huth <thuth@redhat.com>
842f45505SThomas Huth  *
942f45505SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2
1042f45505SThomas Huth  * or later. See the COPYING file in the top-level directory.
1142f45505SThomas Huth  */
1242f45505SThomas Huth 
1342f45505SThomas Huth #include "qemu/osdep.h"
14907b5105SMarc-André Lureau #include "libqtest.h"
1542f45505SThomas Huth #include "boot-sector.h"
1649d43a59SThomas Huth #include "qapi/qmp/qdict.h"
1742f45505SThomas Huth 
1842f45505SThomas Huth static char isoimage[] = "cdrom-boot-iso-XXXXXX";
1942f45505SThomas Huth 
2042f45505SThomas Huth static int exec_genisoimg(const char **args)
2142f45505SThomas Huth {
2242f45505SThomas Huth     gchar *out_err = NULL;
2342f45505SThomas Huth     gint exit_status = -1;
2442f45505SThomas Huth     bool success;
2542f45505SThomas Huth 
2642f45505SThomas Huth     success = g_spawn_sync(NULL, (gchar **)args, NULL,
2742f45505SThomas Huth                            G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
2842f45505SThomas Huth                            NULL, NULL, NULL, &out_err, &exit_status, NULL);
2942f45505SThomas Huth     if (!success) {
3042f45505SThomas Huth         return -ENOENT;
3142f45505SThomas Huth     }
3242f45505SThomas Huth     if (out_err) {
3342f45505SThomas Huth         fputs(out_err, stderr);
3442f45505SThomas Huth         g_free(out_err);
3542f45505SThomas Huth     }
3642f45505SThomas Huth 
3742f45505SThomas Huth     return exit_status;
3842f45505SThomas Huth }
3942f45505SThomas Huth 
4042f45505SThomas Huth static int prepare_image(const char *arch, char *isoimage)
4142f45505SThomas Huth {
4242f45505SThomas Huth     char srcdir[] = "cdrom-test-dir-XXXXXX";
4342f45505SThomas Huth     char *codefile = NULL;
4442f45505SThomas Huth     int ifh, ret = -1;
4542f45505SThomas Huth     const char *args[] = {
4642f45505SThomas Huth         "genisoimage", "-quiet", "-l", "-no-emul-boot",
4742f45505SThomas Huth         "-b", NULL, "-o", isoimage, srcdir, NULL
4842f45505SThomas Huth     };
4942f45505SThomas Huth 
5042f45505SThomas Huth     ifh = mkstemp(isoimage);
5142f45505SThomas Huth     if (ifh < 0) {
5242f45505SThomas Huth         perror("Error creating temporary iso image file");
5342f45505SThomas Huth         return -1;
5442f45505SThomas Huth     }
553c239aa7SBin Meng     if (!g_mkdtemp(srcdir)) {
5642f45505SThomas Huth         perror("Error creating temporary directory");
5742f45505SThomas Huth         goto cleanup;
5842f45505SThomas Huth     }
5942f45505SThomas Huth 
6042f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") ||
6142f45505SThomas Huth         g_str_equal(arch, "s390x")) {
6242f45505SThomas Huth         codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir);
6342f45505SThomas Huth         ret = boot_sector_init(codefile);
6442f45505SThomas Huth         if (ret) {
6542f45505SThomas Huth             goto cleanup;
6642f45505SThomas Huth         }
6742f45505SThomas Huth     } else {
6842f45505SThomas Huth         /* Just create a dummy file */
6942f45505SThomas Huth         char txt[] = "empty disc";
7042f45505SThomas Huth         codefile = g_strdup_printf("%s/readme.txt", srcdir);
7142f45505SThomas Huth         if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) {
7242f45505SThomas Huth             fprintf(stderr, "Failed to create '%s'\n", codefile);
7342f45505SThomas Huth             goto cleanup;
7442f45505SThomas Huth         }
7542f45505SThomas Huth     }
7642f45505SThomas Huth 
7742f45505SThomas Huth     args[5] = strchr(codefile, '/') + 1;
7842f45505SThomas Huth     ret = exec_genisoimg(args);
7942f45505SThomas Huth     if (ret) {
8042f45505SThomas Huth         fprintf(stderr, "genisoimage failed: %i\n", ret);
8142f45505SThomas Huth     }
8242f45505SThomas Huth 
8342f45505SThomas Huth     unlink(codefile);
8442f45505SThomas Huth 
8542f45505SThomas Huth cleanup:
8642f45505SThomas Huth     g_free(codefile);
8742f45505SThomas Huth     rmdir(srcdir);
8842f45505SThomas Huth     close(ifh);
8942f45505SThomas Huth 
9042f45505SThomas Huth     return ret;
9142f45505SThomas Huth }
9242f45505SThomas Huth 
9349d43a59SThomas Huth /**
9449d43a59SThomas Huth  * Check that at least the -cdrom parameter is basically working, i.e. we can
9549d43a59SThomas Huth  * see the filename of the ISO image in the output of "info block" afterwards
9649d43a59SThomas Huth  */
9749d43a59SThomas Huth static void test_cdrom_param(gconstpointer data)
9849d43a59SThomas Huth {
9949d43a59SThomas Huth     QTestState *qts;
10049d43a59SThomas Huth     char *resp;
10149d43a59SThomas Huth 
10288b988c8SMarkus Armbruster     qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage);
10349d43a59SThomas Huth     resp = qtest_hmp(qts, "info block");
10449d43a59SThomas Huth     g_assert(strstr(resp, isoimage) != 0);
10549d43a59SThomas Huth     g_free(resp);
10649d43a59SThomas Huth     qtest_quit(qts);
10749d43a59SThomas Huth }
10849d43a59SThomas Huth 
10949d43a59SThomas Huth static void add_cdrom_param_tests(const char **machines)
11049d43a59SThomas Huth {
11149d43a59SThomas Huth     while (*machines) {
112719051caSThomas Huth         if (qtest_has_machine(*machines)) {
11349d43a59SThomas Huth             char *testname = g_strdup_printf("cdrom/param/%s", *machines);
11449d43a59SThomas Huth             qtest_add_data_func(testname, *machines, test_cdrom_param);
11549d43a59SThomas Huth             g_free(testname);
116719051caSThomas Huth         }
11749d43a59SThomas Huth         machines++;
11849d43a59SThomas Huth     }
11949d43a59SThomas Huth }
12049d43a59SThomas Huth 
12142f45505SThomas Huth static void test_cdboot(gconstpointer data)
12242f45505SThomas Huth {
12342f45505SThomas Huth     QTestState *qts;
12442f45505SThomas Huth 
1256f6e1698SPaolo Bonzini     qts = qtest_initf("-accel kvm -accel tcg -no-shutdown %s%s", (const char *)data,
12642f45505SThomas Huth                       isoimage);
12742f45505SThomas Huth     boot_sector_test(qts);
12842f45505SThomas Huth     qtest_quit(qts);
12942f45505SThomas Huth }
13042f45505SThomas Huth 
13142f45505SThomas Huth static void add_x86_tests(void)
13242f45505SThomas Huth {
133*c726fa70SFabiano Rosas     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
134*c726fa70SFabiano Rosas         g_test_skip("No KVM or TCG accelerator available, skipping boot tests");
135*c726fa70SFabiano Rosas         return;
136*c726fa70SFabiano Rosas     }
137*c726fa70SFabiano Rosas 
13842f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
13942f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
14042f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
14142f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
1424300dadcSAlex Bennée     /*
1434300dadcSAlex Bennée      * Unstable CI test under load
1444300dadcSAlex Bennée      * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
1454300dadcSAlex Bennée      */
146274f5e63SThomas Huth     if (g_test_slow() && qtest_has_machine("isapc")) {
14742f45505SThomas Huth         qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
14842f45505SThomas Huth                             "-drive if=ide,media=cdrom,file=", test_cdboot);
1494300dadcSAlex Bennée     }
15095c0b770SThomas Huth     if (qtest_has_device("am53c974")) {
15142f45505SThomas Huth         qtest_add_data_func("cdrom/boot/am53c974",
15242f45505SThomas Huth                             "-device am53c974 -device scsi-cd,drive=cd1 "
15395c0b770SThomas Huth                             "-drive if=none,id=cd1,format=raw,file=",
15495c0b770SThomas Huth                             test_cdboot);
15595c0b770SThomas Huth     }
15695c0b770SThomas Huth     if (qtest_has_device("dc390")) {
15742f45505SThomas Huth         qtest_add_data_func("cdrom/boot/dc390",
15842f45505SThomas Huth                             "-device dc390 -device scsi-cd,drive=cd1 "
15995c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
16095c0b770SThomas Huth                             test_cdboot);
16195c0b770SThomas Huth     }
16295c0b770SThomas Huth     if (qtest_has_device("lsi53c895a")) {
16342f45505SThomas Huth         qtest_add_data_func("cdrom/boot/lsi53c895a",
16442f45505SThomas Huth                             "-device lsi53c895a -device scsi-cd,drive=cd1 "
16595c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
16695c0b770SThomas Huth                             test_cdboot);
16795c0b770SThomas Huth     }
16895c0b770SThomas Huth     if (qtest_has_device("megasas")) {
16942f45505SThomas Huth         qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
17042f45505SThomas Huth                             "-device megasas -device scsi-cd,drive=cd1 "
17195c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
17295c0b770SThomas Huth                             test_cdboot);
17395c0b770SThomas Huth     }
17495c0b770SThomas Huth     if (qtest_has_device("megasas-gen2")) {
17542f45505SThomas Huth         qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
17642f45505SThomas Huth                             "-device megasas-gen2 -device scsi-cd,drive=cd1 "
17795c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
17895c0b770SThomas Huth                             test_cdboot);
17995c0b770SThomas Huth     }
18042f45505SThomas Huth }
18142f45505SThomas Huth 
18242f45505SThomas Huth static void add_s390x_tests(void)
18342f45505SThomas Huth {
184*c726fa70SFabiano Rosas     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
185*c726fa70SFabiano Rosas         g_test_skip("No KVM or TCG accelerator available, skipping boot tests");
186*c726fa70SFabiano Rosas         return;
187*c726fa70SFabiano Rosas     }
188*c726fa70SFabiano Rosas 
18942f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
19042f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
19142f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
19242f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
193eb32abd8SThomas Huth     qtest_add_data_func("cdrom/boot/with-bootindex",
194eb32abd8SThomas Huth                         "-device virtio-serial -device virtio-scsi "
195eb32abd8SThomas Huth                         "-device virtio-blk,drive=d1 "
196eb32abd8SThomas Huth                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
197eb32abd8SThomas Huth                         "-device virtio-blk,drive=d2,bootindex=1 "
198eb32abd8SThomas Huth                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
19995c0b770SThomas Huth     if (qtest_has_device("x-terminal3270")) {
200eb32abd8SThomas Huth         qtest_add_data_func("cdrom/boot/without-bootindex",
201eb32abd8SThomas Huth                             "-device virtio-scsi -device virtio-serial "
202eb32abd8SThomas Huth                             "-device x-terminal3270 -device virtio-blk,drive=d1 "
203eb32abd8SThomas Huth                             "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
204eb32abd8SThomas Huth                             "-device virtio-blk,drive=d2 "
20595c0b770SThomas Huth                             "-drive if=none,id=d2,media=cdrom,file=",
20695c0b770SThomas Huth                             test_cdboot);
20795c0b770SThomas Huth     }
20842f45505SThomas Huth }
20942f45505SThomas Huth 
21042f45505SThomas Huth int main(int argc, char **argv)
21142f45505SThomas Huth {
21242f45505SThomas Huth     int ret;
21342f45505SThomas Huth     const char *arch = qtest_get_arch();
21442f45505SThomas Huth     const char *genisocheck[] = { "genisoimage", "-version", NULL };
21542f45505SThomas Huth 
21642f45505SThomas Huth     g_test_init(&argc, &argv, NULL);
21742f45505SThomas Huth 
21842f45505SThomas Huth     if (exec_genisoimg(genisocheck)) {
21942f45505SThomas Huth         /* genisoimage not available - so can't run tests */
2204848cb3dSPaolo Bonzini         return g_test_run();
22142f45505SThomas Huth     }
22242f45505SThomas Huth 
22342f45505SThomas Huth     ret = prepare_image(arch, isoimage);
22442f45505SThomas Huth     if (ret) {
22542f45505SThomas Huth         return ret;
22642f45505SThomas Huth     }
22742f45505SThomas Huth 
22842f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
22942f45505SThomas Huth         add_x86_tests();
23042f45505SThomas Huth     } else if (g_str_equal(arch, "s390x")) {
23142f45505SThomas Huth         add_s390x_tests();
23249d43a59SThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
23349d43a59SThomas Huth         const char *ppcmachines[] = {
234b2ce76a0SThomas Huth             "pseries", "mac99", "g3beige", "40p", NULL
23549d43a59SThomas Huth         };
23649d43a59SThomas Huth         add_cdrom_param_tests(ppcmachines);
23749d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc")) {
23849d43a59SThomas Huth         const char *sparcmachines[] = {
23949d43a59SThomas Huth             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
24049d43a59SThomas Huth             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
24149d43a59SThomas Huth         };
24249d43a59SThomas Huth         add_cdrom_param_tests(sparcmachines);
24349d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc64")) {
24449d43a59SThomas Huth         const char *sparc64machines[] = {
24549d43a59SThomas Huth             "niagara", "sun4u", "sun4v", NULL
24649d43a59SThomas Huth         };
24749d43a59SThomas Huth         add_cdrom_param_tests(sparc64machines);
24849d43a59SThomas Huth     } else if (!strncmp(arch, "mips64", 6)) {
24949d43a59SThomas Huth         const char *mips64machines[] = {
250f169413cSPhilippe Mathieu-Daudé             "magnum", "malta", "pica61", NULL
25149d43a59SThomas Huth         };
25249d43a59SThomas Huth         add_cdrom_param_tests(mips64machines);
25349d43a59SThomas Huth     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
25449d43a59SThomas Huth         const char *armmachines[] = {
25549d43a59SThomas Huth             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
25649d43a59SThomas Huth             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
25749d43a59SThomas Huth             "vexpress-a9", "virt", NULL
25849d43a59SThomas Huth         };
25949d43a59SThomas Huth         add_cdrom_param_tests(armmachines);
26049d43a59SThomas Huth     } else {
26149d43a59SThomas Huth         const char *nonemachine[] = { "none", NULL };
26249d43a59SThomas Huth         add_cdrom_param_tests(nonemachine);
26342f45505SThomas Huth     }
26442f45505SThomas Huth 
26542f45505SThomas Huth     ret = g_test_run();
26642f45505SThomas Huth 
26742f45505SThomas Huth     unlink(isoimage);
26842f45505SThomas Huth 
26942f45505SThomas Huth     return ret;
27042f45505SThomas Huth }
271