mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

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

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