Skip to content

Instantly share code, notes, and snippets.

@Balder1840
Last active March 19, 2025 03:03
Show Gist options
  • Save Balder1840/8d7670337039432829ed7d3d9d19494d to your computer and use it in GitHub Desktop.
Save Balder1840/8d7670337039432829ed7d3d9d19494d to your computer and use it in GitHub Desktop.
Run OpenWrt as a custom distro in WSL2 (WSL2下直接运行openwrt)

Here is an instruction to start openwrt as a custom WSL2 distro.
I didn't start it as a gateway in my local because I don't have windows 11 installed, and you know the poor network mode in windows 10 and wsl2.

Found a way to set WSL's network mode to bridged here and have successfully configured it as a gateway in my local.

But anyway, I think it should work under windows 11 and wsl2, since you can config it as bridged mode.
Have fun!

1. Prerequisites

1.1. Import openwrt's rootfs.tar.gz as a custom WSL2 distro

you can download from the official site

https://downloads.openwrt.org/releases/23.05.0-rc3/targets/x86/64/openwrt-23.05.0-rc3-x86-64-rootfs.tar.gz

or from https://openwrt.ai/

https://dl.openwrt.ai/releases/targets/x86/64/openwrt-08.23.2023-x86-64-generic-rootfs.tar.gz

Then execute wsl --import openwrt /path/to/save/the/vhdx/ /path/to/rootfs.tar.gz

check if above operation succeed by wsl -l --all

1.2. Install required pkgs

enter wsl by wsl -d openwrt and then execute following commands:

opkg install unshare
opkg install procps-ng-ps
opkg install nsenter
opkg install shadow-su

https://downloads.openwrt.org/releases/packages-23.05/x86_64/base/unshare_2.39-2_x86_64.ipk https://downloads.openwrt.org/releases/packages-23.05/x86_64/base/nsenter_2.39-2_x86_64.ipk https://downloads.openwrt.org/releases/packages-23.05/x86_64/packages/procps-ng-ps_3.3.16-3_x86_64.ipk https://downloads.openwrt.org/releases/packages-23.05/x86_64/packages/shadow-su_4.8.1-3_x86_64.ipk

2. Create or update following configuration files

2.1. /etc/wsl.conf

[boot]
command = "/usr/bin/env -i /usr/bin/unshare --pid --mount-proc --fork --propagation private -- sh -c 'exec /sbin/init'"

you should quit wsl by wsl --shutdown after you config all of these and restart your openwrt distro

2.2. /etc/profile.d/wsl-init.sh

you will get error Failed to connect to ubus if /sbin/procd not set as pid 1, at least I got the error
and you will get the openwrt banner twice, you can resolve it as you wish

#!/bin/bash

# Get PID of /sbin/procd(init)
# seems that we can't set /sbin/init as pid 1, as it quit immediately after started by above command in `wsl.conf`
sleep 1
pid="$(ps -u root -o pid,args | awk -e '$2 ~ /procd/ { print $1 }')"

# Run WSL service script
if [ "$pid" -ne 1 ]; then
  echo "Entering /sbin/procd(init) PID: $pid"
  exec /usr/bin/nsenter -p -m -t "${pid}" -- su - "$USER"
fi

2.3. /etc/profile

change

[ -n "$FAILSAFE" ] || {
        for FILE in /etc/profile.d/*.sh; do
                #[ -e "$FILE" ] && . "$FILE"
        done
        unset FILE
}

to

[ -n "$FAILSAFE" ] || {
        for FILE in /etc/profile.d/*.sh; do
                if [ "$FILE" == "/etc/profile.d/sysinfo.sh" ]; then
                 [ "$(which bash)" ] && env -i bash "$FILE"
                elif [ "$FILE" == "/etc/profile.d/wsl-init.sh" ]; then
                 [ "$(which sh)" ] && env -i sh "$FILE"
                else
                 [ -e "$FILE" ] && . "$FILE"
                fi
        done
        unset FILE
}

Reference

https://wsl.dev/wsl2init/
https://openwrt.org/docs/techref/init.detail.cc
https://openwrt.org/docs/techref/procd

@mcsys-dev
Copy link

OpenWrt_Wsl2_PID1

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