Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created August 19, 2015 21:08
Show Gist options
  • Save mrunalp/a2a52392a0bead0e5319 to your computer and use it in GitHub Desktop.
Save mrunalp/a2a52392a0bead0e5319 to your computer and use it in GitHub Desktop.
Use dbus to register a container as a machine so it shows up in machine ctl
package main
import (
"encoding/hex"
"flag"
"log"
"github.com/godbus/dbus"
)
var conn *dbus.Conn
// RegisterMachine with systemd on the host system
func RegisterMachine(name string, id string, pid int, root_directory string) error {
var (
av []byte
err error
)
if conn == nil {
conn, err = dbus.SystemBus()
if err != nil {
return (err)
}
}
av, err = hex.DecodeString(id[0:32])
if err != nil {
return err
}
obj := conn.Object("org.freedesktop.machine1", "/org/freedesktop/machine1")
return obj.Call("org.freedesktop.machine1.Manager.RegisterMachine", 0, name, av, "docker.service", "container", uint32(pid), root_directory).Err
}
func main() {
pid := flag.Int("pid", -1, "pid of the machine")
rootDir := flag.String("root", "", "root directory of the machine")
name := flag.String("name", "", "name of the machine")
id := flag.String("id", "", "id of the machine")
flag.Parse()
if *pid == -1 {
log.Fatalf("PID should be specified")
}
if *rootDir == "" || *name == "" || *id == "" {
log.Fatalf("invalid argument")
}
if err := RegisterMachine(*name, *id, *pid, *rootDir); err != nil {
log.Fatalf("Register machine failed: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment