ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
6f381c
From efa2cdb699df3e5d5d7180e50f3ebfff74788c5c Mon Sep 17 00:00:00 2001
6f381c
From: Yu Watanabe <watanabe.yu+github@gmail.com>
6f381c
Date: Tue, 7 Jan 2020 11:49:39 +0900
6f381c
Subject: [PATCH] virt: use string table to detect VM or container
6f381c
6f381c
(cherry picked from commit 735ea55f5cd87a82757a8911edd80fba799b46ee)
6f381c
6f381c
Related: #2117948
6f381c
---
6f381c
 src/basic/virt.c | 73 ++++++++++++++++++++++--------------------------
6f381c
 1 file changed, 33 insertions(+), 40 deletions(-)
6f381c
6f381c
diff --git a/src/basic/virt.c b/src/basic/virt.c
6f381c
index 78c68d66e0..6e4c702051 100644
6f381c
--- a/src/basic/virt.c
6f381c
+++ b/src/basic/virt.c
6f381c
@@ -22,27 +22,26 @@
6f381c
 #include "string-util.h"
6f381c
 #include "virt.h"
6f381c
 
6f381c
+static const char *const vm_table[_VIRTUALIZATION_MAX] = {
6f381c
+        [VIRTUALIZATION_XEN]       = "XenVMMXenVMM",
6f381c
+        [VIRTUALIZATION_KVM]       = "KVMKVMKVM",
6f381c
+        [VIRTUALIZATION_QEMU]      = "TCGTCGTCGTCG",
6f381c
+        /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
6f381c
+        [VIRTUALIZATION_VMWARE]    = "VMwareVMware",
6f381c
+        /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
6f381c
+        [VIRTUALIZATION_MICROSOFT] = "Microsoft Hv",
6f381c
+        /* https://wiki.freebsd.org/bhyve */
6f381c
+        [VIRTUALIZATION_BHYVE]     = "bhyve bhyve ",
6f381c
+        [VIRTUALIZATION_QNX]       = "QNXQVMBSQG",
6f381c
+};
6f381c
+
6f381c
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(vm, int);
6f381c
+
6f381c
 static int detect_vm_cpuid(void) {
6f381c
 
6f381c
         /* CPUID is an x86 specific interface. */
6f381c
 #if defined(__i386__) || defined(__x86_64__)
6f381c
 
6f381c
-        static const struct {
6f381c
-                const char *cpuid;
6f381c
-                int id;
6f381c
-        } cpuid_vendor_table[] = {
6f381c
-                { "XenVMMXenVMM", VIRTUALIZATION_XEN       },
6f381c
-                { "KVMKVMKVM",    VIRTUALIZATION_KVM       },
6f381c
-                { "TCGTCGTCGTCG", VIRTUALIZATION_QEMU      },
6f381c
-                /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
6f381c
-                { "VMwareVMware", VIRTUALIZATION_VMWARE    },
6f381c
-                /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
6f381c
-                { "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
6f381c
-                /* https://wiki.freebsd.org/bhyve */
6f381c
-                { "bhyve bhyve ", VIRTUALIZATION_BHYVE     },
6f381c
-                { "QNXQVMBSQG",   VIRTUALIZATION_QNX       },
6f381c
-        };
6f381c
-
6f381c
         uint32_t eax, ebx, ecx, edx;
6f381c
         bool hypervisor;
6f381c
 
6f381c
@@ -59,7 +58,7 @@ static int detect_vm_cpuid(void) {
6f381c
                         uint32_t sig32[3];
6f381c
                         char text[13];
6f381c
                 } sig = {};
6f381c
-                unsigned j;
6f381c
+                int v;
6f381c
 
6f381c
                 /* There is a hypervisor, see what it is */
6f381c
                 __cpuid(0x40000000U, eax, ebx, ecx, edx);
6f381c
@@ -70,11 +69,11 @@ static int detect_vm_cpuid(void) {
6f381c
 
6f381c
                 log_debug("Virtualization found, CPUID=%s", sig.text);
6f381c
 
6f381c
-                for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++)
6f381c
-                        if (streq(sig.text, cpuid_vendor_table[j].cpuid))
6f381c
-                                return cpuid_vendor_table[j].id;
6f381c
+                v = vm_from_string(sig.text);
6f381c
+                if (v < 0)
6f381c
+                        return VIRTUALIZATION_VM_OTHER;
6f381c
 
6f381c
-                return VIRTUALIZATION_VM_OTHER;
6f381c
+                return v;
6f381c
         }
6f381c
 #endif
6f381c
         log_debug("No virtualization found in CPUID");
6f381c
@@ -434,22 +433,20 @@ finish:
6f381c
         return r;
6f381c
 }
6f381c
 
6f381c
-int detect_container(void) {
6f381c
-        static const struct {
6f381c
-                const char *value;
6f381c
-                int id;
6f381c
-        } value_table[] = {
6f381c
-                { "lxc",            VIRTUALIZATION_LXC            },
6f381c
-                { "lxc-libvirt",    VIRTUALIZATION_LXC_LIBVIRT    },
6f381c
-                { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
6f381c
-                { "docker",         VIRTUALIZATION_DOCKER         },
6f381c
-                { "rkt",            VIRTUALIZATION_RKT            },
6f381c
-        };
6f381c
+static const char *const container_table[_VIRTUALIZATION_MAX] = {
6f381c
+        [VIRTUALIZATION_LXC]            = "lxc",
6f381c
+        [VIRTUALIZATION_LXC_LIBVIRT]    = "lxc-libvirt",
6f381c
+        [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
6f381c
+        [VIRTUALIZATION_DOCKER]         = "docker",
6f381c
+        [VIRTUALIZATION_RKT]            = "rkt",
6f381c
+};
6f381c
+
6f381c
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(container, int);
6f381c
 
6f381c
+int detect_container(void) {
6f381c
         static thread_local int cached_found = _VIRTUALIZATION_INVALID;
6f381c
         _cleanup_free_ char *m = NULL;
6f381c
         const char *e = NULL;
6f381c
-        unsigned j;
6f381c
         int r;
6f381c
 
6f381c
         if (cached_found >= 0)
6f381c
@@ -522,13 +519,9 @@ int detect_container(void) {
6f381c
         goto finish;
6f381c
 
6f381c
 translate_name:
6f381c
-        for (j = 0; j < ELEMENTSOF(value_table); j++)
6f381c
-                if (streq(e, value_table[j].value)) {
6f381c
-                        r = value_table[j].id;
6f381c
-                        goto finish;
6f381c
-                }
6f381c
-
6f381c
-        r = VIRTUALIZATION_CONTAINER_OTHER;
6f381c
+        r = container_from_string(e);
6f381c
+        if (r < 0)
6f381c
+                r = VIRTUALIZATION_CONTAINER_OTHER;
6f381c
 
6f381c
 finish:
6f381c
         log_debug("Found container virtualization %s.", virtualization_to_string(r));