md: Inspect MD devices
[libguestfs.git] / src / inspect.c
1 /* libguestfs
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <endian.h>
31
32 #include <pcre.h>
33
34 #ifdef HAVE_HIVEX
35 #include <hivex.h>
36 #endif
37
38 #include "c-ctype.h"
39 #include "ignore-value.h"
40 #include "xstrtol.h"
41
42 #include "guestfs.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
46
47 #if defined(HAVE_HIVEX)
48
49 /* The main inspection code. */
50 char **
51 guestfs__inspect_os (guestfs_h *g)
52 {
53   /* Remove any information previously stored in the handle. */
54   guestfs___free_inspect_info (g);
55
56   if (guestfs_umount_all (g) == -1)
57     return NULL;
58
59   /* Iterate over all possible devices.  Try to mount each
60    * (read-only).  Examine ones which contain filesystems and add that
61    * information to the handle.
62    */
63   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
64   char **devices = guestfs_list_devices (g);
65   if (devices == NULL)
66     return NULL;
67
68   size_t i;
69   for (i = 0; devices[i] != NULL; ++i) {
70     if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
71       guestfs___free_string_list (devices);
72       guestfs___free_inspect_info (g);
73       return NULL;
74     }
75   }
76   guestfs___free_string_list (devices);
77
78   /* Look at all partitions. */
79   char **partitions = guestfs_list_partitions (g);
80   if (partitions == NULL) {
81     guestfs___free_inspect_info (g);
82     return NULL;
83   }
84
85   for (i = 0; partitions[i] != NULL; ++i) {
86     if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
87       guestfs___free_string_list (partitions);
88       guestfs___free_inspect_info (g);
89       return NULL;
90     }
91   }
92   guestfs___free_string_list (partitions);
93
94   /* Look at MD devices. */
95   char **mds = guestfs_list_md_devices (g);
96   if (mds == NULL) {
97     guestfs___free_inspect_info (g);
98     return NULL;
99   }
100
101   for (i = 0; mds[i] != NULL; ++i) {
102     if (guestfs___check_for_filesystem_on (g, mds[i], 0, i+1) == -1) {
103       guestfs___free_string_list (mds);
104       guestfs___free_inspect_info (g);
105       return NULL;
106     }
107   }
108   guestfs___free_string_list (mds);
109
110   /* Look at all LVs. */
111   if (guestfs___feature_available (g, "lvm2")) {
112     char **lvs;
113     lvs = guestfs_lvs (g);
114     if (lvs == NULL) {
115       guestfs___free_inspect_info (g);
116       return NULL;
117     }
118
119     for (i = 0; lvs[i] != NULL; ++i) {
120       if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
121         guestfs___free_string_list (lvs);
122         guestfs___free_inspect_info (g);
123         return NULL;
124       }
125     }
126     guestfs___free_string_list (lvs);
127   }
128
129   /* At this point we have, in the handle, a list of all filesystems
130    * found and data about each one.  Now we assemble the list of
131    * filesystems which are root devices and return that to the user.
132    * Fall through to guestfs__inspect_get_roots to do that.
133    */
134   char **ret = guestfs__inspect_get_roots (g);
135   if (ret == NULL)
136     guestfs___free_inspect_info (g);
137   return ret;
138 }
139
140 static int
141 compare_strings (const void *vp1, const void *vp2)
142 {
143   const char *s1 = * (char * const *) vp1;
144   const char *s2 = * (char * const *) vp2;
145
146   return strcmp (s1, s2);
147 }
148
149 char **
150 guestfs__inspect_get_roots (guestfs_h *g)
151 {
152   /* NB. Doesn't matter if g->nr_fses == 0.  We just return an empty
153    * list in this case.
154    */
155
156   size_t i;
157   size_t count = 0;
158   for (i = 0; i < g->nr_fses; ++i)
159     if (g->fses[i].is_root)
160       count++;
161
162   char **ret = calloc (count+1, sizeof (char *));
163   if (ret == NULL) {
164     perrorf (g, "calloc");
165     return NULL;
166   }
167
168   count = 0;
169   for (i = 0; i < g->nr_fses; ++i) {
170     if (g->fses[i].is_root) {
171       ret[count] = safe_strdup (g, g->fses[i].device);
172       count++;
173     }
174   }
175   ret[count] = NULL;
176
177   qsort (ret, count, sizeof (char *), compare_strings);
178
179   return ret;
180 }
181
182 char *
183 guestfs__inspect_get_type (guestfs_h *g, const char *root)
184 {
185   struct inspect_fs *fs = guestfs___search_for_root (g, root);
186   if (!fs)
187     return NULL;
188
189   char *ret;
190   switch (fs->type) {
191   case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
192   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
193   case OS_TYPE_NETBSD: ret = safe_strdup (g, "netbsd"); break;
194   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
195   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
196   }
197
198   return ret;
199 }
200
201 char *
202 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
203 {
204   struct inspect_fs *fs = guestfs___search_for_root (g, root);
205   if (!fs)
206     return NULL;
207
208   return safe_strdup (g, fs->arch ? : "unknown");
209 }
210
211 char *
212 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
213 {
214   struct inspect_fs *fs = guestfs___search_for_root (g, root);
215   if (!fs)
216     return NULL;
217
218   char *ret;
219   switch (fs->distro) {
220   case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
221   case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
222   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
223   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
224   case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
225   case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
226   case OS_DISTRO_MAGEIA: ret = safe_strdup (g, "mageia"); break;
227   case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
228   case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
229   case OS_DISTRO_OPENSUSE: ret = safe_strdup (g, "opensuse"); break;
230   case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
231   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
232   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
233   case OS_DISTRO_SCIENTIFIC_LINUX: ret = safe_strdup (g, "scientificlinux"); break;
234   case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
235   case OS_DISTRO_TTYLINUX: ret = safe_strdup (g, "ttylinux"); break;
236   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
237   case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
238   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
239   }
240
241   return ret;
242 }
243
244 int
245 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
246 {
247   struct inspect_fs *fs = guestfs___search_for_root (g, root);
248   if (!fs)
249     return -1;
250
251   return fs->major_version;
252 }
253
254 int
255 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
256 {
257   struct inspect_fs *fs = guestfs___search_for_root (g, root);
258   if (!fs)
259     return -1;
260
261   return fs->minor_version;
262 }
263
264 char *
265 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
266 {
267   struct inspect_fs *fs = guestfs___search_for_root (g, root);
268   if (!fs)
269     return NULL;
270
271   return safe_strdup (g, fs->product_name ? : "unknown");
272 }
273
274 char *
275 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
276 {
277   struct inspect_fs *fs = guestfs___search_for_root (g, root);
278   if (!fs)
279     return NULL;
280
281   return safe_strdup (g, fs->product_variant ? : "unknown");
282 }
283
284 char *
285 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
286 {
287   struct inspect_fs *fs = guestfs___search_for_root (g, root);
288   if (!fs)
289     return NULL;
290
291   if (!fs->windows_systemroot) {
292     error (g, _("not a Windows guest, or systemroot could not be determined"));
293     return NULL;
294   }
295
296   return safe_strdup (g, fs->windows_systemroot);
297 }
298
299 char *
300 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
301                                                   const char *root)
302 {
303   struct inspect_fs *fs = guestfs___search_for_root (g, root);
304   if (!fs)
305     return NULL;
306
307   if (!fs->windows_current_control_set) {
308     error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
309     return NULL;
310   }
311
312   return safe_strdup (g, fs->windows_current_control_set);
313 }
314
315 char *
316 guestfs__inspect_get_format (guestfs_h *g, const char *root)
317 {
318   struct inspect_fs *fs = guestfs___search_for_root (g, root);
319   if (!fs)
320     return NULL;
321
322   char *ret;
323   switch (fs->format) {
324   case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
325   case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
326   case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
327   }
328
329   return ret;
330 }
331
332 int
333 guestfs__inspect_is_live (guestfs_h *g, const char *root)
334 {
335   struct inspect_fs *fs = guestfs___search_for_root (g, root);
336   if (!fs)
337     return -1;
338
339   return fs->is_live_disk;
340 }
341
342 int
343 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
344 {
345   struct inspect_fs *fs = guestfs___search_for_root (g, root);
346   if (!fs)
347     return -1;
348
349   return fs->is_netinst_disk;
350 }
351
352 int
353 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
354 {
355   struct inspect_fs *fs = guestfs___search_for_root (g, root);
356   if (!fs)
357     return -1;
358
359   return fs->is_multipart_disk;
360 }
361
362 char **
363 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
364 {
365   struct inspect_fs *fs = guestfs___search_for_root (g, root);
366   if (!fs)
367     return NULL;
368
369   char **ret;
370
371   /* If no fstab information (Windows) return just the root. */
372   if (fs->nr_fstab == 0) {
373     ret = calloc (3, sizeof (char *));
374     ret[0] = safe_strdup (g, "/");
375     ret[1] = safe_strdup (g, root);
376     ret[2] = NULL;
377     return ret;
378   }
379
380 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
381   size_t i, count = 0;
382   for (i = 0; i < fs->nr_fstab; ++i)
383     if (CRITERION)
384       count++;
385
386   /* Hashtables have 2N+1 entries. */
387   ret = calloc (2*count+1, sizeof (char *));
388   if (ret == NULL) {
389     perrorf (g, "calloc");
390     return NULL;
391   }
392
393   count = 0;
394   for (i = 0; i < fs->nr_fstab; ++i)
395     if (CRITERION) {
396       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
397       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
398       count++;
399     }
400 #undef CRITERION
401
402   return ret;
403 }
404
405 char **
406 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
407 {
408   struct inspect_fs *fs = guestfs___search_for_root (g, root);
409   if (!fs)
410     return NULL;
411
412   char **ret;
413
414   /* If no fstab information (Windows) return just the root. */
415   if (fs->nr_fstab == 0) {
416     ret = calloc (2, sizeof (char *));
417     ret[0] = safe_strdup (g, root);
418     ret[1] = NULL;
419     return ret;
420   }
421
422   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
423   if (ret == NULL) {
424     perrorf (g, "calloc");
425     return NULL;
426   }
427
428   size_t i;
429   for (i = 0; i < fs->nr_fstab; ++i)
430     ret[i] = safe_strdup (g, fs->fstab[i].device);
431
432   return ret;
433 }
434
435 char **
436 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
437 {
438   char **ret;
439   size_t i, count;
440   struct inspect_fs *fs;
441
442   fs = guestfs___search_for_root (g, root);
443   if (!fs)
444     return NULL;
445
446   /* If no drive mappings, return an empty hashtable. */
447   if (!fs->drive_mappings)
448     count = 0;
449   else {
450     for (count = 0; fs->drive_mappings[count] != NULL; count++)
451       ;
452   }
453
454   ret = calloc (count+1, sizeof (char *));
455   if (ret == NULL) {
456     perrorf (g, "calloc");
457     return NULL;
458   }
459
460   /* We need to make a deep copy of the hashtable since the caller
461    * will free it.
462    */
463   for (i = 0; i < count; ++i)
464     ret[i] = safe_strdup (g, fs->drive_mappings[i]);
465
466   ret[count] = NULL;
467
468   return ret;
469 }
470
471 char *
472 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
473 {
474   struct inspect_fs *fs = guestfs___search_for_root (g, root);
475   if (!fs)
476     return NULL;
477
478   char *ret;
479   switch (fs->package_format) {
480   case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
481   case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
482   case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
483   case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
484   case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
485   case OS_PACKAGE_FORMAT_PKGSRC: ret = safe_strdup (g, "pkgsrc"); break;
486   case OS_PACKAGE_FORMAT_UNKNOWN:
487   default:
488     ret = safe_strdup (g, "unknown");
489     break;
490   }
491
492   return ret;
493 }
494
495 char *
496 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
497 {
498   struct inspect_fs *fs = guestfs___search_for_root (g, root);
499   if (!fs)
500     return NULL;
501
502   char *ret;
503   switch (fs->package_management) {
504   case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
505   case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
506   case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
507   case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
508   case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
509   case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
510   case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
511   case OS_PACKAGE_MANAGEMENT_ZYPPER: ret = safe_strdup (g, "zypper"); break;
512   case OS_PACKAGE_MANAGEMENT_UNKNOWN:
513   default:
514     ret = safe_strdup (g, "unknown");
515     break;
516   }
517
518   return ret;
519 }
520
521 char *
522 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
523 {
524   struct inspect_fs *fs = guestfs___search_for_root (g, root);
525   if (!fs)
526     return NULL;
527
528   return safe_strdup (g, fs->hostname ? : "unknown");
529 }
530
531 #else /* no hivex at compile time */
532
533 /* XXX These functions should be in an optgroup. */
534
535 #define NOT_IMPL(r)                                                     \
536   error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
537   return r
538
539 char **
540 guestfs__inspect_os (guestfs_h *g)
541 {
542   NOT_IMPL(NULL);
543 }
544
545 char **
546 guestfs__inspect_get_roots (guestfs_h *g)
547 {
548   NOT_IMPL(NULL);
549 }
550
551 char *
552 guestfs__inspect_get_type (guestfs_h *g, const char *root)
553 {
554   NOT_IMPL(NULL);
555 }
556
557 char *
558 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
559 {
560   NOT_IMPL(NULL);
561 }
562
563 char *
564 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
565 {
566   NOT_IMPL(NULL);
567 }
568
569 int
570 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
571 {
572   NOT_IMPL(-1);
573 }
574
575 int
576 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
577 {
578   NOT_IMPL(-1);
579 }
580
581 char *
582 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
583 {
584   NOT_IMPL(NULL);
585 }
586
587 char *
588 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
589 {
590   NOT_IMPL(NULL);
591 }
592
593 char *
594 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
595 {
596   NOT_IMPL(NULL);
597 }
598
599 char *
600 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
601                                                   const char *root)
602 {
603   NOT_IMPL(NULL);
604 }
605
606 char **
607 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
608 {
609   NOT_IMPL(NULL);
610 }
611
612 char **
613 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
614 {
615   NOT_IMPL(NULL);
616 }
617
618 char **
619 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
620 {
621   NOT_IMPL(NULL);
622 }
623
624 char *
625 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
626 {
627   NOT_IMPL(NULL);
628 }
629
630 char *
631 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
632 {
633   NOT_IMPL(NULL);
634 }
635
636 char *
637 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
638 {
639   NOT_IMPL(NULL);
640 }
641
642 char *
643 guestfs__inspect_get_format (guestfs_h *g, const char *root)
644 {
645   NOT_IMPL(NULL);
646 }
647
648 int
649 guestfs__inspect_is_live (guestfs_h *g, const char *root)
650 {
651   NOT_IMPL(-1);
652 }
653
654 int
655 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
656 {
657   NOT_IMPL(-1);
658 }
659
660 int
661 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
662 {
663   NOT_IMPL(-1);
664 }
665
666 #endif /* no hivex at compile time */
667
668 void
669 guestfs___free_inspect_info (guestfs_h *g)
670 {
671   size_t i;
672   for (i = 0; i < g->nr_fses; ++i) {
673     free (g->fses[i].device);
674     free (g->fses[i].product_name);
675     free (g->fses[i].product_variant);
676     free (g->fses[i].arch);
677     free (g->fses[i].hostname);
678     free (g->fses[i].windows_systemroot);
679     free (g->fses[i].windows_current_control_set);
680     size_t j;
681     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
682       free (g->fses[i].fstab[j].device);
683       free (g->fses[i].fstab[j].mountpoint);
684     }
685     free (g->fses[i].fstab);
686     if (g->fses[i].drive_mappings)
687       guestfs___free_string_list (g->fses[i].drive_mappings);
688   }
689   free (g->fses);
690   g->nr_fses = 0;
691   g->fses = NULL;
692 }
693
694 /* In the Perl code this is a public function. */
695 int
696 guestfs___feature_available (guestfs_h *g, const char *feature)
697 {
698   /* If there's an error we should ignore it, so to do that we have to
699    * temporarily replace the error handler with a null one.
700    */
701   guestfs_error_handler_cb old_error_cb = g->error_cb;
702   g->error_cb = NULL;
703
704   const char *groups[] = { feature, NULL };
705   int r = guestfs_available (g, (char * const *) groups);
706
707   g->error_cb = old_error_cb;
708
709   return r == 0 ? 1 : 0;
710 }
711
712 /* Download a guest file to a local temporary file.  The file is
713  * cached in the temporary directory, and is not downloaded again.
714  *
715  * The name of the temporary (downloaded) file is returned.  The
716  * caller must free the pointer, but does *not* need to delete the
717  * temporary file.  It will be deleted when the handle is closed.
718  *
719  * Refuse to download the guest file if it is larger than max_size.
720  * On this and other errors, NULL is returned.
721  *
722  * There is actually one cache per 'struct inspect_fs *' in order
723  * to handle the case of multiple roots.
724  */
725 char *
726 guestfs___download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
727                            const char *filename,
728                            const char *basename, int64_t max_size)
729 {
730   char *r;
731   int fd;
732   char devfd[32];
733   int64_t size;
734
735   /* Make the basename unique by prefixing it with the fs number. */
736   if (asprintf (&r, "%s/%td-%s", g->tmpdir, fs - g->fses, basename) == -1) {
737     perrorf (g, "asprintf");
738     return NULL;
739   }
740
741   /* If the file has already been downloaded, return. */
742   if (access (r, R_OK) == 0)
743     return r;
744
745   /* Check size of remote file. */
746   size = guestfs_filesize (g, filename);
747   if (size == -1)
748     /* guestfs_filesize failed and has already set error in handle */
749     goto error;
750   if (size > max_size) {
751     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
752            filename, size);
753     goto error;
754   }
755
756   fd = open (r, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0600);
757   if (fd == -1) {
758     perrorf (g, "open: %s", r);
759     goto error;
760   }
761
762   snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd);
763
764   if (guestfs_download (g, filename, devfd) == -1) {
765     unlink (r);
766     close (fd);
767     goto error;
768   }
769
770   if (close (fd) == -1) {
771     perrorf (g, "close: %s", r);
772     unlink (r);
773     goto error;
774   }
775
776   return r;
777
778  error:
779   free (r);
780   return NULL;
781 }
782
783 struct inspect_fs *
784 guestfs___search_for_root (guestfs_h *g, const char *root)
785 {
786   if (g->nr_fses == 0) {
787     error (g, _("no inspection data: call guestfs_inspect_os first"));
788     return NULL;
789   }
790
791   size_t i;
792   struct inspect_fs *fs;
793   for (i = 0; i < g->nr_fses; ++i) {
794     fs = &g->fses[i];
795     if (fs->is_root && STREQ (root, fs->device))
796       return fs;
797   }
798
799   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
800          root);
801   return NULL;
802 }