2 * Copyright (C) 2010-2011 Red Hat Inc.
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.
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.
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
39 #include "ignore-value.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
47 #if defined(HAVE_HIVEX)
49 /* Compile all the regular expressions once when the shared library is
50 * loaded. PCRE is thread safe so we're supposedly OK here if
51 * multiple threads call into the libguestfs API functions below
54 static pcre *re_fedora;
55 static pcre *re_rhel_old;
57 static pcre *re_rhel_no_minor;
58 static pcre *re_centos_old;
59 static pcre *re_centos;
60 static pcre *re_centos_no_minor;
61 static pcre *re_scientific_linux_old;
62 static pcre *re_scientific_linux;
63 static pcre *re_scientific_linux_no_minor;
64 static pcre *re_major_minor;
65 static pcre *re_aug_seq;
67 static pcre *re_first_partition;
68 static pcre *re_freebsd;
69 static pcre *re_netbsd;
71 static void compile_regexps (void) __attribute__((constructor));
72 static void free_regexps (void) __attribute__((destructor));
75 compile_regexps (void)
80 #define COMPILE(re,pattern,options) \
82 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
84 ignore_value (write (2, err, strlen (err))); \
89 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
91 "Red Hat.*release (\\d+).*Update (\\d+)", 0);
93 "Red Hat.*release (\\d+)\\.(\\d+)", 0);
94 COMPILE (re_rhel_no_minor,
95 "Red Hat.*release (\\d+)", 0);
96 COMPILE (re_centos_old,
97 "CentOS.*release (\\d+).*Update (\\d+)", 0);
99 "CentOS.*release (\\d+)\\.(\\d+)", 0);
100 COMPILE (re_centos_no_minor,
101 "CentOS.*release (\\d+)", 0);
102 COMPILE (re_scientific_linux_old,
103 "Scientific Linux.*release (\\d+).*Update (\\d+)", 0);
104 COMPILE (re_scientific_linux,
105 "Scientific Linux.*release (\\d+)\\.(\\d+)", 0);
106 COMPILE (re_scientific_linux_no_minor,
107 "Scientific Linux.*release (\\d+)", 0);
108 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
109 COMPILE (re_aug_seq, "/\\d+$", 0);
110 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
111 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
112 COMPILE (re_netbsd, "^NetBSD (\\d+)\\.(\\d+)", 0);
118 pcre_free (re_fedora);
119 pcre_free (re_rhel_old);
121 pcre_free (re_rhel_no_minor);
122 pcre_free (re_centos_old);
123 pcre_free (re_centos);
124 pcre_free (re_centos_no_minor);
125 pcre_free (re_scientific_linux_old);
126 pcre_free (re_scientific_linux);
127 pcre_free (re_scientific_linux_no_minor);
128 pcre_free (re_major_minor);
129 pcre_free (re_aug_seq);
131 pcre_free (re_freebsd);
132 pcre_free (re_netbsd);
135 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
136 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
137 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
138 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
139 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
140 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
141 const char *spec, const char *mp);
142 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
143 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
145 /* Set fs->product_name to the first line of the release file. */
147 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
148 const char *release_filename)
150 fs->product_name = guestfs___first_line_of_file (g, release_filename);
151 if (fs->product_name == NULL)
156 /* Ubuntu has /etc/lsb-release containing:
157 * DISTRIB_ID=Ubuntu # Distro
158 * DISTRIB_RELEASE=10.04 # Version
159 * DISTRIB_CODENAME=lucid
160 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
162 * [Ubuntu-derived ...] Linux Mint was found to have this:
163 * DISTRIB_ID=LinuxMint
165 * DISTRIB_CODENAME=julia
166 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
167 * Linux Mint also has /etc/linuxmint/info with more information,
168 * but we can use the LSB file.
171 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
172 * DISTRIB_ID=MandrivaLinux
173 * DISTRIB_RELEASE=2010.1
174 * DISTRIB_CODENAME=Henry_Farman
175 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
176 * Mandriva also has a normal release file called /etc/mandriva-release.
179 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
181 const char *filename = "/etc/lsb-release";
187 /* Don't trust guestfs_head_n not to break with very large files.
188 * Check the file size is something reasonable first.
190 size = guestfs_filesize (g, filename);
192 /* guestfs_filesize failed and has already set error in handle */
194 if (size > MAX_SMALL_FILE_SIZE) {
195 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
200 lines = guestfs_head_n (g, 10, filename);
204 for (i = 0; lines[i] != NULL; ++i) {
205 if (fs->distro == 0 &&
206 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
207 fs->distro = OS_DISTRO_UBUNTU;
210 else if (fs->distro == 0 &&
211 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
212 fs->distro = OS_DISTRO_LINUX_MINT;
215 else if (fs->distro == 0 &&
216 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
217 fs->distro = OS_DISTRO_MANDRIVA;
220 else if (fs->distro == 0 &&
221 STREQ (lines[i], "DISTRIB_ID=\"Mageia\"")) {
222 fs->distro = OS_DISTRO_MAGEIA;
225 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
227 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
228 fs->major_version = guestfs___parse_unsigned_int (g, major);
230 if (fs->major_version == -1) {
232 guestfs___free_string_list (lines);
235 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
237 if (fs->minor_version == -1) {
238 guestfs___free_string_list (lines);
243 else if (fs->product_name == NULL &&
244 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
245 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
246 size_t len = strlen (lines[i]) - 21 - 1;
247 fs->product_name = safe_strndup (g, &lines[i][21], len);
250 else if (fs->product_name == NULL &&
251 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
252 size_t len = strlen (lines[i]) - 20;
253 fs->product_name = safe_strndup (g, &lines[i][20], len);
258 guestfs___free_string_list (lines);
262 /* The currently mounted device is known to be a Linux root. Try to
263 * determine from this the distro, version, etc. Also parse
264 * /etc/fstab to determine the arrangement of mountpoints and
265 * associated devices.
268 guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs)
272 fs->type = OS_TYPE_LINUX;
274 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
275 r = parse_lsb_release (g, fs);
276 if (r == -1) /* error */
278 if (r == 1) /* ok - detected the release from this file */
279 goto skip_release_checks;
282 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
283 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
285 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
289 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
290 fs->distro = OS_DISTRO_FEDORA;
291 fs->major_version = guestfs___parse_unsigned_int (g, major);
293 if (fs->major_version == -1)
296 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
297 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
298 fs->distro = OS_DISTRO_RHEL;
299 fs->major_version = guestfs___parse_unsigned_int (g, major);
301 if (fs->major_version == -1) {
305 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
307 if (fs->minor_version == -1)
310 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
311 fs->distro = OS_DISTRO_RHEL;
312 fs->major_version = guestfs___parse_unsigned_int (g, major);
314 if (fs->major_version == -1)
316 fs->minor_version = 0;
318 else if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
319 match2 (g, fs->product_name, re_centos, &major, &minor)) {
320 fs->distro = OS_DISTRO_CENTOS;
321 fs->major_version = guestfs___parse_unsigned_int (g, major);
323 if (fs->major_version == -1) {
327 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
329 if (fs->minor_version == -1)
332 else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
333 fs->distro = OS_DISTRO_CENTOS;
334 fs->major_version = guestfs___parse_unsigned_int (g, major);
336 if (fs->major_version == -1)
338 fs->minor_version = 0;
340 else if (match2 (g, fs->product_name, re_scientific_linux_old, &major, &minor) ||
341 match2 (g, fs->product_name, re_scientific_linux, &major, &minor)) {
342 fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
343 fs->major_version = guestfs___parse_unsigned_int (g, major);
345 if (fs->major_version == -1) {
349 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
351 if (fs->minor_version == -1)
354 else if ((major = match1 (g, fs->product_name, re_scientific_linux_no_minor)) != NULL) {
355 fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
356 fs->major_version = guestfs___parse_unsigned_int (g, major);
358 if (fs->major_version == -1)
360 fs->minor_version = 0;
363 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
364 fs->distro = OS_DISTRO_DEBIAN;
366 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
369 if (guestfs___parse_major_minor (g, fs) == -1)
372 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
373 fs->distro = OS_DISTRO_PARDUS;
375 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
378 if (guestfs___parse_major_minor (g, fs) == -1)
381 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
382 fs->distro = OS_DISTRO_ARCHLINUX;
384 /* /etc/arch-release file is empty and I can't see a way to
385 * determine the actual release or product string.
388 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
389 fs->distro = OS_DISTRO_GENTOO;
391 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
394 if (guestfs___parse_major_minor (g, fs) == -1)
397 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
398 fs->distro = OS_DISTRO_MEEGO;
400 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
403 if (guestfs___parse_major_minor (g, fs) == -1)
406 else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
407 fs->distro = OS_DISTRO_SLACKWARE;
409 if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
412 if (guestfs___parse_major_minor (g, fs) == -1)
415 else if (guestfs_exists (g, "/etc/ttylinux-target") > 0) {
416 fs->distro = OS_DISTRO_TTYLINUX;
418 fs->product_name = guestfs___first_line_of_file (g, "/etc/ttylinux-target");
419 if (fs->product_name == NULL)
422 if (guestfs___parse_major_minor (g, fs) == -1)
425 else if (guestfs_exists (g, "/etc/SuSE-release") > 0) {
426 fs->distro = OS_DISTRO_OPENSUSE;
428 if (parse_release_file (g, fs, "/etc/SuSE-release") == -1)
431 if (guestfs___parse_major_minor (g, fs) == -1)
436 skip_release_checks:;
438 /* Determine the architecture. */
439 check_architecture (g, fs);
441 /* We already know /etc/fstab exists because it's part of the test
442 * for Linux root above. We must now parse this file to determine
443 * which filesystems are used by the operating system and how they
446 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
449 /* Determine hostname. */
450 if (check_hostname_unix (g, fs) == -1)
456 /* The currently mounted device is known to be a FreeBSD root. */
458 guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
460 fs->type = OS_TYPE_FREEBSD;
462 /* FreeBSD has no authoritative version file. The version number is
463 * in /etc/motd, which the system administrator might edit, but
464 * we'll use that anyway.
467 if (guestfs_exists (g, "/etc/motd") > 0) {
468 if (parse_release_file (g, fs, "/etc/motd") == -1)
471 if (guestfs___parse_major_minor (g, fs) == -1)
475 /* Determine the architecture. */
476 check_architecture (g, fs);
478 /* We already know /etc/fstab exists because it's part of the test above. */
479 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
482 /* Determine hostname. */
483 if (check_hostname_unix (g, fs) == -1)
489 /* The currently mounted device is maybe to be a *BSD root. */
491 guestfs___check_netbsd_root (guestfs_h *g, struct inspect_fs *fs)
494 if (guestfs_exists (g, "/etc/release") > 0) {
496 if (parse_release_file (g, fs, "/etc/release") == -1)
499 if (match2 (g, fs->product_name, re_netbsd, &major, &minor)) {
500 fs->type = OS_TYPE_NETBSD;
501 fs->major_version = guestfs___parse_unsigned_int (g, major);
503 if (fs->major_version == -1) {
507 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
509 if (fs->minor_version == -1)
516 /* Determine the architecture. */
517 check_architecture (g, fs);
519 /* We already know /etc/fstab exists because it's part of the test above. */
520 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
523 /* Determine hostname. */
524 if (check_hostname_unix (g, fs) == -1)
532 check_architecture (guestfs_h *g, struct inspect_fs *fs)
534 const char *binaries[] =
535 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
538 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
539 if (guestfs_is_file (g, binaries[i]) > 0) {
540 /* Ignore errors from file_architecture call. */
541 guestfs_error_handler_cb old_error_cb = g->error_cb;
543 char *arch = guestfs_file_architecture (g, binaries[i]);
544 g->error_cb = old_error_cb;
547 /* String will be owned by handle, freed by
548 * guestfs___free_inspect_info.
557 /* Try several methods to determine the hostname from a Linux or
558 * FreeBSD guest. Note that type and distro have been set, so we can
559 * use that information to direct the search.
562 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
566 /* Red Hat-derived would be in /etc/sysconfig/network, and
567 * Debian-derived in the file /etc/hostname. Very old Debian and
568 * SUSE use /etc/HOSTNAME. It's best to just look for each of
569 * these files in turn, rather than try anything clever based on
572 if (guestfs_is_file (g, "/etc/HOSTNAME")) {
573 fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
574 if (fs->hostname == NULL)
577 else if (guestfs_is_file (g, "/etc/hostname")) {
578 fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
579 if (fs->hostname == NULL)
582 else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
583 if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
584 check_hostname_redhat) == -1)
589 case OS_TYPE_FREEBSD:
591 /* /etc/rc.conf contains the hostname, but there is no Augeas lens
594 if (guestfs_is_file (g, "/etc/rc.conf")) {
595 if (check_hostname_freebsd (g, fs) == -1)
600 case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
601 case OS_TYPE_UNKNOWN:
603 /* nothing, keep GCC warnings happy */;
609 /* Parse the hostname from /etc/sysconfig/network. This must be called
610 * from the inspect_with_augeas wrapper.
613 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
617 /* Errors here are not fatal (RHBZ#726739), since it could be
618 * just missing HOSTNAME field in the file.
620 guestfs_error_handler_cb old_error_cb = g->error_cb;
622 hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
623 g->error_cb = old_error_cb;
625 /* This is freed by guestfs___free_inspect_info. Note that hostname
626 * could be NULL because we ignored errors above.
628 fs->hostname = hostname;
632 /* Parse the hostname from /etc/rc.conf. On FreeBSD this file
633 * contains comments, blank lines and:
634 * hostname="freebsd8.example.com"
635 * ifconfig_re0="DHCP"
640 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
642 const char *filename = "/etc/rc.conf";
647 /* Don't trust guestfs_read_lines not to break with very large files.
648 * Check the file size is something reasonable first.
650 size = guestfs_filesize (g, filename);
652 /* guestfs_filesize failed and has already set error in handle */
654 if (size > MAX_SMALL_FILE_SIZE) {
655 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
660 lines = guestfs_read_lines (g, filename);
664 for (i = 0; lines[i] != NULL; ++i) {
665 if (STRPREFIX (lines[i], "hostname=\"") ||
666 STRPREFIX (lines[i], "hostname='")) {
667 size_t len = strlen (lines[i]) - 10 - 1;
668 fs->hostname = safe_strndup (g, &lines[i][10], len);
670 } else if (STRPREFIX (lines[i], "hostname=")) {
671 size_t len = strlen (lines[i]) - 9;
672 fs->hostname = safe_strndup (g, &lines[i][9], len);
677 guestfs___free_string_list (lines);
682 check_fstab (guestfs_h *g, struct inspect_fs *fs)
684 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
688 if (lines[0] == NULL) {
689 error (g, _("could not parse /etc/fstab or empty file"));
690 guestfs___free_string_list (lines);
696 for (i = 0; lines[i] != NULL; ++i) {
697 /* Ignore comments. Only care about sequence lines which
700 if (match (g, lines[i], re_aug_seq)) {
701 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
702 char *spec = guestfs_aug_get (g, augpath);
704 guestfs___free_string_list (lines);
708 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
709 char *mp = guestfs_aug_get (g, augpath);
711 guestfs___free_string_list (lines);
716 int r = add_fstab_entry (g, fs, spec, mp);
721 guestfs___free_string_list (lines);
727 guestfs___free_string_list (lines);
731 /* Add a filesystem and possibly a mountpoint entry for
732 * the root filesystem 'fs'.
734 * 'spec' is the fstab spec field, which might be a device name or a
735 * pseudodevice or 'UUID=...' or 'LABEL=...'.
737 * 'mp' is the mount point, which could also be 'swap' or 'none'.
740 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
741 const char *spec, const char *mp)
743 /* Ignore certain mountpoints. */
744 if (STRPREFIX (mp, "/dev/") ||
745 STREQ (mp, "/dev") ||
746 STRPREFIX (mp, "/media/") ||
747 STRPREFIX (mp, "/proc/") ||
748 STREQ (mp, "/proc") ||
749 STRPREFIX (mp, "/selinux/") ||
750 STREQ (mp, "/selinux") ||
751 STRPREFIX (mp, "/sys/") ||
755 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
756 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
757 STREQ (spec, "/dev/floppy") ||
758 STREQ (spec, "/dev/cdrom"))
761 /* Resolve UUID= and LABEL= to the actual device. */
763 if (STRPREFIX (spec, "UUID="))
764 device = guestfs_findfs_uuid (g, &spec[5]);
765 else if (STRPREFIX (spec, "LABEL="))
766 device = guestfs_findfs_label (g, &spec[6]);
767 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
768 else if (STREQ (spec, "/dev/root"))
769 /* Resolve /dev/root to the current device. */
770 device = safe_strdup (g, fs->device);
771 else if (STRPREFIX (spec, "/dev/"))
772 /* Resolve guest block device names. */
773 device = resolve_fstab_device (g, spec);
775 /* If we haven't resolved the device successfully by this point,
776 * we don't care, just ignore it.
781 char *mountpoint = safe_strdup (g, mp);
783 /* Add this to the fstab entry in 'fs'.
784 * Note these are further filtered by guestfs_inspect_get_mountpoints
785 * and guestfs_inspect_get_filesystems.
787 size_t n = fs->nr_fstab + 1;
788 struct inspect_fstab_entry *p;
790 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
792 perrorf (g, "realloc");
801 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
802 fs->fstab[n-1].device = device;
803 fs->fstab[n-1].mountpoint = mountpoint;
805 debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
810 /* Resolve block device name to the libguestfs device name, eg.
811 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
812 * assumes that disks were added in the same order as they appear to
813 * the real VM, which is a reasonable assumption to make. Return
814 * anything we don't recognize unchanged.
817 resolve_fstab_device (guestfs_h *g, const char *spec)
821 char *bsddisk, *bsdslice, *bsdpart;
823 if (STRPREFIX (spec, "/dev/mapper/")) {
824 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
825 * LVs which contain '-' character:
827 * ><fs> lvcreate LV--test VG--test 32
828 * ><fs> debug ls /dev/mapper
829 * VG----test-LV----test
831 * This makes it impossible to reverse those paths directly, so
832 * we have implemented lvm_canonical_lv_name in the daemon.
834 device = guestfs_lvm_canonical_lv_name (g, spec);
836 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
837 char **devices = guestfs_list_devices (g);
842 for (count = 0; devices[count] != NULL; count++)
845 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
847 size_t len = strlen (devices[i]) + strlen (a1) + 16;
848 device = safe_malloc (g, len);
849 snprintf (device, len, "%s%s", devices[i], &a1[1]);
853 guestfs___free_string_list (devices);
855 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
856 /* FreeBSD disks are organized quite differently. See:
857 * http://www.freebsd.org/doc/handbook/disk-organization.html
858 * FreeBSD "partitions" are exposed as quasi-extended partitions
859 * numbered from 5 in Linux. I have no idea what happens when you
860 * have multiple "slices" (the FreeBSD term for MBR partitions).
862 int disk = guestfs___parse_unsigned_int (g, bsddisk);
863 int slice = guestfs___parse_unsigned_int (g, bsdslice);
864 int part = bsdpart[0] - 'a' /* counting from 0 */;
869 if (disk == -1 || disk > 26 ||
870 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
871 part < 0 || part >= 26)
874 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
878 /* Didn't match device pattern, return original spec unchanged. */
880 device = safe_strdup (g, spec);
885 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
886 * must exist). As a security measure, this bails if the file is too
887 * large for a reasonable configuration file. After the call to 'f'
891 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
892 int (*f) (guestfs_h *, struct inspect_fs *))
894 /* Security: Refuse to do this if filename is too large. */
895 int64_t size = guestfs_filesize (g, filename);
897 /* guestfs_filesize failed and has already set error in handle */
899 if (size > MAX_AUGEAS_FILE_SIZE) {
900 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
905 /* If !feature_available (g, "augeas") then the next call will fail.
906 * Arguably we might want to fall back to a non-Augeas method in
909 if (guestfs_aug_init (g, "/", 16|32) == -1)
914 /* Tell Augeas to only load one file (thanks Raphaƫl Pinson). */
915 char buf[strlen (filename) + 64];
916 snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
918 if (guestfs_aug_rm (g, buf) == -1)
921 if (guestfs_aug_load (g) == -1)
927 guestfs_aug_close (g);
932 #endif /* defined(HAVE_HIVEX) */