toolbox - 随身携带您自己的(调试)工具

2019年10月22日 | Thorsten Kukuk | 无许可

简介

我们的容器宿主机操作系统 openSUSE MicroOS 和我们的 Kubernetes 平台 openSUSE Kubic 都使用 事务更新 来应用系统补丁。这意味着使用了只读根文件系统。虽然这具有很大的优势,例如可以安全地自动更新集群,但也存在一个缺点:您需要重新启动才能激活新安装的软件包。但是,如果您想调试一个问题,而您需要的工具未安装怎么办?谁能保证重启后问题仍然可以调试?

为此,我们现在引入了 toolbox 工具,以及一个 toolbox 容器

toolbox - 自带工具

toolbox 是一个小型脚本,它启动一个特权容器,让您可以在这种系统中引入您最喜欢的调试或管理工具。如果容器不包含您需要的工具,您可以使用 zypper 安装任何您想要的软件。toolbox 是有状态的,如果您退出脚本并在稍后再次启动它,环境将与您离开时完全相同。要重置它,需要删除容器镜像:podman rm toolbox-<user>。根文件系统位于 /media/root

用法

$ /usr/bin/toolbox
Spawning a container 'toolbox-root' with image 'registry.opensuse.org/opensuse/toolbox'
51e475f05d8bb8a5bf110bbecd960383bf8cfade1569587edef92076215f0eba
toolbox-root
Container started successfully. To exit, type 'exit'.
toolbox:/ # ls -alF /media/root
...
toolbox:/ # tcpdump -i ens3
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens3, link-type EN10MB (Ethernet), capture size 65535 bytes
...
toolbox:/ # zypper in vim
Loading repository data...
Reading installed packages...
Resolving package dependencies...

The following 5 NEW packages are going to be installed:
  libgdbm6 libgdbm_compat4 perl vim vim-data-common

5 new packages to install.
Overall download size: 9.0 MiB. Already cached: 0 B. After the operation,
additional 49.4 MiB will be used.
Continue? [y/n/v/...? shows all options] (y):
...
toolbox:/ # vi /media/root/etc/passwd

高级用法

使用自定义镜像

toolbox 默认使用一个基于 openSUSE 的用户空间环境,称为 opensuse/toolbox,但可以更改为任何容器镜像。只需在 $HOME/.toolboxrc 中覆盖环境变量即可,其中每个条目都是可选的

toolbox 配置文件
# cat ~/.toolboxrc
REGISTRY=registry.opensuse.org
IMAGE=opensuse/toolbox:latest
TOOLBOX_NAME=special-debug-container
TOOLBOX_SHELL="/bin/bash"

普通用户作为 root 容器

普通用户调用的 toolbox 也会启动 toolbox 容器,但无法修改根文件系统。使用 sudo 运行 toolbox 的缺点是使用了 root 的 .toolboxrc 而不是用户的 .toolboxrc。要以 root 权限运行 toolbox 容器,必须使用 toolbox --root

登录时自动进入 toolbox

将其中一个用户的 /etc/passwd 条目设置为 /usr/bin/toolbox

useradd bob -m -s /usr/bin/toolbox

现在,当以该用户通过 SSH 登录系统时,toolbox 将自动启动

# ssh bob@hostname.example.com
Last login: Thu Oct  3 16:52:16 2019 from 192.168.107.1
.toolboxrc file detected, overriding defaults...
Container 'toolbox-bob' already exists. Trying to start...
(To remove the container and start with a fresh toolbox, run: podman rm 'toolbox-bob')
toolbox-bob
Container started successfully. To exit, type 'exit'.
toolbox:/ #

类别: 博客

标签

分享这篇文章