From 45e422d01ecce34756b8ab0430c7cfb7dd9021ca Mon Sep 17 00:00:00 2001 From: Sven Dowideit Date: Tue, 20 Jun 2017 21:48:14 +1000 Subject: [PATCH] Add an autologin that allows us to fix the stty Signed-off-by: Sven Dowideit --- cmd/control/autologin.go | 72 +++++++++++++++++++++++++++++++++++++ cmd/control/console_init.go | 19 ++++------ cmd/control/entrypoint.go | 1 + config/config.go | 12 +++++++ init/init.go | 4 +-- init/recovery.go | 11 +++--- main.go | 1 + 7 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 cmd/control/autologin.go diff --git a/cmd/control/autologin.go b/cmd/control/autologin.go new file mode 100644 index 00000000..2254a1e4 --- /dev/null +++ b/cmd/control/autologin.go @@ -0,0 +1,72 @@ +package control + +import ( + "fmt" + "os" + "os/exec" + "runtime" + "strings" + + "github.com/codegangsta/cli" + "github.com/rancher/os/config" + "github.com/rancher/os/log" +) + +func AutologinMain() { + log.InitLogger() + app := cli.NewApp() + + app.Name = os.Args[0] + app.Usage = "autologin console" + app.Version = config.Version + app.Author = "Rancher Labs, Inc." + app.Email = "sven@rancher.com" + app.EnableBashCompletion = true + app.Action = autologinAction + app.HideHelp = true + app.Run(os.Args) +} + + +func autologinAction(c *cli.Context) error { + cmd := exec.Command("/bin/stty", "sane") + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + if err := cmd.Run(); err != nil { + log.Error(err) + } + + usertty := "" + user := "root" + tty := "" + if c.NArg() > 0 { + usertty = c.Args().Get(0) + s := strings.SplitN(usertty, ":", 2) + user = s[0] + if len(s) > 1 { + tty = s[1] + } + } + cfg := config.LoadConfig() + // replace \n and \l + banner := config.Banner + banner = strings.Replace(banner, "\\v", config.Version, -1) + banner = strings.Replace(banner, "\\s", "RancherOS " + runtime.GOARCH, -1) + banner = strings.Replace(banner, "\\r", "4.9....", -1) + banner = strings.Replace(banner, "\\n", cfg.Hostname, -1) + banner = strings.Replace(banner, "\\l", tty, -1) + banner = strings.Replace(banner, "\\\\", "\\", -1) + banner = banner + "\n" + fmt.Printf(banner) + + cmd = exec.Command("/usr/bin/login", "-f", user) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + if err := cmd.Run(); err != nil { + log.Error(err) + } + return nil +} + diff --git a/cmd/control/console_init.go b/cmd/control/console_init.go index 660bd5d4..d2d8fa74 100644 --- a/cmd/control/console_init.go +++ b/cmd/control/console_init.go @@ -109,17 +109,7 @@ func consoleInitFunc() error { } // font backslashes need to be escaped for when issue is output! (but not the others..) - if err := ioutil.WriteFile("/etc/issue", []byte(` - , , ______ _ _____ _____TM - ,------------|'------'| | ___ \\ | | / _ / ___| - / . '-' |- | |_/ /__ _ _ __ ___| |__ ___ _ __ | | | \\ '--. - \\/| | | | // _' | '_ \\ / __| '_ \\ / _ \\ '__' | | | |'--. \\ - | .________.'----' | |\\ \\ (_| | | | | (__| | | | __/ | | \\_/ /\\__/ / - | | | | \\_| \\_\\__,_|_| |_|\\___|_| |_|\\___|_| \\___/\\____/ - \\___/ \\___/ \s \r - - RancherOS `+config.Version+` \n \l `+cpuid.CPU.HypervisorName+` - `), 0644); err != nil { + if err := ioutil.WriteFile("/etc/issue", []byte(config.Banner), 0644); err != nil { log.Error(err) } @@ -163,7 +153,8 @@ func generateRespawnConf(cmdline, user string, sshd bool) string { respawnConf.WriteString(gettyCmd) if strings.Contains(cmdline, fmt.Sprintf("rancher.autologin=%s", tty)) { - respawnConf.WriteString(fmt.Sprintf(" --autologin %s", user)) + respawnConf.WriteString(fmt.Sprintf(" -n -l /usr/bin/autologin -o %s:tty%d", user, i)) + //respawnConf.WriteString(fmt.Sprintf(" --autologin %s", user)) } respawnConf.WriteString(fmt.Sprintf(" --noclear %s linux\n", tty)) } @@ -175,7 +166,9 @@ func generateRespawnConf(cmdline, user string, sshd bool) string { respawnConf.WriteString(gettyCmd) if strings.Contains(cmdline, fmt.Sprintf("rancher.autologin=%s", tty)) { - respawnConf.WriteString(fmt.Sprintf(" --autologin %s", user)) + //respawnConf.WriteString(fmt.Sprintf(" -n -l /usr/bin/autologin -o rancher")) + respawnConf.WriteString(fmt.Sprintf(" -n -l /usr/bin/autologin -o %s:%s", user, tty)) + //respawnConf.WriteString(fmt.Sprintf(" --autologin %s", user)) } respawnConf.WriteString(fmt.Sprintf(" %s\n", tty)) } diff --git a/cmd/control/entrypoint.go b/cmd/control/entrypoint.go index fa23b49f..c5324b10 100644 --- a/cmd/control/entrypoint.go +++ b/cmd/control/entrypoint.go @@ -79,6 +79,7 @@ func writeFiles(cfg *config.CloudConfig) error { func setupCommandSymlinks() { for _, link := range []symlink{ + {config.RosBin, "/usr/bin/autologin"}, {config.RosBin, "/usr/bin/cloud-init-execute"}, {config.RosBin, "/usr/bin/cloud-init-save"}, {config.RosBin, "/usr/bin/dockerlaunch"}, diff --git a/config/config.go b/config/config.go index 259e7f0b..99f54552 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,18 @@ import ( "github.com/rancher/os/util" ) +const Banner = ` + , , ______ _ _____ _____TM + ,------------|'------'| | ___ \\ | | / _ / ___| + / . '-' |- | |_/ /__ _ _ __ ___| |__ ___ _ __ | | | \\ '--. + \\/| | | | // _' | '_ \\ / __| '_ \\ / _ \\ '__' | | | |'--. \\ + | .________.'----' | |\\ \\ (_| | | | | (__| | | | __/ | | \\_/ /\\__/ / + | | | | \\_| \\_\\__,_|_| |_|\\___|_| |_|\\___|_| \\___/\\____/ + \\___/ \\___/ \s \r + + RancherOS \v \n \l + ` + func Merge(bytes []byte) error { data, err := readConfigs(bytes, false, true) if err != nil { diff --git a/init/init.go b/init/init.go index c9139f90..29d1040d 100755 --- a/init/init.go +++ b/init/init.go @@ -263,12 +263,12 @@ func RunInit() error { return cfg, nil }}, config.CfgFuncData{"load modules", loadModules}, - config.CfgFuncData("recovery console", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { + config.CfgFuncData{"recovery console", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { if cfg.Rancher.Recovery { recovery(nil) } return cfg, nil - }, + }}, config.CfgFuncData{"b2d env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { if dev := util.ResolveDevice("LABEL=B2D_STATE"); dev != "" { boot2DockerEnvironment = true diff --git a/init/recovery.go b/init/recovery.go index eb6e4ff5..d4a447fe 100644 --- a/init/recovery.go +++ b/init/recovery.go @@ -6,6 +6,7 @@ import ( "github.com/docker/libcompose/yaml" "github.com/rancher/os/compose" "github.com/rancher/os/config" + "github.com/rancher/os/netconf" ) var ( @@ -55,8 +56,8 @@ func recovery(initFailure error) { var recoveryConfig config.CloudConfig recoveryConfig.Rancher.Defaults = config.Defaults{ - Network: config.NetworkConfig{ - DNS: config.DNSConfig{ + Network: netconf.NetworkConfig{ + DNS: netconf.DNSConfig{ Nameservers: []string{ "8.8.8.8", "8.8.4.4", @@ -82,8 +83,10 @@ func recovery(initFailure error) { } _, err = config.ChainCfgFuncs(&recoveryConfig, - loadImages, - recoveryServices) + []config.CfgFuncData{ + config.CfgFuncData{"loadImages", loadImages}, + config.CfgFuncData{"recovery console", recoveryServices}, + }) if err != nil { log.Fatal(err) } diff --git a/main.go b/main.go index 0e24e386..68a3e715 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( ) var entrypoints = map[string]func(){ + "autologin": control.AutologinMain, "cloud-init-execute": cloudinitexecute.Main, "cloud-init-save": cloudinitsave.Main, "console": control.ConsoleInitMain,