naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
6f381c
From 6ffd3de2ccc5901974f292c9694829e25441060d Mon Sep 17 00:00:00 2001
6f381c
From: Bertrand Jacquin <bertrand@jacquin.bzh>
6f381c
Date: Sun, 11 Oct 2020 21:25:00 +0100
6f381c
Subject: [PATCH] virt: detect Amazon EC2 Nitro instance
6f381c
6f381c
Amazon EC2 Nitro hypervisor is technically based on KVM[1], which
6f381c
systemd-detect-virt identify propely from CPUID. However the lack of
6f381c
CPUID on aarch64 (A1, T4 instance type) prevents a correct
6f381c
identification, impacting hostnamectl and systemd-random-seed. Instead
6f381c
it's possible to identify virtualization from DMI vendor ID.
6f381c
6f381c
Prior to this commit:
6f381c
  # hostnamectl
6f381c
     Static hostname: n/a
6f381c
  Transient hostname: ip-10-97-8-12
6f381c
           Icon name: computer
6f381c
          Machine ID: 8e3772fbcfa3dd6f330a12ff5df5a63b
6f381c
             Boot ID: b7b7e2fe0079448db664839df59f9817
6f381c
    Operating System: Gentoo/Linux
6f381c
              Kernel: Linux 5.4.69-longterm
6f381c
        Architecture: arm64
6f381c
6f381c
After this commit:
6f381c
  # hostnamectl
6f381c
     Static hostname: n/a
6f381c
  Transient hostname: ip-10-97-8-12
6f381c
           Icon name: computer-vm
6f381c
             Chassis: vm
6f381c
          Machine ID: 8e3772fbcfa3dd6f330a12ff5df5a63b
6f381c
             Boot ID: bd04da57084e41078f20541101867113
6f381c
      Virtualization: amazon
6f381c
    Operating System: Gentoo/Linux
6f381c
              Kernel: Linux 5.4.69-longterm
6f381c
        Architecture: arm64
6f381c
6f381c
[1] https://aws.amazon.com/ec2/faqs/
6f381c
6f381c
(cherry picked from commit b6eca3731dd92b009b182f188936e1c2544574da)
6f381c
6f381c
Resolves: #2117948
6f381c
---
6f381c
 man/systemd-detect-virt.xml | 7 ++++++-
6f381c
 man/systemd.unit.xml        | 1 +
6f381c
 src/basic/virt.c            | 8 +++++---
6f381c
 src/basic/virt.h            | 1 +
6f381c
 src/test/test-condition.c   | 1 +
6f381c
 5 files changed, 14 insertions(+), 4 deletions(-)
6f381c
6f381c
diff --git a/man/systemd-detect-virt.xml b/man/systemd-detect-virt.xml
6f381c
index 6beb2c2aa1..61c210e24d 100644
6f381c
--- a/man/systemd-detect-virt.xml
6f381c
+++ b/man/systemd-detect-virt.xml
6f381c
@@ -72,7 +72,12 @@
6f381c
 
6f381c
           <row>
6f381c
             <entry><varname>kvm</varname></entry>
6f381c
-            <entry>Linux KVM kernel virtual machine, with whatever software, except Oracle Virtualbox</entry>
6f381c
+            <entry>Linux KVM kernel virtual machine, in combination with QEMU. Not used for other virtualizers using the KVM interfaces, such as Oracle VirtualBox or Amazon EC2 Nitro, see below.</entry>
6f381c
+          </row>
6f381c
+
6f381c
+          <row>
6f381c
+            <entry><varname>amazon</varname></entry>
6f381c
+            <entry>Amazon EC2 Nitro using Linux KVM</entry>
6f381c
           </row>
6f381c
 
6f381c
           <row>
6f381c
diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
6f381c
index 6f213ccd56..5207a5bb3c 100644
6f381c
--- a/man/systemd.unit.xml
6f381c
+++ b/man/systemd.unit.xml
6f381c
@@ -1068,6 +1068,7 @@
6f381c
         virtualization solution, or one of
6f381c
         <varname>qemu</varname>,
6f381c
         <varname>kvm</varname>,
6f381c
+        <literal>amazon</literal>,
6f381c
         <varname>zvm</varname>,
6f381c
         <varname>vmware</varname>,
6f381c
         <varname>microsoft</varname>,
6f381c
diff --git a/src/basic/virt.c b/src/basic/virt.c
6f381c
index 8d862b6d67..78c68d66e0 100644
6f381c
--- a/src/basic/virt.c
6f381c
+++ b/src/basic/virt.c
6f381c
@@ -147,6 +147,7 @@ static int detect_vm_dmi(void) {
6f381c
                 int id;
6f381c
         } dmi_vendor_table[] = {
6f381c
                 { "KVM",           VIRTUALIZATION_KVM       },
6f381c
+                { "Amazon EC2",          VIRTUALIZATION_AMAZON    },
6f381c
                 { "QEMU",          VIRTUALIZATION_QEMU      },
6f381c
                 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
6f381c
                 { "VMware",        VIRTUALIZATION_VMWARE    },
6f381c
@@ -339,8 +340,8 @@ int detect_vm(void) {
6f381c
 
6f381c
         /* We have to use the correct order here:
6f381c
          *
6f381c
-         * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
6f381c
-         *   Hyper-V.
6f381c
+         * → First, try to detect Oracle Virtualbox and Amazon EC2 Nitro, even if they use KVM, as well as Xen even if
6f381c
+         *   it cloaks as Microsoft Hyper-V.
6f381c
          *
6f381c
          * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
6f381c
          *   overwritten.
6f381c
@@ -348,7 +349,7 @@ int detect_vm(void) {
6f381c
          * → Third, try to detect from DMI. */
6f381c
 
6f381c
         dmi = detect_vm_dmi();
6f381c
-        if (IN_SET(dmi, VIRTUALIZATION_ORACLE, VIRTUALIZATION_XEN)) {
6f381c
+        if (IN_SET(dmi, VIRTUALIZATION_ORACLE, VIRTUALIZATION_XEN, VIRTUALIZATION_AMAZON)) {
6f381c
                 r = dmi;
6f381c
                 goto finish;
6f381c
         }
6f381c
@@ -631,6 +632,7 @@ int running_in_chroot(void) {
6f381c
 static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
6f381c
         [VIRTUALIZATION_NONE] = "none",
6f381c
         [VIRTUALIZATION_KVM] = "kvm",
6f381c
+        [VIRTUALIZATION_AMAZON] = "amazon",
6f381c
         [VIRTUALIZATION_QEMU] = "qemu",
6f381c
         [VIRTUALIZATION_BOCHS] = "bochs",
6f381c
         [VIRTUALIZATION_XEN] = "xen",
6f381c
diff --git a/src/basic/virt.h b/src/basic/virt.h
6f381c
index 640b3ed779..ed4ff063e0 100644
6f381c
--- a/src/basic/virt.h
6f381c
+++ b/src/basic/virt.h
6f381c
@@ -10,6 +10,7 @@ enum {
6f381c
 
6f381c
         VIRTUALIZATION_VM_FIRST,
6f381c
         VIRTUALIZATION_KVM = VIRTUALIZATION_VM_FIRST,
6f381c
+        VIRTUALIZATION_AMAZON,
6f381c
         VIRTUALIZATION_QEMU,
6f381c
         VIRTUALIZATION_BOCHS,
6f381c
         VIRTUALIZATION_XEN,
6f381c
diff --git a/src/test/test-condition.c b/src/test/test-condition.c
6f381c
index 24395dafc6..29ea63c4ff 100644
6f381c
--- a/src/test/test-condition.c
6f381c
+++ b/src/test/test-condition.c
6f381c
@@ -510,6 +510,7 @@ static void test_condition_test_virtualization(void) {
6f381c
 
6f381c
         NULSTR_FOREACH(virt,
6f381c
                        "kvm\0"
6f381c
+                       "amazon\0"
6f381c
                        "qemu\0"
6f381c
                        "bochs\0"
6f381c
                        "xen\0"