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

@pchuan98
Copy link

Hello, thank you very much for your documentation; it has been very helpful to me. However, I encountered an issue during usage:

In section 2.2 regarding PID retrieval, I was getting two PIDs which caused errors in the program. Therefore, I modified it to:

pid="$(ps -u root -o pid,args | awk '$2 ~ /procd/ { print $1; exit }')"

Thanks again for sharing.

@mcsys-dev
Copy link

mcsys-dev commented Jul 18, 2024

Thank you for documenting something that niche 🥇 That is exactly what is needed :)

But haven't yet got it to work. I did exactly what you have written in your docs multiply times (maybe I did an oopsie). But everytime after section 2.1. the wsl network stack stop working for all machines until you do a wsl --shutdown but if you launch openWrt again it stops again. Has anyone an Idea why this is happening?

  • I am using Windows 11 Pro 23H2
  • and used openwrt-23.05.3-x86-generic-rootfs.tar.gz

wsl --version

WSL version: 2.2.4.0
Kernel version: 5.15.153.1-2
WSLg version: 1.0.61
MSRDC version: 1.2.5326
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26091.1-240325-1447.ge-release
Windows version: 10.0.22631.3880

I did try the tweak what @pchuan98 suggested but I still get time same result :/

Btw this is the Result of doing all steps, should it look like that?

BusyBox v1.36.1 (2024-03-22 22:09:42 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.3, r23809-234f1a2efa
 -----------------------------------------------------
Entering /sbin/procd(init) PID: 20


BusyBox v1.36.1 (2024-03-22 22:09:42 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.3, r23809-234f1a2efa
 -----------------------------------------------------
Entering /sbin/procd(init) PID: 3


BusyBox v1.36.1 (2024-03-22 22:09:42 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.3, r23809-234f1a2efa
 -----------------------------------------------------
=== WARNING! =====================================
There is no root password defined on this device!
Use the "passwd" command to set up a new password
in order to prevent unauthorized SSH logins.
--------------------------------------------------
root@Laptop-*****:~#

Edit:
It worked the whole time, the networking issue is a different problem.

/sbin/procd has PID1
OpenWrt_Wsl2_PID1

root@OpenWrt:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br-lan state UP qlen 1000
    link/ether 5e:bb:f6:9e:ee:fa brd ff:ff:ff:ff:ff:ff
3: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 5e:bb:f6:9e:ee:fa brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
       valid_lft forever preferred_lft forever

The Problem is that OpenWrt hast changed the network for all WSL machines and wants to use 192.168.1.1, but it is already in use.

@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