WSL: Blockers for systemd?

For various reasons, it isn’t obvious to me exactly how to determine what features are missing from WSL’s implementation in order to get something like systemd up and running. From the documentation, we know for sure that WSL does not yet implement any of the syscalls related to cgroups, and doesn’t include any of the kernel namespaces, which is probably a blocker at the moment. Are there any other missing syscalls or socket options that would block systemd from running?

I also am attaching the output of strace systemd --test

systemd-trace.txt

(using the docker image of Ubuntu yakkety installed with RoliSoft’s python script)

Also, I know that WSL currently uses a kind of hardcoded init process (which sets up the network interfaces, remakes the /etc/hosts file, does some other stuff, then (I think) forks /bin/bash by default). Would the whole initialization process need to be rewritten from scratch to use systemd as the init, assuming all of the other dependencies were eventually implemented (cgroups, aforementioned missing syscalls and socket options)?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 187
  • Comments: 163 (2 by maintainers)

Most upvoted comments

You may want to try using Upstart to run and manage third-party services Also, Follow me on linkedIn , I am going to publish an article very soon on WSL and running opensource project with WSL. Hopefully, it should give you a comprehensive guide while you start working with WSL

If you are using Windows 10 insider builds 20201 or above then add the following two lines in /etc/wsl.conf file in the specific distribution:

[boot]
command = "sudo daemonize /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target"

Now shutdown every running WSL instances with wsl.exe --shutdown command. Then restart the distribution in WSL. systemd service will be spun up automatically with that distribution. The command field can be replaced with any command you know. It can be used to execute systemctl or daemonize or xinit or service, you get the idea 😉

So, how does this work? you may ask. Before initializing the WSL instance /init binary parses the /etc/wsl.conf file. It reads the command from that field and fork(2) a child process. The child process then executes the command in system(3) call which in turns call posix_spawn(3) 🤓

Considering how open the Devs have been hereabouts, I’d say the issue isn’t so much “keeping it secret” as “it’s a moving target”.

I don’t think it will take too long for the Devs to figure out that what they are presenting to us is “single-user, console”. To a Linux person it is like Safe Mode is to a Windows person. Yes you can do stuff in Safe Mode, and sometimes you are amazed that it even works at all, but it’s not the same as running Windows, just as running WSL without cron or syslog or sshd or a terminal emulator (what we have got is sort of a console emulator) and other servers/daemons/background tasks, is not the same as running an Ubuntu developer machine.

I can be happy without upstart/init/systemd running everything that comes with a fresh Ubuntu install. But the things I work on generally do logging to syslog, and don’t have an option for logging to a file; so trying to work on them without rsyslogd is quite difficult. It’s nice to be able to run a bash script from a shell prompt, but what would be nicer is being able to run a few from cron, to automate things. And so forth.

So let’s see if we can figure out what is blocking systemd, but I’d guess that when the Devs decide it’s something they need to implement, they’ll figure out a way.

@fpqc - is there a reason for implementing systemd in WSL other than bragging rights? It looks to me like that would bring along a lot of baggage we might not need. Could we go to something closer to sysvinit or whatever for starting a few services, or would having systemd implemented give us a lot of benefits I’m not seeing?

if is for only windows 11, is useless 😦

this need to be retro compatible with windows 10

greetings

There is now support for systemd in WSL, for more info on how to use it please read here.

There are no blockers for systemd. I use Arkane-system genie systemd for 2 separate distros and it works. Although genie was developed for Debian it has only one dependency - .Net runtime 5.0 that is available for all major distros. I also moved essential genie files to Windows program files and added it for the Windows files, and it just works. Podman is not a Server, it doesn’t need systemd to start. It needs it as the cgroup’s manager by default unless you don’t start pods as services. Hello from RHEL UBI8 on Laptop running on buildah on Fedora33 on WSL on Windows: cat /etc/system-release Red Hat Enterprise Linux release 8.3 (Ootpa) I would like to have Kernel 5.8 for a full pledge RHEL.

It’s still considered a blocker if the system doesn’t support it natively. Third party tools are not a valid excuse to call it a non-blocker since this is considered a workaround, not a valid fix.

@carwyn All of the distro devs with whom MS has announced support (Ubuntu, Redhat, and OpenSuse) are now firmly in the Systemd camp, and all of their package managers and packages assume systemd is running. MS does not want to maintain its own distribution of Linux userland for WSL. Presumably if they do make a switch over to systemd from WSL-init, there should be nothing stopping you from writing shellscripts to wrap the interop daemon to work with sysVinit scripts.

@Justsnoopy30 Let me explain… no, there is too much. Let me sum up.

  1. The WSL2 kernel is the Ubuntu (or rather Linux) kernel. And in any case, while you could compile your own kernel from the Ubuntu source rather than the essentially identical Microsoft source and set your new kernel in .wslconfig as described here, it wouldn’t help you with systemd/changing init, because the stock Microsoft kernel doesn’t do anything different with regard to init. See here: https://github.com/microsoft/WSL2-Linux-Kernel/blob/master/init/main.c#L1089

  2. The init you see as pid 1 within a WSL 2 distro isn’t the top-of-the-tree first-process-run-by-the-kernel init anyway, because WSL 2 has to run multiple parallel distributions/Linux environments on top of a single kernel. You’re inside a PID namespace, and there’s a whole superior namespace, etc., that you can’t see from inside a distro. See #4425 for gory details.

  3. Because of (2), you can’t replace the Microsoft init without having a new init that understands how to do all the distro-environment-managing, interop, etc., stuff that the Microsoft init does, and since we don’t have one, we can’t.

  4. But what you can do is replicate the scenario in (2), and run systemd as pid 1 inside your own PID namespace, which behaves - okay, well, it doesn’t behave exactly identically to Linux-on-bare-metal, but because of (2), any WSL 2 distro doesn’t behave exactly identically to Linux-on-bare-metal in exactly the same ways anyway, so you aren’t actually losing anything. More to the point, it’s possible to rig this up such that it happens automatically and for the most part transparently.

I wrote a tool to automate the process, which you can find here: https://github.com/arkane-systems/genie . @onomatopellan posted a link to a shell-script hack to do something similar a few posts above this one, and if you go far enough back, you can find @therealkenc 's original post. These are currently the best options available to get a systemd environment under WSL 2.

  1. Seriously, this is it. As you can see by the length of this thread, literally everything that someone has been able to think of has been tried already, and as you can see by the enthusiasm in this thread, the moment anything changes with regard to systemd on WSL2 it will be posted here, probably several times.

This is a load of rubbish. Microsoft announce a Linux subsystem. They announce you can use all these wonderful operating systems. But hey, we are not providing a systemd - and you have to sort these issues out for yourselves.

Sorry but that is simply an unacceptable position for a responsible Software Vendor to take (however, not surprising given its MS).

Either do or do not, but Microsoft should not badge this lovely container/runtime environment and then tell people tough luck, people have to change what they love about that environment.

It sounds much like Microsoft know they know that they cannot implement this feature, due to problems in the Windows, and are trying to move the community with them. Totally unacceptable.

As I said, either do or do not, there is not middle ground.

I did my own take on this problem based on genie and the Snapstore guide, while it is inherently possible to use systemd using a namespace, there’s still a problem which is making sure the PWD interop with bash works as well (some people needed this)

So my approach was basically adopt Genie’s approach and remixed the Snapstore guide, this is my take, I appreciate patches and suggestions.

I also raised this issue on the Windows Insider Feedback Hub so give that a upvote so Microsoft is aware of what people wants from a Linux environment.

I had to change the /usr/bin/systemd-start file to:

Better. But you don’t need to mount cgroups. And it is better not to futz with the default target, unless you’ve got a specific reason for a custom target. [And if you do, use --system-unit=your-target.]

One liner:

$  sudo daemonize /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target 

Now find out the PID of the agetty that was launched, which is in the namespace we want. [Also kill the superfluous systemd-udevd while we’re there. I haven’t looked into why so many get spawned. Shrug.]

$ sudo killall systemd-udevd
$ ps ax | grep agetty
  229 tty1     Ss+    0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
  273 pts/0    D+     0:00 /bin/sh /bin/egrep --color=auto .*tty1.*agetty

Now enter your poor-man’s container.

$ sudo nsenter -t 229 -m -p /bin/login -f ken

And there you have it.

image

any update of this?

No, I keep putting off making the concerted effort because (a) there have been hints and more hints the devs are working towards getting the necessary related pieces working, and (b) the problem isn’t actually systemd per-se at this point (per screencap above). The problem is the next thing systemd is going to want to do is fire up stuff like systemd-logind, systemd-udevd, systemd-networkd, systemd-resolved and those exercise other largish gaps in the WSL ABI.

So… what would happen is… I post a systemd PoC and get an all-too-predictable “this sux” from the audience. Getting those things above limping (or just configuring them away) is “doable”, mind - with effort. But without a roadmap to what the devs are actually working on (or not), duplicating the effort becomes less appealing as a time sink. The irony is if the devs had posted “we are never working on systemd it is totally out of scope” I probably would have worked on it harder. But realize: the fact they’ve never said anything like that is a good thing.

I’ll post if I ever pick up banging on it some more. The interest is understood and appreciated.

For myself, I would like to be able to run systemd not to replace the WSL init, but because being able to start a pseudo-init with systemd --system --unit=foo.target would be a convenient way to start and subsequently manage a set of services/daemons required for a particular development, etc., configuration. By having multiple .target configuration files, that way systemd could be used to set up whatever service-set happened to be needed for a given session in a suitably Linuxy way.

(And, I suppose, those who want services running all the time could put bash.exe -c /bin/systemd into a startup shortcut, but that’s just gravy compared to the convenient start-and-manage service functionality that’d come out of it.)

((I added a uservoice request for this here, BTW: https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/16571479-support-for-running-systemd .))

Something in this old thread ( @jackchammons ), but it seems that WSL (and all the Win10 kernel design) is a Top Secret. 😃 https://blogs.msdn.microsoft.com/wsl/2016/06/08/wsl-system-calls/

Let’s stay on topic of systemd in WSL please.

I have the opposite opinion of @TBBle. I personally think that sytemd should be supported and by default available for all distros which use systemd outside of WSL too. If anything, it should be opt-out

I think it really comes down to what WSL2 wants to be and what users expect from it

I would like to use WSL2 to run quite a few applications in it and many won’t work without workarounds or at all when systemd does not work.

Well, folks, I’ve got systemd up and running happily in WSL 2. Just a matter of some config tweaking.

Blog post with the juicy details follows:

https://randomactsofcoding.wordpress.com/2019/06/13/systemd-on-wsl2/

Above way too verbose. From the FAQ:

Can I run ALL Linux apps in WSL? No!

Which is kinda clear.

It sounds much like Microsoft know they know that they cannot implement this feature, due to problems in the Windows

MSFT knows they can implement this feature. I know this because I can implement this feature.

If the desire is to manage a set of services under WSL in a more “linuxy” way it may be worth generalizing this discussion to look at other light weight approaches of doing this. SysV init is one option but there are others that have been developed for more constrained environments that may be of interest for example:

I’d hazard a guess that systemd may be one of the trickier ones to implement let alone track.

I suspect the main motivation for WSL is twofold: for developer tools and for running containers. In both cases they tend to be single thing or chain/group of things per invocation.

And to add something, keep in mind Crostini supports systemd by default (at CrOS, we use LXD as our backend, so we use the distro-standard init). It should be standard that whatever init exists in a distro, the VM/distro manager should use it, not use some shim and limit its usage. We’re talking about a full Linux experience here after all, currently, WSL isn’t a “full” experience yet.

FYI: https://www.theregister.com/2021/11/17/ubuntu_team_systemd_windows_wsl2/

Canonical may be working on introducing systemd to Ubuntu on Windows Subsystem for Linux (WSL), according to a post by Ubuntu Desktop Team Technical Leader Didier Roche

I wonder if Canonical is in contact with people from MS WSL?

WSL2 is good for play, but not production. I have switched to vmware based solution. When I using wsl2, there are many times I remind myself wsl2 have some features broken, be careful. Seaching for systemctl, acrossing IO speed bewteen window and wsl2 etc solution is too waste time for me. I just want to coding. 😥

Find an interesting solution here. https://github.com/yuk7/ArchWSL/issues/20#issuecomment-525916104

You can work around the issues with systemctl by replacing it with this script by @gdraheim: https://github.com/gdraheim/docker-systemctl-replacement/blob/master/files/docker/systemctl.py just do the following (assumes /usr/bin/systemctl) : mv /usr/bin/systemctl /usr/bin/systemctl.old curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl chmod +x /usr/bin/systemctl

I’d presumed we’d specifically not want systemd-timesyncd because the Windows host is already doing time sync for the system clock, and doing it twice over seems unhealthy.

Anyway: thanks muchly for the education. Since no-one seems likely to treat anything of mine as remotely official, I’ve hacked together my own fairly-minimal helper tool for this, which anyone is welcome to use in an entirely unofficial way:

https://github.com/arkane-systems/genie

I doubt that if they enable init-replacement it will be hardcoded for systemd

Technically WSL /init isn’t hardcoded right now (and never has been). WSL respects the init path specified in HKCU\Microsoft\Windows\CurrentVersion\lxss\[gid]\KernelCommandLine along the lines of GRUB. The catch is we don’t know right now the minimal requirements of init to make lxssmanager (and by extension wsl.exe) happy. I was going to look into it but never really got around before they made /init static.

In any case, there is no need to worry about WSL making some kind of unilateral move to systemd. If or when systemd is supported by WSL, every other init under the sun be it OpenRC or MSFT’s home grown /init will be supported too. Because systemd depends on all the things.

@fpqc

Basically, I want to compare against this: Gentoo Kernel Options for Systemd. Aside from cgroups, the requirements don’t seem that crazy to emulate.

Yeah, that’s my wild speculation too. Note you can basically ignore devtmpfs as well (on the flipside, you are definitely relying on PTY support). And you don’t need to support any writes to sysfs: systemd already supports running in a container, where sysfs is read-only.

writev(2, [{“Trying to run as user instance, “…, 64}, {”\n”, 1}], 2) = 65

If you’re trying to make any sense out of this, you probably want to look at what the full error message says! Fortunately I can guess.

$ git grep 'Trying to run as user instance, '
src/core/main.c:                log_error("Trying to run as user instance, but the system has not been booted with systemd.");
src/core/main.c:                log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");

I’m not clear on the exact requirements, but you can’t run subsidiary systemd instances - like the per-user managers for user services - unless the system itself is managed by a systemd manager. The per-user managers work within the cgroup hierachy which is set up by the system manager. The system manager expects to have overall control of the cgroup hierarchy. You probably want a system manager anyway. https://www.freedesktop.org/wiki/Software/systemd/ControlGroupInterface/

IMO, if you can’t manage to run systemd as PID 1, then you’re struggling.

It’s absolutely allowed to have processes that run before the systemd system manager, to do the initialization tasks that have been mentioned here so far. We call them initrds or container managers 😛. Specifically, your proprietary init can set everything up as PID 1, and then transmogrify into /lib/systemd/systemd using the magic of exec().

My feeling is the above would be very easy for the distro partners to see if they look at it.

However the architectural diagram is a bit misleading in this sense IMO. I think you need to refer to https://blogs.msdn.microsoft.com/commandline/2017/12/04/background-task-support-in-wsl/ as well. Since you can open multiple bash windows on the same underlying system, WSL needs to launch processes inside the Linux system. This is what makes it somewhat less clear:

Although systemd already supports containers with dynamic consoles (machinectl login), this presents a few challenges. You have to talk DBus to systemd.

Instead, I would try injecting the process complete with PTY, as WSL does currently, but have the process first talk DBus to systemd to place it in a proper scope unit before it exec()s bash. This might be as simple as just launching systemd-run --quiet --scope /bin/bash in place of bash! The point of this is to avoid the bash shells ending up in root.slice, which doesn’t look good.

@kenshen112 no. Only changes or custom systemd unitfiles go into /etc/systemd.

default unitfiles belong in /usr/lib/systemd/system

Systemd provides tooling to build and store diffs of default unitfiles in your /etc/systemd/system as well as completely custom unitfiles.

But it break interrop function (can not used cmd.exe or ps).

The one liner being pasted around takes down interop because both $PATH and $WSL_INTEROP get blown away by the nsenter. To be clear, nsenter is not a particularly good way to get into the container. That was a de-minimis hack. But setting that aside, you can get back your interop by restoring the environment variables:

image

Well this future would be great to use PostgreSQL or MySQL in Xenial instead of the Windows Installer. Which would be really really huge, especially if systemd would be invoked on Windows startup and not on bash Startup.

The creators update did many things right, but this would be extremly huge as well. The best thing would be that Windows would be clean and the Development environment can be trashed and cleaned really really easy.

@fpqc init, upstart etc etc requires a continuous operation; in this moment WSL exists only when calling bash … perhaps in the future will be different … (?) I think now you could only simulate an init or an upstart, but does not implement them really (emulate them) for system calls I think that certain things cannot be implemented (emulated) because WSL is not a standalone operating system, but I see no reason why they cannot be simulated at least providing a dummy response. I think the reason is only the young age of WSL, just a few months, but at the end we will have a very close to the “true” Linux.

Sorry for my bad English.

Agreed. Plus Wine is pretty much a non-solution. systemd for WSL is still a huge blocker, and IMO the largest blocker yet.

How can I salvage my WSL2 instance with a faulty init script?

If you only need to remove the script, try from cmd wsl.exe -u root -e rm /etc/profile.d/00-wsl2.systemd.sh.

If you have bad startup config in /etc/wsl.conf, perhaps you can run wsl -u root -e mv /etc/wsl.conf /etc/wsl.conf.bak.

EDIT: you know what -u root does, don’t you 😉 As for -e, it means do not invoke the default login shell and run the rm or mv binaries directly.

Yes it has been a while; I got sidetracked on a “what would it take” time-sink on nmap. I’ll try to get back to systemd and will follow up if there is anything worth posting (friendly or otherwise).

I suggest creating an issue in the project’s repo instead.

I’ve been experimenting with that. In theory, it would be useful as a way to automatically ensure genie -i is run on every distro start, but when I try that, the genie spawned from that option hangs – apparently, waiting for a subprocess to terminate, which subprocess is left sitting around as a zombie.

So that’s an issue I’ll probably be reporting just as soon as I check that it’s still in the latest Insider build, and assemble a smaller repro case than the whole of genie.

Unfortunately is not enough for running things

As I said before, you will get the idea, didn’t you? 😊 Also try to add

  • In /etc/wsl.conf file:
[boot]
command = "sudo daemonize /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target"
  • In %UserProfile%\.wslconfig file:
[wsl2]
debugConsole=true

The debug window will show the systemd login prompt, like Ubuntu 20.04.1 LTS HostName ttyS0. FYI, the debug window opens with console=ttyS0,115200 debug Linux kernel options.

I’m not sure what the concern is about having extra things running or WSL 2 hanging around in the background. My debian/genie/systemd installation has all the default Debian background services, plus snapd, cups, autofs, tor, pulseaudio, keybase – hell, I’ve got a full copy of emacs running under my user systemd – and not only is total memory usage of WSL still under 2GB, but net effect on system performance when left in the background is, so far as I can tell, zero. And this isn’t what you might call the newest or most endowed of machines, either.

Anyone seen noticeable-without-looking-at-the-numbers performance degradation from having WSL 2 idling in the background?

@therealkenc I at least had to manually mount the cgroups or else systemd errored out on startup.

Try the one liner off a clean boot and clean install without whatever it is was futzed in /etc/systemd (tbh I didn’t look). If it errors out post a screencap whatever other details you can think of, and we can dive a bit. The PID 1 systemd doesn’t expect a cgroups to be there before it was run. It is the first thing running (see, because PID 1).

it seemed to me that this adds a fair bit of overhead in terms of things I don’t want or need (looking at the above, timesyncd, irqbalance, lvmetad, accounts-daemon, sshd

Sure, absolutely valid to decide you don’t want systemd-timesyncd. But not special. If you don’t fancy systemd-timesyncd (maybe you prefer chrony instead), then inside your container do:

$ systemctl stop systemd-timesyncd
$ systemctl disable systemd-timesyncd

The next time you start up it won’t be there.

But n.b., none of those daemons are there for their health. Or see, they wouldn’t fire up on Ubuntu. I’ll give ya irqbalance, for us, maybe, for our not-quite-type-1 hypervisor. But then irqbalance isn’t hurting anybody either. There might even be some weird use case that wishes it were running. Disable it. Or not.

That all said, a custom target is of course a perfectly valid way to go in some special cases. That’s why custom targets exist. And indeed, containers are ‘Exhibit A’ of a special case.

Bringing us to the one elephant in the room which you mention: agetty. Ubuntu’s stock targets launch agetty, and we don’t really have a tty1 (or tty2 or tty3) in a container scenario (unless you were to fake them).

When you start up WSL you’d see more than one /init process. One of those is agetty by another name. In a more perfect world, WSL /init would be broken up and we could use it.

I’d need to enter the namespace every time I run a new shell or wsl something.

Sure do.

A complete solution would cleverly wrap pretty how to enter (log in to) the namespace and slowly converge on lxc in a death march. You can also forgo wsl.exe entirely and enter your container with Windows ssh.exe (wrapped up all pretty natch). Tempting for me to post something minimal on github, but then there is the risk of it being treated as some kind of official “WSL” systemd solution.

The only drawback I’ve run into so far

No. You aren’t even in the right cgroup tree let alone the right mount and PID namespace. Not really worth getting into the weeds here, since none of this is related to WSL and this post is TL already. Lots of sites dedicated to systemd and containers. I just trying to head off a spiritual repeat of # 637 and have a dupe target.

Keep experimenting though and hopefully have some fun doing so! ❤️

There really isn’t anything to see. I’m not just being coy; there just isn’t.

But here is the code in the state I last left it. All whopping 221 lines of it. It references wslshim.so, which you aren’t gonna need in WSL2. You can run the code in a VM right now without the shim. But then you can also run Docker or LXC in a VM right now too. n.b. I haven’t even compiled the thing since at least last fall. I’m posting only because saying “no you don’t get to see the code” would just be rude.

Sólo digo.

wsltub-first-light

any update of this?

@fpqc - The Microsoft init (/init, rather than /sbin/init or whatever) is definitely proprietary. It and any other parts of the WSL environment are probably written to meet the bare minimum for whatever requirements they thought would be needed. If the Devs decided they needed cgroups, it will be there, and if they didn’t, it probably won’t. 😛

Not saying you shouldn’t be curious, or that trying to figure it out based on clues might not provide an interesting way to pass time.

It looks to me as though they could implement most of a whole linux userland system if they wanted, and much more of the rest of the system, by which I mean to a user everything would work as expected including services. So the issue will likely be how much of that can users like us convince them (in Uservoice or whatever) is worthwhile.

@iz0eyj - right now, most of what you describe is happening: when Bash.exe runs, init is running (apparently launched or managed from svchost.exe or lxssmanager or something). No, it doesn’t start when Windows starts (yet) but neither does each Bash.exe console start another init. And yes, it does stop (and tear down the environment) when the last Bash.exe is closed; but until that last Bash.exe closes, init is there, running along in the background. From my perspective, the tearing down of the linux environment is just a matter of the Devs requiring that the system “shutdown -h now” when the last Bash.exe closes. So in a sense, init is running continuously … when the environment is running.

I agree that for many things it would be good enough for init (or whatever accepts system calls) to return dummy responses that satisfy whatever we are trying to run. For example, ifconfig could return what appear to be two devices, a loopback device and an ethernet device – even though they don’t really exist. (Or maybe there is a pseudo-device that would work without pretending to be a hardware adapter.) As long as networking works, I won’t be too picky. I suppose someone will need a VPN, or more control over a tun/tap, and maybe that can come in the future.

@fpqc - For some things it is helpful to look in Process Explorer to see that init is running under a svchost.exe. What you see from ps -A (or ps -Af) could just be a different init in each different bash.exe console, but checking Process Explorer shows that it’s the same one (or at least it appears to be) and it is running independently of Bash.exe (aside from being shutdown when the last Bash.exe exits).

@iz0eyj That’s not true, do $ ps -A

You will see that the init daemon is running at pid 1.

Some people recommend to just a proper VM, for example with VMware Workstation. But with this we lose the great Windows integration WSL gives us. For example with wslview I can open anything in WSL with a Windows tool. For example the explorer. This is not possible with a “normal” VM.

Genie seems to be the most used systemd solution for WSL

https://github.com/arkane-systems/genie

But there are still quite a lot of issues with it

I’m using SUSE Linux Enterprise Server 15 SP1 on my windows 10 machine. I installed it from microsoft store. I facing the same issue mentioned here. Is there any workaround to start (force) the services. I tried all the things mentioned. Please help.

@khuongduybui MVP, thank you very much. I tried logging in as root before, but because init was borked, it failed just like my own users ordinary login shell. That one time I tried removing the file with --exec as well, I had a typo in the filename and it didn’t do as intended. Now that you mentioned it can be removed this way, I looked closer and found the typo. Thanks again! Life saved!

Following @Biswa96’s instructions we still have one issue, if we try to run sudo systemctl enable <service>, I get this error:

image

Going to dig further into this but I think everyone should look into this.

If you are using Windows 10 insider builds 20201 or above then add the following two lines in /etc/wsl.conf file in the specific distribution:

[boot]
command = "sudo daemonize /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target"

Now shutdown every running WSL instances with wsl.exe --shutdown command. Then restart the distribution in WSL. systemd service will be spun up automatically with that distribution. The command field can be replaced with any command you know. It can be used to execute systemctl or daemonize or xinit or service, you get the idea 😉

So, how does this work? you may ask. Before initializing the WSL instance /init binary parses the /etc/wsl.conf file. It reads the command from that field and fork(2) a child process. The child process then executes the command in system(3) call which in turns call posix_spawn(3) 🤓

To add to this, some distros like PengWin omits daemonize and systemd so if you happen to use them, make sure to install them

@mangelozzi As that developer, while I flatter myself that it works well enough for most purposes and would be delighted to collaborate on this, genie is essentially a series of hacks wrapped around a workaround @therealkenc first came up with, much nearer the top of this thread. There’s nothing terribly special about it except for time spent poking at edge cases, and there are still cases (vscode wsl extension, pstree inside the bottle, etc.) that it can’t handle cleanly.

Doing it “properly” would be a rather more complex matter.

Running interop from UNC paths (meaning, as a practical matter, in a pwd that is not some subdirectory of /mnt/c) is currently not supported. They run anyway, but with a warning. Changing the directory quashes that warning. Yes one could create a bash shell function runwin32 (or somesuch), if they wanted, or thought it was useful, as opposed to just disregarding the warning message.

image

Here is a method to have systemd on Debian. Works for me (works fine with docker, swarm, K8s, etc…)

But it break interrop function (can not used cmd.exe or ps).

https://github.com/shayne/wsl2-hacks/blob/master/README.md

@wmertens No. Per manpage:

These options will have no effect when invoked without --test, as during regular invocations the service manager will automatically detect whether it shall operate in system or per-user mode, by checking whether the PID it is run as is 1 or not. Note that it is not supported booting and maintaining a system with the service manager running in --system mode but with a PID other than one.

If you try it, all you get is:

$ systemd --system Can't run system mode unless PID 1.

I assume in the future Distros with wsl will come with systemd running correctly?

Dunno (wouldn’t know). I wouldn’t personally assume one way or the other, at least in the medium term.

what in the world is wsltub

A tub in which wsl can splash around. I’ll post code if I take it to the point of being consumer friendly. This is just first light.

Just 'sayin.

wsltub-first-light

<del> > I imagine MS might want to hack in a few /sys features, whereas systemd in container has so far been written to expect a read-only /sys. If the /sys mount exists and is not marked as a read-only mount, we will try to run udev, and that probably won't do anything sensible :). If nothing else it might have to get ConditionVirtualization=!wsl. </del>

<del>https://github.com/systemd/systemd/issues/8036#issuecomment-361223115</del>

edit: this example seems to have been good derail-bait. speculative and not sure it’s a useful approach.

@kenshen112 yes, because systemd is not launched. Right now WSL uses MS’s custom init daemon, which also sets up the whole interop and tty piping infrastructure

It makes no sense to go back to the old *sh init files. SystemD IMO provides a superios way to manage services and all modern services in a linux distro have a service file ready. Why would one do the effort to write init files?

one of Ubuntu official develops says M$s intent is not a complete linux, its just something like chroot.