文档说明:只记录关键地方;
试验环境: linux debian 11
基础软件: qemu 6.2
目标:编译linux riscv64 内核 并用qemu-riscv64启动
环境准备
| #!/bin/bash |
| set -exu |
| |
| __DIR__=$( |
| cd "$(dirname "$0")" |
| pwd |
| ) |
| cd ${__DIR__} |
| |
| test ! -f /etc/apt/source.list.save && cp /etc/apt/sources.list /etc/apt/sources.list.save |
| sed -i "s@security.ubuntu.com@mirrors.ustc.edu.cn@g" /etc/apt/sources.list |
| sed -i "s@archive.ubuntu.com@mirrors.ustc.edu.cn@g" /etc/apt/sources.list |
| |
| apt update -y && apt install -y git curl python3 python3-pip python3-dev |
| apt install -y libssl-dev ca-certificates make cmake gcc g++ zip |
| |
| apt install -y tcpdump nmap traceroute net-tools dnsutils iproute2 procps iputils-ping rsync |
| |
| |
| apt install -y qemu qemu-system qemu-system-riscv64 |
| apt install -y gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu |
| |
| apt install -y sudo |
| apt install -y flex bison bc libncurses5-dev libncursesw5-dev cpio |
| |
下载linux
| #!/bin/bash |
| set -exu |
| |
| __DIR__=$(cd "$(dirname "$0")";pwd) |
| cd ${__DIR__} |
| |
| test -d linux/.git && git -C linux pull --depth=1 --progress --rebase=true |
| test -d linux/.git || git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux.git --depth=1 --progress |
| |
| |
| if [ ! -d busybox ] ;then |
| { |
| git clone -b master https://github.com/mirror/busybox.git --depth=1 --progress |
| } else { |
| git -C busybox pull origin master --progress --depth=1 --rebase=true |
| } |
| fi |
| |
prepare-riscv64-image.sh
| |
| |
| |
| set -eux |
| |
| __DIR__=$(cd "$(dirname "$0")";pwd) |
| cd ${__DIR__} |
| |
| export DEBIAN_FRONTEND=noninteractive |
| |
| |
| |
| cpu_numbers=`grep "processor" /proc/cpuinfo | sort -u | wc -l` |
| build_target_dir=`readlink -f ${__DIR__}/build/` |
| |
| echo build_target_dir |
| build_dir=`readlink -f ${__DIR__}/../` |
| |
| |
| qemu-system-riscv64 --version |
| riscv64-linux-gnu-gcc --version |
| riscv64-linux-gnu-gcc -v |
| |
| |
| cd ${build_dir}/linux |
| |
| test -f arch/riscv64/boot/Image && rm -rf arch/riscv64/boot/Image |
| |
| make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- defconfig |
| make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- Image -j $cpu_numbers |
| |
| |
| cd ${build_dir}/busybox |
| test -d _install && rm -rf _install |
| |
| |
| |
| |
| |
| |
| test -f ${build_dir}/busybox/.config && make oldconfig |
| test -f ${build_dir}/busybox/.config || make menuconfig |
| make -j $cpu_numbers && make install |
| |
| cd ${build_dir}/busybox/_install |
| |
| mkdir dev etc lib sys proc tmp var home root mnt |
| mkdir -p /sys/dev |
| cd dev |
| sudo mknod console c 5 1 |
| sudo mknod null c 1 3 |
| |
| sudo mknod tty1 c 4 1 |
| sudo mknod tty2 c 4 2 |
| sudo mknod tty3 c 4 3 |
| sudo mknod tty4 c 4 4 |
| |
| |
| cd ${build_dir}/busybox/_install |
| cd etc |
| cat <<EOF | tee profile |
| #!/bin/sh |
| export HOSTNAME=dev |
| export USER=root |
| export HOME=/home |
| export PS1="[$USER@$HOSTNAME \W]\# " |
| PATH=/bin:/sbin:/usr/bin:/usr/sbin |
| LD_LIBRARY_PATH=/lib:/usr/lib:$LD_LIBRARY_PATH |
| export PATH LD_LIBRARY_PATH |
| EOF |
| |
| |
| cd ${build_dir}/busybox/_install |
| |
| cd etc |
| cat <<EOF | tee inittab |
| |
| #this is run first except when booting in single-user mode. |
| ::sysinit:/etc/init.d/rcS |
| |
| #/bin/sh invocations on selected ttys |
| #Start an "askfirst" shell on the console(whatever that may be) |
| #::askfirst:-/bin/sh |
| ::respawn:-/bin/sh |
| |
| #Stuff to do when restarting the init process |
| ::restart:/sbin/init |
| |
| #Stuff to do before rebooting |
| ::ctrlaltdel:/sbin/reboot |
| |
| |
| EOF |
| cat <<EOF | tee fstab |
| #device mount-point type options dump fsck order |
| proc /proc proc defaults 0 0 |
| tmpfs /tmp tmpfs defaults 0 0 |
| sysfs /sys sysfs defaults 0 0 |
| tmpfs /dev tmpfs defaults 0 0 |
| |
| |
| EOF |
| |
| cd ${build_dir}/busybox/_install |
| cd etc |
| mkdir -p init.d |
| cd init.d |
| touch rcS |
| cat <<EOF | tee rcS |
| #!/bin/sh |
| |
| #This is the first script called by init process |
| /bin/mount -a |
| mdev -s |
| ip addr add 127.0.0.1/8 dev lo |
| ip link set lo up |
| #ip link set eth0 down |
| #ip link set eth0 up |
| busybox --help |
| #udhcpc eth0 |
| EOF |
| |
| chmod +x rcS |
| |
| cd ${build_dir}/busybox/_install |
| |
| |
| cd ${build_dir}/busybox/_install |
| mkdir -p usr/share/udhcpc/ |
| cp ../examples/udhcp/simple.script usr/share/udhcpc/default.script |
| chmod 755 usr/share/udhcpc/default.script |
| |
| |
| test -d ${__DIR__}/build || mkdir -p ${__DIR__}/build |
| test -f ${build_dir}/rootfs.cpio.gz && rm -rf ${build_dir}/rootfs.cpio.gz |
| find . | cpio -o -H newc |gzip > ${__DIR__}/build/rootfs.cpio.gz |
| cp -f ${build_dir}/linux/arch/riscv/boot/Image ${__DIR__}/build/Image |
| cd ${__DIR__} |
| |
| |
| |
start-qemu-riscv64.sh
| #!/bin/env bash |
| |
| set -eux |
| |
| __DIR__=$(cd "$(dirname "$0")";pwd) |
| cd ${__DIR__} |
| |
| qemu-system-riscv64 -M help |
| qemu-system-riscv64 -cpu help |
| |
| qemu-system-riscv64 -device help |
| qemu-system-riscv64 -netdev help |
| |
| |
| pwd |
| ls -lh ${__DIR__}/build/ |
| qemu-system-riscv64 \ |
| -machine virt \ |
| -m size=1024M \ |
| -cpu rv64 \ |
| -kernel ${__DIR__}/build/Image \ |
| -initrd ${__DIR__}/build/rootfs.cpio.gz \ |
| -append "console=ttyS0 rdinit=/linuxrc" \ |
| -nographic \ |
| -nic user,model=virtio-net-pci \ |
| -device virtio-net-device,netdev=net0 \ |
| -netdev user,id=net0,hostfwd=tcp::10000-:22 |
关闭qemu
| #!/bin/env bash |
| |
| set -eux |
| |
| __DIR__=$(cd "$(dirname "$0")";pwd) |
| cd ${__DIR__} |
| |
| pid=$(ps -ef | grep 'qemu/qemu-riscv64/' | grep -v 'grep' | awk '{print $2}') |
| kill -15 $pid |
| |
linux kernel 内核源
| |
| git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux.git --depth=1 --progress |
| |
| |
| |
| git clone git://mirrors.ustc.edu.cn/linux.git --depth=1 --progress |
| |
| |
| |
| |
| |
| git -C linux pull --depth=1 --progress --rebase=true |
| |
参考文档
- linux kernel
- linux 内核源码查看
- qemu
- 在 QEMU 上运行 RISC-V 64 位版本的 Linux
- linux 镜像
- 使用最新版 gcc cmake msys2 Cygwin
- alpine、debian、ubuntu 常用的换源命令
- 校园网联合镜像站
- mirrorz
- github mirrorz
- 清华大学开源软件镜像站
- 中国科学技术大学开源软件镜像
- 中国高校教育网及科技网开源镜像列表
- debian packages
- alpine packages
- debian version
- web搜索包或者应用程序入口
- Ventoy:一个新的u盘启动方案,解决了u盘内多镜像启动时需要反复格式化的问题
- MifareClassicTool:经典的安卓 NFC 读写工具
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?