Version 1.8.16.
[libguestfs.git] / test-tool / test-tool.c
1 /* libguestfs-test-tool
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <locale.h>
33 #include <limits.h>
34
35 #include <guestfs.h>
36
37 #ifdef HAVE_GETTEXT
38 #include "gettext.h"
39 #define _(str) dgettext(PACKAGE, (str))
40 #define N_(str) dgettext(PACKAGE, (str))
41 #else
42 #define _(str) str
43 #define N_(str) str
44 #endif
45
46 #if !ENABLE_NLS
47 #undef textdomain
48 #define textdomain(Domainname) /* empty */
49 #undef bindtextdomain
50 #define bindtextdomain(Domainname, Dirname) /* empty */
51 #endif
52
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)
62
63 #ifndef P_tmpdir
64 #define P_tmpdir "/tmp"
65 #endif
66
67 #define DEFAULT_TIMEOUT 120
68
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";
73 static guestfs_h *g;
74
75 static void preruncheck (void);
76 static void make_files (void);
77 static void set_qemu (const char *path, int use_wrapper);
78
79 static void
80 usage (void)
81 {
82   printf (_("libguestfs-test-tool: interactive test tool\n"
83             "Copyright (C) 2009 Red Hat Inc.\n"
84             "Usage:\n"
85             "  libguestfs-test-tool [--options]\n"
86             "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"
92             "  --timeout n\n"
93             "  -t n           Set launch timeout (default: %d seconds)\n"
94             ),
95           DEFAULT_HELPER, DEFAULT_TIMEOUT);
96 }
97
98 int
99 main (int argc, char *argv[])
100 {
101   setlocale (LC_ALL, "");
102   bindtextdomain (PACKAGE, LOCALEBASEDIR);
103   textdomain (PACKAGE);
104
105   static const char *options = "t:?";
106   static const struct option long_options[] = {
107     { "help", 0, 0, '?' },
108     { "helper", 1, 0, 0 },
109     { "qemu", 1, 0, 0 },
110     { "qemudir", 1, 0, 0 },
111     { "timeout", 1, 0, 't' },
112     { 0, 0, 0, 0 }
113   };
114   int c;
115   int option_index;
116   extern char **environ;
117   int i;
118   struct guestfs_version *vers;
119   char *sfdisk_lines[] = { ",", NULL };
120   char *str;
121   /* XXX This is wrong if the user renames the helper. */
122   char *helper_args[] = { "/iso/libguestfs-test-tool-helper", NULL };
123
124   for (;;) {
125     c = getopt_long (argc, argv, options, long_options, &option_index);
126     if (c == -1) break;
127
128     switch (c) {
129     case 0:                     /* options which are long only */
130       if (STREQ (long_options[option_index].name, "helper"))
131         helper = optarg;
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);
136       else {
137         fprintf (stderr,
138                  _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
139                  long_options[option_index].name, option_index);
140         exit (EXIT_FAILURE);
141       }
142       break;
143
144     case 't':
145       if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
146         fprintf (stderr,
147                  _("libguestfs-test-tool: invalid timeout: %s\n"),
148                  optarg);
149         exit (EXIT_FAILURE);
150       }
151       break;
152
153     case '?':
154       usage ();
155       exit (EXIT_SUCCESS);
156
157     default:
158       fprintf (stderr,
159                _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
160                c);
161       exit (EXIT_FAILURE);
162     }
163   }
164
165   preruncheck ();
166   make_files ();
167
168   printf ("===== Test starts here =====\n");
169
170   /* Must set LIBGUESTFS_DEBUG=1 */
171   setenv ("LIBGUESTFS_DEBUG", "1", 1);
172
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]);
177   for (i = 0; environ[i] != NULL; ++i)
178     if (STREQLEN (environ[i], "FEBOOTSTRAP_", 12))
179       printf ("%s\n", environ[i]);
180
181   /* Create the handle and configure it. */
182   g = guestfs_create ();
183   if (g == NULL) {
184     fprintf (stderr,
185              _("libguestfs-test-tool: failed to create libguestfs handle\n"));
186     exit (EXIT_FAILURE);
187   }
188   if (guestfs_add_drive_opts (g, tmpf,
189                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
190                               -1) == -1) {
191     fprintf (stderr,
192              _("libguestfs-test-tool: failed to add drive '%s'\n"),
193              tmpf);
194     exit (EXIT_FAILURE);
195   }
196   if (guestfs_add_drive_opts (g, isof,
197                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
198                               GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
199                               -1) == -1) {
200     fprintf (stderr,
201              _("libguestfs-test-tool: failed to add drive '%s'\n"),
202              isof);
203     exit (EXIT_FAILURE);
204   }
205
206   /* Print any version info etc. */
207   vers = guestfs_version (g);
208   if (vers == NULL) {
209     fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
210     exit (EXIT_FAILURE);
211   }
212   printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
213           vers->major, vers->minor, vers->release, vers->extra);
214   guestfs_free_version (vers);
215
216   printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
217   printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
218   printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
219   printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
220   printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
221   printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
222
223   /* Launch the guest handle. */
224   printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
225   fflush (stdout);
226
227   alarm (timeout);
228
229   if (guestfs_launch (g) == -1) {
230     fprintf (stderr,
231              _("libguestfs-test-tool: failed to launch appliance\n"));
232     exit (EXIT_FAILURE);
233   }
234
235   alarm (0);
236
237   printf ("Guest launched OK.\n");
238   fflush (stdout);
239
240   /* Create the filesystem and mount everything. */
241   if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
242     fprintf (stderr,
243              _("libguestfs-test-tool: failed to run sfdisk\n"));
244     exit (EXIT_FAILURE);
245   }
246
247   if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
248     fprintf (stderr,
249              _("libguestfs-test-tool: failed to mkfs.ext2\n"));
250     exit (EXIT_FAILURE);
251   }
252
253   if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) {
254     fprintf (stderr,
255              _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
256     exit (EXIT_FAILURE);
257   }
258
259   if (guestfs_mkdir (g, "/iso") == -1) {
260     fprintf (stderr,
261              _("libguestfs-test-tool: failed to mkdir /iso\n"));
262     exit (EXIT_FAILURE);
263   }
264
265   if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
266     fprintf (stderr,
267              _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
268     exit (EXIT_FAILURE);
269   }
270
271   /* Let's now run some simple tests using the helper program. */
272   str = guestfs_command (g, helper_args);
273   if (str == NULL) {
274     fprintf (stderr,
275              _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
276     exit (EXIT_FAILURE);
277   }
278   free (str);
279
280   printf ("===== TEST FINISHED OK =====\n");
281   exit (EXIT_SUCCESS);
282 }
283
284 static char qemuwrapper[] = P_tmpdir "/libguestfs-test-tool-wrapper-XXXXXX";
285
286 static void
287 cleanup_wrapper (void)
288 {
289   unlink (qemuwrapper);
290 }
291
292 /* Handle the --qemu and --qemudir parameters.  use_wrapper is true
293  * in the --qemudir (source directory) case, where we have to create
294  * a wrapper shell script.
295  */
296 static void
297 set_qemu (const char *path, int use_wrapper)
298 {
299   char buffer[PATH_MAX];
300   struct stat statbuf;
301   int fd;
302   FILE *fp;
303
304   if (getenv ("LIBGUESTFS_QEMU")) {
305     fprintf (stderr,
306     _("LIBGUESTFS_QEMU environment variable is already set, so\n"
307       "--qemu/--qemudir options cannot be used.\n"));
308     exit (EXIT_FAILURE);
309   }
310
311   if (!use_wrapper) {
312     if (access (path, X_OK) == -1) {
313       fprintf (stderr,
314                _("Binary '%s' does not exist or is not executable\n"),
315                path);
316       exit (EXIT_FAILURE);
317     }
318
319     setenv ("LIBGUESTFS_QEMU", path, 1);
320     return;
321   }
322
323   /* This should be a source directory, so check it. */
324   snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
325   if (stat (buffer, &statbuf) == -1 ||
326       !S_ISDIR (statbuf.st_mode)) {
327     fprintf (stderr,
328              _("%s: does not look like a qemu source directory\n"),
329              path);
330     exit (EXIT_FAILURE);
331   }
332
333   /* Make a wrapper script. */
334   fd = mkstemp (qemuwrapper);
335   if (fd == -1) {
336     perror (qemuwrapper);
337     exit (EXIT_FAILURE);
338   }
339
340   fchmod (fd, 0700);
341
342   fp = fdopen (fd, "w");
343   fprintf (fp,
344            "#!/bin/sh -\n"
345            "qemudir='%s'\n"
346            "\"$qemudir\"/",
347            path);
348
349   /* Select the right qemu binary for the wrapper script. */
350 #ifdef __i386__
351   fprintf (fp, "i386-softmmu/qemu");
352 #else
353   fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
354 #endif
355
356   fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
357
358   fclose (fp);
359
360   setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
361   atexit (cleanup_wrapper);
362 }
363
364 /* After getting the command line args, but before running
365  * anything, we check everything is in place to do the tests.
366  */
367 static void
368 preruncheck (void)
369 {
370   int r;
371   FILE *fp;
372   char cmd[256];
373   char buffer[1024];
374
375   if (access (helper, R_OK) == -1) {
376     fprintf (stderr,
377     _("Test tool helper program 'libguestfs-test-tool-helper' is not\n"
378       "available.  Expected to find it in '%s'\n"
379       "\n"
380       "Use the --helper option to specify the location of this program.\n"),
381              helper);
382     exit (EXIT_FAILURE);
383   }
384
385   snprintf (cmd, sizeof cmd, "file '%s'", helper);
386   fp = popen (cmd, "r");
387   if (fp == NULL) {
388     perror (cmd);
389     exit (EXIT_FAILURE);
390   }
391   r = fread (buffer, 1, sizeof buffer - 1, fp);
392   if (r == 0) {
393     fprintf (stderr, _("command failed: %s"), cmd);
394     exit (EXIT_FAILURE);
395   }
396   pclose (fp);
397   buffer[r] = '\0';
398
399   if (strstr (buffer, "statically linked") == NULL) {
400     fprintf (stderr,
401     _("Test tool helper program %s\n"
402       "is not statically linked.  This is a build error when this test tool\n"
403       "was built.\n"),
404              helper);
405     exit (EXIT_FAILURE);
406   }
407 }
408
409 static void
410 cleanup_tmpfiles (void)
411 {
412   unlink (tmpf);
413   unlink (isof);
414 }
415
416 static void
417 make_files (void)
418 {
419   int fd, r;
420   char cmd[256];
421
422   /* Make the ISO which will contain the helper program. */
423   fd = mkstemp (isof);
424   if (fd == -1) {
425     perror (isof);
426     exit (EXIT_FAILURE);
427   }
428   close (fd);
429
430   snprintf (cmd, sizeof cmd, "mkisofs -quiet -rJT -o '%s' '%s'",
431             isof, helper);
432   r = system (cmd);
433   if (r == -1 || WEXITSTATUS(r) != 0) {
434     fprintf (stderr,
435              _("mkisofs command failed: %s\n"), cmd);
436     exit (EXIT_FAILURE);
437   }
438
439   /* Allocate the sparse file for /dev/sda. */
440   fd = mkstemp (tmpf);
441   if (fd == -1) {
442     perror (tmpf);
443     unlink (isof);
444     exit (EXIT_FAILURE);
445   }
446
447   if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
448     perror ("lseek");
449     close (fd);
450     unlink (tmpf);
451     unlink (isof);
452     exit (EXIT_FAILURE);
453   }
454
455   if (write (fd, "\0", 1) == -1) {
456     perror ("write");
457     close (fd);
458     unlink (tmpf);
459     unlink (isof);
460     exit (EXIT_FAILURE);
461   }
462
463   close (fd);
464
465   atexit (cleanup_tmpfiles);    /* Removes tmpf and isof. */
466 }