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)
64 #define P_tmpdir "/tmp"
67 #define DEFAULT_TIMEOUT 120
69 static const char *helper = DEFAULT_HELPER;
70 static int timeout = DEFAULT_TIMEOUT;
71 static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX";
72 static char isof[] = P_tmpdir "/libguestfs-test-tool-iso-XXXXXX";
75 static void preruncheck (void);
76 static void make_files (void);
77 static void set_qemu (const char *path, int use_wrapper);
82 printf (_("libguestfs-test-tool: interactive test tool\n"
83 "Copyright (C) 2009 Red Hat Inc.\n"
85 " libguestfs-test-tool [--options]\n"
87 " --help Display usage\n"
88 " --helper libguestfs-test-tool-helper\n"
89 " Helper program (default: %s)\n"
90 " --qemudir dir Specify QEMU source directory\n"
91 " --qemu qemu Specify QEMU binary\n"
93 " -t n Set launch timeout (default: %d seconds)\n"
95 DEFAULT_HELPER, DEFAULT_TIMEOUT);
99 main (int argc, char *argv[])
101 setlocale (LC_ALL, "");
102 bindtextdomain (PACKAGE, LOCALEBASEDIR);
103 textdomain (PACKAGE);
105 static const char *options = "t:?";
106 static const struct option long_options[] = {
107 { "help", 0, 0, '?' },
108 { "helper", 1, 0, 0 },
110 { "qemudir", 1, 0, 0 },
111 { "timeout", 1, 0, 't' },
116 extern char **environ;
118 struct guestfs_version *vers;
119 char *sfdisk_lines[] = { ",", NULL };
121 /* XXX This is wrong if the user renames the helper. */
122 char *helper_args[] = { "/iso/libguestfs-test-tool-helper", NULL };
125 c = getopt_long (argc, argv, options, long_options, &option_index);
129 case 0: /* options which are long only */
130 if (STREQ (long_options[option_index].name, "helper"))
132 else if (STREQ (long_options[option_index].name, "qemu"))
133 set_qemu (optarg, 0);
134 else if (STREQ (long_options[option_index].name, "qemudir"))
135 set_qemu (optarg, 1);
138 _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
139 long_options[option_index].name, option_index);
145 if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
147 _("libguestfs-test-tool: invalid timeout: %s\n"),
159 _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
168 printf ("===== Test starts here =====\n");
170 /* Must set LIBGUESTFS_DEBUG=1 */
171 setenv ("LIBGUESTFS_DEBUG", "1", 1);
173 /* Print out any environment variables which may relate to this test. */
174 for (i = 0; environ[i] != NULL; ++i)
175 if (STREQLEN (environ[i], "LIBGUESTFS_", 11))
176 printf ("%s\n", environ[i]);
178 /* Create the handle and configure it. */
179 g = guestfs_create ();
182 _("libguestfs-test-tool: failed to create libguestfs handle\n"));
185 if (guestfs_add_drive_opts (g, tmpf,
186 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
189 _("libguestfs-test-tool: failed to add drive '%s'\n"),
193 if (guestfs_add_drive_opts (g, isof,
194 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
195 GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
198 _("libguestfs-test-tool: failed to add drive '%s'\n"),
203 /* Print any version info etc. */
204 vers = guestfs_version (g);
206 fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
209 printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
210 vers->major, vers->minor, vers->release, vers->extra);
211 guestfs_free_version (vers);
213 printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
214 printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
215 printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
216 printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
217 printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
218 printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
220 /* Launch the guest handle. */
221 printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
226 if (guestfs_launch (g) == -1) {
228 _("libguestfs-test-tool: failed to launch appliance\n"));
234 printf ("Guest launched OK.\n");
237 /* Create the filesystem and mount everything. */
238 if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
240 _("libguestfs-test-tool: failed to run sfdisk\n"));
244 if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
246 _("libguestfs-test-tool: failed to mkfs.ext2\n"));
250 if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) {
252 _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
256 if (guestfs_mkdir (g, "/iso") == -1) {
258 _("libguestfs-test-tool: failed to mkdir /iso\n"));
262 if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
264 _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
268 /* Let's now run some simple tests using the helper program. */
269 str = guestfs_command (g, helper_args);
272 _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
277 printf ("===== TEST FINISHED OK =====\n");
281 static char qemuwrapper[] = P_tmpdir "/libguestfs-test-tool-wrapper-XXXXXX";
284 cleanup_wrapper (void)
286 unlink (qemuwrapper);
289 /* Handle the --qemu and --qemudir parameters. use_wrapper is true
290 * in the --qemudir (source directory) case, where we have to create
291 * a wrapper shell script.
294 set_qemu (const char *path, int use_wrapper)
296 char buffer[PATH_MAX];
301 if (getenv ("LIBGUESTFS_QEMU")) {
303 _("LIBGUESTFS_QEMU environment variable is already set, so\n"
304 "--qemu/--qemudir options cannot be used.\n"));
309 if (access (path, X_OK) == -1) {
311 _("Binary '%s' does not exist or is not executable\n"),
316 setenv ("LIBGUESTFS_QEMU", path, 1);
320 /* This should be a source directory, so check it. */
321 snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
322 if (stat (buffer, &statbuf) == -1 ||
323 !S_ISDIR (statbuf.st_mode)) {
325 _("%s: does not look like a qemu source directory\n"),
330 /* Make a wrapper script. */
331 fd = mkstemp (qemuwrapper);
333 perror (qemuwrapper);
339 fp = fdopen (fd, "w");
346 /* Select the right qemu binary for the wrapper script. */
348 fprintf (fp, "i386-softmmu/qemu");
350 fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
353 fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
357 setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
358 atexit (cleanup_wrapper);
361 /* After getting the command line args, but before running
362 * anything, we check everything is in place to do the tests.
372 if (access (helper, R_OK) == -1) {
374 _("Test tool helper program 'libguestfs-test-tool-helper' is not\n"
375 "available. Expected to find it in '%s'\n"
377 "Use the --helper option to specify the location of this program.\n"),
382 snprintf (cmd, sizeof cmd, "file '%s'", helper);
383 fp = popen (cmd, "r");
388 r = fread (buffer, 1, sizeof buffer - 1, fp);
390 fprintf (stderr, _("command failed: %s"), cmd);
396 if (strstr (buffer, "statically linked") == NULL) {
398 _("Test tool helper program %s\n"
399 "is not statically linked. This is a build error when this test tool\n"
407 cleanup_tmpfiles (void)
419 /* Make the ISO which will contain the helper program. */
427 snprintf (cmd, sizeof cmd, "mkisofs -quiet -rJT -o '%s' '%s'",
430 if (r == -1 || WEXITSTATUS(r) != 0) {
432 _("mkisofs command failed: %s\n"), cmd);
436 /* Allocate the sparse file for /dev/sda. */
444 if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
452 if (write (fd, "\0", 1) == -1) {
462 atexit (cleanup_tmpfiles); /* Removes tmpf and isof. */