This commit is contained in:
xyou
2026-07-28 21:11:10 +08:00
parent 537890df9e
commit 2774c10d0c
49 changed files with 4019 additions and 94 deletions
+73 -10
View File
@@ -31,9 +31,15 @@ echo "[build] Linux source: $LINUX_SRC"
# 1. Clone Linux 6.6 LTS if not present
if [ ! -d "$LINUX_SRC" ]; then
echo "[build] Cloning Linux 6.6 LTS..."
# 优先 kernel.org,失败则 fallback 清华镜像
git clone --depth 1 --branch v6.6 \
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git \
"$LINUX_SRC"
"$LINUX_SRC" || {
echo "[build] kernel.org failed, trying TUNA mirror..."
git clone --depth 1 --branch v6.6 \
https://mirrors.tuna.tsinghua.edu.cn/git/linux.git \
"$LINUX_SRC"
}
fi
cd "$LINUX_SRC"
@@ -101,19 +107,32 @@ echo "[build] bzImage: $OUTPUT_DIR/linux-bzImage ($(du -h $OUTPUT_DIR/linux-bzIm
# 6. Build initramfs if initramfs/ directory exists
if [ -d "$INITRAMFS_DIR" ]; then
echo "[build] Building initramfs..."
# Create a minimal initramfs with busybox or a simple init
INITRAMFS_TMP="$SCRIPT_DIR/initramfs.tmp"
rm -rf "$INITRAMFS_TMP"
mkdir -p "$INITRAMFS_TMP"
mkdir -p "$INITRAMFS_TMP/bin"
# Copy initramfs contents
# Copy initramfs contents (init + bin/)
cp -a "$INITRAMFS_DIR"/* "$INITRAMFS_TMP"/
# Build utsm_exec_daemon (static binary for initramfs)
# 6a. Static busybox(缓存于 CODE/linux/.cache/,只下载一次)
# busybox.net 官方预编译 x86_64 musl 静态二进制,含 sh/mount/uname 等 applet。
BUSYBOX_CACHE="$SCRIPT_DIR/.cache/busybox"
BUSYBOX_URL="https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox"
if [ ! -x "$BUSYBOX_CACHE" ]; then
echo "[build] Downloading static busybox..."
mkdir -p "$(dirname "$BUSYBOX_CACHE")"
curl -fL --retry 3 -o "$BUSYBOX_CACHE" "$BUSYBOX_URL" || \
wget -O "$BUSYBOX_CACHE" "$BUSYBOX_URL"
chmod +x "$BUSYBOX_CACHE"
fi
cp "$BUSYBOX_CACHE" "$INITRAMFS_TMP/bin/busybox"
chmod +x "$INITRAMFS_TMP/bin/busybox"
echo "[build] busybox: $(du -h "$INITRAMFS_TMP/bin/busybox" | cut -f1)"
# 6b. Build utsm_exec_daemon (static binary for initramfs)
DAEMON_SRC="$INITRAMFS_DIR/bin/utsm_exec_daemon.c"
if [ -f "$DAEMON_SRC" ]; then
echo "[build] Compiling utsm_exec_daemon (static)..."
mkdir -p "$INITRAMFS_TMP/bin"
cc -static -O2 -Wall -I"$PROJECT_ROOT/CODE/utsm-ipc" \
-o "$INITRAMFS_TMP/bin/utsm_exec_daemon" "$DAEMON_SRC"
strip "$INITRAMFS_TMP/bin/utsm_exec_daemon" 2>/dev/null || true
@@ -128,18 +147,62 @@ mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
echo "[utsm-linux] Boot complete"
echo "[utsm-linux] Console ready on /dev/hvc0 (virtio-console)"
exec /bin/sh
INITEOF
chmod +x "$INITRAMFS_TMP/init"
fi
# Pack initramfs
cd "$INITRAMFS_TMP"
find . | cpio -H newc -o | gzip -9 > "$OUTPUT_DIR/linux-initrd.img"
# 6c. 用内核自带 gen_init_cpio 打包(无需 root、无需 cpio 二进制)。
# spec 文件声明目录/设备节点/文件/符号链接;设备节点元数据直接写入
# cpio 归档,guest 内核 unpack 时即有 /dev/console 与 /dev/ttyS0。
GEN_INIT_CPIO="$LINUX_SRC/usr/gen_init_cpio"
if [ ! -x "$GEN_INIT_CPIO" ]; then
echo "[build] Building gen_init_cpio host tool..."
make usr/ >/dev/null
fi
SPEC="$SCRIPT_DIR/initramfs.tmp.spec"
{
echo "dir /bin 755 0 0"
echo "dir /dev 755 0 0"
echo "dir /proc 755 0 0"
echo "dir /sys 755 0 0"
# 控制台设备节点:kernel console=ttyS0 输出与 busybox sh 依赖
echo "nod /dev/console 600 0 0 c 5 1"
echo "nod /dev/null 666 0 0 c 1 3"
echo "nod /dev/ttyS0 600 0 0 c 4 64"
echo "file /init $INITRAMFS_TMP/init 755 0 0"
echo "file /bin/busybox $INITRAMFS_TMP/bin/busybox 755 0 0"
if [ -f "$INITRAMFS_TMP/bin/utsm_exec_daemon" ]; then
echo "file /bin/utsm_exec_daemon $INITRAMFS_TMP/bin/utsm_exec_daemon 755 0 0"
fi
# busybox applet 符号链接(init 脚本与调试命令依赖)
for app in sh mount umount echo sleep uname ls cat ps setsid cttyhack; do
echo "slink /bin/$app /bin/busybox 777 0 0"
done
} > "$SPEC"
"$GEN_INIT_CPIO" "$SPEC" | gzip -9 > "$OUTPUT_DIR/linux-initrd.img"
echo "[build] initrd: $OUTPUT_DIR/linux-initrd.img ($(du -h $OUTPUT_DIR/linux-initrd.img | cut -f1))"
rm -rf "$INITRAMFS_TMP" "$SPEC"
cd "$LINUX_SRC"
fi
echo "[build] Done. Copy linux-bzImage (and linux-initrd.img) to your FAT32 image."
echo "[build] Limine will load them as boot modules for UTSM."
# 7. Enable Linux modules in limine.conf (uncomment the 4 commented lines)
LIMINE_CONF="$PROJECT_ROOT/SYSTEM/limine/limine.conf"
if [ -f "$LIMINE_CONF" ]; then
if grep -q "^ # module_path: boot():/boot/linux-bzImage" "$LIMINE_CONF"; then
echo "[build] Enabling Linux modules in limine.conf..."
sed -i 's|^ # module_path: boot():/boot/linux-bzImage| module_path: boot():/boot/linux-bzImage|' "$LIMINE_CONF"
sed -i 's|^ # module_cmdline: linux:bzImage| module_cmdline: linux:bzImage|' "$LIMINE_CONF"
sed -i 's|^ # module_path: boot():/boot/linux-initrd.img| module_path: boot():/boot/linux-initrd.img|' "$LIMINE_CONF"
sed -i 's|^ # module_cmdline: linux:initrd| module_cmdline: linux:initrd|' "$LIMINE_CONF"
echo "[build] Linux modules enabled in $LIMINE_CONF"
echo "[build] Run build.bat to rebuild the disk image with Linux modules"
else
echo "[build] limine.conf already has Linux modules enabled (or pattern changed)"
fi
fi
+117
View File
@@ -0,0 +1,117 @@
#!/bin/bash
#
# check_env.sh — Linux 内核构建环境依赖检查脚本
#
# 用途: 在 WSL/Linux 中运行,验证构建 Linux 6.6 LTS bzImage 所需依赖
#
# 使用:
# ! wsl bash /mnt/d/Code/Deshab/CODE/linux/check_env.sh
#
# 退出码:
# 0 = 所有依赖已满足
# 1 = 缺少依赖(脚本会打印安装命令)
set -e
echo "[check] Linux 内核构建环境检查"
echo "[check] ==========================="
# 1. 检查操作系统
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "[check] OS: $NAME $VERSION"
if [ "$ID" != "ubuntu" ] && [ "$ID_LIKE" != "debian" ]; then
echo "[check] WARNING: 非 Ubuntu/Debian,依赖名可能不同"
fi
else
echo "[check] WARNING: 无法识别操作系统"
fi
# 2. 检查必要包
echo "[check]"
echo "[check] 检查构建依赖..."
MISSING=()
for pkg in build-essential bc flex bison libelf-dev libssl-dev cpio gzip git; do
if dpkg -s "$pkg" >/dev/null 2>&1; then
echo "[check] ✓ $pkg"
else
echo "[check] ✗ $pkg (缺失)"
MISSING+=("$pkg")
fi
done
# 3. 检查 gcc 版本
echo "[check]"
echo "[check] 工具链版本:"
if command -v gcc >/dev/null 2>&1; then
GCC_VER=$(gcc -dumpversion)
echo "[check] gcc: $GCC_VER"
if [ "$(printf '%s\n' "11" "$GCC_VER" | sort -V | head -n1)" != "11" ]; then
echo "[check] WARNING: gcc 版本 < 11,Linux 6.6 可能需要 gcc 11+"
fi
else
echo "[check] ✗ gcc 未安装"
MISSING+=("gcc")
fi
if command -v make >/dev/null 2>&1; then
echo "[check] make: $(make --version | head -1 | awk '{print $3}')"
fi
# 4. 检查磁盘空间(需要 ~3GB)
echo "[check]"
PROJECT_ROOT="/mnt/d/Code/Deshab"
if [ -d "$PROJECT_ROOT" ]; then
AVAILABLE_KB=$(df "$PROJECT_ROOT" | awk 'NR==2 {print $4}')
AVAILABLE_GB=$((AVAILABLE_KB / 1024 / 1024))
echo "[check] 磁盘空间: ${AVAILABLE_GB}GB 可用 ($PROJECT_ROOT)"
if [ "$AVAILABLE_GB" -lt 3 ]; then
echo "[check] ✗ 磁盘空间不足 3GB"
MISSING+=("disk_space")
else
echo "[check] ✓ 磁盘空间充足"
fi
fi
# 5. 检查访问 Windows D: 盘
echo "[check]"
if [ -d "/mnt/d/Code/Deshab" ]; then
echo "[check] ✓ WSL 可访问 /mnt/d/Code/Deshab"
else
echo "[check] ✗ WSL 无法访问 /mnt/d/Code/Deshab"
echo "[check] 请确认在 WSL 中执行,且 D: 盘已挂载"
MISSING+=("wsl_mount")
fi
# 6. 检查 git 网络(克隆 Linux 6.6 需要)
echo "[check]"
echo "[check] 测试 Git 网络连通..."
if timeout 10 git ls-remote https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git v6.6 >/dev/null 2>&1; then
echo "[check] ✓ kernel.org 可达"
else
echo "[check] ✗ kernel.org 不可达,尝试清华镜像..."
if timeout 10 git ls-remote https://mirrors.tuna.tsinghua.edu.cn/git/linux.git v6.6 >/dev/null 2>&1; then
echo "[check] ✓ 清华镜像可达(build.sh 会自动使用)"
else
echo "[check] ✗ 两个源都不可达,请检查网络/代理"
MISSING+=("network")
fi
fi
# 7. 总结
echo "[check]"
echo "[check] ==========================="
if [ ${#MISSING[@]} -gt 0 ]; then
echo "[check] 缺失: ${MISSING[*]}"
echo "[check]"
echo "[check] 请运行以下命令安装依赖:"
echo "[check] sudo apt update"
echo "[check] sudo apt install -y build-essential bc flex bison libelf-dev libssl-dev cpio gzip git"
exit 1
fi
echo "[check] ✓ 所有依赖已满足,可以构建 Linux 内核"
echo "[check]"
echo "[check] 下一步执行:"
echo "[check] cd /mnt/d/Code/Deshab/CODE/linux && chmod +x build.sh && ./build.sh"
exit 0
+16
View File
@@ -0,0 +1,16 @@
#!/bin/bash
# Fix UTSM driver entries in Linux source tree
SRC="${1:-/tmp/linux-6.6}"
# Fix Kconfig: remove broken line, add correct one
sed -i '/utsm/d' "$SRC/drivers/Kconfig"
echo 'source "drivers/utsm/Kconfig"' >> "$SRC/drivers/Kconfig"
# Fix Makefile: remove broken line, add correct one
sed -i '/utsm/d' "$SRC/drivers/Makefile"
echo 'obj-$(CONFIG_UTSM_HCALL) += utsm/' >> "$SRC/drivers/Makefile"
echo "Fixed UTSM driver entries"
tail -2 "$SRC/drivers/Kconfig"
echo "---"
tail -2 "$SRC/drivers/Makefile"
+13 -4
View File
@@ -33,10 +33,19 @@ fi
# Start the exec daemon in the background.
# The daemon sends EXEC_READY then HLTs (parks the guest).
# UTSM's linux_launch() returns after this first HLT.
echo "[utsm-linux] starting utsm_exec_daemon..."
/bin/utsm_exec_daemon &
DAEMON_PID=$!
echo "[utsm-linux] daemon PID=$DAEMON_PID"
if [ -x /bin/utsm_exec_daemon ]; then
echo "[utsm-linux] starting utsm_exec_daemon..."
/bin/utsm_exec_daemon &
DAEMON_PID=$!
echo "[utsm-linux] daemon PID=$DAEMON_PID"
else
# 无 daemon(第一阶段 bring-up 冒烟测试构建):
# 直接在 ttyS0 上起交互 shell,验证 console 输入输出。
# 注意:此模式下 guest 不会 park,UTSM linux_launch() 不返回,
# 仅用于验证"Linux guest 进入 shell 提示符、可执行 uname -a"。
echo "[utsm-linux] WARNING: no daemon, starting interactive debug shell on ttyS0"
exec /bin/setsid /bin/cttyhack /bin/sh
fi
# init must not exit — sleep forever to keep the kernel happy.
# The daemon handles all UTSM exec requests; init just keeps the