用标准的GNU/Linux命令替换Alpine上的精简版命令

发布于:2024-05-11 ⋅ 阅读:(26) ⋅ 点赞:(0)

Alpine Linux 是一个基于 musl libcbusybox 的轻量级Linux发行版,busybox 实现了很多常用类Unix命令的精简版,特点是体积很小,舍弃了很多不常用参数,我们简单对比一下标准Linux自带的 date 命令 和 Alpine下默认的 date 命令便可略知一二:

GNU/Linux 版:

root@spacex:~# date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING          display time described by STRING, not 'now'
      --debug                annotate the parsed date,
                              and warn about questionable usage to stderr
  -f, --file=DATEFILE        like --date; once for each line of DATEFILE
  -I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                               FMT='date' for date only (the default),
                               'hours', 'minutes', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14T02:34:56-06:00
  -R, --rfc-email            output date and time in RFC 5322 format.
                               Example: Mon, 14 Aug 2006 02:34:56 -0600
      --rfc-3339=FMT         output date/time in RFC 3339 format.
                               FMT='date', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14 02:34:56-06:00
  -r, --reference=FILE       display the last modification time of FILE
  -s, --set=STRING           set time described by STRING
  -u, --utc, --universal     print or set Coordinated Universal Time (UTC)
      --help     display this help and exit
      --version  output version information and exit

FORMAT controls the output.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; like %+4Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %q   quarter of year (1..4)
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric time zone (e.g., -0400)
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow '%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  +  pad with zeros, and put '+' before future years with >4 digits
  ^  use upper case if possible
  #  use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
  $ date --date='@2147483647'

Show the time on the west coast of the US (use tzselect(1) to find TZ)
  $ TZ='America/Los_Angeles' date

Show the local time for 9AM next Friday on the west coast of the US
  $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/date>
or available locally via: info '(coreutils) date invocation'
root@spacex:~#

busybox版:

在这里插入图片描述

可以看到参数少了很多,虽然大部分情况下busybox的命令都足够用了,但遇到确实不能满足我们需求的时候,比如要运行一个基于标准Linux编写的shell脚本,其中用了很多命令的高级参数,对此,Alpine Linux 也提供了替换标准Linux 命令的方法。

默认情况下,Alpine下的一些常用Linux 命令其实都是指向 busybox 的软链,包括 ls 命令本身
在这里插入图片描述
除了上述列出的 /bin 目录下的命令,还有 /sbin//usr/bin//usr/sbin/ 目录下的大多命令都是如此。

要想替换Alpine 下的 busybox 命令,我们可以执行:

apk add util-linux pciutils hwdata-pci usbutils hwdata-usb coreutils binutils findutils grep iproute2 bash bash-completion

替换完之后,这些软链接要么被替换成了具体的可执行文件,要么还是软链,但不再指向busybox了:
在这里插入图片描述

如果只想替换某个具体用到的,那就要找出该命令所在的 GNU 工具包了,然后只安装该工具包即可,从而尽量保持体积不要太过膨胀,根据我目前的经验找出某个具体命令所在的工具包大致有以下几种方法供参考:

  1. 在 Alpine apk仓库 中搜索指定命令有没有提供单独的软件包,比如 grepsed 即是此类

在这里插入图片描述
在这里插入图片描述
如果存在就好办了,直接执行 apk add xxx 即可完成替换
在这里插入图片描述

  1. 查看标准Linux命令的帮助文档,看有没有提供包信息

比如从 date --help 的帮助文档我们可以知道它在 GNU coreutils 工具包下:
在这里插入图片描述

  1. 查看busybox项目的源码结构和文件命名,busybox通常一个命令就是一个源码文件。

比如 fdisk,flock, getopt 这些命令都是由 util-linux 包提供的,如果我们只用到其中的命令,只安装 util-linux 即可
在这里插入图片描述
同理,find, grep, xargs 三个命令由 findutils 包提供
在这里插入图片描述
还可以直接尝试搜索某个命令,看是否有对应的源文件,直接就能定位到软件包:
在这里插入图片描述

  1. apk info 查看某个包的具体介绍,从而推断可能包含哪些命令

在这里插入图片描述

  1. 实在不行只能一个个的尝试,看到安装了哪个包之后,目标软件的软链被替换掉了,说明这个就是我们要找的。

  2. 没办法的办法,自己编译,因为 Alpine 使用 musl libc 而不是标准 glibc,编译会更加麻烦, 这里以编译 debian cron 为例简单演示下编译步骤

#安装编译工具链
apk add build-base abuild autoconf automake libtool

# 下载源码包,解压
wget https://salsa.debian.org/debian/cron/-/archive/updates/buster/cron-updates-buster.tar.gz

# 编译
cd cron-updates-buster && make
cc -I. -DPOSIX -DDEBUGGING=0      -c -o cron.o cron.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o database.o database.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o user.o user.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o entry.o entry.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o job.o job.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o do_command.o do_command.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o misc.o misc.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o env.o env.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o popen.o popen.c
cc -I. -DPOSIX -DDEBUGGING=0      -c -o compat.o compat.c
cc  -o cron cron.o database.o user.o entry.o job.o do_command.o misc.o env.o popen.o compat.o
cc -I. -DPOSIX -DDEBUGGING=0      -c -o crontab.o crontab.c
cc  -o crontab crontab.o misc.o entry.o env.o compat.o

# 替换busybox版本
bash-5.1# which crontab
/usr/bin/crontab
bash-5.1# ls -l /usr/bin/crontab
lrwxrwxrwx    1 root     root            12 Nov 24  2021 /usr/bin/crontab -> /bin/busybox
bash-5.1# install ./crontab /usr/bin/crontab
bash-5.1# ls -l /usr/bin/crontab
-rwxr-xr-x    1 root     root         53696 May  7 04:27 /usr/bin/crontab
bash-5.1#
bash-5.1# which crond
/usr/sbin/crond
bash-5.1#
bash-5.1# ls -l /usr/sbin/crond
lrwxrwxrwx    1 root     root            12 Nov 24  2021 /usr/sbin/crond -> /bin/busybox
bash-5.1#
bash-5.1# install ./cron /usr/sbin/cron
bash-5.1#
bash-5.1# ln -sf /usr/sbin/cron /usr/sbin/crond
bash-5.1#
bash-5.1# ls -l /usr/sbin/crond
lrwxrwxrwx    1 root     root            14 May  7 04:30 /usr/sbin/crond -> /usr/sbin/cron

# 测试cron能否正常工作,略..

网站公告

今日签到

点亮在社区的每一天
去签到