1 /* libguestfs-test-tool
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
39 #define _(str) dgettext(PACKAGE, (str))
40 #define N_(str) dgettext(PACKAGE, (str))
48 #define textdomain(Domainname) /* empty */
50 #define bindtextdomain(Domainname, Dirname) /* empty */
53 #define STREQ(a,b) (strcmp((a),(b)) == 0)
54 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
55 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
56 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
57 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
58 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
59 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
60 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
61 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
63 #define DEFAULT_TIMEOUT 120
65 static const char *helper = DEFAULT_HELPER;
66 static int timeout = DEFAULT_TIMEOUT;
67 static char tmpf[] = "/tmp/libguestfs-test-tool-sda-XXXXXX";
68 static char isof[] = "/tmp/libguestfs-test-tool-iso-XXXXXX";
71 static void preruncheck (void);
72 static void make_files (void);
73 static void set_qemu (const char *path, int use_wrapper);
78 printf (_("libguestfs-test-tool: interactive test tool\n"
79 "Copyright (C) 2009 Red Hat Inc.\n"
81 " libguestfs-test-tool [--options]\n"
83 " --help Display usage\n"
84 " --helper libguestfs-test-tool-helper\n"
85 " Helper program (default: %s)\n"
86 " --qemudir dir Specify QEMU source directory\n"
87 " --qemu qemu Specify QEMU binary\n"
89 " -t n Set launch timeout (default: %d seconds)\n"
91 DEFAULT_HELPER, DEFAULT_TIMEOUT);
95 main (int argc, char *argv[])
97 setlocale (LC_ALL, "");
98 bindtextdomain (PACKAGE, LOCALEBASEDIR);
101 static const char *options = "t:?";
102 static const struct option long_options[] = {
103 { "help", 0, 0, '?' },
104 { "helper", 1, 0, 0 },
106 { "qemudir", 1, 0, 0 },
107 { "timeout", 1, 0, 't' },
112 extern char **environ;
114 struct guestfs_version *vers;
115 char *sfdisk_lines[] = { ",", NULL };
117 /* XXX This is wrong if the user renames the helper. */
118 char *helper_args[] = { "/iso/libguestfs-test-tool-helper", NULL };
121 c = getopt_long (argc, argv, options, long_options, &option_index);
125 case 0: /* options which are long only */
126 if (STREQ (long_options[option_index].name, "helper"))
128 else if (STREQ (long_options[option_index].name, "qemu"))
129 set_qemu (optarg, 0);
130 else if (STREQ (long_options[option_index].name, "qemudir"))
131 set_qemu (optarg, 1);
134 _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
135 long_options[option_index].name, option_index);
141 if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
143 _("libguestfs-test-tool: invalid timeout: %s\n"),
155 _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
164 printf ("===== Test starts here =====\n");
166 /* Must set LIBGUESTFS_DEBUG=1 */
167 setenv ("LIBGUESTFS_DEBUG", "1", 1);
169 /* Print out any environment variables which may relate to this test. */
170 for (i = 0; environ[i] != NULL; ++i)
171 if (STREQLEN (environ[i], "LIBGUESTFS_", 11))
172 printf ("%s\n", environ[i]);
174 /* Create the handle and configure it. */
175 g = guestfs_create ();
178 _("libguestfs-test-tool: failed to create libguestfs handle\n"));
181 if (guestfs_add_drive (g, tmpf) == -1) {
183 _("libguestfs-test-tool: failed to add drive '%s'\n"),
187 if (guestfs_add_drive (g, isof) == -1) {
189 _("libguestfs-test-tool: failed to add drive '%s'\n"),
194 /* Print any version info etc. */
195 vers = guestfs_version (g);
197 fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
200 printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
201 vers->major, vers->minor, vers->release, vers->extra);
202 guestfs_free_version (vers);
204 printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
205 printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
206 printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
207 printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
208 printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
209 printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
211 /* Launch the guest handle. */
212 printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
217 if (guestfs_launch (g) == -1) {
219 _("libguestfs-test-tool: failed to launch appliance\n"));
225 printf ("Guest launched OK.\n");
228 /* Create the filesystem and mount everything. */
229 if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
231 _("libguestfs-test-tool: failed to run sfdisk\n"));
235 if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
237 _("libguestfs-test-tool: failed to mkfs.ext2\n"));
241 if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) {
243 _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
247 if (guestfs_mkdir (g, "/iso") == -1) {
249 _("libguestfs-test-tool: failed to mkdir /iso\n"));
253 if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
255 _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
259 /* Let's now run some simple tests using the helper program. */
260 str = guestfs_command (g, helper_args);
263 _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
268 printf ("===== TEST FINISHED OK =====\n");
272 static char qemuwrapper[] = "/tmp/libguestfs-test-tool-wrapper-XXXXXX";
275 cleanup_wrapper (void)
277 unlink (qemuwrapper);
280 /* Handle the --qemu and --qemudir parameters. use_wrapper is true
281 * in the --qemudir (source directory) case, where we have to create
282 * a wrapper shell script.
285 set_qemu (const char *path, int use_wrapper)
287 char buffer[PATH_MAX];
292 if (getenv ("LIBGUESTFS_QEMU")) {
294 _("LIBGUESTFS_QEMU environment variable is already set, so\n"
295 "--qemu/--qemudir options cannot be used.\n"));
300 if (access (path, X_OK) == -1) {
302 _("Binary '%s' does not exist or is not executable\n"),
307 setenv ("LIBGUESTFS_QEMU", path, 1);
311 /* This should be a source directory, so check it. */
312 snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
313 if (stat (buffer, &statbuf) == -1 ||
314 !S_ISDIR (statbuf.st_mode)) {
316 _("%s: does not look like a qemu source directory\n"),
321 /* Make a wrapper script. */
322 fd = mkstemp (qemuwrapper);
324 perror (qemuwrapper);
330 fp = fdopen (fd, "w");
337 /* Select the right qemu binary for the wrapper script. */
339 fprintf (fp, "i386-softmmu/qemu");
341 fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
344 fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
348 setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
349 atexit (cleanup_wrapper);
352 /* After getting the command line args, but before running
353 * anything, we check everything is in place to do the tests.
363 if (access (helper, R_OK) == -1) {
365 _("Test tool helper program 'libguestfs-test-tool-helper' is not\n"
366 "available. Expected to find it in '%s'\n"
368 "Use the --helper option to specify the location of this program.\n"),
373 snprintf (cmd, sizeof cmd, "file '%s'", helper);
374 fp = popen (cmd, "r");
379 r = fread (buffer, 1, sizeof buffer - 1, fp);
381 fprintf (stderr, _("command failed: %s"), cmd);
387 if (strstr (buffer, "statically linked") == NULL) {
389 _("Test tool helper program %s\n"
390 "is not statically linked. This is a build error when this test tool\n"
398 cleanup_tmpfiles (void)
410 /* Make the ISO which will contain the helper program. */
418 snprintf (cmd, sizeof cmd, "mkisofs -quiet -rJT -o '%s' '%s'",
421 if (r == -1 || WEXITSTATUS(r) != 0) {
423 _("mkisofs command failed: %s\n"), cmd);
427 /* Allocate the sparse file for /dev/sda. */
435 if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
443 if (write (fd, "\0", 1) == -1) {
453 atexit (cleanup_tmpfiles); /* Removes tmpf and isof. */