xref: /qemu/tests/qtest/cdrom-test.c (revision 719051ca3fd0ee765d8a80670df1a0292aa566f0)
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"
14a2ce7dbdSPaolo Bonzini #include "libqos/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     }
5542f45505SThomas Huth     if (!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) {
112*719051caSThomas 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);
116*719051caSThomas 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 {
13342f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
13442f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
13542f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
13642f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
1374300dadcSAlex Bennée     /*
1384300dadcSAlex Bennée      * Unstable CI test under load
1394300dadcSAlex Bennée      * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
1404300dadcSAlex Bennée      */
1414300dadcSAlex Bennée     if (g_test_slow()) {
14242f45505SThomas Huth         qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
14342f45505SThomas Huth                             "-drive if=ide,media=cdrom,file=", test_cdboot);
1444300dadcSAlex Bennée     }
14542f45505SThomas Huth     qtest_add_data_func("cdrom/boot/am53c974",
14642f45505SThomas Huth                         "-device am53c974 -device scsi-cd,drive=cd1 "
14742f45505SThomas Huth                         "-drive if=none,id=cd1,format=raw,file=", test_cdboot);
14842f45505SThomas Huth     qtest_add_data_func("cdrom/boot/dc390",
14942f45505SThomas Huth                         "-device dc390 -device scsi-cd,drive=cd1 "
15042f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15142f45505SThomas Huth     qtest_add_data_func("cdrom/boot/lsi53c895a",
15242f45505SThomas Huth                         "-device lsi53c895a -device scsi-cd,drive=cd1 "
15342f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15442f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
15542f45505SThomas Huth                         "-device megasas -device scsi-cd,drive=cd1 "
15642f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15742f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
15842f45505SThomas Huth                         "-device megasas-gen2 -device scsi-cd,drive=cd1 "
15942f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
16042f45505SThomas Huth }
16142f45505SThomas Huth 
16242f45505SThomas Huth static void add_s390x_tests(void)
16342f45505SThomas Huth {
16442f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
16542f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
16642f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
16742f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
168eb32abd8SThomas Huth     qtest_add_data_func("cdrom/boot/with-bootindex",
169eb32abd8SThomas Huth                         "-device virtio-serial -device virtio-scsi "
170eb32abd8SThomas Huth                         "-device virtio-blk,drive=d1 "
171eb32abd8SThomas Huth                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
172eb32abd8SThomas Huth                         "-device virtio-blk,drive=d2,bootindex=1 "
173eb32abd8SThomas Huth                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
174eb32abd8SThomas Huth     qtest_add_data_func("cdrom/boot/without-bootindex",
175eb32abd8SThomas Huth                         "-device virtio-scsi -device virtio-serial "
176eb32abd8SThomas Huth                         "-device x-terminal3270 -device virtio-blk,drive=d1 "
177eb32abd8SThomas Huth                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
178eb32abd8SThomas Huth                         "-device virtio-blk,drive=d2 "
179eb32abd8SThomas Huth                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
18042f45505SThomas Huth }
18142f45505SThomas Huth 
18242f45505SThomas Huth int main(int argc, char **argv)
18342f45505SThomas Huth {
18442f45505SThomas Huth     int ret;
18542f45505SThomas Huth     const char *arch = qtest_get_arch();
18642f45505SThomas Huth     const char *genisocheck[] = { "genisoimage", "-version", NULL };
18742f45505SThomas Huth 
18842f45505SThomas Huth     g_test_init(&argc, &argv, NULL);
18942f45505SThomas Huth 
19042f45505SThomas Huth     if (exec_genisoimg(genisocheck)) {
19142f45505SThomas Huth         /* genisoimage not available - so can't run tests */
1924848cb3dSPaolo Bonzini         return g_test_run();
19342f45505SThomas Huth     }
19442f45505SThomas Huth 
19542f45505SThomas Huth     ret = prepare_image(arch, isoimage);
19642f45505SThomas Huth     if (ret) {
19742f45505SThomas Huth         return ret;
19842f45505SThomas Huth     }
19942f45505SThomas Huth 
20042f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
20142f45505SThomas Huth         add_x86_tests();
20242f45505SThomas Huth     } else if (g_str_equal(arch, "s390x")) {
20342f45505SThomas Huth         add_s390x_tests();
20449d43a59SThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
20549d43a59SThomas Huth         const char *ppcmachines[] = {
206b2ce76a0SThomas Huth             "pseries", "mac99", "g3beige", "40p", NULL
20749d43a59SThomas Huth         };
20849d43a59SThomas Huth         add_cdrom_param_tests(ppcmachines);
20949d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc")) {
21049d43a59SThomas Huth         const char *sparcmachines[] = {
21149d43a59SThomas Huth             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
21249d43a59SThomas Huth             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
21349d43a59SThomas Huth         };
21449d43a59SThomas Huth         add_cdrom_param_tests(sparcmachines);
21549d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc64")) {
21649d43a59SThomas Huth         const char *sparc64machines[] = {
21749d43a59SThomas Huth             "niagara", "sun4u", "sun4v", NULL
21849d43a59SThomas Huth         };
21949d43a59SThomas Huth         add_cdrom_param_tests(sparc64machines);
22049d43a59SThomas Huth     } else if (!strncmp(arch, "mips64", 6)) {
22149d43a59SThomas Huth         const char *mips64machines[] = {
222f169413cSPhilippe Mathieu-Daudé             "magnum", "malta", "pica61", NULL
22349d43a59SThomas Huth         };
22449d43a59SThomas Huth         add_cdrom_param_tests(mips64machines);
22549d43a59SThomas Huth     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
22649d43a59SThomas Huth         const char *armmachines[] = {
22749d43a59SThomas Huth             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
22849d43a59SThomas Huth             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
22949d43a59SThomas Huth             "vexpress-a9", "virt", NULL
23049d43a59SThomas Huth         };
23149d43a59SThomas Huth         add_cdrom_param_tests(armmachines);
23249d43a59SThomas Huth     } else {
23349d43a59SThomas Huth         const char *nonemachine[] = { "none", NULL };
23449d43a59SThomas Huth         add_cdrom_param_tests(nonemachine);
23542f45505SThomas Huth     }
23642f45505SThomas Huth 
23742f45505SThomas Huth     ret = g_test_run();
23842f45505SThomas Huth 
23942f45505SThomas Huth     unlink(isoimage);
24042f45505SThomas Huth 
24142f45505SThomas Huth     return ret;
24242f45505SThomas Huth }
243