New FAMILUG

The PyMiers

Thursday 28 July 2016

[CLI/docker] busybox - con dao lính tây của hệ thống nhúng Linux

xin được cười cái title một cái đã =))

Busybox là một software, nó tập hợp nhiều UNIX utilities (các câu lệnh) thành một file duy nhất.
Các câu lệnh cung cấp bởi busybox thường tương đương với các câu lệnh cung cấp bởi GNU nhưng có ít option hơn.

Cài đặt:
$ sudo apt-get install -y busybox
Hỏi nào: 
$ whatis busybox
busybox (1)          - The Swiss Army Knife of Embedded Linux
Swiss Army Knife

Chạy lệnh ``du`` cung cấp bởi busybox
$ busybox du --help
BusyBox v1.25.0 (2016-06-23 20:12:29 UTC) multi-call binary.
Usage: du [-aHLdclsxhmk] [FILE]...

Summarize disk space used for each FILE and/or directory

    -a    Show file sizes too
    -L    Follow all symlinks
    -H    Follow symlinks on command line
    -d N    Limit output to directories (and files with -a) of depth < N
    -c    Show grand total
    -l    Count sizes many times if hard linked
    -s    Display only a total for each argument
    -x    Skip directories on different filesystems
    -h    Sizes in human readable format (e.g., 1K 243M 2G)
    -m    Sizes in megabytes
    -k    Sizes in kilobytes (default)

So với GNU ``du`` trên Ubuntu 14.04:
$ du --help | wc -l
61
busybox được viết nhằm tối ưu về kích thước, thích hợp cho các hệ thống nhúng. Nó chỉ nặng chưa đến 1 MB:
/ # du -h /bin/busybox
988.0K    /bin/busybox
Nhưng chứa đầy đủ các câu lệnh thường dùng:
/ # find /bin  | wc -l
371
Chạy busybox --help để xem đầy đủ các câu lệnh nó hỗ trợ.

Sử dụng Docker, ta có thể chạy ngay một container sử dụng busybox:
$ docker run -it --rm busybox
/ # busybox
BusyBox v1.25.0 (2016-06-23 20:12:29 UTC) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2015.
...
Toàn bộ trong image này chỉ có hơn 9000 file
/ # find / | wc -l
9342
Nếu bỏ đi các file cần thiết của kernel và hệ thống sinh ra khi chạy thì còn lại:
/ # find / -not -path '/dev*' -not -path '/sys*' -not -path '/proc*' | wc -l
392
Bỏ qua các file /bin sinh ra bởi busybox, ta còn lại 21 file/directory thiết yếu: 
/ # find / -not -path '/dev*' -not -path '/sys*' -not -path '/proc*' -not -path '/bin*' | wc -l
21
Trong đó chỉ có 9 file:
/ # find / -not -path '/dev*' -not -path '/sys*' -not -path '/proc*' -not -path '/bin*' -type f | wc -l
9
/ # find / -not -path '/dev*' -not -path '/sys*' -not -path '/proc*' -not -path '/bin*' -type f
/root/.ash_history
/.dockerenv
/etc/hosts
/etc/resolv.conf
/etc/hostname
/etc/localtime
/etc/group
/etc/passwd
/etc/shadow
file /.dockerenv luôn tồn tại trong các docker container. Ở đây, file này trống:
/ # wc -l /.dockerenv
0 /.dockerenv
Các file còn lại trong /etc là các file cấu hình thiết yếu của hệ thống.

Image busybox rất nhẹ, thích hợp làm base image cho các image khác:
$ docker images | grep busybox
busybox                         latest              2b8fd9751c4c        4 weeks ago         1.093 MB

Hết.

Tham khảo:
Credit:
Image by http://www.swisstool.co.uk/st/handyman-red-swiss-army-knife/
HVN @ FAMILUG DOT ORG

No comments:

Post a Comment