Try to guess the possible osinfo-db short ID for the specified OS. Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1544842 --- 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 d8cf571c9..d913f53e3 100644 --- a/generator/actions_inspection.ml +++ b/generator/actions_inspection.ml @@ -774,4 +774,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 d075174d9..742b182cc 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.14.3
Pino Toscano
2018-Feb-21 16:40 UTC
[Libguestfs] [PATCH] inspector: show the per-OS osinfo guess (RHBZ#1544842)
Output also the osinfo guess for each OS in the generated XML output; adapt the RNG schema, and the test data to it. --- inspector/expected-coreos.img.xml | 1 + inspector/expected-debian.img.xml | 1 + inspector/expected-fedora.img.xml | 1 + inspector/expected-ubuntu.img.xml | 1 + inspector/inspector.c | 7 +++++++ inspector/virt-inspector.rng | 1 + 6 files changed, 12 insertions(+) diff --git a/inspector/expected-coreos.img.xml b/inspector/expected-coreos.img.xml index e4a5d1134..0cdfba6e6 100644 --- a/inspector/expected-coreos.img.xml +++ b/inspector/expected-coreos.img.xml @@ -8,6 +8,7 @@ <major_version>899</major_version> <minor_version>13</minor_version> <hostname>coreos.invalid</hostname> + <osinfo>coreos899.13</osinfo> <mountpoints> <mountpoint dev="/dev/sda5">/</mountpoint> <mountpoint dev="/dev/sda3">/usr</mountpoint> diff --git a/inspector/expected-debian.img.xml b/inspector/expected-debian.img.xml index 37ecfa049..6583dc262 100644 --- a/inspector/expected-debian.img.xml +++ b/inspector/expected-debian.img.xml @@ -11,6 +11,7 @@ <package_format>deb</package_format> <package_management>apt</package_management> <hostname>debian.invalid</hostname> + <osinfo>debian5</osinfo> <mountpoints> <mountpoint dev="/dev/debian/root">/</mountpoint> <mountpoint dev="/dev/debian/usr">/usr</mountpoint> diff --git a/inspector/expected-fedora.img.xml b/inspector/expected-fedora.img.xml index 8d40e8cb7..df6060a73 100644 --- a/inspector/expected-fedora.img.xml +++ b/inspector/expected-fedora.img.xml @@ -11,6 +11,7 @@ <package_format>rpm</package_format> <package_management>yum</package_management> <hostname>fedora.invalid</hostname> + <osinfo>fedora14</osinfo> <mountpoints> <mountpoint dev="/dev/VG/Root">/</mountpoint> <mountpoint dev="/dev/sda1">/boot</mountpoint> diff --git a/inspector/expected-ubuntu.img.xml b/inspector/expected-ubuntu.img.xml index c19c14cd5..4ebcd76d6 100644 --- a/inspector/expected-ubuntu.img.xml +++ b/inspector/expected-ubuntu.img.xml @@ -11,6 +11,7 @@ <package_format>deb</package_format> <package_management>apt</package_management> <hostname>ubuntu.invalid</hostname> + <osinfo>ubuntu10.10</osinfo> <mountpoints> <mountpoint dev="/dev/sda2">/</mountpoint> <mountpoint dev="/dev/sda1">/boot</mountpoint> diff --git a/inspector/inspector.c b/inspector/inspector.c index d608b1b63..5075a8f04 100644 --- a/inspector/inspector.c +++ b/inspector/inspector.c @@ -443,6 +443,13 @@ output_root (xmlTextWriterPtr xo, char *root) BAD_CAST str)); free (str); + str = guestfs_inspect_get_osinfo (g, root); + if (!str) exit (EXIT_FAILURE); + if (STRNEQ (str, "unknown")) + XMLERROR (-1, + xmlTextWriterWriteElement (xo, BAD_CAST "osinfo", BAD_CAST str)); + free (str); + output_mountpoints (xo, root); output_filesystems (xo, root); diff --git a/inspector/virt-inspector.rng b/inspector/virt-inspector.rng index 314785202..1e3a58af8 100644 --- a/inspector/virt-inspector.rng +++ b/inspector/virt-inspector.rng @@ -38,6 +38,7 @@ <optional><ref name="ospackageformat"/></optional> <optional><ref name="ospackagemanagement"/></optional> <optional><element name="hostname"><text/></element></optional> + <optional><element name="osinfo"><text/></element></optional> <ref name="mountpoints"/> <ref name="filesystems"/> -- 2.14.3
Pino Toscano
2018-Feb-21 16:40 UTC
[Libguestfs] [PATCH] make-repository: use inspect_get_osinfo
Use the newly added API instead of the local implementation. --- builder/repository_main.ml | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/builder/repository_main.ml b/builder/repository_main.ml index bb440563b..c020a6413 100644 --- a/builder/repository_main.ml +++ b/builder/repository_main.ml @@ -170,23 +170,6 @@ let get_disk_image_info filepath size = object_get_number "virtual-size" infos } -let compute_short_id distro major minor - match distro with - | "centos" when major >= 7 -> - sprintf "%s%d.0" distro major - | "debian" when major >= 4 -> - sprintf "%s%d" distro major - | ("fedora"|"mageia") -> - sprintf "%s%d" distro major - | "sles" when major = 0 -> - sprintf "%s%d" distro major - | "sles" -> - sprintf "%s%dsp%d" distro major minor - | "ubuntu" -> - sprintf "%s%d.%02d" distro major minor - | _ (* Any other combination. *) -> - sprintf "%s%d.%d" distro major minor - let cmp a b Index.string_of_arch a = Index.string_of_arch b @@ -260,14 +243,10 @@ let process_image acc_entries filename repo tmprepo index interactive let root = Array.get roots 0 in let inspected_arch = g#inspect_get_arch root in let product = g#inspect_get_product_name root in - let distro = g#inspect_get_distro root in - let version_major = g#inspect_get_major_version root in - let version_minor = g#inspect_get_minor_version root in + let shortid = g#inspect_get_osinfo root in let lvs = g#lvs () in let filesystems = g#inspect_get_filesystems root in - let shortid = compute_short_id distro version_major version_minor in - g#close (); let id -- 2.14.3
Richard W.M. Jones
2018-Feb-22 14:28 UTC
Re: [Libguestfs] [PATCH] make-repository: use inspect_get_osinfo
ACK series. Was expecting an OCaml implementation though :-) Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Apparently Analagous Threads
- [PATCH v10 00/10] Reimplement inspection in the daemon.
- [PATCH v9 00/11] Reimplement inspection in the daemon.
- [PATCH v12 00/11] Reimplement inspection in the daemon.
- [PATCH v11 00/10] Reimplement inspection in the daemon.
- [PATCH 0/5] Small inspection improvements