summaryrefslogtreecommitdiffstats
path: root/examples/copy_over.c
blob: ec0261683281c3fee33c70d56a7a4c3ad98ddef2 (plain)
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
/* Copy a directory from one libvirt guest to another.
 *
 * This is a more substantial example of using the libguestfs API,
 * demonstrating amongst other things:
 *
 * - using multiple handles with threads
 * - upload and downloading (using a pipe between handles)
 * - inspection
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>

#include <pthread.h>

#include <guestfs.h>

struct threaddata {
  const char *src;
  const char *srcdir;
  int fd;
  pthread_t mainthread;
};

static void *start_srcthread (void *);
static int open_guest (guestfs_h *g, const char *dom, int readonly);
static int64_t timeval_diff (const struct timeval *x, const struct timeval *y);
static int compare_keys_len (const void *p1, const void *p2);
static size_t count_strings (char *const *argv);

static void
usage (void)
{
  fprintf (stderr,
    "Usage: copy_over source srcdir dest destdir\n"
    "\n"
    "  source  : the source domain (a libvirt guest name)\n"
    "  srcdir  : the directory to copy from the source guest\n"
    "  dest    : the destination domain (a libvirt guest name)\n"
    "  destdir : the destination directory (must exist at destination)\n"
    "\n"
    "eg: copy_over Src /home/rjones Dest /tmp/dir\n"
    "would copy /home/rjones from Src to /tmp/dir on Dest\n"
    "\n"
    "The destination guest cannot be running.\n");
}

int
main (int argc, char *argv[])
{
  const char *src, *srcdir, *dest, *destdir;
  guestfs_h *destg;
  int fd[2];
  pthread_t srcthread;
  struct threaddata threaddata;
  int err;
  char fdname[128];
  struct timeval start_t, end_t;
  int64_t ms;

  if (argc != 5) {
    usage ();
    exit (EXIT_FAILURE);
  }

  src = argv[1];
  srcdir = argv[2];
  dest = argv[3];
  destdir = argv[4];

  /* Instead of downloading to local disk and uploading, we are going
   * to connect the source download and destination upload using a
   * pipe.  Create that pipe.
   */
  if (pipe (fd) == -1) {
    perror ("pipe");
    exit (EXIT_FAILURE);
  }

  /* We don't want the pipe to be passed to subprocesses. */
  if (fcntl (fd[0], F_SETFD, FD_CLOEXEC) == -1 ||
      fcntl (fd[1], F_SETFD, FD_CLOEXEC) == -1) {
    perror ("fcntl");
    exit (EXIT_FAILURE);
  }

  /* The libguestfs API is synchronous, so if we want to use two
   * handles concurrently, then we have to have two threads.  In this
   * case the main thread (this one) is handling the destination
   * domain (uploading), and we create one more thread to handle the
   * source domain (downloading).
   */
  threaddata.src = src;
  threaddata.srcdir = srcdir;
  threaddata.fd = fd[1];
  threaddata.mainthread = pthread_self ();
  err = pthread_create (&srcthread, NULL, start_srcthread, &threaddata);
  if (err != 0) {
    fprintf (stderr, "pthread_create: %s\n", strerror (err));
    exit (EXIT_FAILURE);
  }

  /* Open the destination domain. */
  destg = guestfs_create ();
  if (!destg) {
    perror ("failed to create libguestfs handle");
    pthread_cancel (srcthread);
    exit (EXIT_FAILURE);
  }
  if (open_guest (destg, dest, 0) == -1) {
    pthread_cancel (srcthread);
    exit (EXIT_FAILURE);
  }

  gettimeofday (&start_t, NULL);

  /* Begin the upload. */
  snprintf (fdname, sizeof fdname, "/dev/fd/%d", fd[0]);
  if (guestfs_tar_in (destg, fdname, destdir) == -1) {
    pthread_cancel (srcthread);
    exit (EXIT_FAILURE);
  }

  /* Close our end of the pipe.  The other thread will close the
   * other side of the pipe.
   */
  close (fd[0]);

  /* Wait for the other thread to finish. */
  err = pthread_join (srcthread, NULL);
  if (err != 0) {
    fprintf (stderr, "pthread_join: %s\n", strerror (err));
    exit (EXIT_FAILURE);
  }

  /* Clean up. */
  if (guestfs_shutdown (destg) == -1)
    exit (EXIT_FAILURE);
  guestfs_close (destg);

  gettimeofday (&end_t, NULL);

  /* Print the elapsed time. */
  ms = timeval_diff (&start_t, &end_t);
  printf ("copy finished, elapsed time (excluding launch) was "
          "%" PRIi64 ".%03" PRIi64 " s\n",
          ms / 1000, ms % 1000);

  exit (EXIT_SUCCESS);
}

static void *
start_srcthread (void *arg)
{
  struct threaddata *threaddata = arg;
  guestfs_h *srcg;
  char fdname[128];

  /* Open the source domain. */
  srcg = guestfs_create ();
  if (!srcg) {
    perror ("failed to create libguestfs handle");
    pthread_cancel (threaddata->mainthread);
    exit (EXIT_FAILURE);
  }
  if (open_guest (srcg, threaddata->src, 1) == -1) {
    pthread_cancel (threaddata->mainthread);
    exit (EXIT_FAILURE);
  }

  /* Begin the download. */
  snprintf (fdname, sizeof fdname, "/dev/fd/%d", threaddata->fd);
  if (guestfs_tar_out (srcg, threaddata->srcdir, fdname) == -1) {
    pthread_cancel (threaddata->mainthread);
    exit (EXIT_FAILURE);
  }

  /* Close the pipe; this will cause the receiver to finish the upload. */
  if (close (threaddata->fd) == -1) {
    pthread_cancel (threaddata->mainthread);
    exit (EXIT_FAILURE);
  }

  /* Clean up. */
  guestfs_close (srcg);

  return NULL;
}

/* This function deals with the complexity of adding the domain,
 * launching the handle, and mounting up filesystems.  See
 * 'examples/inspect_vm.c' to understand how this works.
 */
static int
open_guest (guestfs_h *g, const char *dom, int readonly)
{
  char **roots, *root, **mountpoints;
  size_t i;

  /* Use libvirt to find the guest disks and add them to the handle. */
  if (guestfs_add_domain (g, dom,
                          GUESTFS_ADD_DOMAIN_READONLY, readonly,
                          -1) == -1)
    return -1;

  if (guestfs_launch (g) == -1)
    return -1;

  /* Inspect the guest, looking for operating systems. */
  roots = guestfs_inspect_os (g);
  if (roots == NULL)
    return -1;

  if (roots[0] == NULL || roots[1] != NULL) {
    fprintf (stderr, "copy_over: %s: no operating systems or multiple operating systems found\n", dom);
    return -1;
  }

  root = roots[0];

  /* Mount up the filesystems (like 'guestfish -i'). */
  mountpoints = guestfs_inspect_get_mountpoints (g, root);
  if (mountpoints == NULL)
    return -1;

  qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
         compare_keys_len);
  for (i = 0; mountpoints[i] != NULL; i += 2) {
    /* Ignore failures from this call, since bogus entries can
     * appear in the guest's /etc/fstab.
     */
    (readonly ? guestfs_mount_ro : guestfs_mount)
      (g, mountpoints[i+1], mountpoints[i]);
    free (mountpoints[i]);
    free (mountpoints[i+1]);
  }

  free (mountpoints);

  free (root);
  free (roots);

  /* Everything ready, no error. */
  return 0;
}

/* Compute Y - X and return the result in milliseconds.
 * Approximately the same as this code:
 * http://www.mpp.mpg.de/~huber/util/timevaldiff.c
 */
static int64_t
timeval_diff (const struct timeval *x, const struct timeval *y)
{
  int64_t msec;

  msec = (y->tv_sec - x->tv_sec) * 1000;
  msec += (y->tv_usec - x->tv_usec) / 1000;
  return msec;
}

static int
compare_keys_len (const void *p1, const void *p2)
{
  const char *key1 = * (char * const *) p1;
  const char *key2 = * (char * const *) p2;
  return strlen (key1) - strlen (key2);
}

static size_t
count_strings (char *const *argv)
{
  size_t c;

  for (c = 0; argv[c]; ++c)
    ;
  return c;
}