Mit6.S081-实验环境搭建

注:大家每次做一些操作的时候觉得不太保险就先把虚拟机克隆一份

前言

qemu(quick emulator):这是一个模拟硬件环境的软件,利用它可以运行我们编译好的操作系统。
准备一个Linux系统,安装qemu以及其他依赖,通过git克隆下github的xv6源码,利用gcc编译源码得到可运行的操作系统,再利用qemu加载这个操作系统。

一、Linux系统

笔者用的是VM的ubuntu24.04,大家可以到网上找找安装教程,很多很详细,就不多说了

二、SSH连接工具

笔者使用的是xshell,大家可以到网上找找安装教程,很多很详细,就不多说了

三、环境搭建

6.S081 / 2020 年秋季

1、安装依赖

1
sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu 

2、克隆源码

只有克隆了源码才会有xv6-labs-2020目录,不然是没有的

1
git clone git://g.csail.mit.edu/xv6-labs-2020

3、分支说明

前面克隆下来的git代码库包含了多个分支,每个分支对应一个实验作业。
切换到克隆下来的代码库:cd xv6-labs-2020
查看此代码库的所有分支:git branch –remote
image-20241108222705579

4、源码目录简析

kernel:内核源码,system call的实现
user:系统自带工具源码,shell、echo、cat等工具实现
grade-lab-util:python实现的代码测试工具
Makefile:make配置文件

四、检测qemu是否安装成功

1.测试

依次输入

1
2
riscv64-unknown-elf-gcc --version
qemu-system-riscv64 --version

笔者的结果

1
2
3
4
5
6
7
8
9
darling@darling:~/MIT6.S081$ riscv64-unknown-elf-gcc --version
riscv64-unknown-elf-gcc (13.2.0-11ubuntu1+12) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

darling@darling:~/MIT6.S081$ qemu-system-riscv64 --version
QEMU emulator version 4.2.0 (Debian 1:4.2-3ubuntu6)
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

2.执行make qemu

如果出现make: *** 没有规则可制作目标“qemu”。 停止。

执行

1
git checkout util

然后再次执行make qemu

可能的第一个错误

1
2
3
4
5
user/sh.c: In function 'runcmd':
user/sh.c:58:1: error: infinite recursion detected [-Werror=infinite-recursion]
58 | runcmd(struct cmd *cmd)
| ^~~~~~
cc1: all warnings being treated as errors

就是检测到了这个递归函数没有合理的终止条件可能会无线递归

解决方法

在 runcmd 前添加 attribute((noreturn))

1
2
3
4
// Execute cmd.  Never returns.
__attribute__((noreturn))
void
runcmd(struct cmd *cmd)

就是给系统说你别管,我自己能管好我自己

可能的第二个错误

执行了make qemu,卡住动不了了

1
2
3
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp
3 -nographic -drive file=fs. img, if=none, format=raw, id=x0 -device virtio-blk-devi
ce, drive=x0,bus=virtio-mmio-bus.0

停在了这么个倒霉地方

此时此刻,似乎软件包 qemu-system-misc 收到了一个更新,该更新破坏了它与我们内核的兼容性。如果运行 make qemu 并且脚本在 qemu-system-riscv64 -machine virt -bios none -kernel/kernel -m 128M -smp 3 -nographic -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 之后出现挂起,则需要卸载该软件包并安装旧版本:

1
2
$ sudo apt-get remove qemu-system-misc
$ sudo apt-get install qemu-system-misc=1:4.2-3ubuntu6

成功运行界面

image-20241108222046344

执行ls命令

image-20241108222133732

qemu的退出方法

两种方法:

  1. 在另一个终端中输入 killall qemu-system-arm
  2. 在 qemu 中 按下ctrl+a 抬起后,再输入’x’。

真实一把辛酸泪啊

期间还因为软件源不能用去换了软件源

找了很多杂七杂八的方法,克隆了两次虚拟机,搞了2小时总算是给我搞定了