2

I deployed a dev environment on wsl and I'm so excited about it. My only problem is the existence of an init file in the root path of the system.

root subdirectories

For one of my clients, I need to have an /init directory, this is a constraint that cannot be changed.

Needless to say, this renders my exciting and newly found wsl dev environment unusable for this project.

Is there a way to change the path of the wsl init file?

Aetos
  • 125

2 Answers2

3

Late to answer; the project originally mentioned is almost certainly over; and no one else is likely to ever have this need again. But oh well, here goes ...

Create a chroot jail. Do the following as root:

  • Create a location for the chroot: sudo mkdir /newroot; cd /newroot
  • Recreate/bindmount all of the necessary directories inside newroot:
    • mkdir usr; mount --bind /usr usr
    • mkdir etc; mount --bind /etc etc
    • mkdir root
    • mkdir -p /home/user; mount --bind /home/user home/user (substitute the default username for the WSl instance).
    • mkdir var; mount --bind /var var
    • mkdir dev; mount --bind /dev dev
    • And so on for any other required directories.
  • Pay attention to the root directory symlinks in your distribution. For instance, at least Ubuntu would also require:
    • ln -s usr/lib lib
    • ln -s usr/lib32 lib32
    • and so on ...
  • mount -t proc none proc
  • mkdir init (The customer required init directory rather than the "real" root init file)

Now, to launch the wsl session, set up a shortcut for wsl -u root -e sh -c "cd /newroot; exec /usr/sbin/chroot /newroot/" su - username. Substitute the default username of course.

This will launch the user into a chroot jail with just the directory structure you have selected, which obviously does not include the init file, but your init directory instead.

NotTheDr01ds
  • 28,025
1

tl;dr: The /init file can not be removed from a running WSL distribution. Because the subsystem is using that file and all processes in that running WSL distribution are forked from it.

  • So, what we can do? The file is locked by the subsystem while running that distribution. The workaround is to wait or to terminate that running distribution. Use wsl.exe --terminate Distro command where Distro is the name of running WSL distribution which you want to terminate. Go to the folder where the distribution is installed, follow this or this. Delete the init file.

  • Or, if you want to make a tarball of the whole WSL distribution then there is no need to terminate the running distribution. Create tarball and exclude init:

cd /
tar -cpf backup.tar --exclude=/backup.tar --exclude=/init --xattrs --one-file-system /

Check all the commandline options before running this command. Preserving file permission is must.

  • Where does the init file comes from? The init file is created by Microsoft WSL developers and not similar with systemd, sysVinit or other traditional GNU/Linux init systems. The actual file is System32\lxss\tools\init. When a distribution is launched the Linux Subsystem Manager Service (LXSS) copies that file in the rootfs folder (with CopyFileW() Win32 API). For WSL2 first the \tools folder is mounted with 9p protocol then used as above.
Biswapriyo
  • 11,584