Open8
DockerでRISC-V Linux、QEMU、Busyboxのソースコードをビルドし、QEMUでRISC-V Linuxを起動する
以下のスクラップで使ったDockerfileをベースにRISC-Vのクロスコンパイラを使ってRISC-VのLinuxを動かす
Dockerfile
FROM ubuntu:24.04
ENV TZ=Asia/Tokyo
ENV RISCV=/opt/riscv
ENV PATH=$PATH:$RISCV/bin
ENV LINUX_VERSION=6.7
ENV BUSYBOX_VERSION=1.36.1
ENV QEMU_VERSION=8.2.1
ENV RISCV_GCC_VERSION=2024.02.02
ENV OPENSBI_VERSION=1.4
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone && \
sed -i 's@archive.ubuntu.com@ftp.jaist.ac.jp/pub/Linux@g' /etc/apt/sources.list && \
echo "bind 'set bell-style none'" >> .bashrc && \
apt-get update && \
apt-get install -y --no-install-recommends build-essential libncurses-dev bison \
flex libssl-dev libelf-dev bc cpio git ca-certificates python3 python3-venv \
ninja-build pkg-config libglib2.0-dev libpixman-1-dev wget cloc autoconf automake \
autotools-dev curl python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk texinfo gperf \
libtool patchutils zlib1g-dev libexpat-dev cmake && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
wget -q --no-check-certificate https://github.com/torvalds/linux/archive/refs/tags/v${LINUX_VERSION}.tar.gz && \
tar xf v${LINUX_VERSION}.tar.gz && \
wget -q --no-check-certificate https://busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2 && \
tar xf busybox-${BUSYBOX_VERSION}.tar.bz2 && \
wget -q --no-check-certificate https://github.com/qemu/qemu/archive/refs/tags/v${QEMU_VERSION}.tar.gz && \
tar xf v${QEMU_VERSION}.tar.gz && \
git clone https://github.com/riscv/riscv-gnu-toolchain && \
wget -q --no-check-certificate https://github.com/riscv-software-src/opensbi/archive/refs/tags/v${OPENSBI_VERSION}.tar.gz && \
tar xf v${OPENSBI_VERSION}.tar.gz
WORKDIR /riscv-gnu-toolchain
RUN git checkout 2024.02.02 && \
git submodule update --init --recursive && \
./configure --prefix=${RISCV} && \
make linux -j$(nproc)
WORKDIR /linux-${LINUX_VERSION}
RUN make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig && \
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j$(nproc)
WORKDIR /busybox-${BUSYBOX_VERSION}
RUN make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig && \
sed -i.bak 's/^#.*CONFIG_STATIC.*/CONFIG_STATIC=y/' .config && \
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j$(nproc) install && \
cd _install && \
mkdir proc sys dev pts run etc etc/init.d \
mknod -m 600 console c 5 1 && \
mknod -m 666 null c 1 3 && \
mknod -m 666 tty c 5 0 && \
mknod ram b 1 0 && \
echo '#!/bin/sh\n\
mount -v --bind /dev /dev\n\
mount -v --bind /dev/pts /dev/pts\n\
mount -vt proc proc /proc\n\
mount -vt sysfs sysfs /sys\n\
mount -vt tmpfs tmpfs /run\n\
/sbin/mdev -s\n' > etc/init.d/rcS && \
chmod +x etc/init.d/rcS && \
find . | cpio -o -H newc | gzip > ../rootfs.img
WORKDIR /qemu-${QEMU_VERSION}
RUN ./configure --target-list=riscv64-softmmu && \
make -j$(nproc)
WORKDIR /opensbi-${OPENSBI_VERSION}
RUN make CROSS_COMPILE=riscv64-unknown-linux-gnu- \
PLATFORM=generic \
FW_PAYLOAD_PATH=/linux-${LINUX_VERSION}/arch/riscv/boot/Image \
-j$(nproc)
WORKDIR /
$ docker --version
Docker version 24.0.5, build ced0996
$ uname -a
Linux MyComputer 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
コンテナイメージをビルドする
$ docker build -t riscv_qemu_linux .
[+] Building 7160.4s (16/16) FINISHED docker:default
=> [internal] load .dockerignore 4.2s
=> => transferring context: 2B 0.1s
=> [internal] load build definition from Dockerfile 4.9s
=> => transferring dockerfile: 3.06kB 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:24.04 0.0s
=> [ 1/13] FROM docker.io/library/ubuntu:24.04 0.0s
=> CACHED [ 2/13] RUN ln -snf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo A 0.0s
=> [ 3/13] WORKDIR /riscv-gnu-toolchain 6.2s
=> [ 4/13] RUN git checkout 2024.02.02 && git submodule update --init --recursiv 5387.0s
=> [ 5/13] WORKDIR /linux-6.7 1.6s
=> [ 6/13] RUN make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig & 458.7s
=> [ 7/13] WORKDIR /busybox-1.36.1 1.5s
=> [ 8/13] RUN make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig && 32.0s
=> [ 9/13] WORKDIR /qemu-8.2.1 1.5s
=> [10/13] RUN ./configure --target-list=riscv64-softmmu && make -j$(nproc) 149.0s
=> [11/13] WORKDIR /opensbi-1.4 1.5s
=> [12/13] RUN make CROSS_COMPILE=riscv64-unknown-linux-gnu- PLATFORM=generic FW_ 7.1s
=> exporting to image 1105.7s
=> => exporting layers 1105.4s
=> => writing image sha256:442faf40fac29d9dc7ac278d8222009d0fe46753a64f4fdccf5d4b3ee5 0.1s
=> => naming to docker.io/library/riscv_qemu_linux 0.1s
What's Next?
View summary of image vulnerabilities and recommendations → docker scout quickview
$ docker run -it --rm --name riscv_qemu_linux_container riscv_qemu_linux bash
root@721a64761272:/# riscv64-unknown-linux-gnu-gcc --version
riscv64-unknown-linux-gnu-gcc (gc891d8dc23e) 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.
root@721a64761272:/# /qemu-8.2.1/build/qemu-system-riscv64 --version
QEMU emulator version 8.2.1
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
root@721a64761272:/# ls $RISCV/bin
riscv64-unknown-linux-gnu-addr2line riscv64-unknown-linux-gnu-gdb
riscv64-unknown-linux-gnu-ar riscv64-unknown-linux-gnu-gdb-add-index
riscv64-unknown-linux-gnu-as riscv64-unknown-linux-gnu-gfortran
riscv64-unknown-linux-gnu-c++ riscv64-unknown-linux-gnu-gprof
riscv64-unknown-linux-gnu-c++filt riscv64-unknown-linux-gnu-ld
riscv64-unknown-linux-gnu-cpp riscv64-unknown-linux-gnu-ld.bfd
riscv64-unknown-linux-gnu-elfedit riscv64-unknown-linux-gnu-lto-dump
riscv64-unknown-linux-gnu-g++ riscv64-unknown-linux-gnu-nm
riscv64-unknown-linux-gnu-gcc riscv64-unknown-linux-gnu-objcopy
riscv64-unknown-linux-gnu-gcc-13.2.0 riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-gcc-ar riscv64-unknown-linux-gnu-ranlib
riscv64-unknown-linux-gnu-gcc-nm riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-gcc-ranlib riscv64-unknown-linux-gnu-run
riscv64-unknown-linux-gnu-gcov riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-gcov-dump riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-gcov-tool riscv64-unknown-linux-gnu-strip
root@721a64761272:/# ls opensbi-1.4/build/platform/generic/firmware/
fw_dynamic.bin fw_dynamic.o fw_jump.elf.ld fw_payload.elf.dep
fw_dynamic.dep fw_jump.bin fw_jump.o fw_payload.elf.ld
fw_dynamic.elf fw_jump.dep fw_payload.bin fw_payload.o
fw_dynamic.elf.dep fw_jump.elf fw_payload.dep payloads
fw_dynamic.elf.ld fw_jump.elf.dep fw_payload.elf
QEMUでRISC-VのLinuxが起動できたことが確認できる
root@721a64761272:/# /qemu-8.2.1/build/qemu-system-riscv64 -M virt -m 256M -nographic \
-bios /opensbi-1.4/build/platform/generic/firmware/fw_jump.bin \
-kernel /linux-6.7/arch/riscv/boot/Image \
-initrd /busybox-1.36.1/rootfs.img \
-append "rdinit=/sbin/init"
OpenSBI v1.4
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : medeleg
Platform HART Count : 1
Platform IPI Device : aclint-mswi
Platform Timer Device : aclint-mtimer @ 10000000Hz
Platform Console Device : uart8250
Platform HSM Device : ---
Platform PMU Device : ---
Platform Reboot Device : syscon-reboot
Platform Shutdown Device : syscon-poweroff
Platform Suspend Device : ---
Platform CPPC Device : ---
Firmware Base : 0x80000000
Firmware Size : 323 KB
Firmware RW Offset : 0x40000
Firmware RW Size : 67 KB
Firmware Heap Offset : 0x48000
Firmware Heap Size : 35 KB (total), 2 KB (reserved), 9 KB (used), 23 KB (free)
Firmware Scratch Size : 4096 B (total), 328 B (used), 3768 B (free)
Runtime SBI Version : 2.0
...
[ 0.885523] 9pnet: Installing 9P2000 support
[ 0.886841] Key type dns_resolver registered
[ 0.946383] debug_vm_pgtable: [debug_vm_pgtable ]: Validating architecture page table helpers
[ 0.957954] clk: Disabling unused clocks
[ 0.958796] ALSA device list:
[ 0.959595] No soundcards found.
[ 1.042472] Freeing unused kernel image (initmem) memory: 2236K
[ 1.044659] Run /sbin/init as init process
mount: mounting /dev/pts on /dev/pts failed: No such file or directory
Please press Enter to activate this console.
~ #
~ # uname -a
Linux (none) 6.7.0 #1 SMP Sun Feb 18 12:08:12 JST 2024 riscv64 GNU/Linux
riscv-gnu-toolchainは複数のsub moduleを含んでいるので巨大で全部合わせて約5370万行
OpenSBIは約32000行
root@721a64761272:/# cloc riscv-gnu-toolchain/
442110 text files.
358094 unique files.
119163 files ignored.
9 errors:
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/golang.org/x/net/idna/tables10.0.0.go
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/golang.org/x/net/idna/tables11.0.0.go
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/golang.org/x/net/idna/tables12.0.0.go
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/golang.org/x/net/idna/tables13.0.0.go
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/golang.org/x/net/idna/tables9.0.0.go
Line count, exceeded timeout: riscv-gnu-toolchain/gcc/libgo/go/net/http/requestwrite_test.go
Line count, exceeded timeout: riscv-gnu-toolchain/llvm/clang/include/clang/Basic/DiagnosticLexKinds.td
Line count, exceeded timeout: riscv-gnu-toolchain/qemu/roms/edk2/MdePkg/Library/MipiSysTLib/mipisyst/external/pugixml/tests/test_xpath_xalan_2.cpp
Line count, exceeded timeout: riscv-gnu-toolchain/qemu/roms/edk2/MdePkg/Library/MipiSysTLib/mipisyst/external/pugixml/tests/test_xpath_xalan_4.cpp
github.com/AlDanial/cloc v 1.98 T=1792.29 s (199.8 files/s, 46141.1 lines/s)
---------------------------------------------------------------------------------------
Language files blank comment code
---------------------------------------------------------------------------------------
C 102734 2681566 4601695 14766926
C++ 62103 1692304 2695086 10165677
C/C++ Header 41453 1150556 2047316 5741386
D 21847 158274 162147 4213886
LLVM IR 34529 616228 5587409 2899551
Assembly 25742 655695 1468691 2810354
PO File 247 572886 758289 1579781
Text 3244 278540 0 1497732
Bourne Shell 1684 207815 177404 1172477
JSON 596 1328 0 1022616
YAML 5040 66316 65655 852827
Ada 7634 293601 411247 842204
Go 5367 102227 191163 770718
Expect 6288 132480 197487 596172
Python 4287 141804 180144 580238
Markdown 964 87214 548 575014
TableGen 1260 90804 80981 560457
Bourne Again Shell 427 45099 66049 281377
Fortran 90 9258 57459 151582 279350
XML 943 11295 4819 273656
reStructuredText 3001 128794 125655 272321
make 2475 50256 39681 243288
Perl 758 37852 33344 242465
m4 735 21373 8965 195025
Windows Module Definition 900 29829 647 147323
Pascal 204 40534 287199 128646
CMake 2208 13788 10078 96335
Objective-C 2426 26123 37207 91207
HTML 286 6319 688 84965
lex 1277 1394 862 74900
TNSDL 1184 9217 910 58688
yacc 52 9115 6321 56003
TeX 42 6451 18375 46914
SVG 94 14 163 39977
Objective-C++ 821 9752 8782 35983
Scheme 166 3258 767 28078
Forth 225 5772 6505 25687
Logos 209 4140 1005 21301
OpenCL 595 5450 9031 20406
Linker Script 389 1825 5492 19101
Java 107 1791 5679 18560
Starlark 76 2354 14539 16974
Rust 801 3217 1697 16898
CSV 79 4 0 15140
Fortran 77 630 1736 6702 14808
DOS Batch 142 3024 2832 14056
Tcl/Tk 101 2717 2827 13519
SWIG 131 1480 53 12837
R 368 1013 1207 12067
DenizenScript 51 1950 3472 12066
awk 77 1189 1582 11426
Meson 218 1027 658 10218
ANTLR Grammar 21 891 610 8931
JavaScript 45 667 3330 7859
Haxe 5 1518 13 7374
MSBuild script 17 1 7 7210
CUDA 234 2411 7937 5810
C# 38 644 1430 5488
Lisp 42 707 1046 5482
OCaml 36 1451 2418 5222
CSS 32 818 299 4798
Windows Resource File 162 557 199 4007
Verilog-SystemVerilog 2 0 0 3452
Fortran 95 127 1234 3484 3375
XSD 2 116 72 3291
PHP 28 740 693 2973
AsciiDoc 3 911 2 2421
Visual Studio Solution 20 17 19 2407
WiX include 10 133 206 2316
Jinja Template 3 254 14 2315
Smalltalk 187 0 3 2298
Bazel 34 248 150 1563
HLSL 92 482 1613 1451
INI 40 138 20 1364
Dockerfile 44 184 204 1356
F# 43 224 0 1067
SQL 5 73 9 1002
Clojure 1 124 0 846
diff 10 93 827 804
XSLT 19 198 157 803
vim script 20 140 190 737
IDL 3 8 0 714
GDScript 49 163 660 670
TypeScript 11 105 203 618
Glade 1 58 0 603
Julia 1 164 574 597
Lua 8 79 25 475
NAnt script 4 105 0 468
sed 33 26 86 441
Korn Shell 1 55 68 435
MATLAB 20 160 6 423
Oracle Forms 6 108 162 403
WiX source 2 40 36 389
Maven 3 17 0 324
DTD 22 120 116 250
Protocol Buffers 6 55 125 239
Specman e 29 1 0 216
Standard ML 1 34 28 215
Jupyter Notebook 2 0 1253 179
SCSS 2 37 3 165
TOML 7 27 25 161
C Shell 1 3 3 148
Mathematica 9 11 0 146
Haskell 36 17 0 120
PowerShell 2 29 0 99
Kermit 3 4 20 83
Elixir 11 0 0 45
Ruby 3 11 6 40
DIET 3 14 0 38
WiX string localization 1 15 21 34
SAS 1 14 22 32
GLSL 3 6 0 21
ProGuard 2 2 14 20
Swift 2 6 0 17
Gencat NLS 5 0 3 16
Ant 1 0 0 14
Brainfuck 1 3 4 10
Windows Message File 1 1 0 8
Nemerle 1 1 0 4
---------------------------------------------------------------------------------------
SUM: 358094 9492722 19519052 53686483
---------------------------------------------------------------------------------------
root@721a64761272:/# cloc opensbi-1.4/
533 text files.
334 unique files.
358 files ignored.
github.com/AlDanial/cloc v 1.98 T=2.67 s (125.1 files/s, 20239.2 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 147 4255 3200 18460
C/C++ Header 99 1472 4117 4899
Python 10 2518 4059 4574
Markdown 28 497 0 2211
Assembly 10 155 518 1039
make 30 216 333 912
Bourne Shell 3 38 12 265
Linker Script 3 108 0 172
YAML 2 0 0 17
Text 1 2 0 3
DOS Batch 1 0 0 1
-------------------------------------------------------------------------------
SUM: 334 9261 12239 32553
-------------------------------------------------------------------------------
root@721a64761272:/#