いますぐ使える! BusyBox活用術

いますぐ使える! BusyBox活用術(1)

BusyBoxをクロスコンパイルする環境を構築しよう

宍道 洋 busybox同好会 2008/7/2

Linuxを搭載した組み込み機器で「BusyBox」を活用するケースが増えている。本連載では、特集「組み込みLinuxで際立つ『BusyBox』の魅力」からさらに一歩踏み込んで、BusyBoxのより詳しい解説やBusyBoxを使った組み込み機器への適用方法、BusyBoxの拡張、コミュニティとのかかわり方など、いますぐ使えるすべを伝授する。(編集部)

- PR -

 携帯電話やPDA(Personal Digital Assistant)、液晶テレビなど、組み込み機器にLinuxを使用するケースが増えてきました。皆さんも日ごろからLinux搭載の組み込み機器を知らず知らずのうちに使っているかもしれませんね。

 本連載では、こうしたLinux搭載の組み込み機器には欠かせない存在ともいえる「BusyBox」について詳しく解説していきます。

 「ちょっと待って、BusyBoxって何?」という方は本連載を読み進める前に、特集「組み込みLinuxで際立つ『BusyBox』の魅力」に目を通しておいてください。“BusyBoxの概要”について分かりやすくまとめてありますので、本連載を読み解くうえで必ずや役に立つと思います。

 さて、本連載ではBusyBoxについて“より詳しく”解説していくわけですが、

  • BusyBoxの詳細
  • BusyBoxを使った組み込み機器への適用
  • BusyBoxの拡張の仕方
  • BusyBoxコミュニティとのかかわり方

の4つのテーマについて紹介していく予定です。

 前置きはこれくらいにして、早速本題に入っていきましょう。記念すべき連載第1回は「BusyBoxのおさらい」と、BusyBoxを組み込み機器向けにコンパイルするのに必要な「クロスコンパイル環境の構築」について説明していきます。

関連リンク:
組み込みLinuxで際立つ「BusyBox」の魅力
http://monoist.atmarkit.co.jp/fembedded/articles/busybox/busyboxa.html
組み込みLinux関連記事−@IT MONOist
http://monoist.atmarkit.co.jp/fembedded/index/embedded.html#linux
@IT 「Linux Square」フォーラム
http://www.atmarkit.co.jp/flinux/

BusyBoxのおさらい

 まずは、BusyBoxについておさらいしておきましょう。

 BusyBoxとは、いわゆるコマンドの集合体で、“busybox”という1つの実行ファイルで「ls」「cat」「vi」「httpd」といったさまざまなコマンドの機能を使うことができます。

 以下は、BusyBoxのUsageを表示した一例ですが、“Currently defined functions:”以下に利用可能なコマンドが表示されています。

$ /bin/busybox
BusyBox v1.10.2 (2008-05-22 04:45:56 JST) multi-call binary
Copyright (C) 1998-2007 Erik Andersen, Rob Landley, Denys Vlasenko
and others. Licensed under GPLv2.
See source distribution for full notice.

Usage: busybox [function] [arguments]...
   or: function [arguments]...

        BusyBox is a multi-call binary that combines many common Unix
        utilities into a single executable.  Most people will create a
        link to busybox for each function they wish to use and BusyBox
        will act like whatever it was invoked as!

Currently defined functions:
        [, [[, addgroup, adduser, ar, arping, ash, awk, basename,
        bunzip2, bzcat, cat, catv, chattr, chgrp, chmod, chown,
        chroot, chrt, chvt, cksum, clear, cmp, cp, cpio, crond,
        crontab, cut, date, dc, dd, deallocvt, delgroup, deluser,


(中略)


        time, top, touch, tr, traceroute, true, tty, udhcpc, udhcpd,
        umount, uname, uniq, unix2dos, unlzma, unzip, uptime,
        usleep, uudecode, uuencode, vconfig, vi, vlock, watch,
        watchdog, wc, wget, which, who, whoami, xargs, yes, zcat


$

 原稿執筆時点での最新バージョンは「1.10.2」ですが、このバージョンでは約300個ものコマンドが用意されています。

 実際にそれぞれのコマンドの機能を使う場合は、busyboxという実行ファイルにコマンド名のリンクを張って利用します(実際には、BusyBoxインストール時に自動的にリンクを作ってくれます)。例えば、BusyBoxをインストールした環境の“/bin”ディレクトリを「ls」コマンドで見てみると、

$ ls -l /bin/
lrwxrwxrwx    1 root     root            7 May 22 13:47 addgroup -> busybox
lrwxrwxrwx    1 root     root            7 May 22 13:47 adduser -> busybox
lrwxrwxrwx    1 root     root            7 May 22 13:47 ash -> busybox
-rwsr-xr-x    1 root     root       620344 May 22 13:47 busybox
lrwxrwxrwx    1 root     root            7 May 22 13:47 cat -> busybox


(中略)


lrwxrwxrwx    1 500      500             7 May 21 13:47 login -> busybox
lrwxrwxrwx    1 500      500             7 May 21 13:47 ls -> busybox
lrwxrwxrwx    1 500      500             7 May 21 13:47 lsattr -> busybox


(以下略)

といったように、「ls」コマンドを含め、さまざまなコマンドがbusyboxにリンクされていることが分かります。

 では、なぜ複数のコマンドを1つの実行ファイルにする必要があるのでしょうか?

 それは、複数のコマンドに必要な同じような処理(例えば、エラー処理やファイル操作など)や各実行ファイルに含まれるヘッダ情報などを1つにまとめることで、ディスク上のフットプリント(占有領域)を削減できるからです。組み込み機器のように限られたリソースしか使えない環境では、必要な機能を入れるためのサイズの縮小化は重要な課題となります。また、個々のコマンドを別々にコンパイルするのに比べ、BusyBox1つをコンパイルすることで、環境構築がとても簡単になります。そのため、組み込み向けのLinux環境構築では欠かせないツールといえます。

 ただしサイズ縮小化のため、コマンドによってはあまり使われないオプションや機能は省略されています。例えば、プロセスを表示する「ps」コマンドのオプションの場合、FedoraやDebianなどで使われているフル機能では、

[shinji@Fedora9 ~]$ ps --help
********* simple selection *********  ********* selection by list *********
-A all processes                      -C by command name
-N negate selection                   -G by real group ID (supports names)
-a all w/ tty except session leaders  -U by real user ID (supports names)
-d all except session leaders         -g by session OR by effective group name
-e all processes                      -p by process ID
T  all processes on this terminal     -s processes in the sessions given
a  all w/ tty, including other users  -t by tty
g  OBSOLETE -- DO NOT USE             -u by effective user ID (supports names)
r  only running processes             U  processes for specified users
x  processes w/o controlling ttys     t  by tty
*********** output format **********  *********** long options ***********
-o,o user-defined  -f full            --Group --User --pid --cols --ppid
-j,j job control   s  signal          --group --user --sid --rows --info
-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect
-l,l long          u  user-oriented   --sort --tty --forest --version
-F   extra full    X  registers       --heading --no-heading --context
                    ********* misc options *********
-V,V  show version      L  list format codes  f  ASCII art forest
-m,m,-L,-T,H  threads   S  children in sum    -y change -l format
-M,Z  security data     c  true command name  -c scheduling class
-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy
[shinji@Fedora9 ~]$

といったように多くのオプションを利用することができますが、BusyBoxの「ps」コマンドでは次のように1つしか提供されていません。

$ ps --help
BusyBox v1.10.2 (2008-05-22 04:45:56 JST) multi-call binary

Usage: ps 

Report process status

Options:
        -o col1,col2=header     Select columns for display

$

 そのため、もし省略された機能を使いたい場合には、BusyBoxで提供されているコマンドをフル機能のコマンドに置き換えるか、もしくはBusyBoxのコマンドを拡張する必要があります。

>>おさらいはこれくらいにして、クロスコンパイル環境の構築を行ってみましょう。

  • 連載バックナンバー
  • 全記事インデックス
  • 組み込み開発トップ
  • MONOistトップ

スキルアップ/キャリアアップ(JOB@IT)

スポンサーからのお知らせ

- PR -
@IT Sepcial

震災関連・復興支援情報

震災関連・復興支援情報
@IT MONOist/EE Times Japan/環境メディアの製造業技術者向け3メディアを中心に、震災関連/復興支援情報を集めました

次世代エンベデッドコーナー

次世代エンベデッド
“次世代”の組み込み機器を開発するエンジニアを支援するコーナー。新潮流・新技術をインタビューやコラム、解説記事で分かりやすく紹介!

Windows Embeddedコーナー

Windows Embedded
Windows Embedded専門コーナー。Windows Embedded StandardやWindows Embedded CEをはじめとする「Windows Embedded」ファミリの最新動向や技術情報をお届けします!!

Androidコーナー

Android
Android専門コーナー。組み込みデバイスへの適用からアプリケーション開発、イベントレポート、ニュースなどAndroidに関するさまざまな技術情報がここに集結!!

@IT MONOist 求人情報

- PR -