From 3bc6a8d247737483edb28959d7ae3708074cf8ea Mon Sep 17 00:00:00 2001 From: Balint Reczey Date: Thu, 14 Mar 2019 18:57:43 +0100 Subject: [PATCH] virt: detect WSL environment as a container LP: #1816753 --- debian/patches/series | 1 + ...SL-environment-as-a-container-id-wsl.patch | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 debian/patches/virt-detect-WSL-environment-as-a-container-id-wsl.patch diff --git a/debian/patches/series b/debian/patches/series index e8ac7f1445..6c14dffb3a 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -97,3 +97,4 @@ resolved-add-comment-to-dns_stream_complete-about-its-err.patch resolved-keep-stub-stream-connections-up-for-as-long-as-c.patch network-remove-routing-policy-rule-from-foreign-rule.patch network-do-not-remove-rule-when-it-is-requested-by-e.patch +virt-detect-WSL-environment-as-a-container-id-wsl.patch diff --git a/debian/patches/virt-detect-WSL-environment-as-a-container-id-wsl.patch b/debian/patches/virt-detect-WSL-environment-as-a-container-id-wsl.patch new file mode 100644 index 0000000000..467d6aaded --- /dev/null +++ b/debian/patches/virt-detect-WSL-environment-as-a-container-id-wsl.patch @@ -0,0 +1,116 @@ +From: Balint Reczey +Date: Wed, 6 Mar 2019 18:46:04 +0100 +Subject: virt: detect WSL environment as a container (id: wsl) + +--- + man/systemd-detect-virt.xml | 13 ++++++++++++- + man/systemd.unit.xml | 3 ++- + src/basic/virt.c | 12 ++++++++++++ + src/basic/virt.h | 1 + + 4 files changed, 27 insertions(+), 2 deletions(-) + +diff --git a/man/systemd-detect-virt.xml b/man/systemd-detect-virt.xml +index c4763fd..9e37fd1 100644 +--- a/man/systemd-detect-virt.xml ++++ b/man/systemd-detect-virt.xml +@@ -126,7 +126,7 @@ + + + +- Container ++ Container + openvz + OpenVZ/Virtuozzo + +@@ -155,6 +155,11 @@ + rkt + rkt app container runtime + ++ ++ ++ wsl ++ Windows Subsystem for Linux ++ + + + +@@ -164,6 +169,12 @@ + machine and container virtualization are used in + conjunction, only the latter will be identified (unless + is passed). ++ Windows Subsystem for Linux is not a Linux container, ++ but an environment for running Linux userspace applications on ++ top of the Windows kernel using a Linux-compatible interface. ++ WSL is categorized as a container for practical purposes. ++ Multiple WSL environments share the same kernel and services ++ should generally behave like when being run in a container. + + + +diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml +index 7e1b3cb..6c86eba 100644 +--- a/man/systemd.unit.xml ++++ b/man/systemd.unit.xml +@@ -1093,7 +1093,8 @@ + lxc-libvirt, + systemd-nspawn, + docker, +- rkt to test ++ rkt, ++ wsl to test + against a specific implementation, or + private-users to check whether we are running in a user namespace. See + systemd-detect-virt1 +diff --git a/src/basic/virt.c b/src/basic/virt.c +index f63f15f..9e12069 100644 +--- a/src/basic/virt.c ++++ b/src/basic/virt.c +@@ -436,10 +436,12 @@ int detect_container(void) { + { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN }, + { "docker", VIRTUALIZATION_DOCKER }, + { "rkt", VIRTUALIZATION_RKT }, ++ { "wsl", VIRTUALIZATION_WSL }, + }; + + static thread_local int cached_found = _VIRTUALIZATION_INVALID; + _cleanup_free_ char *m = NULL; ++ _cleanup_free_ char *o = NULL; + const char *e = NULL; + unsigned j; + int r; +@@ -454,6 +456,15 @@ int detect_container(void) { + goto finish; + } + ++ /* "Official" way of detecting WSL https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 */ ++ r = read_one_line_file("/proc/sys/kernel/osrelease", &o); ++ if (r >= 0) { ++ if (strstr(o, "Microsoft") || strstr(o, "WSL")) { ++ r = VIRTUALIZATION_WSL; ++ goto finish; ++ } ++ } ++ + if (getpid_cached() == 1) { + /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */ + +@@ -636,6 +647,7 @@ static const char *const virtualization_table[_VIRTUALIZATION_MAX] = { + [VIRTUALIZATION_OPENVZ] = "openvz", + [VIRTUALIZATION_DOCKER] = "docker", + [VIRTUALIZATION_RKT] = "rkt", ++ [VIRTUALIZATION_WSL] = "wsl", + [VIRTUALIZATION_CONTAINER_OTHER] = "container-other", + }; + +diff --git a/src/basic/virt.h b/src/basic/virt.h +index c4cf4bf..a603fd4 100644 +--- a/src/basic/virt.h ++++ b/src/basic/virt.h +@@ -31,6 +31,7 @@ enum { + VIRTUALIZATION_OPENVZ, + VIRTUALIZATION_DOCKER, + VIRTUALIZATION_RKT, ++ VIRTUALIZATION_WSL, + VIRTUALIZATION_CONTAINER_OTHER, + VIRTUALIZATION_CONTAINER_LAST = VIRTUALIZATION_CONTAINER_OTHER, + -- 2.17.1