Skip to content

Commit 373921d

Browse files
Add SetUnitProperties on a unit
Allows cgroup settings to be applied directly
1 parent 3207a34 commit 373921d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

dbus/methods.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,18 @@ func (c *Conn) GetUnitProperties(unit string) (map[string]interface{}, error) {
171171
// Valid values for unitType: Service, Socket, Target, Device, Mount, Automount, Snapshot, Timer, Swap, Path, Slice, Scope
172172
// return "dbus.Error: Unknown interface" if the unitType is not the correct type of the unit
173173
func (c *Conn) GetUnitTypeProperties(unit string, unitType string) (map[string]interface{}, error) {
174-
return c.getProperties(unit, "org.freedesktop.systemd1." + unitType)
174+
return c.getProperties(unit, "org.freedesktop.systemd1."+unitType)
175+
}
176+
177+
// SetUnitProperties() may be used to modify certain unit properties at runtime.
178+
// Not all properties may be changed at runtime, but many resource management
179+
// settings (primarily those in systemd.cgroup(5)) may. The changes are applied
180+
// instantly, and stored on disk for future boots, unless runtime is true, in which
181+
// case the settings only apply until the next reboot. name is the name of the unit
182+
// to modify. properties are the settings to set, encoded as an array of property
183+
// name and value pairs.
184+
func (c *Conn) SetUnitProperties(name string, runtime bool, properties ...Property) error {
185+
return c.sysobj.Call("SetUnitProperties", 0, name, runtime, properties).Store()
175186
}
176187

177188
// ListUnits returns an array with all currently loaded units. Note that

dbus/methods_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package dbus
1818

1919
import (
2020
"fmt"
21+
"github.com/guelfey/go.dbus"
2122
"math/rand"
2223
"os"
2324
"path/filepath"
@@ -159,6 +160,29 @@ func TestGetUnitPropertiesRejectsInvalidName(t *testing.T) {
159160
}
160161
}
161162

163+
// TestSetUnitProperties changes a cgroup setting on the `tmp.mount`
164+
// which should exist on all systemd systems and ensures that the
165+
// property was set.
166+
func TestSetUnitProperties(t *testing.T) {
167+
conn := setupConn(t)
168+
169+
unit := "tmp.mount"
170+
171+
if err := conn.SetUnitProperties(unit, true, Property{"CPUShares", dbus.MakeVariant(uint64(1023))}); err != nil {
172+
t.Fatal(err)
173+
}
174+
175+
info, err := conn.GetUnitTypeProperties(unit, "Mount")
176+
if err != nil {
177+
t.Fatal(err)
178+
}
179+
180+
value := info["CPUShares"].(uint64)
181+
if value != 1023 {
182+
t.Fatal("CPUShares of unit is not 1023, %s", value)
183+
}
184+
}
185+
162186
// Ensure that basic transient unit starting and stopping works.
163187
func TestStartStopTransientUnit(t *testing.T) {
164188
conn := setupConn(t)

0 commit comments

Comments
 (0)