1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package startunit
import (
"context"
"fmt"
"strconv"
"strings"
sd "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5"
)
func Props(argv []string, extra map[string]string) ([]sd.Property, error) {
flag := extra["X-PropExecStartBool"] == "true"
delete(extra, "X-PropExecStartBool")
props := []sd.Property{
sd.PropDescription("swproof stand-in VM " + strings.Join(argv, " ")),
sd.PropExecStart(argv, flag),
{Name: "Type", Value: dbus.MakeVariant("exec")},
}
for k, v := range extra {
switch k {
case "Restart", "KillMode", "OOMPolicy", "ManagedOOMPreference", "CollectMode", "ManagedOOMMemoryPressure":
props = append(props, sd.Property{Name: k, Value: dbus.MakeVariant(v)})
case "MemoryMax", "MemorySwapMax", "TasksMax", "CPUQuotaPerSecUSec", "IOWeight", "MemoryHigh":
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
props = append(props, sd.Property{Name: k, Value: dbus.MakeVariant(n)})
case "AllowedCPUs":
n, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
mask := make([]byte, n/8+1)
mask[n/8] |= 1 << (n % 8)
props = append(props, sd.Property{Name: k, Value: dbus.MakeVariant(mask)})
default:
return nil, fmt.Errorf("unsupported property %s", k)
}
}
return props, nil
}
func Start(ctx context.Context, c *sd.Conn, unit string, argv []string, extra map[string]string) (string, error) {
props, err := Props(argv, extra)
if err != nil {
return "", err
}
ch := make(chan string, 1)
if _, err := c.StartTransientUnitContext(ctx, unit, "fail", props, ch); err != nil {
return "", err
}
select {
case r := <-ch:
return r, nil
case <-ctx.Done():
return "job-signal-timeout", ctx.Err()
}
}