@@ -3,20 +3,8 @@ package containerd
33import (
44 "fmt"
55 "log/slog"
6- "os"
7- "os/exec"
86)
97
10- // UsesSystemd checks if the system is using systemd.
11- func UsesSystemd () bool {
12- cmd := nsenterCmd ("systemctl" , "list-units" , "|" , "grep" , "-q" , "containerd.service" )
13- if err := cmd .Run (); err != nil {
14- slog .Info ("Error with systemctl: %w\n " , "error" , err )
15- return false
16- }
17- return true
18- }
19-
208// InstallDbus checks if D-Bus service is installed and active. If not, installs D-Bus
219// and starts the service.
2210// NOTE: this limits support to systems using systemctl to manage systemd.
@@ -31,18 +19,24 @@ func InstallDbus() error {
3119 type pkgManager struct {
3220 name string
3321 check []string
22+ update []string
3423 install []string
3524 }
3625
3726 managers := []pkgManager {
38- {"apt-get" , []string {"which" , "apt-get" }, []string {"apt-get" , "update" , "--yes" , "&&" , "apt-get" , "install" , "--yes" , "dbus" }},
39- {"dnf" , []string {"which" , "dnf" }, []string {"dnf" , "install" , "--yes" , "dbus" }},
40- {"apk" , []string {"which" , "apk" }, []string {"apk" , "add" , "dbus" }},
41- {"yum" , []string {"which" , "yum" }, []string {"yum" , "install" , "--yes" , "dbus" }},
27+ {"apt-get" , []string {"which" , "apt-get" }, []string {"apt-get" , "update" , "--yes" }, [] string { "apt-get" , "install" , "--yes" , "dbus" }},
28+ {"dnf" , []string {"which" , "dnf" }, []string {}, [] string { "dnf" , "install" , "--yes" , "dbus" }},
29+ {"apk" , []string {"which" , "apk" }, []string {}, [] string { "apk" , "add" , "dbus" }},
30+ {"yum" , []string {"which" , "yum" }, []string {}, [] string { "yum" , "install" , "--yes" , "dbus" }},
4231 }
4332 installed := false
4433 for _ , mgr := range managers {
4534 if err := nsenterCmd (mgr .check ... ).Run (); err == nil {
35+ if len (mgr .update ) != 0 {
36+ if err := nsenterCmd (mgr .update ... ).Run (); err != nil {
37+ return fmt .Errorf ("failed to update package manager %s: %w" , mgr .name , err )
38+ }
39+ }
4640 if err := nsenterCmd (mgr .install ... ).Run (); err != nil {
4741 return fmt .Errorf ("failed to install D-Bus with %s: %w" , mgr .name , err )
4842 }
@@ -63,8 +57,3 @@ func InstallDbus() error {
6357
6458 return nil
6559}
66-
67- func nsenterCmd (cmd ... string ) * exec.Cmd {
68- return exec .Command ("nsenter" ,
69- append ([]string {fmt .Sprintf ("-m/%s/proc/1/ns/mnt" , os .Getenv ("HOST_ROOT" )), "--" }, cmd ... )... ) // #nosec G204
70- }
0 commit comments