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