Skip to content

Commit

Permalink
🎨 WSL2 开发环境
Browse files Browse the repository at this point in the history
  • Loading branch information
nmy authored and StevenBaby committed Sep 20, 2023
1 parent 6668ed7 commit 658cc39
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 60 deletions.
63 changes: 63 additions & 0 deletions docs/12 调试工具/999 WSL2 开发环境.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# WSL2 开发环境

## minix 文件系统

mount 的时候 minix 文件系统未知,通过查证,需要重新编译 Linux,然后开启 minix 文件系统支持。

需要安装一些包:

sudo pacman -S bc
sudo pacman -S pahole

---

从下面的地址下载的源码。

> <https://github.com/microsoft/WSL2-Linux-Kernel/releases>
---

修改文件 `Microsoft/config-wsl`,添加下面一行,以支持 minix 文件系统:

CONFIG_MINIX_FS=y

我还改了:

CONFIG_CC_VERSION_TEXT="gcc (GCC) 13.2.1 20230801"

---

然后:

make KCONFIG_CONFIG=Microsoft/config-wsl -j16


后面 -j16 表示 16 个任务同时编译,可以根据自己的 CPU 数量来设置,等待,编译完成。。。

---

然后将编译好的文件 `arch/x86/boot/bzImage` 复制到用户目录 `%UserProfile%`, 该目录一般为 `C:/Users/xxx/`,当然也可以放在其他位置,自行决定;也有人直接替换了 `C:\Windows\System32\lxss\tools\kernel`,然后无需下面的配置文件,不过直觉告诉我,这种方式不推荐。

在用户目录添加或修改配置文件 `.wslconfig`

```
[wsl2]
kernel=C:\\Users\\xxx\\bzImage
```

然后重启 `wsl2`

wsl --shutdown

重新进入 `wsl`,输入命令 `uname -r` 得到:

5.15.123.1-microsoft-standard-WSL2

## 网络 NAT 模式

这部分内容来自 [@znyin](https://github.com/znyinyyniu),经过了优化。

## 参考

- <https://gist.github.com/oleksis/eb6d2f1cd2a6946faefb139aa0e38c35>
- <https://wiki.archlinux.org/title/Internet_sharing#Enable_NAT>
9 changes: 7 additions & 2 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SRC:=.
ONIX_VERSION:=1.0.0
MULTIBOOT2:=0x18000
ENTRYPOINT:=$(shell python -c "print(f'0x{$(MULTIBOOT2) + 0x8000:x}')")
WSL2:=$(findstring WSL2,$(shell uname -r))

CFLAGS:= -m32 # 32 位的程序
CFLAGS+= -march=pentium # pentium 处理器
Expand Down Expand Up @@ -93,7 +94,7 @@ LDFLAGS:= -m elf_i386 \
--section-start=.multiboot2=$(MULTIBOOT2)
LDFLAGS:=$(strip ${LDFLAGS})

$(BUILD)/kernel.bin: \
$(BUILD)/kernel.raw.bin: \
$(BUILD)/kernel/start.o \
$(BUILD)/kernel/onix.o \
$(BUILD)/kernel/main.o \
Expand Down Expand Up @@ -186,9 +187,13 @@ $(BUILD)/kernel.bin: \
$(shell mkdir -p $(dir $@))
ld ${LDFLAGS} $^ -o $@

$(BUILD)/system.bin: $(BUILD)/kernel.bin
$(BUILD)/system.raw.bin: $(BUILD)/kernel.raw.bin
objcopy -O binary $< $@

$(BUILD)/kernel.bin: $(BUILD)/system.raw.bin
python utils/generate.py

$(BUILD)/system.bin: $(BUILD)/kernel.bin
objcopy -O binary $< $@

$(BUILD)/system.map: $(BUILD)/kernel.bin
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cmd.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ QEMU_DISK_BOOT:=-boot c
QEMU_DEBUG:= -s -S

.PHONY: qemu
qemu: $(IMAGES) $(BR0) $(TAPS)
qemu: $(IMAGES) $(TAP0)
$(QEMU) $(QEMU_DISK) $(QEMU_DISK_BOOT)

.PHONY: qemug
qemug: $(IMAGES) $(BR0) $(TAPS)
qemug: $(IMAGES) $(TAP0)
$(QEMU) $(QEMU_DISK) $(QEMU_DISK_BOOT) $(QEMU_DEBUG)

# VMWare 磁盘转换
Expand Down
22 changes: 18 additions & 4 deletions src/utils/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@

import os
import binascii
from elftools.elf.elffile import ELFFile
import shutil
try:
from elftools.elf.elffile import ELFFile
except ImportError:
print('''
import elftools failure.
You may need to install elftools packet using either
pip install pyelftools
or in Archlinux
sudo pacman -S python-pyelftools
''')
exit(-1)

DIRNAME = os.path.dirname(os.path.abspath(__file__))
BUILD = os.path.abspath(os.path.join(DIRNAME, "../../build"))
KERNELRAWBIN = os.path.join(BUILD, 'kernel.raw.bin')
SYSTEMRAWBIN = os.path.join(BUILD, 'system.raw.bin')
KERNELBIN = os.path.join(BUILD, 'kernel.bin')
SYSTEMBIN = os.path.join(BUILD, 'system.bin')


def main():
start = 0x8000
size = os.path.getsize(SYSTEMBIN) - start
size = os.path.getsize(SYSTEMRAWBIN) - start

with open(SYSTEMBIN, 'rb') as file:
with open(SYSTEMRAWBIN, 'rb') as file:
data = file.read()
chksum = binascii.crc32(data[start:])
print(f"system.bin chksum: {chksum} size {size}")

shutil.copy(KERNELRAWBIN, KERNELBIN)

with open(KERNELBIN, 'rb+') as file:
elf = ELFFile(file)
section = elf.get_section_by_name(".onix.magic")
Expand Down
21 changes: 11 additions & 10 deletions src/utils/image.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MUSIC:= ./utils/deer.mp3
LOOP:= $(shell sudo losetup --find)

$(BUILD)/mono.wav: $(MUSIC)
ffmpeg -i $< -ac 1 -ar 44100 -acodec pcm_u8 -y $@
Expand Down Expand Up @@ -36,13 +37,13 @@ $(BUILD)/master.img: $(BUILD)/boot/boot.bin \
sfdisk $@ < $(SRC)/utils/master.sfdisk

# 挂载设备
sudo losetup /dev/loop0 --partscan $@
sudo losetup $(LOOP) --partscan $@

# 创建 minux 文件系统
sudo mkfs.minix -1 -n 14 /dev/loop0p1
sudo mkfs.minix -1 -n 14 $(LOOP)p1

# 挂载文件系统
sudo mount /dev/loop0p1 /mnt
sudo mount $(LOOP)p1 /mnt

# 切换所有者
sudo chown ${USER} /mnt
Expand Down Expand Up @@ -74,7 +75,7 @@ $(BUILD)/master.img: $(BUILD)/boot/boot.bin \
sudo umount /mnt

# 卸载设备
sudo losetup -d /dev/loop0
sudo losetup -d $(LOOP)

$(BUILD)/slave.img: $(SRC)/utils/slave.sfdisk

Expand All @@ -85,13 +86,13 @@ $(BUILD)/slave.img: $(SRC)/utils/slave.sfdisk
sfdisk $@ < $(SRC)/utils/slave.sfdisk

# 挂载设备
sudo losetup /dev/loop0 --partscan $@
sudo losetup $(LOOP) --partscan $@

# 创建 minux 文件系统
sudo mkfs.minix -1 -n 14 /dev/loop0p1
sudo mkfs.minix -1 -n 14 $(LOOP)p1

# 挂载文件系统
sudo mount /dev/loop0p1 /mnt
sudo mount $(LOOP)p1 /mnt

# 切换所有者
sudo chown ${USER} /mnt
Expand All @@ -103,7 +104,7 @@ $(BUILD)/slave.img: $(SRC)/utils/slave.sfdisk
sudo umount /mnt

# 卸载设备
sudo losetup -d /dev/loop0
sudo losetup -d $(LOOP)

$(BUILD)/floppya.img:

Expand All @@ -112,8 +113,8 @@ $(BUILD)/floppya.img:

.PHONY: mount0
mount0: $(BUILD)/master.img
sudo losetup /dev/loop0 --partscan $<
sudo mount /dev/loop0p1 /mnt
sudo losetup $(LOOP) --partscan $<
sudo mount $(LOOP) /mnt
sudo chown ${USER} /mnt

.PHONY: umount0
Expand Down
51 changes: 12 additions & 39 deletions src/utils/net.mk
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
IFACE:=onix
IFACE:=$(shell sudo ip -o -4 route show to default | awk '{print $$5}')

BR0:=/sys/class/net/br0
TAPS:= /sys/class/net/tap0 \
/sys/class/net/tap1 \
/sys/class/net/tap2 \
TAP0:=/sys/class/net/tap0

.SECONDARY: $(TAPS) $(BR0)
.SECONDARY: $(TAP0)

# 网桥 IP 地址
BNAME:=br0
IP0:=192.168.111.22
MAC0:=5a:5a:5a:5a:5a:22
GATEWAY:=192.168.111.2
GATEWAY:=172.16.16.1

$(BR0):
sudo ip link add $(BNAME) type bridge
sudo ip link set $(BNAME) type bridge ageing_time 0

sudo ip link set $(IFACE) down
sudo ip link set $(IFACE) master $(BNAME)
sudo ip link set $(IFACE) up

sudo ip link set dev $(BNAME) address $(MAC0)
sudo ip link set $(BNAME) up

# sudo iptables -A FORWARD -i $(BNAME) -o $(BNAME) -j ACCEPT
sudo iptables -F
sudo iptables -X
sudo iptables -P FORWARD ACCEPT
sudo iptables -P INPUT ACCEPT
sudo iptables -P OUTPUT ACCEPT

sudo ip addr add $(IP0)/24 brd + dev $(BNAME) metric 10000
sudo ip route add default via $(GATEWAY) dev $(BNAME) proto static metric 10000
# sudo dhclient -v -4 br0

br0: $(BR0)
-

/sys/class/net/tap%: $(BR0)
sudo ip tuntap add mode tap $(notdir $@)
sudo ip link set $(notdir $@) master $(BNAME)
$(TAP0):
sudo ip tuntap add mode tap $(notdir $@) user $(USER)
sudo ip addr add $(GATEWAY)/24 dev $(notdir $@)
sudo ip link set dev $(notdir $@) up

tap%: /sys/class/net/tap%
sudo sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -s $(GATEWAY)/24 -o $(IFACE) -j MASQUERADE
sudo iptables -A FORWARD -i $(notdir $@) -o $(IFACE) -j ACCEPT

tap0: $(TAP0)
-
6 changes: 3 additions & 3 deletions src/utils/network.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# ipaddr=192.168.111.33
# netmask=255.255.255.0
# gateway=192.168.111.2
ipaddr=172.16.16.11
netmask=255.255.0.0
gateway=172.16.16.1

0 comments on commit 658cc39

Please sign in to comment.