xref: /openbmc/qemu/tests/qtest/cdrom-test.c (revision ca5aa28e)
1 /*
2  * Various tests for emulated CD-ROM drives.
3  *
4  * Copyright (c) 2018 Red Hat Inc.
5  *
6  * Author:
7  *    Thomas Huth <thuth@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2
10  * or later. See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "boot-sector.h"
16 #include "qapi/qmp/qdict.h"
17 
18 static char isoimage[] = "cdrom-boot-iso-XXXXXX";
19 
20 static int exec_xorrisofs(const char **args)
21 {
22     gchar *out_err = NULL;
23     gint exit_status = -1;
24     bool success;
25 
26     success = g_spawn_sync(NULL, (gchar **)args, NULL,
27                            G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
28                            NULL, NULL, NULL, &out_err, &exit_status, NULL);
29     if (!success) {
30         return -ENOENT;
31     }
32     if (out_err) {
33         fputs(out_err, stderr);
34         g_free(out_err);
35     }
36 
37     return exit_status;
38 }
39 
40 static int prepare_image(const char *arch, char *isoimagepath)
41 {
42     char srcdir[] = "cdrom-test-dir-XXXXXX";
43     char *codefile = NULL;
44     int ifh, ret = -1;
45     const char *args[] = {
46         "xorrisofs", "-quiet", "-l", "-no-emul-boot",
47         "-b", NULL, "-o", isoimagepath, srcdir, NULL
48     };
49 
50     ifh = mkstemp(isoimagepath);
51     if (ifh < 0) {
52         perror("Error creating temporary iso image file");
53         return -1;
54     }
55     if (!g_mkdtemp(srcdir)) {
56         perror("Error creating temporary directory");
57         goto cleanup;
58     }
59 
60     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") ||
61         g_str_equal(arch, "s390x")) {
62         codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir);
63         ret = boot_sector_init(codefile);
64         if (ret) {
65             goto cleanup;
66         }
67     } else {
68         /* Just create a dummy file */
69         char txt[] = "empty disc";
70         codefile = g_strdup_printf("%s/readme.txt", srcdir);
71         if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) {
72             fprintf(stderr, "Failed to create '%s'\n", codefile);
73             goto cleanup;
74         }
75     }
76 
77     args[5] = strchr(codefile, '/') + 1;
78     ret = exec_xorrisofs(args);
79     if (ret) {
80         fprintf(stderr, "xorrisofs failed: %i\n", ret);
81     }
82 
83     unlink(codefile);
84 
85 cleanup:
86     g_free(codefile);
87     rmdir(srcdir);
88     close(ifh);
89 
90     return ret;
91 }
92 
93 /**
94  * Check that at least the -cdrom parameter is basically working, i.e. we can
95  * see the filename of the ISO image in the output of "info block" afterwards
96  */
97 static void test_cdrom_param(gconstpointer data)
98 {
99     QTestState *qts;
100     char *resp;
101 
102     qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage);
103     resp = qtest_hmp(qts, "info block");
104     g_assert(strstr(resp, isoimage) != 0);
105     g_free(resp);
106     qtest_quit(qts);
107 }
108 
109 static void add_cdrom_param_tests(const char **machines)
110 {
111     while (*machines) {
112         if (qtest_has_machine(*machines)) {
113             char *testname = g_strdup_printf("cdrom/param/%s", *machines);
114             qtest_add_data_func(testname, *machines, test_cdrom_param);
115             g_free(testname);
116         }
117         machines++;
118     }
119 }
120 
121 static void test_cdboot(gconstpointer data)
122 {
123     QTestState *qts;
124 
125     qts = qtest_initf("-accel kvm -accel tcg -no-shutdown %s%s", (const char *)data,
126                       isoimage);
127     boot_sector_test(qts);
128     qtest_quit(qts);
129 }
130 
131 static void add_x86_tests(void)
132 {
133     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
134         g_test_skip("No KVM or TCG accelerator available, skipping boot tests");
135         return;
136     }
137 
138     if (qtest_has_machine("pc")) {
139         qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
140         if (qtest_has_device("virtio-scsi-ccw")) {
141             qtest_add_data_func("cdrom/boot/virtio-scsi",
142                                 "-device virtio-scsi -device scsi-cd,drive=cdr "
143                                 "-blockdev file,node-name=cdr,filename=",
144                                 test_cdboot);
145         }
146 
147         if (qtest_has_device("am53c974")) {
148             qtest_add_data_func("cdrom/boot/am53c974",
149                                 "-device am53c974 -device scsi-cd,drive=cd1 "
150                                 "-drive if=none,id=cd1,format=raw,file=",
151                                 test_cdboot);
152         }
153         if (qtest_has_device("dc390")) {
154             qtest_add_data_func("cdrom/boot/dc390",
155                                 "-device dc390 -device scsi-cd,drive=cd1 "
156                                 "-blockdev file,node-name=cd1,filename=",
157                                 test_cdboot);
158         }
159         if (qtest_has_device("lsi53c895a")) {
160             qtest_add_data_func("cdrom/boot/lsi53c895a",
161                                 "-device lsi53c895a -device scsi-cd,drive=cd1 "
162                                 "-blockdev file,node-name=cd1,filename=",
163                                 test_cdboot);
164         }
165     }
166 
167     /*
168      * Unstable CI test under load
169      * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
170      */
171     if (g_test_slow() && qtest_has_machine("isapc")) {
172         qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
173                             "-drive if=ide,media=cdrom,file=", test_cdboot);
174     }
175 
176     if (qtest_has_machine("q35")) {
177         if (qtest_has_device("megasas")) {
178             qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
179                                 "-device megasas -device scsi-cd,drive=cd1 "
180                                 "-blockdev file,node-name=cd1,filename=",
181                                 test_cdboot);
182         }
183         if (qtest_has_device("megasas-gen2")) {
184             qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
185                                 "-device megasas-gen2 -device scsi-cd,drive=cd1 "
186                                 "-blockdev file,node-name=cd1,filename=",
187                                 test_cdboot);
188         }
189     }
190 }
191 
192 static void add_s390x_tests(void)
193 {
194     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
195         g_test_skip("No KVM or TCG accelerator available, skipping boot tests");
196     }
197     if (!qtest_has_device("virtio-blk-ccw")) {
198         return;
199     }
200 
201     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
202 
203     if (!qtest_has_device("virtio-scsi-ccw")) {
204         return;
205     }
206 
207     qtest_add_data_func("cdrom/boot/virtio-scsi",
208                         "-device virtio-scsi -device scsi-cd,drive=cdr "
209                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
210     qtest_add_data_func("cdrom/boot/with-bootindex",
211                         "-device virtio-serial -device virtio-scsi "
212                         "-device virtio-blk,drive=d1 "
213                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
214                         "-device virtio-blk,drive=d2,bootindex=1 "
215                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
216     qtest_add_data_func("cdrom/boot/as-fallback-device",
217                         "-device virtio-serial -device virtio-scsi "
218                         "-device virtio-blk,drive=d1,bootindex=1 "
219                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
220                         "-device virtio-blk,drive=d2,bootindex=2 "
221                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
222     qtest_add_data_func("cdrom/boot/as-last-option",
223                         "-device virtio-serial -device virtio-scsi "
224                         "-device virtio-blk,drive=d1,bootindex=1 "
225                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
226                         "-device virtio-blk,drive=d2,bootindex=2 "
227                         "-drive driver=null-co,read-zeroes=on,if=none,id=d2 "
228                         "-device virtio-blk,drive=d3,bootindex=3 "
229                         "-drive driver=null-co,read-zeroes=on,if=none,id=d3 "
230                         "-device scsi-hd,drive=d4,bootindex=4 "
231                         "-drive driver=null-co,read-zeroes=on,if=none,id=d4 "
232                         "-device scsi-hd,drive=d5,bootindex=5 "
233                         "-drive driver=null-co,read-zeroes=on,if=none,id=d5 "
234                         "-device virtio-blk,drive=d6,bootindex=6 "
235                         "-drive driver=null-co,read-zeroes=on,if=none,id=d6 "
236                         "-device scsi-hd,drive=d7,bootindex=7 "
237                         "-drive driver=null-co,read-zeroes=on,if=none,id=d7 "
238                         "-device scsi-cd,drive=d8,bootindex=8 "
239                         "-drive if=none,id=d8,media=cdrom,file=", test_cdboot);
240     if (qtest_has_device("x-terminal3270")) {
241         qtest_add_data_func("cdrom/boot/without-bootindex",
242                             "-device virtio-scsi -device virtio-serial "
243                             "-device x-terminal3270 -device virtio-blk,drive=d1 "
244                             "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
245                             "-device virtio-blk,drive=d2 "
246                             "-drive if=none,id=d2,media=cdrom,file=",
247                             test_cdboot);
248     }
249 }
250 
251 int main(int argc, char **argv)
252 {
253     int ret;
254     const char *arch = qtest_get_arch();
255     const char *xorrisocheck[] = { "xorrisofs", "-version", NULL };
256 
257     g_test_init(&argc, &argv, NULL);
258 
259     if (exec_xorrisofs(xorrisocheck)) {
260         /* xorrisofs not available - so can't run tests */
261         return g_test_run();
262     }
263 
264     ret = prepare_image(arch, isoimage);
265     if (ret) {
266         return ret;
267     }
268 
269     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
270         add_x86_tests();
271     } else if (g_str_equal(arch, "s390x")) {
272         add_s390x_tests();
273     } else if (g_str_equal(arch, "ppc64")) {
274         const char *ppcmachines[] = {
275             "pseries", "mac99", "g3beige", "40p", NULL
276         };
277         add_cdrom_param_tests(ppcmachines);
278     } else if (g_str_equal(arch, "sparc")) {
279         const char *sparcmachines[] = {
280             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
281             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
282         };
283         add_cdrom_param_tests(sparcmachines);
284     } else if (g_str_equal(arch, "sparc64")) {
285         const char *sparc64machines[] = {
286             "niagara", "sun4u", "sun4v", NULL
287         };
288         add_cdrom_param_tests(sparc64machines);
289     } else if (!strncmp(arch, "mips64", 6)) {
290         const char *mips64machines[] = {
291             "magnum", "malta", "pica61", NULL
292         };
293         add_cdrom_param_tests(mips64machines);
294     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
295         const char *armmachines[] = {
296             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
297             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
298             "vexpress-a9", NULL
299         };
300         add_cdrom_param_tests(armmachines);
301         if (qtest_has_device("virtio-blk-pci")) {
302             const char *virtmachine[] = { "virt", NULL };
303             add_cdrom_param_tests(virtmachine);
304         }
305     } else if (g_str_equal(arch, "loongarch64")) {
306         if (qtest_has_device("virtio-blk-pci")) {
307             const char *virtmachine[] = { "virt", NULL };
308             add_cdrom_param_tests(virtmachine);
309         }
310     } else {
311         const char *nonemachine[] = { "none", NULL };
312         add_cdrom_param_tests(nonemachine);
313     }
314 
315     ret = g_test_run();
316 
317     unlink(isoimage);
318 
319     return ret;
320 }
321