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