はじめに

Dockerのイメージを見てると、busybox という単語をよくみる。
実際に使ってみたほうが理解が早そうということで、WSL2busyboxを入れて試してみる。

環境

1
2
3
4
Windows 10 Professional

WSL2(Ubuntu 22.04 LTS)
BusyBox v1.30.1 (Ubuntu 1:1.30.1-7ubuntu3) multi-call binary.

busyboxとは

BusyBox(ビジーボックス)は、Coreutilsなど標準UNIXコマンドで重要な多数のプログラムを単一の実行ファイルに「詰め込んで」提供する、特殊な方式のプログラムである(その詰め込み方法を指して呼ぶこともある)。BusyBoxの実行ファイルはLinux上で最小の実行ファイルとなるよう設計されており、各コマンドの実行ファイルをインストールするのに比べディスクの使用量を大幅に削減することができる。そのため、特定用途のLinuxディストリビューションや組み込みシステムに適しており、「組み込みLinuxの十徳ナイフ」とも呼ばれている。GPLv2でリリースされているフリーソフトウェアである。

wikipediaより引用 (https://ja.wikipedia.org/wiki/BusyBox)

/bin/busybox [コマンド] とすることで コマンドが実行できる。
例えば、 /bin/busybox ls とすれば、ls と同じ結果が得られる。
wikipediaに記載の通り、busybox というバイナリの中に実行コマンドが詰められているという感じだ。

busyboxのインストール

1
sudo apt-get install busybox
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  golang-1.18 golang-1.18-doc golang-1.18-go golang-1.18-src golang-doc golang-go golang-src pkg-config
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  busybox-static ubuntu-standard
The following NEW packages will be installed:
  busybox
0 upgraded, 1 newly installed, 2 to remove and 0 not upgraded.
Need to get 436 kB of archives.
After this operation, 1543 kB disk space will be freed.
Do you want to continue? [Y/n]
1
busybox --help
1
2
3
4
BusyBox v1.30.1 (Ubuntu 1:1.30.1-7ubuntu3) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2015.
Licensed under GPLv2. See source distribution for detailed
copyright notices.

実際に使ってみる

lsコマンドを使う

1
busybox ls
1
2
3
4
5
6
7
8
9
busybox ls
IMG_4010.jpeg                  gitlab-docker                  secret2.pdf
IMG_4010.jpeg:Zone.Identifier  logs                           secret3.pdf
calendar.pdf                   mail-for-ocr.png               secret4.pdf
calendar.xlsx                  mount-bucket-test              work
config                         output.txt.txt                 workspace
data                           qrencode.pdf                   zip_secret1.zip
error.log                      qrencode.svg
error1.log                     secret1.pdf
1
busybox ls --help
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
BusyBox v1.30.1 (Ubuntu 1:1.30.1-7ubuntu3) multi-call binary.

Usage: ls [-1AaCxdLHRFplinshrSXvctu] [-w WIDTH] [FILE]...

List directory contents

        -1      One column output
        -a      Include entries which start with .
        -A      Like -a, but exclude . and ..
        -x      List by lines
        -d      List directory entries instead of contents
        -L      Follow symlinks
        -H      Follow symlinks on command line
        -R      Recurse
        -p      Append / to dir entries
        -F      Append indicator (one of */=@|) to entries
        -l      Long listing format
        -i      List inode numbers
        -n      List numeric UIDs and GIDs instead of names
        -s      List allocated blocks
        -lc     List ctime
        -lu     List atime
        --full-time     List full date and time
        -h      Human readable sizes (1K 243M 2G)
        --group-directories-first
        -S      Sort by size
        -X      Sort by extension
        -v      Sort by version
        -t      Sort by mtime
        -tc     Sort by ctime
        -tu     Sort by atime
        -r      Reverse sort order
        -w N    Format N columns wide
        --color[={always,never,auto}]   Control coloring

コマンドの定義について

ちなみに、なんのコマンドが使えるかは busybox --help に記載がある。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Currently defined functions:
        [, [[, acpid, adjtimex, ar, arch, arp, arping, ash, awk, basename, bc, blkdiscard, blockdev, brctl, bunzip2,
        bzcat, bzip2, cal, cat, chgrp, chmod, chown, chpasswd, chroot, chvt, clear, cmp, cp, cpio, cttyhack, cut, date,
        dc, dd, deallocvt, depmod, devmem, df, diff, dirname, dmesg, dnsdomainname, dos2unix, du, dumpkmap, dumpleases,
        echo, egrep, env, expand, expr, factor, fallocate, false, fatattr, fgrep, find, fold, free, freeramdisk,
        fsfreeze, fstrim, ftpget, ftpput, getopt, getty, grep, groups, gunzip, gzip, halt, head, hexdump, hostid,
        hostname, httpd, hwclock, i2cdetect, i2cdump, i2cget, i2cset, id, ifconfig, ifdown, ifup, init, insmod, ionice,
        ip, ipcalc, ipneigh, kill, killall, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont, loadkmap,
        logger, login, logname, logread, losetup, ls, lsmod, lsscsi, lzcat, lzma, lzop, md5sum, mdev, microcom, mkdir,
        mkdosfs, mke2fs, mkfifo, mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mt, mv, nameif, nc,
        netstat, nl, nologin, nproc, nsenter, nslookup, nuke, od, openvt, partprobe, paste, patch, pidof, ping, ping6,
        pivot_root, poweroff, printf, ps, pwd, rdate, readlink, realpath, reboot, renice, reset, resume, rev, rm,
        rmdir, rmmod, route, rpm, rpm2cpio, run-init, run-parts, sed, seq, setkeycodes, setpriv, setsid, sh, sha1sum,
        sha256sum, sha512sum, shred, shuf, sleep, sort, ssl_client, start-stop-daemon, stat, strings, stty, svc, svok,
        swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tee, telnet, test, tftp, time,
        timeout, top, touch, tr, traceroute, traceroute6, true, truncate, tty, ubirename, udhcpc, udhcpd, uevent,
        umount, uname, uncompress, unexpand, uniq, unix2dos, unlink, unlzma, unshare, unxz, unzip, uptime, usleep,
        uudecode, uuencode, vconfig, vi, w, watch, watchdog, wc, wget, which, who, whoami, xargs, xxd, xz, xzcat, yes,
        zcat

必要なコマンドは一通り揃っているみたい。

参考

おわりに

busybox について実際に触れてみて中身の理解について多少知識がつけられた。
コマンドが使えない~ってなったときにこれあると助かりそうだ。