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!
you can download from the official site
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
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
[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
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
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
}
https://wsl.dev/wsl2init/
https://openwrt.org/docs/techref/init.detail.cc
https://openwrt.org/docs/techref/procd
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.