WSL icon indicating copy to clipboard operation
WSL copied to clipboard

How to tell in Linux under WSL if using WSL1 vs WSL2 ?

Open k7aay opened this issue 7 years ago • 37 comments

Documentation missing instructions in how to tell within Linux under WSL whether using WSL1 or WSL2.

k7aay avatar Sep 30 '19 18:09 k7aay

Here is a helpful doc - https://docs.microsoft.com/en-us/windows/wsl/wsl2-install

while setting a distro to be backed by WSL 2, I would run the following command in Powershell:

wsl -s 2

You can find your Distro by running: wsl -l

bytebounder avatar Oct 01 '19 03:10 bytebounder

You can find your Distro by running: wsl -l

Yes, but there is no particularly good way right now to know your own distribution name once you are inside. There was another ask for that recently #4479.

Probably (possibly?) one way is to run a regex on /proc/version. So WSL1 looks like:

Linux version 4.4.0-18985-Microsoft ([email protected]) (gcc version 5.4.0 (GCC) ) #1-Microsoft Fri Sep 13 14:26:00 PST 2019

While WSL2:

Linux version 4.19.67-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Sun Aug 18 13:37:54 UTC 2019

If it were me I wouldn't make a hard bet on the WSL2 string being set in concrete, but conversely, I can't imagine the WSL1 format changing gratuitously (not without grumbling, anyway). Matching that "gcc version n.x.x" and testing for n < 8 probably pretty future proof. There are other heuristics that would work. Calling dmesg and looking for a stable string like "Hypervisor" (or pick another).

therealkenc avatar Oct 01 '19 04:10 therealkenc

Here is a sample C program on how /init takes care of WSL1 vs WSL2:

#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>

int main(void)
{
    struct utsname buf;
    memset(&buf, 0, sizeof buf);

    int ret = uname(&buf);
    if (ret == 0)
    {
        if (strstr(buf.release, "Microsoft"))
            printf("WSL1\n");
        else
            printf("WSL2\n");
    }
    else
        printf("uname error\n");

}

Biswa96 avatar Oct 01 '19 15:10 Biswa96

+1 for code always. But implied in my post was maybe better not to rely on oe-user@oe-host never ever changing. That strstr() is leaning pretty hard on a capital letter 'M'. [The string "icrosoft" matches both.]

therealkenc avatar Oct 01 '19 16:10 therealkenc

There may be hundreds of ad-hoc way to do it. For example, only WSL2 has $WSL_INTEROP, /proc/config.gz, /dev/vsock, /dev/sda, tty shows /dev/pts etc.

Biswa96 avatar Oct 01 '19 16:10 Biswa96

Money ➡️ mouth:

// g++ -std=c++17 wslver.cpp -o wslver
#include <fstream>
#include <iostream>
#include <array>
#include <string>
#include <charconv>
#include <regex>
using namespace std::string_literals;

int main(int argc, const char *argv[])
{
    std::ifstream ifs;
    ifs.open("/proc/version");
    constexpr size_t bufsz = 128;
    std::array<char, bufsz> buf;
    ifs.getline(buf.data(), bufsz, '\n');
    std::string proc_ver(buf.data(), buf.size());
    std::regex rx("Linux version [0-9]+\\.[0-9]+\\.[0-9]+.*[Mm]icrosoft.*gcc version ([0-9]+)\\.[0-9]+\\.[0-9]+");
    std::smatch match;
    auto gcc_major = (std::regex_search(proc_ver, match, rx) && match.size() == 2) ? match[1] : "5"s;
    int maj = 0;
    std::from_chars(gcc_major.data(), gcc_major.data() + gcc_major.size(), maj);
    std::cout << ((maj < 8) ? "1\n" : "2\n");
    return 0;
}

therealkenc avatar Oct 01 '19 18:10 therealkenc

detect wsl1:) uname -r | grep Microsoft > /dev/null && echo "WSL1"

throwable-one avatar Oct 08 '19 16:10 throwable-one

[ $(grep -oE 'gcc version ([0-9]+)' /proc/version | awk '{print $3}') -gt 5 ] && \
    echo "WSL2" || echo "WSL1"

therealkenc avatar Oct 08 '19 19:10 therealkenc

Update (Windows 10 version 2004):

I'm brand new to WSL2, however...

  • uname -r says microsoft-standard in it, so all checks for the word [mM]icrosoft would fail
  • (opinion) The gcc method seems arbitrary, assuming WSL1 never changes to a new gcc
  • (opinion) WSL_INTEROP still works, and seems like the best test for now, but that's just my opinion

andyneff avatar Mar 24 '20 23:03 andyneff

WSL2 has the directory /run/WSL/ and not WSL1.

levesquejf avatar Apr 06 '20 16:04 levesquejf

I think that perhaps a belt & braces approach is needed. We have don't have any control over WSL1 getting a similar folder added. Likewise, whether 'Microsoft' has upper or lower case.

Using [[ -d "/run/WSL" ]] && echo "WSL2 present" is nice and easy, I acknowledge. But IMO a better method is to test for the underlying hyper-v running system that WSL2 uses.

Try lshw | grep smp for instance. WSL1 shows 'smp' while WSL2 will show both smp & vsyscall32.

We can write something like WSL2_Flag="$(lshw | grep vsyscall32) ; if [[ $WSL2_Flag ]] ; then echo "WSL2 present" ; fi

Or alternatively, we can look in '/proc'. We can't guarantee 'lshw' to be present in every Linux distro, although it usually is:

In WSL1 cat /proc/interrupts is empty whereas with WSL2 it is positively active, especially the line for HVS Hyper -V !

eg. WSL2_Flag="$(cat /proc/interrupts ) ; if [[ $WSL2_Flag ]] ; then echo "WSL2 present" ; fi

TuxVinyards avatar Jun 22 '20 14:06 TuxVinyards

What about the environment variable WSL_DISTRO_NAME? [[ -n "$WSL_DISTRO_NAME" ]]. Is that a suitable & cheap way to detect WSL 1 or 2?

vlovich avatar Sep 09 '20 22:09 vlovich

No. WSL_DISTRO_NAME variable is set by /init binary blob for both WSL1 and WSL2.

Biswa96 avatar Sep 10 '20 03:09 Biswa96

@Biswa96 I asked if it's suitable for WSL1 OR WSL 2, not WSL 2 vs WSL 1 so it sounds like the answer to my question is actually yes?

vlovich avatar Sep 10 '20 21:09 vlovich

Yes but not retroactively.

therealkenc avatar Sep 10 '20 22:09 therealkenc

@therealkenc is the way you described (distinguish between WSL 1 and WSL 2 using the GCC version) going to be guaranteed stable, even in future versions of WSL 1?

Which is the guaranteed way to distinguish between these two and nōn-WSL Linux then? The presence of the string icrosoft?

Someone suggested checking the root filesystem type, which for WSL 1 is wslfs, but that won’t help for WSL 2. Any other filesystem can be umounted… and some people have trouble with custom kernels and the detection mechanism you provided (especially in the “WSL vs Linux” case).

Anything that needs superuser privilegues (lshw…) is also out, for hopefully obvious reasons.

mirabilos avatar Sep 28 '20 14:09 mirabilos

No guarantees in life. Although given that the unspoken textual API has now irretrievably leaked into userspace, yes, I would treat regex [Mm] difference as pretty future proof.

That said, users can (and do) compile their own kernels in WSL2 and then misname the their kernel, or compile with clang, so you can't count on the kernel string in WSL2 to be anything in particular. Fortunately (notwithstanding the env pollution) recent WSL has the WSL_DISTRO_NAME and WSL_INTEROP environment variables. WSL_INTEROP is only present in WSL2. Using them is not backwards compatible with older WSL though, and they won't EOL for years.

Possible approach:

  • Test for existence WSL_DISTRO_NAME. If it exists, test for WSL_INTEROP. Yes, WSL2; no, WSL1.
  • If WSL_DISTRO_NAME does not exist, fall back on the version string regex.

therealkenc avatar Sep 28 '20 18:09 therealkenc

This might do. I was thinking of simplifying this a bit:

  • if / is wslfs, then it’s WSL 1, else it’s either Linux/Android or WSL 2
  • if WSL_INTEROP or even WSL_DISTRO_NAME is set or the version contains icrosoft it’s WSL 2, else Linux/Android

Doing this without /proc (which is not guaranteed to be mounted) is more tricky. After statfs("/", &buf), buf.f_type is 0x53464846 on my test setup. Is this true for all WSL 1 systems?

The WSL 2 detection is even more fragile. Addition of an API from the start would have helped :/

For version detection, uname(2) exists, so this can also be done without procfs. The WSL 2 caveats apply :/

mirabilos avatar Sep 28 '20 20:09 mirabilos

Use uname(2) if you can't depend on /proc. The gcc test was belt+suspenders overkill, and given existence possibility clang probably does more harm than good. strstr(buf.release, "Microsoft") means WSL1. Else Linux/Android/WSL2. For your purposes WSL2 is Linux anyway.

therealkenc avatar Sep 28 '20 22:09 therealkenc

therealkenc dixit:

In uname(2) if you can't depend on /proc. The gcc test was belt+suspenders overkill, and given existence possibility clang probably does more harm than good. strstr(buf.release, "Microsoft") means WSL1. Else Linux/Android/WSL2. For [your

OK, great, that will work.

purposes](https://github.com/tarent/ECN-Bits/blob/18320ef9e0b0a674e09668392224770d7ba6a7be/c/lib/tc.c#L53) WSL2 is Linux anyway.

Oh, kudos for finding this one. For that purpose, WSL 2 probably is “don’t use” anyway, considering the networking issues. But I guess there are other people around who might wish to figure out WSL 2, too. Perhaps an identifier can be added in a next version?

Thanks, I’ll adapt the code to use uname(2) and check for WSL 1 only and document that.

mirabilos avatar Sep 28 '20 22:09 mirabilos