[PATCH virt-what] Add detection for systemd-nspawn instances

Richard W.M. Jones rjones at redhat.com
Mon Dec 19 10:53:45 UTC 2022


Please see the updated patch below which adds documentation.

This also needs tests which you'll need to provide.  You can look at
an existing test to see how to add one, eg:

http://git.annexia.org/?p=virt-what.git;a=tree;f=tests/lxc;h=243ce9f4a5fddef65163de34a5e10fa86ed09c80;hb=HEAD

Note you must get copies of the files from inside your systemd-nspawn
container.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
-------------- next part --------------
>From 95e10ec1b248d6e46c9d0a4f04981ec44d74696e Mon Sep 17 00:00:00 2001
From: Elias Probst <mail at eliasprobst.eu>
Date: Thu, 15 Dec 2022 10:46:02 +0100
Subject: [PATCH] Add detection for systemd-nspawn instances

Also improve LXC/nspawn checks by not invoking `cat`

Co-authored-by: Elias Probst <e.probst at ssc-services.de>
Signed-off-by: Elias Probst <e.probst at ssc-services.de>
---
 virt-what.in  | 9 ++++++---
 virt-what.pod | 6 ++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/virt-what.in b/virt-what.in
index d090898..d48de92 100644
--- a/virt-what.in
+++ b/virt-what.in
@@ -174,9 +174,12 @@ fi
 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
 # Added by Marc Fournier
 
-if [ -e "${root}/proc/1/environ" ] &&
-    cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container=lxc'; then
-    echo lxc
+if [ -e "${root}/proc/1/environ" ]; then
+    if tr '\000' '\n' < "${root}/proc/1/environ" | grep -Eiq '^container=lxc'; then
+        echo lxc
+    elif tr '\000' '\n' < "${root}/proc/1/environ" | grep -Eiq '^container=systemd-nspawn'; then
+        echo systemd_nspawn
+    fi
 fi
 
 if [ -e "${root}/proc/1/environ" ] &&
diff --git a/virt-what.pod b/virt-what.pod
index 2c346bd..e1db53a 100644
--- a/virt-what.pod
+++ b/virt-what.pod
@@ -258,6 +258,12 @@ The guest is running on the Red Hat hypervisor.
 
 Status: confirmed by RWMJ
 
+=item B<systemd_nspawn>
+
+This process is running in a L<systemd-nspawn(1)> container.
+
+Status: contributed by Elias Probst
+
 =item B<uml>
 
 This is a User-Mode Linux (UML) guest.
-- 
2.37.3



More information about the virt-tools-list mailing list