From 5fb23eb5171b6b93adf513ea9025199e2a21c076 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Wed, 21 Feb 2018 17:33:28 +0100
Subject: [PATCH] New API: inspect_get_osinfo
Try to guess the possible osinfo-db short ID for the specified OS.
Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1544842
(cherry picked from commit 286b88891c2288fb7f64c9538296599ece04bcb1)
---
generator/actions_inspection.ml | 14 ++++++
lib/Makefile.am | 1 +
lib/inspect-osinfo.c | 75 +++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+)
create mode 100644 lib/inspect-osinfo.c
diff --git a/generator/actions_inspection.ml b/generator/actions_inspection.ml
index 0ac282435..ff5083114 100644
--- a/generator/actions_inspection.ml
+++ b/generator/actions_inspection.ml
@@ -770,4 +770,18 @@ advice before using trademarks in applications.
=back" };
+ { defaults with
+ name = "inspect_get_osinfo"; added = (1, 39, 1);
+ style = RString (RPlainString, "id"), [String (Mountable, "root")], [];
+ shortdesc = "get a possible osinfo short ID corresponding to this operating system";
+ longdesc = "\
+This function returns a possible short ID for libosinfo corresponding
+to the guest.
+
+I<Note:> The returned ID is only a guess by libguestfs, and nothing
+ensures that it actually exists in osinfo-db.
+
+If no ID could not be determined, then the string C<unknown> is
+returned." };
+
]
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 91c4e0a2e..bd753d786 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -95,6 +95,7 @@ libguestfs_la_SOURCES = \
info.c \
inspect-apps.c \
inspect-icon.c \
+ inspect-osinfo.c \
journal.c \
launch.c \
launch-direct.c \
diff --git a/lib/inspect-osinfo.c b/lib/inspect-osinfo.c
new file mode 100644
index 000000000..816d317f1
--- /dev/null
+++ b/lib/inspect-osinfo.c
@@ -0,0 +1,75 @@
+/* libguestfs
+ * Copyright (C) 2018 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <config.h>
+
+#include "guestfs.h"
+#include "guestfs-internal.h"
+#include "guestfs-internal-actions.h"
+
+char *
+guestfs_impl_inspect_get_osinfo (guestfs_h *g, const char *root)
+{
+ CLEANUP_FREE char *type = NULL;
+ CLEANUP_FREE char *distro = NULL;
+ int major, minor;
+
+ type = guestfs_inspect_get_type (g, root);
+ if (!type)
+ return NULL;
+ distro = guestfs_inspect_get_distro (g, root);
+ if (!distro)
+ return NULL;
+ major = guestfs_inspect_get_major_version (g, root);
+ minor = guestfs_inspect_get_minor_version (g, root);
+
+ if (STREQ (type, "linux")) {
+ if (STREQ (distro, "centos")) {
+ if (major >= 7)
+ return safe_asprintf (g, "%s%d.0", distro, major);
+ else if (major == 6)
+ return safe_asprintf (g, "%s%d.%d", distro, major, minor);
+ }
+ else if (STREQ (distro, "debian")) {
+ if (major >= 4)
+ return safe_asprintf (g, "%s%d", distro, major);
+ }
+ else if (STREQ (distro, "fedora") || STREQ (distro, "mageia"))
+ return safe_asprintf (g, "%s%d", distro, major);
+ else if (STREQ (distro, "sles")) {
+ if (minor == 0)
+ return safe_asprintf (g, "%s%d", distro, major);
+ else
+ return safe_asprintf (g, "%s%dsp%d", distro, major, minor);
+ }
+ else if (STREQ (distro, "ubuntu"))
+ return safe_asprintf (g, "%s%d.%02d", distro, major, minor);
+
+ if (major > 0 || minor > 0)
+ return safe_asprintf (g, "%s%d.%d", distro, major, minor);
+ }
+ else if (STREQ (type, "freebsd") || STREQ (type, "netbsd") || STREQ (type, "openbsd"))
+ return safe_asprintf (g, "%s%d.%d", distro, major, minor);
+ else if (STREQ (type, "dos")) {
+ if (STREQ (distro, "msdos"))
+ return safe_strdup (g, "msdos6.22");
+ }
+
+ /* No ID could be guessed, return "unknown". */
+ return safe_strdup (g, "unknown");
+}
--
2.17.2