千家信息网

busybox怎么使用

发表于:2024-10-08 作者:千家信息网编辑
千家信息网最后更新 2024年10月08日,这篇文章主要讲解了"busybox怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"busybox怎么使用"吧!什么是基础镜像(base imag
千家信息网最后更新 2024年10月08日busybox怎么使用

这篇文章主要讲解了"busybox怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"busybox怎么使用"吧!

什么是基础镜像(base image)

简单来说,基础镜像就是没有from或者from scratch开头的dockerfile所构建出来的镜像。比如alpine,这个很小的linux镜像目前只有4m左右

[root@kong ~]# docker images |grep alpinedocker.io/alpine        latest       3fd9065eaf02    4 months ago    4.15 mb[root@kong ~]#

它的dockerfile很简单,只有三行,这就是一个基础镜像,

from scratchadd rootfs.tar.xz /cmd ["/bin/sh"]

busybox

概要说明

busybox被称为嵌入式linux的瑞士军刀,这句话是在busybox自己介绍自己的时候提出的(the swiss army knife of embedded linux)。busybox整合了很多小的unix下的通用功能到一个小的可执行文件之中,简单来说在unix或者linux下常用的那些功能在这里你都能找到,但是为了busybox的目标:嵌入式的linux,大小对于busybox来说是非常重要的优化要素和限制,这些功能有可能会有所阉割,但是对于一般需求来说已经足够。而alpine就是在busybox基础上增加了自己的包管理工具apk等功能创建了风靡一时的小巧镜像。busybox是用c语言开发的基于gpl的开源项目,目前的稳定版本为1.28.4

宿主机器

[root@kong ~]# uname -alinux kong 3.10.0-693.el7.x86_64 #1 smp tue aug 22 21:09:27 utc 2017 x86_64 x86_64 x86_64 gnu/linux[root@kong ~]# cat /etc/redhat-release centos linux release 7.4.1708 (core) [root@kong ~]#

下载busybox

简单可以直接使用的二进制最新的版本为1.28.1

[root@kong ~]# wget https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64--2018-05-25 04:51:20-- https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64resolving busybox.net (busybox.net)... 140.211.167.122connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected.http request sent, awaiting response... 200 oklength: 1001112 (978k)saving to: 'busybox-x86_64'100%[==============================================================================================>] 1,001,112  19.3kb/s  in 30s  2018-05-25 04:51:57 (32.4 kb/s) - 'busybox-x86_64' saved [1001112/1001112][root@kong ~]#

设定busybox

[root@kong ~]# cp busybox-x86_64 /usr/local/bin/busybox[root@kong ~]# chmod +x /usr/local/bin/busybox[root@kong ~]# which busybox/usr/local/bin/busybox[root@kong ~]#

版本确认

输入busybox可以看出版本以及熟悉的linux下的工具,仔细看一遍就会理解busybox号称自己是瑞士军刀一点都不夸张,反过来说,瑞士军刀如果敢号称linux里的busybox可能会引起非议。

[root@kong ~]# busyboxbusybox v1.28.1 (2018-02-15 14:34:02 cet) multi-call binary.busybox is copyrighted by many authors between 1998-2015.licensed under gplv2. see source distribution for detailedcopyright notices.usage: busybox [function [arguments]...]  or: busybox --list[-full]  or: busybox --install [-s] [dir]  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:  [, [[, acpid, add-shell, addgroup, adduser, adjtimex, arch, arp, arping, ash, awk, base64, basename, beep, blkdiscard, blkid,  blockdev, bootchartd, brctl, bunzip2, bzcat, bzip2, cal, cat, chat, chattr, chgrp, chmod, chown, chpasswd, chpst, chroot, chrt,  chvt, cksum, clear, cmp, comm, conspy, cp, cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc, dd, deallocvt, delgroup,  deluser, depmod, devmem, df, dhcprelay, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, dpkg, dpkg-deb, du, dumpkmap,  dumpleases, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr, factor, fakeidentd, fallocate, false,  fatattr, fbset, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs, flock, fold, free, freeramdisk, fsck,  fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fuser, getopt, getty, grep, groups, gunzip, gzip, halt, hd, hdparm,  head, hexdump, hexedit, hostid, hostname, httpd, hush, hwclock, i2cdetect, i2cdump, i2cget, i2cset, id, ifconfig, ifdown,  ifenslave, ifplugd, ifup, inetd, init, insmod, install, ionice, iostat, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, ipneigh,  iproute, iprule, iptunnel, kbd_mode, kill, killall, killall5, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont,  loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma,  lzop, makedevs, makemime, man, md5sum, mdev, mesg, microcom, mkdir, mkdosfs, mke2fs, mkfifo, mkfs.ext2, mkfs.minix, mkfs.vfat,  mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mountpoint, mpstat, mt, mv, nameif, nanddump, nandwrite,  nbd-client, nc, netstat, nice, nl, nmeter, nohup, nproc, nsenter, nslookup, ntpd, nuke, od, openvt, partprobe, passwd, paste,  patch, pgrep, pidof, ping, ping6, pipe_progress, pivot_root, pkill, pmap, popmaildir, poweroff, powertop, printenv, printf, ps,  pscan, pstree, pwd, pwdx, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, reformime,  remove-shell, renice, reset, resize, resume, rev, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-parts,  runlevel, runsv, runsvdir, rx, script, scriptreplay, sed, sendmail, seq, setarch, setconsole, setfattr, setfont, setkeycodes,  setlogcons, setpriv, setserial, setsid, setuidgid, sh, sha1sum, sha256sum, sha3sum, sha512sum, showkey, shred, shuf, slattach,  sleep, smemcap, softlimit, sort, split, ssl_client, start-stop-daemon, stat, strings, stty, su, sulogin, sum, sv, svc, svlogd,  swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tcpsvd, tee, telnet, telnetd, test, tftp, tftpd,  time, timeout, top, touch, tr, traceroute, traceroute6, true, truncate, tty, ttysize, tunctl, ubiattach, ubidetach, ubimkvol,  ubirename, ubirmvol, ubirsvol, ubiupdatevol, udhcpc, udhcpd, udpsvd, uevent, umount, uname, unexpand, uniq, unix2dos, unlink,  unlzma, unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vconfig, vi, vlock, volname, w, wall, watch, watchdog,  wc, wget, which, who, whoami, whois, xargs, xxd, xz, xzcat, yes, zcat, zcip[root@kong ~]

创建rootfs

这张图在镜像基础介绍的时候已经被无数次看到了,rootfs是linux中重要的概念,而alpine中也有add rootfs.tar.xz这样一句,接下来我们将了解一下如何生成一个简单的rootfs

创建目录并进入

[root@kong ~]# mkdir rootfs[root@kong ~]# cd rootfs/[root@kong rootfs]#

创建rootfs

执行如下语句

for module in `busybox --list-modules`do mkdir -p `dirname "$module"` ln -sf /bin/busybox "$module"done

执行日志

[root@kong rootfs]# for module in `busybox --list-modules`> do>  mkdir -p `dirname "$module"`>  ln -sf /bin/busybox "$module"> done[root@kong rootfs]#

结果确认

[root@kong rootfs]# lsbin linuxrc sbin usr[root@kong rootfs]# find . -type d../usr./usr/bin./usr/sbin./sbin./bin[root@kong rootfs]#

将busybox拷贝至新创建的./bin目录下

这样,上述命令的链接对象就存在了

[root@kong rootfs]# cp /usr/local/bin/busybox bin/[root@kong rootfs]# ls -l bin/busybox-rwxr-xr-x. 1 root root 1001112 may 25 05:27 bin/busybox[root@kong rootfs]#

创建rootfs.tar

此处注意相对路径,而后续次相对路径会展开至/下,从而创建新的系统的rootfs,这也是从零搭建linux(linux from scratch)的重要操作之一。

[root@kong rootfs]# tar cpf rootfs.tar .tar: ./rootfs.tar: file is the archive; not dumped[root@kong rootfs]#

简单说明:busybox -list-modules列出了busybox的所有模块,然后以此为基础,创建了一个小型的rootfs
[root@kong rootfs]# busybox -list-modules |wc -l
389
[root@kong rootfs]#

准备dockerfile

准备一个一行的dockerfile

[root@kong rootfs]# vi dockerfile[root@kong rootfs]# cat dockerfile from scratch[root@kong rootfs]#

创建base镜像,由于没有发现具体的内容,所以未创建出具体镜像。另外,本文为了演示方便,直接在此处创建dockerfile,这并不是一个好主意,实际的时候请不要这样做,如果当前目录下有100g的文件,就会无比缓慢,而且也不规范,无关物品需要清场。

[root@kong rootfs]# docker build -t busyboxbase:latest .sending build context to docker daemon 2.415 mbstep 1/1 : from scratch ---> no image was generated. is your dockerfile empty?[root@kong rootfs]# docker images |grep busyboxbase[root@kong rootfs]#

from scracth

有from,这个scratch可以pull么,目前的版本已经将其作为一个保留名称

[root@kong rootfs]# docker search scratch |grep 'an explicitly empty'docker.io  docker.io/scratch              an explicitly empty image, especially for ...  407    [ok]    [root@kong rootfs]# docker pull scratchusing default tag: latesterror response from daemon: 'scratch' is a reserved name[root@kong rootfs]#

将此dockerfile添加一行没有实际作用的,看看scratch到底是什么

[root@kong rootfs]# vi dockerfile [root@kong rootfs]# cat dockerfile from scratchmaintainer liumiao [root@kong rootfs]#

进行构建,发现产生了一个0字节的镜像文件,也与scratch的原意相通

[root@kong rootfs]# docker build -t busyboxbase:latest .sending build context to docker daemon 2.415 mbstep 1/2 : from scratch ---> step 2/2 : maintainer liumiao  ---> running in b118fd7c73a7 ---> 2074dc76c09eremoving intermediate container b118fd7c73a7successfully built 2074dc76c09e[root@kong rootfs]# docker images |grep busyboxbasebusyboxbase           latest       2074dc76c09e    14 seconds ago   0 b[root@kong rootfs]#

至此,我们理解了from scratch确实不会有额外的添加,接下来我们像alpine那样添加如下两句

add rootfs.tar /cmd ["/bin/sh"]

我们的dockerfile也是几乎一样的三行

[root@kong rootfs]# cat dockerfile from scratchadd rootfs.tar /cmd ["/bin/sh"][root@kong rootfs]#

这样就创建了一个1m的busybox为基础的镜像

[root@kong rootfs]# docker build -t busyboxbase:latest .sending build context to docker daemon 2.415 mbstep 1/3 : from scratch ---> step 2/3 : add rootfs.tar / ---> 0fbb0c8c7579removing intermediate container 8311e96f456cstep 3/3 : cmd /bin/sh ---> running in efb85c4526bf ---> 02270c80a4e4removing intermediate container efb85c4526bfsuccessfully built 02270c80a4e4[root@kong rootfs]# docker images |grep busyboxbasebusyboxbase           latest       02270c80a4e4    9 seconds ago    1.01 mb[root@kong rootfs]#

运行并使用

使用docker run发现此镜像所启动的容器并无异常之处

[root@kong rootfs]# docker run --rm -it busyboxbase sh/ # hostnameb7f9e9646746/ # uname -alinux b7f9e9646746 3.10.0-693.el7.x86_64 #1 smp tue aug 22 21:09:27 utc 2017 x86_64 gnu/linux/ #

感谢各位的阅读,以上就是"busybox怎么使用"的内容了,经过本文的学习后,相信大家对busybox怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0