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>
37 #define _(str) dgettext(PACKAGE, (str))
38 #define N_(str) dgettext(PACKAGE, (str))
44 #define DEFAULT_TIMEOUT 120
46 static const char *helper = DEFAULT_HELPER;
47 static int timeout = DEFAULT_TIMEOUT;
48 static char tmpf[] = "/tmp/libguestfs-test-tool-sda-XXXXXX";
49 static char isof[] = "/tmp/libguestfs-test-tool-iso-XXXXXX";
52 static void preruncheck (void);
53 static void make_files (void);
54 static void set_qemu (const char *path, int use_wrapper);
59 printf (_("libguestfs-test-tool: interactive test tool\n"
60 "Copyright (C) 2009 Red Hat Inc.\n"
62 " libguestfs-test-tool [--options]\n"
64 " --help Display usage\n"
65 " --helper libguestfs-test-tool-helper\n"
66 " Helper program (default: %s)\n"
67 " --qemudir dir Specify QEMU source directory\n"
68 " --qemu qemu Specify QEMU binary\n"
70 " -t n Set launch timeout (default: %d seconds)\n"
72 DEFAULT_HELPER, DEFAULT_TIMEOUT);
76 main (int argc, char *argv[])
78 static const char *options = "?";
79 static const struct option long_options[] = {
80 { "help", 0, 0, '?' },
81 { "helper", 1, 0, 0 },
83 { "qemudir", 1, 0, 0 },
84 { "timeout", 1, 0, 't' },
89 extern char **environ;
91 struct guestfs_version *vers;
92 char *sfdisk_lines[] = { ",", NULL };
94 /* XXX This is wrong if the user renames the helper. */
95 char *helper_args[] = { "/iso/libguestfs-test-tool-helper", NULL };
98 c = getopt_long (argc, argv, options, long_options, &option_index);
102 case 0: /* options which are long only */
103 if (strcmp (long_options[option_index].name, "helper") == 0)
105 else if (strcmp (long_options[option_index].name, "qemu") == 0)
106 set_qemu (optarg, 0);
107 else if (strcmp (long_options[option_index].name, "qemudir") == 0)
108 set_qemu (optarg, 1);
111 _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
112 long_options[option_index].name, option_index);
118 if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
120 _("libguestfs-test-tool: invalid timeout: %s\n"),
132 _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
141 printf ("===== Test starts here =====\n");
143 /* Must set LIBGUESTFS_DEBUG=1 */
144 setenv ("LIBGUESTFS_DEBUG", "1", 1);
146 /* Print out any environment variables which may relate to this test. */
147 for (i = 0; environ[i] != NULL; ++i)
148 if (strncmp (environ[i], "LIBGUESTFS_", 11) == 0)
149 printf ("%s\n", environ[i]);
151 /* Create the handle and configure it. */
152 g = guestfs_create ();
155 _("libguestfs-test-tool: failed to create libguestfs handle\n"));
158 if (guestfs_add_drive (g, tmpf) == -1) {
160 _("libguestfs-test-tool: failed to add drive '%s'\n"),
164 if (guestfs_add_drive (g, isof) == -1) {
166 _("libguestfs-test-tool: failed to add drive '%s'\n"),
171 /* Print any version info etc. */
172 vers = guestfs_version (g);
174 fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
177 printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
178 vers->major, vers->minor, vers->release, vers->extra);
179 guestfs_free_version (vers);
181 printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
182 printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
183 printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
184 printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
185 printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
186 printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
188 /* Launch the guest handle. */
189 if (guestfs_launch (g) == -1) {
191 _("libguestfs-test-tool: failed to launch appliance\n"));
195 printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
200 if (guestfs_wait_ready (g) == -1) {
202 _("libguestfs-test-tool: failed or timed out in 'wait_ready'\n"));
208 printf ("Guest launched OK.\n");
211 /* Create the filesystem and mount everything. */
212 if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
214 _("libguestfs-test-tool: failed to run sfdisk\n"));
218 if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
220 _("libguestfs-test-tool: failed to mkfs.ext2\n"));
224 if (guestfs_mount (g, "/dev/sda1", "/") == -1) {
226 _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
230 if (guestfs_mkdir (g, "/iso") == -1) {
232 _("libguestfs-test-tool: failed to mkdir /iso\n"));
236 if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
238 _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
242 /* Let's now run some simple tests using the helper program. */
243 str = guestfs_command (g, helper_args);
246 _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
251 printf ("===== TEST FINISHED OK =====\n");
255 static char qemuwrapper[] = "/tmp/libguestfs-test-tool-wrapper-XXXXXX";
258 cleanup_wrapper (void)
260 unlink (qemuwrapper);
263 /* Handle the --qemu and --qemudir parameters. use_wrapper is true
264 * in the --qemudir (source directory) case, where we have to create
265 * a wrapper shell script.
268 set_qemu (const char *path, int use_wrapper)
270 char buffer[PATH_MAX];
275 if (getenv ("LIBGUESTFS_QEMU")) {
277 _("LIBGUESTFS_QEMU environment variable is already set, so\n"
278 "--qemu/--qemudir options cannot be used.\n"));
283 if (access (path, X_OK) == -1) {
285 _("Binary '%s' does not exist or is not executable\n"),
290 setenv ("LIBGUESTFS_QEMU", path, 1);
294 /* This should be a source directory, so check it. */
295 snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
296 if (stat (buffer, &statbuf) == -1 ||
297 !S_ISDIR (statbuf.st_mode)) {
299 _("%s: does not look like a qemu source directory\n"),
304 /* Make a wrapper script. */
305 fd = mkstemp (qemuwrapper);
307 perror (qemuwrapper);
313 fp = fdopen (fd, "w");
320 /* Select the right qemu binary for the wrapper script. */
322 fprintf (fp, "i386-softmmu/qemu");
324 fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
327 fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
331 setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
332 atexit (cleanup_wrapper);
335 /* After getting the command line args, but before running
336 * anything, we check everything is in place to do the tests.
346 if (access (helper, R_OK) == -1) {
348 _("Test tool helper program 'libguestfs-test-tool-helper' is not\n"
349 "available. Expected to find it in '%s'\n"
351 "Use the --helper option to specify the location of this program.\n"),
356 snprintf (cmd, sizeof cmd, "file '%s'", helper);
357 fp = popen (cmd, "r");
362 r = fread (buffer, 1, sizeof buffer - 1, fp);
364 fprintf (stderr, _("command failed: %s"), cmd);
370 if (strstr (buffer, "statically linked") == NULL) {
372 _("Test tool helper program %s\n"
373 "is not statically linked. This is a build error when this test tool\n"
381 cleanup_tmpfiles (void)
393 /* Make the ISO which will contain the helper program. */
401 snprintf (cmd, sizeof cmd, "mkisofs -quiet -rJT -o '%s' '%s'",
404 if (r == -1 || WEXITSTATUS(r) != 0) {
406 _("mkisofs command failed: %s\n"), cmd);
410 /* Allocate the sparse file for /dev/sda. */
418 if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
426 if (write (fd, "\0", 1) == -1) {
436 atexit (cleanup_tmpfiles); /* Removes tmpf and isof. */