Open10

DockerでLinux、QEMU、Busyboxのソースコードをビルドし、QEMUでLinuxを起動する

tommy34tommy34

Linux、QEMU、Busyboxのバージョンはスクラップ作成日時点(2024/02/15)での最新のバージョンを使用する

  • Linux v6.7
  • QEMU v8.2.1
  • BusyBox v1.36.1
tommy34tommy34

Dockerfile

FROM ubuntu:24.04

ENV TZ=Asia/Tokyo
ENV LINUX_VERSION=6.7
ENV BUSYBOX_VERSION=1.36.1
ENV QEMU_VERSION=8.2.1

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 && \
  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

WORKDIR /linux-${LINUX_VERSION}

RUN make defconfig && make -j$(nproc)

WORKDIR /busybox-${BUSYBOX_VERSION}

RUN make defconfig && \
  sed -i.bak 's/^#.*CONFIG_STATIC.*/CONFIG_STATIC=y/' .config && \
  make -j$(nproc) && \
  make install && \
  cd _install && \
  mkdir proc sys dev 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=x86_64-softmmu && \
  make -j$(nproc)

WORKDIR /
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 qemu_linux .
[+] Building 2.5s (12/12) FINISHED                                            docker:default
 => [internal] load .dockerignore                                                       0.5s
 => => transferring context: 2B                                                         0.0s
 => [internal] load build definition from Dockerfile                                    0.4s
 => => transferring dockerfile: 2.51kB                                                  0.1s
 => [internal] load metadata for docker.io/library/ubuntu:24.04                         0.0s
 => [1/9] FROM docker.io/library/ubuntu:24.04                                           0.0s
 => CACHED [2/9] RUN ln -snf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo Asi  0.0s
 => CACHED [3/9] WORKDIR /linux-6.7                                                     0.0s
 => CACHED [4/9] RUN make defconfig && make -j$(nproc)                                  0.0s
 => CACHED [5/9] WORKDIR /busybox-1.36.1                                                0.0s
 => CACHED [6/9] RUN make defconfig &&   sed -i.bak 's/^#.*CONFIG_STATIC.*/CONFIG_STAT  0.0s
 => CACHED [7/9] WORKDIR /qemu-8.2.1                                                    0.0s
 => CACHED [8/9] RUN ./configure --target-list=x86_64-softmmu &&   make -j$(nproc)      0.0s
 => exporting to image                                                                  0.2s
 => => exporting layers                                                                 0.0s
 => => writing image sha256:443d0e0f7f403bbec46b15684044aabb3168f16438bd5f5be883258828  0.1s
 => => naming to docker.io/library/qemu_linux                                           0.1s

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview
tommy34tommy34
$ docker run -it --rm --name qemu_linux_container qemu_linux bash
root@193ac5e6a27f:/# qemu-8.2.1/build/qemu-system-x86_64 --version
QEMU emulator version 8.2.1
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
root@193ac5e6a27f:/# qemu-8.2.1/build/qemu-system-x86_64 \
  -m 128M \
  -kernel linux-6.7/arch/x86/boot/bzImage \
  -initrd busybox-1.36.1/rootfs.img \
  --append "root=/dev/ram rw console=ttyS0 rdinit=/sbin/init init=/sbin/init" \
  -chardev stdio,id=char0,mux=on,logfile=linux.log,signal=off \
  -serial chardev:char0 \
  -mon chardev=char0
VNC server running on 127.0.0.1:5900
[    0.000000] Linux version 6.7.0 (root@buildkitsandbox) (gcc (Ubuntu 13.2.0-13ubuntu1) 13.2.0, GNU ld (GNU Binutils for Ubuntu) 2.42) #1 SMP PREEMPT_DYNAMIC Thu Feb 15 21:30:02 JST 2024
[    0.000000] Command line: root=/dev/ram rw console=ttyS0 rdinit=/sbin/init init=/sbin/init
[    0.000000] BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fdffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000007fe0000-0x0000000007ffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[    0.000000] BIOS-e820: [mem 0x000000fd00000000-0x000000ffffffffff] reserved
[    0.000000] NX (Execute Disable) protection: active
[    0.000000] APIC: Static calls initialized
[    0.000000] SMBIOS 3.0.0 present.
[    0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[    0.000000] tsc: Fast TSC calibration using PIT
[    0.000000] tsc: Detected 3191.947 MHz processor
[    0.016644] last_pfn = 0x7fe0 max_arch_pfn = 0x400000000
[    0.017362] MTRR map: 4 entries (3 fixed + 1 variable; max 19), built from 8 variable MTRRs
...
[    3.126874] Freeing unused kernel image (initmem) memory: 2648K
[    3.129105] Write protecting the kernel read-only data: 26624k
[    3.133544] Freeing unused kernel image (rodata/data gap) memory: 1640K
[    3.445518] x86/mm: Checked W+X mappings: passed, no W+X pages found.
[    3.447824] Run /sbin/init as init process
mount: mounting /dev/pts on /dev/pts failed: No such file or directory
[    3.654216] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[    3.673279] mdev (54) used greatest stack depth: 14032 bytes left

Please press Enter to activate this console.
~ # pwd
/
~ # ls
1        c        etc      null     root     sys
5        console  linuxrc  proc     run      tty
bin      dev      mknod    ram      sbin     usr
~ # echo Hello
Hello
~ # uname -a
Linux (none) 6.7.0 #1 SMP PREEMPT_DYNAMIC Thu Feb 15 21:30:02 JST 2024 x86_64 GNU/Linux
tommy34tommy34
~ # ps
PID   USER     TIME  COMMAND
    1 0         0:01 init
    2 0         0:00 [kthreadd]
    3 0         0:00 [pool_workqueue_]
    4 0         0:00 [kworker/R-rcu_g]
    5 0         0:00 [kworker/R-rcu_p]
    6 0         0:00 [kworker/R-slub_]
    7 0         0:00 [kworker/R-netns]
    8 0         0:00 [kworker/0:0-eve]
    9 0         0:00 [kworker/0:0H-ev]
   10 0         0:00 [kworker/0:1-mm_]
   11 0         0:00 [kworker/u2:0-ev]
   12 0         0:00 [kworker/R-mm_pe]
   13 0         0:00 [rcu_tasks_kthre]
   14 0         0:00 [ksoftirqd/0]
   15 0         0:00 [rcu_preempt]
   16 0         0:00 [migration/0]
   17 0         0:00 [cpuhp/0]
   18 0         0:00 [kdevtmpfs]
   19 0         0:00 [kworker/R-inet_]
   20 0         0:00 [kauditd]
   21 0         0:00 [kworker/u2:1-ev]
   22 0         0:00 [oom_reaper]
   23 0         0:00 [kworker/R-write]
   24 0         0:00 [kcompactd0]
   25 0         0:00 [kworker/R-kbloc]
   26 0         0:00 [kworker/R-ata_s]
   27 0         0:00 [kworker/R-md]
   28 0         0:00 [kworker/R-md_bi]
   29 0         0:00 [kworker/u2:2-ev]
   30 0         0:00 [kworker/0:1H-kb]
   31 0         0:00 [kworker/R-rpcio]
   32 0         0:00 [kworker/R-xprti]
   33 0         0:00 [kworker/R-cfg80]
   34 0         0:00 [kworker/u2:3]
   35 0         0:00 [kswapd0]
   36 0         0:00 [kworker/R-nfsio]
   37 0         0:00 [kworker/R-acpi_]
   38 0         0:00 [scsi_eh_0]
   39 0         0:00 [kworker/R-scsi_]
   40 0         0:00 [scsi_eh_1]
   41 0         0:00 [kworker/R-scsi_]
   42 0         0:00 [kworker/0:2]
   43 0         0:00 [kworker/R-mld]
   44 0         0:00 [kworker/R-ipv6_]
   55 0         0:00 -/bin/sh
   56 0         0:00 init
   57 0         0:00 init
   58 0         0:00 init
   62 0         0:00 ps
~ #
~ # ls /usr/bin
[            du           less         pkill        sort         unexpand
[[           dumpleases   logger       pmap         split        uniq
ascii        eject        logname      printf       ssl_client   unix2dos
awk          env          lpq          pscan        strings      unlink
basename     envdir       lpr          pstree       sum          unlzma
bc           envuidgid    lsof         pwdx         sv           unshare
beep         expand       lspci        readlink     svc          unxz
blkdiscard   expr         lsscsi       realpath     svok         unzip
bunzip2      factor       lsusb        renice       tac          uptime
bzcat        fallocate    lzcat        reset        tail         users
bzip2        fgconsole    lzma         resize       taskset      uudecode
cal          find         man          rpm2cpio     tcpsvd       uuencode
chpst        flock        md5sum       runsv        tee          vlock
chrt         fold         mesg         runsvdir     telnet       volname
chvt         free         microcom     rx           test         w
cksum        ftpget       mkfifo       script       tftp         wall
clear        ftpput       mkpasswd     seq          time         wc
cmp          fuser        nc           setfattr     timeout      wget
comm         groups       nl           setkeycodes  top          which
crc32        hd           nmeter       setsid       tr           who
crontab      head         nohup        setuidgid    traceroute   whoami
cryptpw      hexdump      nproc        sha1sum      traceroute6  whois
cut          hexedit      nsenter      sha256sum    tree         xargs
dc           hostid       nslookup     sha3sum      truncate     xxd
deallocvt    id           od           sha512sum    ts           xz
diff         install      openvt       showkey      tsort        xzcat
dirname      ipcrm        passwd       shred        tty          yes
dos2unix     ipcs         paste        shuf         ttysize
dpkg         killall      patch        smemcap      udhcpc6
dpkg-deb     last         pgrep        softlimit    udpsvd
~ #
tommy34tommy34

Linuxのソースコードの行数は約2780万行
BusyBoxは約26万行
QEMUは約320万行

root@193ac5e6a27f:/# cloc linux-6.7
   87623 text files.
   74198 unique files.
   16422 files ignored.

github.com/AlDanial/cloc v 1.98  T=480.52 s (154.4 files/s, 76211.7 lines/s)
---------------------------------------------------------------------------------------
Language                             files          blank        comment           code
---------------------------------------------------------------------------------------
C                                    33241        3399886        2644658       17563162
C/C++ Header                         24065         724066        1404120        7359984
Assembly                              1323          48367          98578        1232963
reStructuredText                      3420         169724          70233         463015
JSON                                   597              2              0         403264
YAML                                  3803          70159          17549         332571
Text                                  1769          27249              0         123969
Bourne Shell                           994          31286          21278         122533
make                                  2932          11277          12153          52405
Python                                 197           9774           8453          48486
SVG                                     74             90           1171          48159
Perl                                    69           7577           5184          37721
Rust                                    61           1456           9387           8838
DOS Batch                             1465            103              0           5350
yacc                                    10            710            413           4946
PO File                                  6            948           1088           3733
lex                                     10            360            312           2202
C++                                      7            336            127           1800
Bourne Again Shell                      57            395            311           1633
awk                                     12            195            118           1076
D                                       10              0              0            722
CSV                                     10             74              0            660
Glade                                    1             58              0            603
NAnt script                              2            152              0            542
Cucumber                                 1             34             58            198
TeX                                      1              6             74            156
TNSDL                                    2             33              0            140
CSS                                      3             41             60            136
Linker Script                            4             23             11            115
Windows Module Definition                2             15              0            113
m4                                       1             15              1             95
Clojure                                 33              0              0             75
XSLT                                     5             13             26             61
Umka                                     2             17              0             45
MATLAB                                   1             17             37             35
Markdown                                 1              9              0             27
vim script                               1              3             12             27
Ruby                                     1              4              0             25
INI                                      2              2              0             11
HTML                                     1              1              5             10
sed                                      1              2              5              5
TOML                                     1              1              9              2
---------------------------------------------------------------------------------------
SUM:                                 74198        4504480        4295431       27821613
---------------------------------------------------------------------------------------
root@193ac5e6a27f:/# cloc busybox-1.36.1
    4609 text files.
    2179 unique files.
    3460 files ignored.

github.com/AlDanial/cloc v 1.98  T=17.06 s (127.8 files/s, 21461.0 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C                               685          28569          62131         186278
C/C++ Header                   1153           1363           2729          33738
Text                             73           2793              0          13461
Bourne Shell                    188           1732           1687           9778
HTML                             10           2062             32           7962
DOS Batch                        36             18              0           2404
Assembly                          5            153            479           1897
C++                               1            166             62           1197
make                              7            306            466            909
Glade                             1             56              0            592
yacc                              1             93             20            570
Perl                              3            100            185            337
lex                               1             40             12            303
NAnt script                       1             84              0            260
diff                              4             42             85            241
Bourne Again Shell                5             51             94            238
Python                            1             12             15            113
IDL                               1              0              0             33
awk                               1              2              8             30
D                                 1              3              0             29
bc                                1             10              0             25
--------------------------------------------------------------------------------
SUM:                           2179          37655          68005         260395
--------------------------------------------------------------------------------
root@193ac5e6a27f:/# cloc qemu-8.2.1/
   14488 text files.
   12637 unique files.
    5518 files ignored.

github.com/AlDanial/cloc v 1.98  T=86.80 s (145.6 files/s, 50241.2 lines/s)
---------------------------------------------------------------------------------------
Language                             files          blank        comment           code
---------------------------------------------------------------------------------------
C                                     4607         269051         230125        1680988
D                                     2206              0              0         518251
C/C++ Header                          2842          67975         102278         412201
Python                                1266          58735          75688         280913
Pascal                                  64          35271         257190         107593
JSON                                   282           1314              0          44020
reStructuredText                       253          12173           4885          31031
Text                                    72           3009              0          23109
Bourne Again Shell                     249           5549           7962          18403
Assembly                               237           2969           3099          15343
Meson                                  220           1050            658          10394
Bourne Shell                            83           1111           1513          10081
Haxe                                     5           1518             13           7374
Perl                                     7           1088            864           6175
make                                    37            666            777           6124
Windows Module Definition               12           1403              0           5559
CSV                                      3              0              0           5266
HTML                                    14            821              1           5208
YAML                                    41            301            369           3166
Objective-C                              4            446            412           2484
XML                                     51            495           1383           2332
SVG                                      2              2              2           1982
C++                                      6            289            160           1444
yacc                                     2            114             95           1282
Windows Resource File                    3            141              0           1023
lex                                      3            111             40            856
SWIG                                     1            258             28            832
Linker Script                           21            115            239            738
TeX                                      2             82              1            557
PO File                                  9            172             40            465
WiX source                               1              6              0            176
Markdown                                 6             97             47            171
CMake                                    2             30             29            125
TNSDL                                    2             24              0            121
PowerShell                               2             50             95            119
CSS                                      1             33             29             99
INI                                      4             16              0             84
DenizenScript                            1             12             14             43
Fish Shell                               1             13             14             42
Dockerfile                               1              4              6             24
Rust                                     1              6              0             22
GLSL                                     3              6              0             21
IDL                                      1              1              0             19
TOML                                     2              5             23             15
C Shell                                  1              9              5             12
Windows Message File                     1              1              0              8
JavaScript                               1              1              1              7
Lisp                                     1              0              0              2
DOS Batch                                1              0              0              1
---------------------------------------------------------------------------------------
SUM:                                 12637         466543         688085        3206305
---------------------------------------------------------------------------------------
root@193ac5e6a27f:/#