e058dbce81b6f8805f272122149c9749c21afa2f
[libguestfs.git] / test-tool / test-tool.c
1 /* libguestfs-test-tool
2  * Copyright (C) 2009-2011 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 600
68
69 static int timeout = DEFAULT_TIMEOUT;
70 static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX";
71 static guestfs_h *g;
72
73 static void make_files (void);
74 static void set_qemu (const char *path, int use_wrapper);
75
76 static void
77 usage (void)
78 {
79   printf (_("libguestfs-test-tool: interactive test tool\n"
80             "Copyright (C) 2009-2011 Red Hat Inc.\n"
81             "Usage:\n"
82             "  libguestfs-test-tool [--options]\n"
83             "Options:\n"
84             "  --help         Display usage\n"
85             "  --qemudir dir  Specify QEMU source directory\n"
86             "  --qemu qemu    Specify QEMU binary\n"
87             "  --timeout n\n"
88             "  -t n           Set launch timeout (default: %d seconds)\n"
89             ),
90           DEFAULT_TIMEOUT);
91 }
92
93 extern char **environ;
94
95 int
96 main (int argc, char *argv[])
97 {
98   setlocale (LC_ALL, "");
99   bindtextdomain (PACKAGE, LOCALEBASEDIR);
100   textdomain (PACKAGE);
101
102   static const char *options = "t:?";
103   static const struct option long_options[] = {
104     { "help", 0, 0, '?' },
105     { "qemu", 1, 0, 0 },
106     { "qemudir", 1, 0, 0 },
107     { "timeout", 1, 0, 't' },
108     { 0, 0, 0, 0 }
109   };
110   int c;
111   int option_index;
112   int i;
113   struct guestfs_version *vers;
114
115   for (;;) {
116     c = getopt_long (argc, argv, options, long_options, &option_index);
117     if (c == -1) break;
118
119     switch (c) {
120     case 0:                     /* options which are long only */
121       if (STREQ (long_options[option_index].name, "qemu"))
122         set_qemu (optarg, 0);
123       else if (STREQ (long_options[option_index].name, "qemudir"))
124         set_qemu (optarg, 1);
125       else {
126         fprintf (stderr,
127                  _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
128                  long_options[option_index].name, option_index);
129         exit (EXIT_FAILURE);
130       }
131       break;
132
133     case 't':
134       if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
135         fprintf (stderr,
136                  _("libguestfs-test-tool: invalid timeout: %s\n"),
137                  optarg);
138         exit (EXIT_FAILURE);
139       }
140       break;
141
142     case '?':
143       usage ();
144       exit (EXIT_SUCCESS);
145
146     default:
147       fprintf (stderr,
148                _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
149                c);
150       exit (EXIT_FAILURE);
151     }
152   }
153
154   make_files ();
155
156   printf ("===== Test starts here =====\n");
157
158   /* Must set LIBGUESTFS_DEBUG=1 */
159   setenv ("LIBGUESTFS_DEBUG", "1", 1);
160
161   /* Print out any environment variables which may relate to this test. */
162   for (i = 0; environ[i] != NULL; ++i)
163     if (STREQLEN (environ[i], "LIBGUESTFS_", 11))
164       printf ("%s\n", environ[i]);
165   for (i = 0; environ[i] != NULL; ++i)
166     if (STREQLEN (environ[i], "FEBOOTSTRAP_", 12))
167       printf ("%s\n", environ[i]);
168   printf ("TMPDIR=%s\n", getenv ("TMPDIR") ? : "(not set)");
169
170   /* Create the handle and configure it. */
171   g = guestfs_create ();
172   if (g == NULL) {
173     fprintf (stderr,
174              _("libguestfs-test-tool: failed to create libguestfs handle\n"));
175     exit (EXIT_FAILURE);
176   }
177   if (guestfs_add_drive_opts (g, tmpf,
178                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
179                               -1) == -1) {
180     fprintf (stderr,
181              _("libguestfs-test-tool: failed to add drive '%s'\n"),
182              tmpf);
183     exit (EXIT_FAILURE);
184   }
185
186   /* Print any version info etc. */
187   vers = guestfs_version (g);
188   if (vers == NULL) {
189     fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
190     exit (EXIT_FAILURE);
191   }
192   printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
193           vers->major, vers->minor, vers->release, vers->extra);
194   guestfs_free_version (vers);
195
196   printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
197   printf ("guestfs_get_attach_method: %s\n",
198           guestfs_get_attach_method (g) ? : "(null)");
199   printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
200   printf ("guestfs_get_direct: %d\n", guestfs_get_direct (g));
201   printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
202   printf ("guestfs_get_network: %d\n", guestfs_get_network (g));
203   printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
204   printf ("guestfs_get_pgroup: %d\n", guestfs_get_pgroup (g));
205   printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
206   printf ("guestfs_get_recovery_proc: %d\n",
207           guestfs_get_recovery_proc (g));
208   printf ("guestfs_get_selinux: %d\n", guestfs_get_selinux (g));
209   printf ("guestfs_get_smp: %d\n", guestfs_get_smp (g));
210   printf ("guestfs_get_trace: %d\n", guestfs_get_trace (g));
211   printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
212
213   printf ("host_cpu: %s\n", host_cpu);
214
215   /* Launch the guest handle. */
216   printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
217   fflush (stdout);
218
219   alarm (timeout);
220
221   if (guestfs_launch (g) == -1) {
222     fprintf (stderr,
223              _("libguestfs-test-tool: failed to launch appliance\n"));
224     exit (EXIT_FAILURE);
225   }
226
227   alarm (0);
228
229   printf ("Guest launched OK.\n");
230   fflush (stdout);
231
232   /* Create the filesystem and mount everything. */
233   if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) {
234     fprintf (stderr,
235              _("libguestfs-test-tool: failed to run part-disk\n"));
236     exit (EXIT_FAILURE);
237   }
238
239   if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
240     fprintf (stderr,
241              _("libguestfs-test-tool: failed to mkfs.ext2\n"));
242     exit (EXIT_FAILURE);
243   }
244
245   if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) {
246     fprintf (stderr,
247              _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
248     exit (EXIT_FAILURE);
249   }
250
251   /* Touch a file. */
252   if (guestfs_touch (g, "/hello") == -1) {
253     fprintf (stderr,
254              _("libguestfs-test-tool: failed to touch file\n"));
255     exit (EXIT_FAILURE);
256   }
257
258   /* Booted and performed some simple operations -- success! */
259   printf ("===== TEST FINISHED OK =====\n");
260   exit (EXIT_SUCCESS);
261 }
262
263 static char qemuwrapper[] = P_tmpdir "/libguestfs-test-tool-wrapper-XXXXXX";
264
265 static void
266 cleanup_wrapper (void)
267 {
268   unlink (qemuwrapper);
269 }
270
271 /* Handle the --qemu and --qemudir parameters.  use_wrapper is true
272  * in the --qemudir (source directory) case, where we have to create
273  * a wrapper shell script.
274  */
275 static void
276 set_qemu (const char *path, int use_wrapper)
277 {
278   char buffer[PATH_MAX];
279   struct stat statbuf;
280   int fd;
281   FILE *fp;
282
283   if (getenv ("LIBGUESTFS_QEMU")) {
284     fprintf (stderr,
285     _("LIBGUESTFS_QEMU environment variable is already set, so\n"
286       "--qemu/--qemudir options cannot be used.\n"));
287     exit (EXIT_FAILURE);
288   }
289
290   if (!use_wrapper) {
291     if (access (path, X_OK) == -1) {
292       fprintf (stderr,
293                _("Binary '%s' does not exist or is not executable\n"),
294                path);
295       exit (EXIT_FAILURE);
296     }
297
298     setenv ("LIBGUESTFS_QEMU", path, 1);
299     return;
300   }
301
302   /* This should be a source directory, so check it. */
303   snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
304   if (stat (buffer, &statbuf) == -1 ||
305       !S_ISDIR (statbuf.st_mode)) {
306     fprintf (stderr,
307              _("%s: does not look like a qemu source directory\n"),
308              path);
309     exit (EXIT_FAILURE);
310   }
311
312   /* Make a wrapper script. */
313   fd = mkstemp (qemuwrapper);
314   if (fd == -1) {
315     perror (qemuwrapper);
316     exit (EXIT_FAILURE);
317   }
318
319   fchmod (fd, 0700);
320
321   fp = fdopen (fd, "w");
322   fprintf (fp,
323            "#!/bin/sh -\n"
324            "qemudir='%s'\n"
325            "\"$qemudir\"/",
326            path);
327
328   /* Select the right qemu binary for the wrapper script. */
329 #ifdef __i386__
330   fprintf (fp, "i386-softmmu/qemu");
331 #else
332   fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
333 #endif
334
335   fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
336
337   fclose (fp);
338
339   setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
340   atexit (cleanup_wrapper);
341 }
342
343 static void
344 cleanup_tmpfiles (void)
345 {
346   unlink (tmpf);
347 }
348
349 static void
350 make_files (void)
351 {
352   int fd;
353
354   /* Allocate the sparse file for /dev/sda. */
355   fd = mkstemp (tmpf);
356   if (fd == -1) {
357     perror (tmpf);
358     exit (EXIT_FAILURE);
359   }
360
361   if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
362     perror ("lseek");
363     close (fd);
364     unlink (tmpf);
365     exit (EXIT_FAILURE);
366   }
367
368   if (write (fd, "\0", 1) == -1) {
369     perror ("write");
370     close (fd);
371     unlink (tmpf);
372     exit (EXIT_FAILURE);
373   }
374
375   close (fd);
376
377   atexit (cleanup_tmpfiles);    /* Removes tmpf. */
378 }