cdrom-test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "qemu/osdep.h"
  13. #include "libqtest.h"
  14. #include "boot-sector.h"
  15. #include "qapi/qmp/qdict.h"
  16. static char isoimage[] = "cdrom-boot-iso-XXXXXX";
  17. static int exec_genisoimg(const char **args)
  18. {
  19. gchar *out_err = NULL;
  20. gint exit_status = -1;
  21. bool success;
  22. success = g_spawn_sync(NULL, (gchar **)args, NULL,
  23. G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
  24. NULL, NULL, NULL, &out_err, &exit_status, NULL);
  25. if (!success) {
  26. return -ENOENT;
  27. }
  28. if (out_err) {
  29. fputs(out_err, stderr);
  30. g_free(out_err);
  31. }
  32. return exit_status;
  33. }
  34. static int prepare_image(const char *arch, char *isoimage)
  35. {
  36. char srcdir[] = "cdrom-test-dir-XXXXXX";
  37. char *codefile = NULL;
  38. int ifh, ret = -1;
  39. const char *args[] = {
  40. "genisoimage", "-quiet", "-l", "-no-emul-boot",
  41. "-b", NULL, "-o", isoimage, srcdir, NULL
  42. };
  43. ifh = mkstemp(isoimage);
  44. if (ifh < 0) {
  45. perror("Error creating temporary iso image file");
  46. return -1;
  47. }
  48. if (!mkdtemp(srcdir)) {
  49. perror("Error creating temporary directory");
  50. goto cleanup;
  51. }
  52. if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") ||
  53. g_str_equal(arch, "s390x")) {
  54. codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir);
  55. ret = boot_sector_init(codefile);
  56. if (ret) {
  57. goto cleanup;
  58. }
  59. } else {
  60. /* Just create a dummy file */
  61. char txt[] = "empty disc";
  62. codefile = g_strdup_printf("%s/readme.txt", srcdir);
  63. if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) {
  64. fprintf(stderr, "Failed to create '%s'\n", codefile);
  65. goto cleanup;
  66. }
  67. }
  68. args[5] = strchr(codefile, '/') + 1;
  69. ret = exec_genisoimg(args);
  70. if (ret) {
  71. fprintf(stderr, "genisoimage failed: %i\n", ret);
  72. }
  73. unlink(codefile);
  74. cleanup:
  75. g_free(codefile);
  76. rmdir(srcdir);
  77. close(ifh);
  78. return ret;
  79. }
  80. /**
  81. * Check that at least the -cdrom parameter is basically working, i.e. we can
  82. * see the filename of the ISO image in the output of "info block" afterwards
  83. */
  84. static void test_cdrom_param(gconstpointer data)
  85. {
  86. QTestState *qts;
  87. char *resp;
  88. qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage);
  89. resp = qtest_hmp(qts, "info block");
  90. g_assert(strstr(resp, isoimage) != 0);
  91. g_free(resp);
  92. qtest_quit(qts);
  93. }
  94. static void add_cdrom_param_tests(const char **machines)
  95. {
  96. while (*machines) {
  97. char *testname = g_strdup_printf("cdrom/param/%s", *machines);
  98. qtest_add_data_func(testname, *machines, test_cdrom_param);
  99. g_free(testname);
  100. machines++;
  101. }
  102. }
  103. static void test_cdboot(gconstpointer data)
  104. {
  105. QTestState *qts;
  106. qts = qtest_initf("-M accel=kvm:tcg -no-shutdown %s%s", (const char *)data,
  107. isoimage);
  108. boot_sector_test(qts);
  109. qtest_quit(qts);
  110. }
  111. static void add_x86_tests(void)
  112. {
  113. qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
  114. qtest_add_data_func("cdrom/boot/virtio-scsi",
  115. "-device virtio-scsi -device scsi-cd,drive=cdr "
  116. "-blockdev file,node-name=cdr,filename=", test_cdboot);
  117. /*
  118. * Unstable CI test under load
  119. * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
  120. */
  121. if (g_test_slow()) {
  122. qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
  123. "-drive if=ide,media=cdrom,file=", test_cdboot);
  124. }
  125. qtest_add_data_func("cdrom/boot/am53c974",
  126. "-device am53c974 -device scsi-cd,drive=cd1 "
  127. "-drive if=none,id=cd1,format=raw,file=", test_cdboot);
  128. qtest_add_data_func("cdrom/boot/dc390",
  129. "-device dc390 -device scsi-cd,drive=cd1 "
  130. "-blockdev file,node-name=cd1,filename=", test_cdboot);
  131. qtest_add_data_func("cdrom/boot/lsi53c895a",
  132. "-device lsi53c895a -device scsi-cd,drive=cd1 "
  133. "-blockdev file,node-name=cd1,filename=", test_cdboot);
  134. qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
  135. "-device megasas -device scsi-cd,drive=cd1 "
  136. "-blockdev file,node-name=cd1,filename=", test_cdboot);
  137. qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
  138. "-device megasas-gen2 -device scsi-cd,drive=cd1 "
  139. "-blockdev file,node-name=cd1,filename=", test_cdboot);
  140. }
  141. static void add_s390x_tests(void)
  142. {
  143. qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
  144. qtest_add_data_func("cdrom/boot/virtio-scsi",
  145. "-device virtio-scsi -device scsi-cd,drive=cdr "
  146. "-blockdev file,node-name=cdr,filename=", test_cdboot);
  147. }
  148. int main(int argc, char **argv)
  149. {
  150. int ret;
  151. const char *arch = qtest_get_arch();
  152. const char *genisocheck[] = { "genisoimage", "-version", NULL };
  153. g_test_init(&argc, &argv, NULL);
  154. if (exec_genisoimg(genisocheck)) {
  155. /* genisoimage not available - so can't run tests */
  156. return g_test_run();
  157. }
  158. ret = prepare_image(arch, isoimage);
  159. if (ret) {
  160. return ret;
  161. }
  162. if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
  163. add_x86_tests();
  164. } else if (g_str_equal(arch, "s390x")) {
  165. add_s390x_tests();
  166. } else if (g_str_equal(arch, "ppc64")) {
  167. const char *ppcmachines[] = {
  168. "pseries", "mac99", "g3beige", "40p", "prep", NULL
  169. };
  170. add_cdrom_param_tests(ppcmachines);
  171. } else if (g_str_equal(arch, "sparc")) {
  172. const char *sparcmachines[] = {
  173. "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
  174. "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
  175. };
  176. add_cdrom_param_tests(sparcmachines);
  177. } else if (g_str_equal(arch, "sparc64")) {
  178. const char *sparc64machines[] = {
  179. "niagara", "sun4u", "sun4v", NULL
  180. };
  181. add_cdrom_param_tests(sparc64machines);
  182. } else if (!strncmp(arch, "mips64", 6)) {
  183. const char *mips64machines[] = {
  184. "magnum", "malta", "mips", "pica61", NULL
  185. };
  186. add_cdrom_param_tests(mips64machines);
  187. } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
  188. const char *armmachines[] = {
  189. "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
  190. "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
  191. "vexpress-a9", "virt", NULL
  192. };
  193. add_cdrom_param_tests(armmachines);
  194. } else {
  195. const char *nonemachine[] = { "none", NULL };
  196. add_cdrom_param_tests(nonemachine);
  197. }
  198. ret = g_test_run();
  199. unlink(isoimage);
  200. return ret;
  201. }