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?

5
  • Does the directory have to be called exactly /init? Can you change the name of the directory at least?
    – Fanatique
    Commented Nov 5, 2019 at 9:41
  • @Fanatique Unfortunately no, as I mentioned it is a constraint that cannot be changed.
    – Aetos
    Commented Nov 5, 2019 at 9:41
  • 1
    FYI, WSL isn't the first system to reserve this path – the Linux kernel has used /init as the initramfs main executable's path since 2005. Creating a custom directory there was already a somewhat bad choice. Commented Nov 5, 2019 at 9:42
  • @grawity I agree, but it was not my choice unfortunately and I have to work with it as is. My client is a big one (Very difficult to make change occur in their processes), and this directory is key to the project.
    – Aetos
    Commented Nov 5, 2019 at 9:43
  • @Biswapriyo, thank you for the warning. Will be glad to read your response :)
    – Aetos
    Commented Nov 5, 2019 at 10:34

2 Answers 2

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.

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.
6
  • Even when I delete it, as soon as I start wsl again the init will show up once more. So how to liberate the name init in the root of the folder?? When I create a directory /init in the root folder via windows, and I try to start wsl, it just says Access denied
    – Aetos
    Commented Nov 5, 2019 at 17:11
  • As I mentioned before LxssManager service copies it everytime after WSL distribution is launched. So you can't remove it if the distro is running.
    – Biswapriyo
    Commented Nov 5, 2019 at 17:20
  • Then I'm not sure what you suggest I do, If I can't remove it while the distro is running. Then I can't remove it at all? I must be misunderstanding something
    – Aetos
    Commented Nov 5, 2019 at 17:21
  • So what is the purpose of removing the init file? or "tarballing" the whole wsl distro?
    – Aetos
    Commented Nov 5, 2019 at 17:25
  • @Aetos The answer doesn't suggest that init can be removed entirely. It requires for WSL and that's how it works. As grawity said, the directory name isn't a good choice.
    – Biswapriyo
    Commented Nov 5, 2019 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .