Blame SOURCES/0050-New-API-inspect_get_osinfo.patch

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