...

Source file src/github.com/opencontainers/runc/contrib/cmd/sd-helper/helper.go

Documentation: github.com/opencontainers/runc/contrib/cmd/sd-helper

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"github.com/opencontainers/runc/libcontainer/cgroups"
    11  	"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
    12  	"github.com/opencontainers/runc/libcontainer/configs"
    13  )
    14  
    15  func usage() {
    16  	fmt.Print(`Open Container Initiative contrib/cmd/sd-helper
    17  
    18  sd-helper is a tool that uses runc/libcontainer/cgroups/systemd package
    19  functionality to communicate to systemd in order to perform various operations.
    20  Currently this is limited to starting and stopping systemd transient slice
    21  units.
    22  
    23  Usage:
    24  	sd-helper [-debug] [-parent <pname>] {start|stop} <name>
    25  
    26  Example:
    27  	sd-helper -parent system.slice start system-pod123.slice
    28  `)
    29  	os.Exit(1)
    30  }
    31  
    32  var (
    33  	debug  = flag.Bool("debug", false, "enable debug output")
    34  	parent = flag.String("parent", "", "parent unit name")
    35  )
    36  
    37  func main() {
    38  	if !systemd.IsRunningSystemd() {
    39  		logrus.Fatal("systemd is required")
    40  	}
    41  
    42  	// Set the flags.
    43  	flag.Parse()
    44  	if *debug {
    45  		logrus.SetLevel(logrus.DebugLevel)
    46  	}
    47  	if flag.NArg() != 2 {
    48  		usage()
    49  	}
    50  
    51  	cmd := flag.Arg(0)
    52  	unit := flag.Arg(1)
    53  
    54  	err := unitCommand(cmd, unit, *parent)
    55  	if err != nil {
    56  		logrus.Fatal(err)
    57  	}
    58  }
    59  
    60  func newManager(config *configs.Cgroup) (cgroups.Manager, error) {
    61  	if cgroups.IsCgroup2UnifiedMode() {
    62  		return systemd.NewUnifiedManager(config, "")
    63  	}
    64  	return systemd.NewLegacyManager(config, nil)
    65  }
    66  
    67  func unitCommand(cmd, name, parent string) error {
    68  	podConfig := &configs.Cgroup{
    69  		Name:      name,
    70  		Parent:    parent,
    71  		Resources: &configs.Resources{},
    72  	}
    73  	pm, err := newManager(podConfig)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	switch cmd {
    79  	case "start":
    80  		return pm.Apply(-1)
    81  	case "stop":
    82  		return pm.Destroy()
    83  	}
    84  
    85  	return fmt.Errorf("unknown command: %s", cmd)
    86  }
    87  

View as plain text