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