Skip to content

Commit 658cc39

Browse files
nmyStevenBaby
authored andcommitted
🎨 WSL2 开发环境
1 parent 6668ed7 commit 658cc39

File tree

7 files changed

+116
-60
lines changed

7 files changed

+116
-60
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# WSL2 开发环境
2+
3+
## minix 文件系统
4+
5+
mount 的时候 minix 文件系统未知,通过查证,需要重新编译 Linux,然后开启 minix 文件系统支持。
6+
7+
需要安装一些包:
8+
9+
sudo pacman -S bc
10+
sudo pacman -S pahole
11+
12+
---
13+
14+
从下面的地址下载的源码。
15+
16+
> <https://github.com/microsoft/WSL2-Linux-Kernel/releases>
17+
18+
---
19+
20+
修改文件 `Microsoft/config-wsl`,添加下面一行,以支持 minix 文件系统:
21+
22+
CONFIG_MINIX_FS=y
23+
24+
我还改了:
25+
26+
CONFIG_CC_VERSION_TEXT="gcc (GCC) 13.2.1 20230801"
27+
28+
---
29+
30+
然后:
31+
32+
make KCONFIG_CONFIG=Microsoft/config-wsl -j16
33+
34+
35+
后面 -j16 表示 16 个任务同时编译,可以根据自己的 CPU 数量来设置,等待,编译完成。。。
36+
37+
---
38+
39+
然后将编译好的文件 `arch/x86/boot/bzImage` 复制到用户目录 `%UserProfile%`, 该目录一般为 `C:/Users/xxx/`,当然也可以放在其他位置,自行决定;也有人直接替换了 `C:\Windows\System32\lxss\tools\kernel`,然后无需下面的配置文件,不过直觉告诉我,这种方式不推荐。
40+
41+
在用户目录添加或修改配置文件 `.wslconfig`
42+
43+
```
44+
[wsl2]
45+
kernel=C:\\Users\\xxx\\bzImage
46+
```
47+
48+
然后重启 `wsl2`
49+
50+
wsl --shutdown
51+
52+
重新进入 `wsl`,输入命令 `uname -r` 得到:
53+
54+
5.15.123.1-microsoft-standard-WSL2
55+
56+
## 网络 NAT 模式
57+
58+
这部分内容来自 [@znyin](https://github.com/znyinyyniu),经过了优化。
59+
60+
## 参考
61+
62+
- <https://gist.github.com/oleksis/eb6d2f1cd2a6946faefb139aa0e38c35>
63+
- <https://wiki.archlinux.org/title/Internet_sharing#Enable_NAT>

src/makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SRC:=.
44
ONIX_VERSION:=1.0.0
55
MULTIBOOT2:=0x18000
66
ENTRYPOINT:=$(shell python -c "print(f'0x{$(MULTIBOOT2) + 0x8000:x}')")
7+
WSL2:=$(findstring WSL2,$(shell uname -r))
78

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

96-
$(BUILD)/kernel.bin: \
97+
$(BUILD)/kernel.raw.bin: \
9798
$(BUILD)/kernel/start.o \
9899
$(BUILD)/kernel/onix.o \
99100
$(BUILD)/kernel/main.o \
@@ -186,9 +187,13 @@ $(BUILD)/kernel.bin: \
186187
$(shell mkdir -p $(dir $@))
187188
ld ${LDFLAGS} $^ -o $@
188189

189-
$(BUILD)/system.bin: $(BUILD)/kernel.bin
190+
$(BUILD)/system.raw.bin: $(BUILD)/kernel.raw.bin
190191
objcopy -O binary $< $@
192+
193+
$(BUILD)/kernel.bin: $(BUILD)/system.raw.bin
191194
python utils/generate.py
195+
196+
$(BUILD)/system.bin: $(BUILD)/kernel.bin
192197
objcopy -O binary $< $@
193198

194199
$(BUILD)/system.map: $(BUILD)/kernel.bin

src/utils/cmd.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ QEMU_DISK_BOOT:=-boot c
3333
QEMU_DEBUG:= -s -S
3434

3535
.PHONY: qemu
36-
qemu: $(IMAGES) $(BR0) $(TAPS)
36+
qemu: $(IMAGES) $(TAP0)
3737
$(QEMU) $(QEMU_DISK) $(QEMU_DISK_BOOT)
3838

3939
.PHONY: qemug
40-
qemug: $(IMAGES) $(BR0) $(TAPS)
40+
qemug: $(IMAGES) $(TAP0)
4141
$(QEMU) $(QEMU_DISK) $(QEMU_DISK_BOOT) $(QEMU_DEBUG)
4242

4343
# VMWare 磁盘转换

src/utils/generate.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,37 @@
22

33
import os
44
import binascii
5-
from elftools.elf.elffile import ELFFile
5+
import shutil
6+
try:
7+
from elftools.elf.elffile import ELFFile
8+
except ImportError:
9+
print('''
10+
import elftools failure.
11+
You may need to install elftools packet using either
12+
pip install pyelftools
13+
or in Archlinux
14+
sudo pacman -S python-pyelftools
15+
''')
16+
exit(-1)
617

718
DIRNAME = os.path.dirname(os.path.abspath(__file__))
819
BUILD = os.path.abspath(os.path.join(DIRNAME, "../../build"))
20+
KERNELRAWBIN = os.path.join(BUILD, 'kernel.raw.bin')
21+
SYSTEMRAWBIN = os.path.join(BUILD, 'system.raw.bin')
922
KERNELBIN = os.path.join(BUILD, 'kernel.bin')
10-
SYSTEMBIN = os.path.join(BUILD, 'system.bin')
1123

1224

1325
def main():
1426
start = 0x8000
15-
size = os.path.getsize(SYSTEMBIN) - start
27+
size = os.path.getsize(SYSTEMRAWBIN) - start
1628

17-
with open(SYSTEMBIN, 'rb') as file:
29+
with open(SYSTEMRAWBIN, 'rb') as file:
1830
data = file.read()
1931
chksum = binascii.crc32(data[start:])
2032
print(f"system.bin chksum: {chksum} size {size}")
2133

34+
shutil.copy(KERNELRAWBIN, KERNELBIN)
35+
2236
with open(KERNELBIN, 'rb+') as file:
2337
elf = ELFFile(file)
2438
section = elf.get_section_by_name(".onix.magic")

src/utils/image.mk

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
MUSIC:= ./utils/deer.mp3
2+
LOOP:= $(shell sudo losetup --find)
23

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

3839
# 挂载设备
39-
sudo losetup /dev/loop0 --partscan $@
40+
sudo losetup $(LOOP) --partscan $@
4041

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

4445
# 挂载文件系统
45-
sudo mount /dev/loop0p1 /mnt
46+
sudo mount $(LOOP)p1 /mnt
4647

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

7677
# 卸载设备
77-
sudo losetup -d /dev/loop0
78+
sudo losetup -d $(LOOP)
7879

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

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

8788
# 挂载设备
88-
sudo losetup /dev/loop0 --partscan $@
89+
sudo losetup $(LOOP) --partscan $@
8990

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

9394
# 挂载文件系统
94-
sudo mount /dev/loop0p1 /mnt
95+
sudo mount $(LOOP)p1 /mnt
9596

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

105106
# 卸载设备
106-
sudo losetup -d /dev/loop0
107+
sudo losetup -d $(LOOP)
107108

108109
$(BUILD)/floppya.img:
109110

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

113114
.PHONY: mount0
114115
mount0: $(BUILD)/master.img
115-
sudo losetup /dev/loop0 --partscan $<
116-
sudo mount /dev/loop0p1 /mnt
116+
sudo losetup $(LOOP) --partscan $<
117+
sudo mount $(LOOP) /mnt
117118
sudo chown ${USER} /mnt
118119

119120
.PHONY: umount0

src/utils/net.mk

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,20 @@
1-
IFACE:=onix
1+
IFACE:=$(shell sudo ip -o -4 route show to default | awk '{print $$5}')
22

3-
BR0:=/sys/class/net/br0
4-
TAPS:= /sys/class/net/tap0 \
5-
/sys/class/net/tap1 \
6-
/sys/class/net/tap2 \
3+
TAP0:=/sys/class/net/tap0
74

8-
.SECONDARY: $(TAPS) $(BR0)
5+
.SECONDARY: $(TAP0)
96

107
# 网桥 IP 地址
11-
BNAME:=br0
12-
IP0:=192.168.111.22
13-
MAC0:=5a:5a:5a:5a:5a:22
14-
GATEWAY:=192.168.111.2
8+
GATEWAY:=172.16.16.1
159

16-
$(BR0):
17-
sudo ip link add $(BNAME) type bridge
18-
sudo ip link set $(BNAME) type bridge ageing_time 0
19-
20-
sudo ip link set $(IFACE) down
21-
sudo ip link set $(IFACE) master $(BNAME)
22-
sudo ip link set $(IFACE) up
23-
24-
sudo ip link set dev $(BNAME) address $(MAC0)
25-
sudo ip link set $(BNAME) up
26-
27-
# sudo iptables -A FORWARD -i $(BNAME) -o $(BNAME) -j ACCEPT
28-
sudo iptables -F
29-
sudo iptables -X
30-
sudo iptables -P FORWARD ACCEPT
31-
sudo iptables -P INPUT ACCEPT
32-
sudo iptables -P OUTPUT ACCEPT
33-
34-
sudo ip addr add $(IP0)/24 brd + dev $(BNAME) metric 10000
35-
sudo ip route add default via $(GATEWAY) dev $(BNAME) proto static metric 10000
36-
# sudo dhclient -v -4 br0
37-
38-
br0: $(BR0)
39-
-
40-
41-
/sys/class/net/tap%: $(BR0)
42-
sudo ip tuntap add mode tap $(notdir $@)
43-
sudo ip link set $(notdir $@) master $(BNAME)
10+
$(TAP0):
11+
sudo ip tuntap add mode tap $(notdir $@) user $(USER)
12+
sudo ip addr add $(GATEWAY)/24 dev $(notdir $@)
4413
sudo ip link set dev $(notdir $@) up
4514

46-
tap%: /sys/class/net/tap%
15+
sudo sysctl net.ipv4.ip_forward=1
16+
sudo iptables -t nat -A POSTROUTING -s $(GATEWAY)/24 -o $(IFACE) -j MASQUERADE
17+
sudo iptables -A FORWARD -i $(notdir $@) -o $(IFACE) -j ACCEPT
18+
19+
tap0: $(TAP0)
4720
-

src/utils/network.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# ipaddr=192.168.111.33
2-
# netmask=255.255.255.0
3-
# gateway=192.168.111.2
1+
ipaddr=172.16.16.11
2+
netmask=255.255.0.0
3+
gateway=172.16.16.1

0 commit comments

Comments
 (0)