Skip to content

Commit ed0d358

Browse files
committed
Merge pull request #139 from jklincn/main
1 parent 526c7e7 commit ed0d358

File tree

4 files changed

+59
-74
lines changed

4 files changed

+59
-74
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*/*
1+
*/*
2+
!rust-toolchain.toml

Dockerfile

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,72 @@
11
# syntax=docker/dockerfile:1
2-
# This Dockerfile is adapted from https://github.com/LearningOS/rCore-Tutorial-v3/blob/main/Dockerfile
3-
# with the following major updates:
4-
# - ubuntu 18.04 -> 20.04
5-
# - qemu 5.0.0 -> 7.0.0
6-
# - Extensive comments linking to relevant documentation
7-
FROM ubuntu:20.04
82

9-
ARG QEMU_VERSION=7.0.0
10-
ARG GDB_VERSION=14.1
11-
ARG HOME=/root
12-
13-
# 0. Install general tools
14-
ARG DEBIAN_FRONTEND=noninteractive
15-
RUN apt-get update && \
16-
apt-get install -y \
17-
curl \
18-
git \
19-
python3 \
20-
wget
21-
22-
# 1. Set up QEMU RISC-V
23-
# - https://learningos.github.io/rust-based-os-comp2022/0setup-devel-env.html#qemu
3+
# Stage 1 Build QEMU
244
# - https://www.qemu.org/download/
5+
# - https://wiki.qemu.org/Hosts/Linux#Building_QEMU_for_Linux
256
# - https://wiki.qemu.org/Documentation/Platforms/RISCV
26-
# - https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html
27-
28-
# 1.1. Download source
29-
WORKDIR ${HOME}
30-
RUN wget https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz && \
31-
tar xvJf qemu-${QEMU_VERSION}.tar.xz
327

33-
RUN wget https://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.xz && \
34-
tar xvJf gdb-${GDB_VERSION}.tar.xz
8+
FROM ubuntu:20.04 as build_qemu
359

36-
# 1.2. Install dependencies
37-
# - https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html#prerequisites
38-
RUN apt-get install -y \
39-
autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
40-
gawk build-essential bison flex texinfo gperf libtool patchutils bc \
41-
zlib1g-dev libexpat-dev git \
42-
ninja-build pkg-config libglib2.0-dev libpixman-1-dev libsdl2-dev \
43-
libncurses5-dev python2 python2-dev libreadline-dev tmux
10+
ARG QEMU_VERSION=7.0.0
4411

12+
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
13+
sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
14+
apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -y wget build-essential libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build
4516

46-
# 1.3. Build and install from source
47-
WORKDIR ${HOME}/qemu-${QEMU_VERSION}
48-
RUN ./configure --target-list=riscv64-softmmu,riscv64-linux-user && \
17+
RUN wget https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz && \
18+
tar xf qemu-${QEMU_VERSION}.tar.xz && \
19+
cd qemu-${QEMU_VERSION} && \
20+
./configure --target-list=riscv64-softmmu,riscv64-linux-user && \
4921
make -j$(nproc) && \
5022
make install
5123

52-
WORKDIR ${HOME}/gdb-${GDB_VERSION}
53-
RUN ./configure --prefix=/usr/local --target=riscv64-unknown-elf --enable-tui=yes && \
54-
make -j$(nproc) && \
55-
make install
24+
# Stage 2 Set Lab Environment
25+
FROM ubuntu:20.04 as build
5626

57-
# 1.4. Clean up
58-
WORKDIR ${HOME}
59-
RUN rm -rf qemu-${QEMU_VERSION} qemu-${QEMU_VERSION}.tar.xz ${HOME}/gdb-${GDB_VERSION} gdb-${GDB_VERSION}.tar.xz
27+
WORKDIR /tmp
6028

61-
# 1.5. Sanity checking
62-
RUN qemu-system-riscv64 --version && \
63-
qemu-riscv64 --version
29+
# 2.0. Install general tools
30+
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
31+
apt-get update && \
32+
DEBIAN_FRONTEND=noninteractive apt-get install -y jq curl git python3 wget build-essential \
33+
# qemu dependency
34+
libglib2.0-0 libfdt1 libpixman-1-0 zlib1g \
35+
# gdb
36+
gdb-multiarch
6437

65-
# 2. Set up Rust
66-
# - https://learningos.github.io/rust-based-os-comp2022/0setup-devel-env.html#qemu
67-
# - https://www.rust-lang.org/tools/install
68-
# - https://github.com/rust-lang/docker-rust/blob/master/Dockerfile-debian.template
38+
# 2.1. Copy qemu
39+
COPY --from=build_qemu /usr/local/bin/* /usr/local/bin
6940

70-
# 2.1. Install
41+
# 2.2. Install Rust
42+
# - https://www.rust-lang.org/tools/install
7143
ENV RUSTUP_HOME=/usr/local/rustup \
7244
CARGO_HOME=/usr/local/cargo \
7345
PATH=/usr/local/cargo/bin:$PATH \
74-
RUST_VERSION=nightly
75-
RUN set -eux; \
76-
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup-init; \
77-
chmod +x rustup-init; \
78-
./rustup-init -y --no-modify-path --profile minimal --default-toolchain $RUST_VERSION; \
79-
rm rustup-init; \
80-
chmod -R a+w $RUSTUP_HOME $CARGO_HOME;
46+
RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static \
47+
RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
48+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
49+
sh -s -- -y --no-modify-path --profile minimal --default-toolchain nightly
8150

82-
# 2.2. Sanity checking
83-
RUN rustup --version && \
84-
cargo --version && \
85-
rustc --version
51+
# 2.3. Build env for labs
52+
# See os/Makefile `env:` for example.
53+
# This avoids having to wait for these steps each time using a new container.
54+
COPY rust-toolchain.toml rust-toolchain.toml
55+
RUN rustup target add riscv64gc-unknown-none-elf && \
56+
cargo install toml-cli cargo-binutils && \
57+
RUST_VERSION=$(toml get -r rust-toolchain.toml toolchain.channel) && \
58+
Components=$(toml get -r rust-toolchain.toml toolchain.components | jq -r 'join(" ")') && \
59+
rustup install $RUST_VERSION && \
60+
rustup component add --toolchain $RUST_VERSION $Components
61+
62+
# 2.4. Set GDB
63+
RUN ln -s /usr/bin/gdb-multiarch /usr/bin/riscv64-unknown-elf-gdb
8664

87-
# Ready to go
88-
WORKDIR ${HOME}
65+
# Stage 3 Sanity checking
66+
FROM build as test
67+
RUN qemu-system-riscv64 --version && \
68+
qemu-riscv64 --version && \
69+
rustup --version && \
70+
cargo --version && \
71+
rustc --version && \
72+
riscv64-unknown-elf-gdb --version

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
DOCKER_NAME ?= rcore-tutorial-v3
1+
DOCKER_TAG ?= rcore-tutorial-v3:latest
22
.PHONY: docker build_docker
33

44
docker:
5-
docker run --rm -it -v ${PWD}:/mnt -w /mnt ${DOCKER_NAME} bash
5+
docker run --rm -it -v ${PWD}:/mnt -w /mnt --name rcore-tutorial-v3 ${DOCKER_TAG} bash
66

77
build_docker:
8-
docker build -t ${DOCKER_NAME} .
8+
docker build -t ${DOCKER_TAG} --target build .
99

1010
fmt:
1111
cd os ; cargo fmt; cd ..

rust-toolchain.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
profile = "minimal"
33
# use the nightly version of the last stable toolchain, see <https://forge.rust-lang.org/>
44
channel = "nightly-2024-01-18"
5-
components = ["rust-src", "llvm-tools-preview", "rustfmt", "clippy"]
6-
targets = ["riscv64gc-unknown-none-elf"]
5+
components = ["rust-src", "llvm-tools", "rustfmt", "clippy"]
6+
targets = ["riscv64gc-unknown-none-elf"]

0 commit comments

Comments
 (0)