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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
liblxc "gopkg.in/lxc/go-lxc.v2"
"github.com/spf13/cobra"
)
type cmdForkconsole struct {
global *cmdGlobal
}
func (c *cmdForkconsole) command() *cobra.Command {
// Main subcommand
cmd := &cobra.Command{}
cmd.Use = "forkconsole <container name> <containers path> <config> <tty> <escape>"
cmd.Short = "Attach to the console of a container"
cmd.Long = `Description:
Attach to the console of a container
This internal command is used to attach to one of the container's tty devices.
`
cmd.RunE = c.run
cmd.Hidden = true
return cmd
}
func (c *cmdForkconsole) run(cmd *cobra.Command, args []string) error {
// Quick checks.
if len(args) != 5 {
_ = cmd.Help()
if len(args) == 0 {
return nil
}
return errors.New("Missing required arguments")
}
// Only root should run this
if os.Geteuid() != 0 {
return errors.New("This must be run as root")
}
name := args[0]
lxcpath := args[1]
configPath := args[2]
ttyNum := strings.TrimPrefix(args[3], "tty=")
tty, err := strconv.Atoi(ttyNum)
if err != nil {
return fmt.Errorf("Failed to retrieve tty number: %q", err)
}
escapeNum := strings.TrimPrefix(args[4], "escape=")
escape, err := strconv.Atoi(escapeNum)
if err != nil {
return fmt.Errorf("Failed to retrieve escape character: %q", err)
}
d, err := liblxc.NewContainer(name, lxcpath)
if err != nil {
return fmt.Errorf("Error initializing container: %q", err)
}
err = d.LoadConfigFile(configPath)
if err != nil {
return fmt.Errorf("Error opening config file: %q", err)
}
opts := liblxc.ConsoleOptions{}
opts.Tty = tty
opts.StdinFd = uintptr(os.Stdin.Fd())
opts.StdoutFd = uintptr(os.Stdout.Fd())
opts.StderrFd = uintptr(os.Stderr.Fd())
opts.EscapeCharacter = rune(escape)
err = d.Console(opts)
if err != nil {
return fmt.Errorf("Failed running forkconsole: %q", err)
}
return nil
}
|