X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=src%2Finspect.c;h=531e3f76dbb970e50bdee3b9545b021dc91b16f8;hp=6938c106db203d6efb71de476cb21bf69c09be43;hb=8ac9575b204a8ac85f9030500cb6eabbf6aa3226;hpb=6e9917f72528d75e819b810bea8e0abd875b6810 diff --git a/src/inspect.c b/src/inspect.c index 6938c10..531e3f7 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -29,7 +29,6 @@ #include #include #include -#include #include "c-ctype.h" #include "ignore-value.h" @@ -46,13 +45,12 @@ * simultaneously. */ static pcre *re_file_elf; -static pcre *re_file_win64; static pcre *re_elf_ppc64; static pcre *re_fedora; static pcre *re_rhel_old; static pcre *re_rhel; static pcre *re_rhel_no_minor; -static pcre *re_debian; +static pcre *re_major_minor; static pcre *re_aug_seq; static pcre *re_xdev; static pcre *re_windows_version; @@ -85,7 +83,7 @@ compile_regexps (void) "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0); COMPILE (re_rhel_no_minor, "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0); - COMPILE (re_debian, "(\\d+)\\.(\\d+)", 0); + COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0); COMPILE (re_aug_seq, "/\\d+$", 0); COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0); COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0); @@ -95,13 +93,12 @@ static void free_regexps (void) { pcre_free (re_file_elf); - pcre_free (re_file_win64); pcre_free (re_elf_ppc64); pcre_free (re_fedora); pcre_free (re_rhel_old); pcre_free (re_rhel); pcre_free (re_rhel_no_minor); - pcre_free (re_debian); + pcre_free (re_major_minor); pcre_free (re_aug_seq); pcre_free (re_xdev); pcre_free (re_windows_version); @@ -423,28 +420,11 @@ guestfs__inspect_os (guestfs_h *g) /* At this point we have, in the handle, a list of all filesystems * found and data about each one. Now we assemble the list of * filesystems which are root devices and return that to the user. + * Fall through to guestfs__inspect_get_roots to do that. */ - size_t count = 0; - for (i = 0; i < g->nr_fses; ++i) - if (g->fses[i].is_root) - count++; - - char **ret = calloc (count+1, sizeof (char *)); - if (ret == NULL) { - perrorf (g, "calloc"); + char **ret = guestfs__inspect_get_roots (g); + if (ret == NULL) guestfs___free_inspect_info (g); - return NULL; - } - - count = 0; - for (i = 0; i < g->nr_fses; ++i) { - if (g->fses[i].is_root) { - ret[count] = safe_strdup (g, g->fses[i].device); - count++; - } - } - ret[count] = NULL; - return ret; } @@ -645,6 +625,88 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs, return 0; } +/* Parse generic MAJOR.MINOR from the fs->product_name string. */ +static int +parse_major_minor (guestfs_h *g, struct inspect_fs *fs) +{ + char *major, *minor; + + if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) { + fs->major_version = parse_unsigned_int (g, major); + free (major); + if (fs->major_version == -1) { + free (minor); + return -1; + } + fs->minor_version = parse_unsigned_int (g, minor); + free (minor); + if (fs->minor_version == -1) + return -1; + } + return 0; +} + +/* Ubuntu has /etc/lsb-release containing: + * DISTRIB_ID=Ubuntu # Distro + * DISTRIB_RELEASE=10.04 # Version + * DISTRIB_CODENAME=lucid + * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name + * In theory other distros could have this LSB file, but none do. + */ +static int +parse_lsb_release (guestfs_h *g, struct inspect_fs *fs) +{ + char **lines; + size_t i; + int r = 0; + + lines = guestfs_head_n (g, 10, "/etc/lsb-release"); + if (lines == NULL) + return -1; + + for (i = 0; lines[i] != NULL; ++i) { + if (fs->distro == 0 && + STREQ (lines[i], "DISTRIB_ID=Ubuntu")) { + fs->distro = OS_DISTRO_UBUNTU; + r = 1; + } + else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) { + char *major, *minor; + if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) { + fs->major_version = parse_unsigned_int (g, major); + free (major); + if (fs->major_version == -1) { + free (minor); + free_string_list (lines); + return -1; + } + fs->minor_version = parse_unsigned_int (g, minor); + free (minor); + if (fs->minor_version == -1) { + free_string_list (lines); + return -1; + } + } + } + else if (fs->product_name == NULL && + (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") || + STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) { + size_t len = strlen (lines[i]) - 21 - 1; + fs->product_name = safe_strndup (g, &lines[i][21], len); + r = 1; + } + else if (fs->product_name == NULL && + STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) { + size_t len = strlen (lines[i]) - 20; + fs->product_name = safe_strndup (g, &lines[i][20], len); + r = 1; + } + } + + free_string_list (lines); + return r; +} + /* The currently mounted device is known to be a Linux root. Try to * determine from this the distro, version, etc. Also parse * /etc/fstab to determine the arrangement of mountpoints and @@ -653,8 +715,18 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs, static int check_linux_root (guestfs_h *g, struct inspect_fs *fs) { + int r; + fs->type = OS_TYPE_LINUX; + if (guestfs_exists (g, "/etc/lsb-release") > 0) { + r = parse_lsb_release (g, fs); + if (r == -1) /* error */ + return -1; + if (r == 1) /* ok - detected the release from this file */ + goto skip_release_checks; + } + if (guestfs_exists (g, "/etc/redhat-release") > 0) { fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */ @@ -698,20 +770,45 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs) if (parse_release_file (g, fs, "/etc/debian_version") == -1) return -1; - char *major, *minor; - if (match2 (g, fs->product_name, re_debian, &major, &minor)) { - fs->major_version = parse_unsigned_int (g, major); - free (major); - if (fs->major_version == -1) { - free (minor); - return -1; - } - fs->minor_version = parse_unsigned_int (g, minor); - free (minor); - if (fs->minor_version == -1) - return -1; - } + if (parse_major_minor (g, fs) == -1) + return -1; } + else if (guestfs_exists (g, "/etc/pardus-release") > 0) { + fs->distro = OS_DISTRO_PARDUS; + + if (parse_release_file (g, fs, "/etc/pardus-release") == -1) + return -1; + + if (parse_major_minor (g, fs) == -1) + return -1; + } + else if (guestfs_exists (g, "/etc/arch-release") > 0) { + fs->distro = OS_DISTRO_ARCHLINUX; + + /* /etc/arch-release file is empty and I can't see a way to + * determine the actual release or product string. + */ + } + else if (guestfs_exists (g, "/etc/gentoo-release") > 0) { + fs->distro = OS_DISTRO_GENTOO; + + if (parse_release_file (g, fs, "/etc/gentoo-release") == -1) + return -1; + + if (parse_major_minor (g, fs) == -1) + return -1; + } + else if (guestfs_exists (g, "/etc/meego-release") > 0) { + fs->distro = OS_DISTRO_MEEGO; + + if (parse_release_file (g, fs, "/etc/meego-release") == -1) + return -1; + + if (parse_major_minor (g, fs) == -1) + return -1; + } + + skip_release_checks:; /* Determine the architecture. */ const char *binaries[] = @@ -741,14 +838,14 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs) * are mounted. * XXX What if !feature_available (g, "augeas")? */ - if (guestfs_aug_init (g, "/", AUG_NO_LOAD|AUG_SAVE_NOOP) == -1) + if (guestfs_aug_init (g, "/", 16|32) == -1) return -1; /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */ guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]"); guestfs_aug_load (g); - int r = check_fstab (g, fs); + r = check_fstab (g, fs); guestfs_aug_close (g); if (r == -1) return -1; @@ -1190,6 +1287,37 @@ search_for_root (guestfs_h *g, const char *root) return NULL; } +char ** +guestfs__inspect_get_roots (guestfs_h *g) +{ + /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty + * list in this case. + */ + + size_t i; + size_t count = 0; + for (i = 0; i < g->nr_fses; ++i) + if (g->fses[i].is_root) + count++; + + char **ret = calloc (count+1, sizeof (char *)); + if (ret == NULL) { + perrorf (g, "calloc"); + return NULL; + } + + count = 0; + for (i = 0; i < g->nr_fses; ++i) { + if (g->fses[i].is_root) { + ret[count] = safe_strdup (g, g->fses[i].device); + count++; + } + } + ret[count] = NULL; + + return ret; +} + char * guestfs__inspect_get_type (guestfs_h *g, const char *root) { @@ -1226,11 +1354,16 @@ guestfs__inspect_get_distro (guestfs_h *g, const char *root) char *ret; switch (fs->distro) { + case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break; case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break; case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break; + case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break; + case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break; + case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break; case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break; case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break; case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break; + case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break; case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break; }