Skip to content

Instantly share code, notes, and snippets.

@kmatt
Last active May 5, 2024 05:13
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kmatt/aad3970a05f72fbbd5f1b9ef7ee1e330 to your computer and use it in GitHub Desktop.
Save kmatt/aad3970a05f72fbbd5f1b9ef7ee1e330 to your computer and use it in GitHub Desktop.
Install Void Linux on WSL2
# Based on https://gist.github.com/kmatt/71603170556ef8ffd14984af77ff10c5
# prompt ">" indicates Powershell commands
# prompt "$" are Linux shell commands
# https://docs.microsoft.com/en-us/windows/wsl/install-win10
> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
# install https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
> wsl --set-default-version 2
# use rootfs tarball from https://voidlinux.org/download
# ex: https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20230628.tar.xz
# uncompress but do not extract tar file (don't tar -x)
> wsl.exe --import $DISTRONAME $STORAGEPATH void-$VERSION.tar
> wsl -d $DISTRONAME
# optional - update xbps mirrors
$ cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/
# if in US https://voidlinux.org/news/2021/10/mirror-retirement.html
$ xbps-install -Su xbps
$ xbps-install -u
$ xbps-install base-system
$ xbps-remove base-voidstrap
$ xbps-reconfigure -fa
$ useradd -m -G wheel -s /bin/bash $USERNAME
$ passwd $USERNAME
# Default user
$ echo -e "[user]\ndefault=$USERNAME" > /etc/wsl.conf
# Grant sudo
$ sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers
> wsl --terminate $DISTRONAME
> wsl -d $DISTRONAME
@jsumners
Copy link

This is fantastic stuff. Thank you for creating this document.

As of at least 2022-11-11, we don't need to use the final Powershell command to set the default user. Instead, we can:

$ echo -e "[user]\ndefault=$USERNAME" > /etc/wsl.conf
$ exit
> wsl --terminate $DISTRONAME
> wsl -d $DISTRONAME

See https://learn.microsoft.com/en-us/windows/wsl/use-custom-distro (https://web.archive.org/web/20221014081630/https://learn.microsoft.com/en-us/windows/wsl/use-custom-distro).

Also, for anyone else reading this, you may want to edit /etc/sudoers prior to exiting the initial root shell. Adjust it so that your newly added user can perform sudo actions.

One other tip: set your default distribution by wsl --setdefault $DISTRONAME.

@kmatt
Copy link
Author

kmatt commented Nov 15, 2022

Simplified with suggestions from @jsumners

@thetredev
Copy link

Have you managed to get runit working? I struggled with setting up runsvdir at my first attempt, because of the volatile /run (or /var/run?) directory within the WSL instance.

@SkyNetMkII
Copy link

SkyNetMkII commented Apr 9, 2023

Have you managed to get runit working? I struggled with setting up runsvdir at my first attempt, because of the volatile /run (or /var/run?) directory within the WSL instance.

Try to add this to /etc/wsl.conf

[boot]
command="/etc/runit/1 && /etc/runit/2 && /etc/runit/3"

@thetredev
Copy link

@SkyNetMkII will try thanks.

@Nequo
Copy link

Nequo commented Apr 21, 2023

Hey, thanks for this! I've written a blog post referencing this gist as it was very helpful.

I think you need -e with echo so that the "\n" gets interpreted as a newline.
Also here is a sed to give users in the wheel group sudo access:

sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers

@kmatt
Copy link
Author

kmatt commented Jun 19, 2023

I think you need -e with echo so that the "\n" gets interpreted as a newline. Also here is a sed to give users in the wheel group sudo access

@Nequo Updated, thanks!

@uraza
Copy link

uraza commented Jul 22, 2023

I just noticed that, with a fresh Windows 11 installation, "wsl --import" is not available right away.
You first need to run wsl --install (which will install all the components + the default Ubuntu distribution), reboot, and then you can proceed with the instructions to import Void.
Ubuntu can be easily removed by running wsl --unregister Ubuntu.

@zombyh
Copy link

zombyh commented Sep 6, 2023

A useful tip, if you are in a corporate environment that uses a proxy, you can use the following command to not verify the SSL certificate: # export SSL_NO_VERIFY_PEER=1

@thetredev
Copy link

thetredev commented Oct 13, 2023

@SkyNetMkII your suggestion worked, thanks!

I configured my /etc/wsl.conf as follows:

[boot]
command="/etc/runit/1 && /etc/runit/2 && /etc/runit/3"

[interop]
appendWindowsPath = false

To still have Windows tools available under WSL2, I did the following:

# ran the following commands:

sudo mkdir -p /opt/windows
sudo ln -sf /mnt/c/Windows /opt/windows/bin
sudo ln -sf /mnt/c/Windows/System32 /opt/windows/system32-bin

# added the following to ~/.bashrc
export PATH=${PATH}:/opt/windows/bin:/opt/windows/system32-bin

To get Docker working, first install it:

sudo xbps-install docker

Then the service's run file (/etc/sv/docker/run) needs to be changed, because /lib/modules is not available under WSL2:

#!/bin/sh
exec 2>&1
[ -r conf ] && . ./conf
-modprobe -q loop || exit 1
mountpoint -q /sys/fs/cgroup/systemd || {
    mkdir -p /sys/fs/cgroup/systemd;
    mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd;
}
exec chpst -o 1048576 -p 1048576 dockerd $OPTS 2>&1

My version of the file is the following (played around a bit):

#!/bin/sh
exec 2>&1
[ -r conf ] && . ./conf

if [ -d /lib/modules ]; then
    modprobe -q loop || exit 1
fi

mountpoint -q /sys/fs/cgroup/systemd || {
    mkdir -p /sys/fs/cgroup/systemd;
    mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd;
}
exec chpst -o 1048576 -p 1048576 dockerd $OPTS 2>&1

I created a PR for that: void-linux/void-packages#46667. Fingers crossed that it gets accepted.

@thetredev
Copy link

thetredev commented Oct 13, 2023

@kmatt @jsumners @Nequo just fyi the official way of setting the default user for a WSL instance is the following:

Assuming your UID is 1000, then open up the registry editor (regedit), go to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss, click through the UUIDs until you find the one with the key DistributionName set to your WSL distro name. Upon finding it, create a new DWORD32 key called DefaultUid and set its value to decimal 1000 which will result in 3e8 hexadecimal.

Afterwards the default user is set and you don't need to configure that within /etc/wsl.conf. It probably doesn't matter which method you choose, but I personally prefer the registry. Microsoft should really allow DefaultUid to be set on the parent (Lxss), so all distros can use that value if it's not explicitly set for a distro. But I guess that's an oversight they won't fix... Gonna create an issue anyway lol.

Edit: done. microsoft/WSL#10631

@jsumners
Copy link

I highly doubt editing the registry is "the official way" when WSL reads the configuration file I noted in order to get the information.

@thetredev
Copy link

@jsumners it reads the registry first, then the config file.

@kmatt
Copy link
Author

kmatt commented Oct 15, 2023

https://learn.microsoft.com/en-us/windows/wsl/wsl-config#automount-options

I prefer not to edit the Windows registry if at all possible.

@thetredev
Copy link

thetredev commented Oct 15, 2023

Well the registry is just another place to configure the distro, and these are per-user settings anyway. It's basically the same in the end. Besides, those automount options are not the same setting as setting the default WSL user in general, they're used to give that user the respective permissions.

@DiogoHSS
Copy link

I don't get what "uncompress but do not unzip tar file" should mean

@kmatt
Copy link
Author

kmatt commented Nov 16, 2023

I don't get what "uncompress but do not unzip tar file" should mean

gunzip rootfs.tar.gz but do not tar xf rootfs.tar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment