der*_*ert 7 linux d-bus systemd cgroups
我有一台运行 cgroups v2(统一)层次结构的机器,因此 systemd 负责管理所有 cgroups 并委托给 systemd 用户实例工作。我想对一组进程执行资源控制,所以我需要将它们放在一个单元中 - 大概是一个systemd scope。
通常,systemd-run会这样做 - 但不幸的是这些进程已经在运行,我不想重新启动它们。
如何从现有流程中创建systemd 范围?在对照组接口文档,告诉我这是可能的,但我一直没能找到通过命令行的方式。无论是systemctl也systemd-run似乎能够做到这一点。
有没有办法从命令行?如果重要的话,我正在运行 systemd v241。
有多种命令行工具可以进行 dbus 调用;systemd 附带了一个名为busctl. 所以你可以StartTransientUnit从命令行调用。
语法确实很烦人,但它看起来像这样(对于一个进程 ID,14460):
busctl call --user org.freedesktop.systemd1 /org/freedesktop/systemd1 \
org.freedesktop.systemd1.Manager StartTransientUnit 'ssa(sv)a(sa(sv))' \
'SCOPE-NAME.scope' fail 1 PIDs au 1 14460 0
Run Code Online (Sandbox Code Playgroud)
这是绝对不透明的(并且尝试了一些尝试以使其正确,并最终使用它dbus-monitor来查看systemd-run它是如何做到的-尽管仅在系统管理器上,systemd-run --user似乎没有通过 dbus)。所以一个解释,逐个参数:
busctl call --user # use user session dbus, not system
org.freedesktop.systemd1 # dbus service name
/org/freedesktop/systemd1 # dbus object in that service
org.freedesktop.systemd1.Manager # interface name in that service
StartTransientUnit # method we're going to call
'ssa(sv)a(sa(sv))' # signature of method, see below
'SCOPE-NAME.scope' # first argument, name of scope
fail # second argument, how to handle conflicts (see below)
1 # start of third argument, number of systemd properties for unit
PIDs # name of first property
au # data type of first property, (a)rray [aka list] of (u)nsigned integers
1 # count of array — that is, number of pids
14460 # first pid
0 # fourth argument: array size=0 (unused parameter)
Run Code Online (Sandbox Code Playgroud)
添加到命令
要将另一个 systemd 属性添加到该单元,您需要增加属性的数量并添加它。请注意,每个属性至少是三个额外的命令行参数:键、值类型和值。例如,添加 Slice 属性将来自:
… fail 1 PIDs au 1 14460 0
Run Code Online (Sandbox Code Playgroud)
到
… fail 2 PIDs au 1 14460 Slice s whatever.slice 0
^ ^^^^^ ^ ^^^^^^^^^^^^^^
count key type value
Run Code Online (Sandbox Code Playgroud)
类型“s”是字符串。它们的列表可以在D-Bus 规范的“类型系统”一章中找到
您当然可以将计数更改为 3 并添加第三个属性。等等。
与更多属性类似,但这次是隐藏在“PIDs”属性值中的计数。一个例子应该更清楚:
… fail 1 PIDs au 1 14460 0
Run Code Online (Sandbox Code Playgroud)
变成
… fail 1 PIDs au 2 14460 14461 0
^ ^^^^^
internal count second pid
Run Code Online (Sandbox Code Playgroud)
如果添加 PID 14461 和 14460。
您可以以相同的方式添加第三个、第四个等 PID。
您当然可以将附加属性与附加 pid 结合起来。请记住,pid 列表是一个属性值,因此需要保持在一起。您不能将 pid 参数与其他属性混合使用。正确的方法是改变:
… fail 1 PIDs au 1 14460 0
Run Code Online (Sandbox Code Playgroud)
到:
… fail 2 PIDs au 2 14460 14461 Slice s whatever.slice 0
Run Code Online (Sandbox Code Playgroud)
(顺序无关紧要,您可以将 Slice 块放在 PIDs 块之前)。
签名是从systemd dbus API 文档中获得的,或者更容易地,通过使用 dbus 自省:
$ busctl introspect org.freedesktop.systemd1 /org/freedesktop/systemd1 \
org.freedesktop.systemd1.Manager | grep1 StartTransientUnit
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
.StartTransientUnit method ssa(sv)a(sa(sv)) o -
Run Code Online (Sandbox Code Playgroud)
(对于grep1,请参阅https://unix.stackexchange.com/a/279518)
列出了很多方法和 dbus 属性,这里有 180 多个。所以不要省略grep.
根据systemd 文档(在“CreateUnit”下查看),有用的值为fail和replace。fail意味着如果存在冲突,您的范围将无法启动。replace意味着 systemd 将摆脱冲突的单元。请注意,这似乎仅适用于当前正在启动或计划启动的单元(它确实说“排队”)——例如,replace 不会用相同的名称替换已经运行的范围。
| 归档时间: |
|
| 查看次数: |
1142 次 |
| 最近记录: |