|  | 
|  | 1 | +package containerd | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"log/slog" | 
|  | 6 | +	"os" | 
|  | 7 | +	"os/exec" | 
|  | 8 | +) | 
|  | 9 | + | 
|  | 10 | +// UsesSystemd checks if the system is using systemd | 
|  | 11 | +// by running the "systemctl is-system-running" command. | 
|  | 12 | +// NOTE: this limits support to systems using systemctl to manage systemd | 
|  | 13 | +func UsesSystemd() bool { | 
|  | 14 | +	// Check if is a systemd system | 
|  | 15 | +	cmd := nsenterCmd("systemctl", "is-system-running", "--quiet") | 
|  | 16 | +	if err := cmd.Run(); err != nil { | 
|  | 17 | +		slog.Info("Error with systemctl: %w\n", "error", err) | 
|  | 18 | +		return false | 
|  | 19 | +	} | 
|  | 20 | +	return true | 
|  | 21 | +} | 
|  | 22 | + | 
|  | 23 | +// InstallDbus checks if D-Bus service is installed and active. If not, installs D-Bus | 
|  | 24 | +// and starts the service. | 
|  | 25 | +// NOTE: this limits support to systems using systemctl to manage systemd. | 
|  | 26 | +func InstallDbus() error { | 
|  | 27 | +	cmd := nsenterCmd("systemctl", "is-active", "dbus", "--quiet") | 
|  | 28 | +	if err := cmd.Run(); err == nil { | 
|  | 29 | +		slog.Info("D-Bus is already installed and running") | 
|  | 30 | +		return nil | 
|  | 31 | +	} | 
|  | 32 | +	slog.Info("Installing D-Bus") | 
|  | 33 | +	whichApt := nsenterCmd("which", "apt-get") | 
|  | 34 | +	whichYum := nsenterCmd("which", "yum") | 
|  | 35 | +	whichDnf := nsenterCmd("which", "dnf") | 
|  | 36 | +	whichApk := nsenterCmd("which", "apk") | 
|  | 37 | +	if err := whichApt.Run(); err == nil { | 
|  | 38 | +		cmd = nsenterCmd("apt-get", "update", "--yes") | 
|  | 39 | +		if err := cmd.Run(); err != nil { | 
|  | 40 | +			return fmt.Errorf("failed to update apt: %w", err) | 
|  | 41 | +		} | 
|  | 42 | +		cmd = nsenterCmd("apt-get", "install", "--yes", "dbus") | 
|  | 43 | +		if err := cmd.Run(); err != nil { | 
|  | 44 | +			return fmt.Errorf("failed to install D-Bus with apt: %w", err) | 
|  | 45 | +		} | 
|  | 46 | +	} else if err = whichDnf.Run(); err == nil { | 
|  | 47 | +		cmd = nsenterCmd("dnf", "install", "--yes", "dbus") | 
|  | 48 | +		if err := cmd.Run(); err != nil { | 
|  | 49 | +			return fmt.Errorf("failed to install D-Bus with dnf: %w", err) | 
|  | 50 | +		} | 
|  | 51 | +	} else if err = whichApk.Run(); err == nil { | 
|  | 52 | +		cmd = nsenterCmd("apk", "add", "dbus") | 
|  | 53 | +		if err := cmd.Run(); err != nil { | 
|  | 54 | +			return fmt.Errorf("failed to install D-Bus with apk: %w", err) | 
|  | 55 | +		} | 
|  | 56 | +	} else if err = whichYum.Run(); err == nil { | 
|  | 57 | +		cmd = nsenterCmd("yum", "install", "--yes", "dbus") | 
|  | 58 | +		if err := cmd.Run(); err != nil { | 
|  | 59 | +			return fmt.Errorf("failed to install D-Bus with yum: %w", err) | 
|  | 60 | +		} | 
|  | 61 | +	} else { | 
|  | 62 | +		slog.Info("WARNING: Could not install D-Bus. No supported package manager found.") | 
|  | 63 | +		return nil | 
|  | 64 | +	} | 
|  | 65 | + | 
|  | 66 | +	slog.Info("Restarting D-Bus") | 
|  | 67 | +	cmd = nsenterCmd("systemctl", "restart", "dbus") | 
|  | 68 | +	if err := cmd.Run(); err != nil { | 
|  | 69 | +		return fmt.Errorf("failed to restart D-Bus: %w", err) | 
|  | 70 | +	} | 
|  | 71 | + | 
|  | 72 | +	return nil | 
|  | 73 | +} | 
|  | 74 | + | 
|  | 75 | +func nsenterCmd(cmd ...string) *exec.Cmd { | 
|  | 76 | +	return exec.Command("nsenter", | 
|  | 77 | +		append([]string{fmt.Sprintf("-m/%s/proc/1/ns/mnt", os.Getenv("HOST_ROOT")), "--"}, cmd...)...) // #nosec G204 | 
|  | 78 | +} | 
0 commit comments