Open8

DockerでRISC-V NuttX、QEMUのソースコードをビルドし、QEMUでRISC-V NuttXを起動する

tommy34tommy34

Dockerfile

FROM ubuntu:24.04

ENV TZ=Asia/Tokyo
ENV RISCV=/opt/riscv
ENV PATH=$PATH:$RISCV/bin
ENV QEMU_VERSION=8.2.1
ENV RISCV_GCC_VERSION=2024.02.02
ENV NUTTX_VERSION=12.4.0

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 gettext kconfig-frontends xxd genromfs && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* && \
  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 --recurse-submodules --depth 1 https://github.com/riscv/riscv-gnu-toolchain && \
  wget -O nuttx.tar.gz https://github.com/apache/nuttx/archive/refs/tags/nuttx-${NUTTX_VERSION}.tar.gz && \
  tar xf nuttx.tar.gz && \
  wget -O nuttx-app.tar.gz https://github.com/apache/nuttx-apps/archive/refs/tags/nuttx-${NUTTX_VERSION}.tar.gz && \
  tar xf nuttx-app.tar.gz && \
  mv nuttx-apps-nuttx-${NUTTX_VERSION} apps

WORKDIR /riscv-gnu-toolchain

RUN git checkout $RISCV_GCC_VERSION && \
  ./configure --prefix=${RISCV} && \
  make linux -j$(nproc) && \
  ln -s $RISCV/bin/riscv64-unknown-linux-gnu-gcc $RISCV/bin/riscv64-unknown-elf-gcc && \
  ln -s $RISCV/bin/riscv64-unknown-linux-gnu-ar $RISCV/bin/riscv64-unknown-elf-ar && \
  ln -s $RISCV/bin/riscv64-unknown-linux-gnu-ld $RISCV/bin/riscv64-unknown-elf-ld && \
  ln -s $RISCV/bin/riscv64-unknown-linux-gnu-nm $RISCV/bin/riscv64-unknown-elf-nm && \
  ln -s $RISCV/bin/riscv64-unknown-linux-gnu-objcopy $RISCV/bin/riscv64-unknown-elf-objcopy

WORKDIR /qemu-${QEMU_VERSION}

RUN ./configure --target-list=riscv64-softmmu && \
  make -j$(nproc)

WORKDIR /nuttx-nuttx-${NUTTX_VERSION}
tommy34tommy34
$ 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
tommy34tommy34

コンテナイメージのビルド

$ docker build -t riscv_nuttx .
[+] Building 7801.9s (11/11) FINISHED                                         docker:default
 => [internal] load build definition from Dockerfile                                    1.3s
 => => transferring dockerfile: 2.24kB                                                  0.1s
 => [internal] load .dockerignore                                                       1.8s
 => => transferring context: 2B                                                         0.0s
 => [internal] load metadata for docker.io/library/ubuntu:24.04                         0.0s
 => CACHED [1/7] FROM docker.io/library/ubuntu:24.04                                    0.0s
 => [2/7] RUN ln -snf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo Asia/To  5272.3s
 => [3/7] WORKDIR /riscv-gnu-toolchain                                                  1.6s
 => [4/7] RUN git checkout 2024.02.02 &&   ./configure --prefix=/opt/riscv &&   mak  1242.9s
 => [5/7] WORKDIR /qemu-8.2.1                                                           1.5s
 => [6/7] RUN ./configure --target-list=riscv64-softmmu &&   make -j$(nproc)          145.4s
 => [7/7] WORKDIR /nuttx-nuttx-12.4.0                                                   1.5s
 => exporting to image                                                               1131.4s
 => => exporting layers                                                              1131.0s
 => => writing image sha256:b5b2fed9187ec4a017cacb923b8aff6eabcce9bc6684bfe88755418fa4  0.1s
 => => naming to docker.io/library/riscv_nuttx                                          0.2s

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview

riscv-gnu-toolchainが大きいのでイメージサイズも20GBと大きくなっている

$ docker images --format "{{.Repository}}:{{.Tag}}\t{{.Size}}" | grep riscv_nuttx
riscv_nuttx:latest      20.5GB
tommy34tommy34
$ docker run -it --rm --name riscv_nuttx_container riscv_nuttx bash
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# ./tools/configure.sh --help
Directory for --help does not exist.

Run tools/configure.sh -L to list available configurations.


USAGE: ./tools/configure.sh [-E] [-e] [-l|m|c|g|n|B] [L] [-a <app-dir>] <board-selection> [make-opts]

Where:
  -E enforces distclean if already configured.
  -e performs distclean if configuration changed.
  -l selects the Linux (l) host environment.
  -m selects the macOS (m) host environment.
  -c selects the Windows host and Cygwin (c) environment.
  -g selects the Windows host and MinGW/MSYS environment.
  -n selects the Windows host and Windows native (n) environment.
  -B selects the *BSD (B) host environment.
  Default: Use host setup in the defconfig file
  Default Windows: Cygwin
  -L  Lists all available configurations.
  -a <app-dir> is the path to the apps/ directory, relative to the nuttx
     directory
  <board-selection> is either:
    For in-tree boards: a <board-name>:<config-name> pair where <board-name> is
    the name of the board in the boards directory and <config-name> is the name
    of the board configuration sub-directory (e.g. boardname:nsh), or: For
    out-of-tree custom boards: a path to the board's configuration directory,
    either relative to TOPDIR (e.g. ../mycustomboards/myboardname/config/nsh)
    or an absolute path.
  make-opts directly pass to make


root@2d5c230adbbb:/nuttx-nuttx-12.4.0#
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# ./tools/configure.sh rv-virt:nsh
  Copy files
  Select CONFIG_HOST_LINUX=y
  Refreshing...
Create .version
CP: arch/dummy/Kconfig to /nuttx-nuttx-12.4.0/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /nuttx-nuttx-12.4.0/boards/dummy/dummy_kconfig
LN: platform/board to /apps/platform/dummy
LN: include/arch to arch/risc-v/include
LN: include/arch/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/include
LN: drivers/platform to /nuttx-nuttx-12.4.0/drivers/dummy
LN: include/arch/chip to /nuttx-nuttx-12.4.0/arch/risc-v/include/qemu-rv
LN: arch/risc-v/src/chip to /nuttx-nuttx-12.4.0/arch/risc-v/src/qemu-rv
LN: arch/risc-v/src/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/src
mkkconfig in /apps/audioutils
mkkconfig in /apps/benchmarks
mkkconfig in /apps/boot
mkkconfig in /apps/canutils
mkkconfig in /apps/crypto
mkkconfig in /apps/examples/mcuboot
mkkconfig in /apps/examples
mkkconfig in /apps/fsutils
mkkconfig in /apps/games
mkkconfig in /apps/graphics
mkkconfig in /apps/industry
mkkconfig in /apps/inertial
mkkconfig in /apps/interpreters/luamodules
mkkconfig in /apps/interpreters
mkkconfig in /apps/logging
mkkconfig in /apps/lte
mkkconfig in /apps/math
mkkconfig in /apps/mlearning
mkkconfig in /apps/netutils
mkkconfig in /apps/sdr
mkkconfig in /apps/system
mkkconfig in /apps/testing
mkkconfig in /apps/wireless/bluetooth
mkkconfig in /apps/wireless/ieee802154
mkkconfig in /apps/wireless
mkkconfig in /apps
#
# configuration written to .config
#
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# # System Type ---> QEMU Chip Selection (QEMU RV64) ---> (X) QEMU RV64
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# make menuconfig
CP: arch/dummy/Kconfig to /nuttx-nuttx-12.4.0/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /nuttx-nuttx-12.4.0/boards/dummy/dummy_kconfig
LN: platform/board to /apps/platform/dummy
LN: include/arch to arch/risc-v/include
LN: include/arch/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/include
LN: drivers/platform to /nuttx-nuttx-12.4.0/drivers/dummy
LN: include/arch/chip to /nuttx-nuttx-12.4.0/arch/risc-v/include/qemu-rv
LN: arch/risc-v/src/chip to /nuttx-nuttx-12.4.0/arch/risc-v/src/qemu-rv
LN: arch/risc-v/src/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/src


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

root@2d5c230adbbb:/nuttx-nuttx-12.4.0#
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# make -j$(nproc)
Create version.h
LN: platform/board to /apps/platform/dummy
Register: nsh
Register: sh
Register: ostest
Register: hello
CPP:  /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/scripts/ld.script-> /nuttx-nuttx-12.4LD: nuttx
riscv64-unknown-elf-ld: warning: /nuttx-nuttx-12.4.0/nuttx has a LOAD segment with RWX permissions
CP: nuttx.hex
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# /qemu-8.2.1/build/qemu-system-riscv64 \
  -M virt -cpu rv64 -smp 8 -bios none -kernel nuttx -nographic

NuttShell (NSH)
nsh> ls
/:
 dev/
 proc/
nsh> help
help usage:  help [-v] [<cmd>]

    .           cp          exit        mkrd        set         unset
    [           cmp         false       mount       sleep       uptime
    ?           dirname     fdinfo      mv          source      usleep
    alias       dd          free        pidof       test        xd
    unalias     df          help        printf      time
    basename    dmesg       hexdump     ps          true
    break       echo        kill        pwd         truncate
    cat         env         ls          rm          uname
    cd          exec        mkdir       rmdir       umount

Builtin Apps:
    nsh       ostest    sh
nsh> time
nsh: time: missing required argument(s)
nsh> pwd
/
nsh> free
                   total       used       free    maxused    maxfree  nused  nfree
        Umem:   33372936      10632   33362304      10600   33362304     36      1
nsh>
tommy34tommy34

make menuconfigは以下のように、System Type ---> QEMU Chip Selection (QEMU RV64) ---> (X) QEMU RV64を選択する




nuttx-appsはappsというディレクトリ名にしないとmake menuconfigを実行したときに次のようなエラーが出るので注意が必要

root@403fd7374d2f:/nuttx-nuttx-12.4.0# make menuconfig
CP: arch/dummy/Kconfig to /nuttx-nuttx-12.4.0/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /nuttx-nuttx-12.4.0/boards/dummy/dummy_kconfig
LN: include/arch to arch/risc-v/include
LN: include/arch/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/include
LN: drivers/platform to /nuttx-nuttx-12.4.0/drivers/dummy
LN: include/arch/chip to /nuttx-nuttx-12.4.0/arch/risc-v/include/qemu-rv
LN: arch/risc-v/src/chip to /nuttx-nuttx-12.4.0/arch/risc-v/src/qemu-rv
LN: arch/risc-v/src/board to /nuttx-nuttx-12.4.0/boards/risc-v/qemu-rv/rv-virt/src
Kconfig:2467: can't open file "../apps/Kconfig"
make: *** [tools/Unix.mk:693: menuconfig] Error 1
tommy34tommy34

NuttXの行数は約274万行

root@2d5c230adbbb:/nuttx-nuttx-12.4.0# cloc .
   21299 text files.
   17679 unique files.
    4775 files ignored.

github.com/AlDanial/cloc v 1.98  T=104.86 s (168.6 files/s, 51377.8 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C                              8411         600818        1060678        1635087
C/C++ Header                   6207         277280         552369         877595
reStructuredText               1057          23695          13559          57413
Text                            165          15810              0          52636
Linker Script                   460           8079          18838          43105
Assembly                        300          11123          28648          27943
make                            326           4614           7603          13388
CMake                           535           3712          10985          13006
Bourne Shell                     77           1518           1761           6078
Python                           30           1341           1280           4986
diff                             50            470           1685           2626
Markdown                         12            649              0           2427
CSV                               3              0              0            669
YAML                             10            116            117            523
DOS Batch                         9            203            175            482
Pascal                            2             77              0            478
Dockerfile                        1             58             98            302
XML                               4              0              0            276
C++                               8             85            294            111
JSON                              3              0              0             67
Perl                              1             14             36             66
Bourne Again Shell                3             13             33             60
CSS                               1             16             27             50
TOML                              1              5              6             39
HTML                              1              9             17             23
INI                               2              1              0             23
--------------------------------------------------------------------------------
SUM:                          17679         949706        1698209        2739459
--------------------------------------------------------------------------------
root@2d5c230adbbb:/nuttx-nuttx-12.4.0# cloc ../apps
    3945 text files.
    2831 unique files.
    1320 files ignored.

github.com/AlDanial/cloc v 1.98  T=7.98 s (354.8 files/s, 80668.0 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C                              1118          71334          87688         250911
C++                             202          13031          21918          37042
C/C++ Header                    515          16129          46540          31016
diff                             42            805           3280          16223
make                            503           4143          10427           9845
CMake                           326            987           6460           5645
lex                               1             37              5           1895
Markdown                          6            445              0           1748
Text                             28            246              0           1507
Python                            5            282            193           1322
Bourne Shell                     13            163            228            644
Visual Basic                     52             11              0            474
YAML                              5             42             38            250
CSS                               2             29              1            140
Nim                               3             32             52            129
DOS Batch                         1             23             26             65
HTML                              4              4              0             55
Rust                              1             15             42             17
PHP                               2              1              3             12
Bourne Again Shell                1              2              7              8
Zig                               1              4             28              8
--------------------------------------------------------------------------------
SUM:                           2831         107765         176936         358956
--------------------------------------------------------------------------------
root@2d5c230adbbb:/nuttx-nuttx-12.4.0#