Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use busybox

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use busybox". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use busybox.

What is a basic image (base image)

To put it simply, a basic image is an image built without a from or a dockerfile that begins with from scratch. For example, alpine, this very small linux image is only about 4m.

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

Its dockerfile is very simple, with only three lines, which is a basic image.

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

Busybox

Summary description

Busybox is called the Swiss Army knife of embedded linux. This sentence was put forward when busybox introduced himself (the swiss army knife of embedded linux). Busybox integrates a lot of common functions under small unix into a small executable file, to put it simply, the functions commonly used in unix or linux can be found here, but for the goal of busybox: embedded linux, size is a very important optimization factor and limitation for busybox, these functions may be castrated, but they are enough for general needs. And alpine is based on busybox to add its own package management tool apk and other functions to create a popular small image. Busybox is an open source project based on gpl developed in C language. The current stable version is 1.28.4.

Host machine

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

Download busybox

The latest version of the binary that can be used directly is 1.28.1.

[root@kong] # wget https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64--2018-05-25 04 wget root@kong 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% [= >] 1001112 19.3kb/s in 30s 2018-05-25 04:51:57 (32.4 kb/s) -' busybox-x86_64' saved [1001112 amp1001112] [root@kong ~] #

Set 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 ~] #

Version confirmation

Type busybox to see the version and the tools under the familiar linux. If you take a closer look, you will understand that it is no exaggeration for busybox to claim to be a Swiss Army knife. On the other hand, it may cause criticism if it dares to claim to be the busybox in linux.

[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, zcat [zcat]

Create rootfs

This picture has been seen countless times when the basic image is introduced. Rootfs is an important concept in linux, and there is also a sentence like add rootfs.tar.xz in alpine. Next we will look at how to generate a simple rootfs.

Create a directory and enter the

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

Create rootfs

Execute the following statement

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

Execution log

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

Result confirmed

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

Copy busybox to the newly created. / bin directory

In this way, the link object for the above command exists

[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] #

Create rootfs.tar

Note the relative path here, while the subsequent secondary relative path expands to / to create a new system's rootfs, which is also one of the important operations of building linux (linux from scratch) from scratch.

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

Brief description: busybox-list-modules lists all the modules of busybox, and then creates a small rootfs based on that.

[root@kong rootfs] # busybox-list-modules | wc-l

three hundred and eighty nine

[root@kong rootfs] #

Prepare dockerfile

Prepare an one-line dockerfile

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

Create a base image, but no specific image was found, so no specific image was created. In addition, this article for the convenience of demonstration, directly create dockerfile here, this is not a good idea, in practice, please do not do this, if there are 100g files in the current directory, it will be extremely slow, and non-standard, irrelevant items need to be cleared.

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

From scracth

There is from. Can this scratch be pulled? the current version already has it as a reserved name.

[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] #

Adding this dockerfile to a line has no practical effect, let's see what the scratch is.

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

After building, it is found that a 0-byte image file is generated, which is also similar to the original intention of scratch.

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

So far, we understand that there is really no additional addition to from scratch, so let's add the following two sentences like alpine

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

Our dockerfile is almost the same three lines.

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

This creates a 1m busybox-based mirror

[root@kong rootfs] # docker build-t busyboxbase:latest. Sending build context to docker daemon 2.415 mbstep 1 pick 3: from scratch-- > step 2 mb 3: add rootfs.tar /-- > 0fbb0c8c7579removing intermediate container 8311e96f456cstep 3 prime 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] #

Run and use

Use docker run to find that the container started by this image is not abnormal

[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 x8664 gnu/linux/ # Thank you for reading. This is the content of "how to use busybox". After the study of this article, I believe you have a deeper understanding of how to use busybox. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report