console_init.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package control
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "regexp"
  10. "strings"
  11. "syscall"
  12. "github.com/codegangsta/cli"
  13. "github.com/rancher/os/cmd/cloudinitexecute"
  14. "github.com/rancher/os/config"
  15. "github.com/rancher/os/log"
  16. "github.com/rancher/os/util"
  17. )
  18. const (
  19. consoleDone = "/run/console-done"
  20. dockerHome = "/home/docker"
  21. gettyCmd = "/sbin/agetty"
  22. rancherHome = "/home/rancher"
  23. startScript = "/opt/rancher/bin/start.sh"
  24. )
  25. type symlink struct {
  26. oldname, newname string
  27. }
  28. func ConsoleInitMain() {
  29. if err := consoleInitFunc(); err != nil {
  30. log.Fatal(err)
  31. }
  32. }
  33. func consoleInitAction(c *cli.Context) error {
  34. return consoleInitFunc()
  35. }
  36. func createHomeDir(homedir string, uid, gid int) {
  37. if _, err := os.Stat(homedir); os.IsNotExist(err) {
  38. if err := os.MkdirAll(homedir, 0755); err != nil {
  39. log.Error(err)
  40. }
  41. if err := os.Chown(homedir, uid, gid); err != nil {
  42. log.Error(err)
  43. }
  44. }
  45. }
  46. func consoleInitFunc() error {
  47. cfg := config.LoadConfig()
  48. // Now that we're booted, stop writing debug messages to the console
  49. cmd := exec.Command("sudo", "dmesg", "--console-off")
  50. if err := cmd.Run(); err != nil {
  51. log.Error(err)
  52. }
  53. createHomeDir(rancherHome, 1100, 1100)
  54. createHomeDir(dockerHome, 1101, 1101)
  55. password := config.GetCmdline("rancher.password")
  56. if password != "" {
  57. cmd := exec.Command("chpasswd")
  58. cmd.Stdin = strings.NewReader(fmt.Sprint("rancher:", password))
  59. if err := cmd.Run(); err != nil {
  60. log.Error(err)
  61. }
  62. cmd = exec.Command("bash", "-c", `sed -E -i 's/(rancher:.*:).*(:.*:.*:.*:.*:.*:.*)$/\1\2/' /etc/shadow`)
  63. if err := cmd.Run(); err != nil {
  64. log.Error(err)
  65. }
  66. }
  67. if err := setupSSH(cfg); err != nil {
  68. log.Error(err)
  69. }
  70. if err := writeRespawn(); err != nil {
  71. log.Error(err)
  72. }
  73. if err := modifySshdConfig(); err != nil {
  74. log.Error(err)
  75. }
  76. for _, link := range []symlink{
  77. {"/var/lib/rancher/engine/docker", "/usr/bin/docker"},
  78. {"/var/lib/rancher/engine/docker-init", "/usr/bin/docker-init"},
  79. {"/var/lib/rancher/engine/docker-containerd", "/usr/bin/docker-containerd"},
  80. {"/var/lib/rancher/engine/docker-containerd-ctr", "/usr/bin/docker-containerd-ctr"},
  81. {"/var/lib/rancher/engine/docker-containerd-shim", "/usr/bin/docker-containerd-shim"},
  82. {"/var/lib/rancher/engine/dockerd", "/usr/bin/dockerd"},
  83. {"/var/lib/rancher/engine/docker-proxy", "/usr/bin/docker-proxy"},
  84. {"/var/lib/rancher/engine/docker-runc", "/usr/bin/docker-runc"},
  85. {"/usr/share/ros/os-release", "/usr/lib/os-release"},
  86. {"/usr/share/ros/os-release", "/etc/os-release"},
  87. } {
  88. syscall.Unlink(link.newname)
  89. if err := os.Symlink(link.oldname, link.newname); err != nil {
  90. log.Error(err)
  91. }
  92. }
  93. // font backslashes need to be escaped for when issue is output! (but not the others..)
  94. if err := ioutil.WriteFile("/etc/issue", []byte(`
  95. , , ______ _ _____ _____TM
  96. ,------------|'------'| | ___ \\ | | / _ / ___|
  97. / . '-' |- | |_/ /__ _ _ __ ___| |__ ___ _ __ | | | \\ '--.
  98. \\/| | | | // _' | '_ \\ / __| '_ \\ / _ \\ '__' | | | |'--. \\
  99. | .________.'----' | |\\ \\ (_| | | | | (__| | | | __/ | | \\_/ /\\__/ /
  100. | | | | \\_| \\_\\__,_|_| |_|\\___|_| |_|\\___|_| \\___/\\____/
  101. \\___/ \\___/ \s \r
  102. RancherOS `+config.Version+` \n \l
  103. `), 0644); err != nil {
  104. log.Error(err)
  105. }
  106. cmd = exec.Command("bash", "-c", `echo $(/sbin/ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3}') >> /etc/issue`)
  107. if err := cmd.Run(); err != nil {
  108. log.Error(err)
  109. }
  110. cloudinitexecute.ApplyConsole(cfg)
  111. if err := util.RunScript(config.CloudConfigScriptFile); err != nil {
  112. log.Error(err)
  113. }
  114. if err := util.RunScript(startScript); err != nil {
  115. log.Error(err)
  116. }
  117. if err := ioutil.WriteFile(consoleDone, []byte(cfg.Rancher.Console), 0644); err != nil {
  118. log.Error(err)
  119. }
  120. if err := util.RunScript("/etc/rc.local"); err != nil {
  121. log.Error(err)
  122. }
  123. os.Setenv("TERM", "linux")
  124. respawnBinPath, err := exec.LookPath("respawn")
  125. if err != nil {
  126. return err
  127. }
  128. return syscall.Exec(respawnBinPath, []string{"respawn", "-f", "/etc/respawn.conf"}, os.Environ())
  129. }
  130. func generateRespawnConf(cmdline string) string {
  131. var respawnConf bytes.Buffer
  132. for i := 1; i < 7; i++ {
  133. tty := fmt.Sprintf("tty%d", i)
  134. respawnConf.WriteString(gettyCmd)
  135. if strings.Contains(cmdline, fmt.Sprintf("rancher.autologin=%s", tty)) {
  136. respawnConf.WriteString(" --autologin rancher")
  137. }
  138. respawnConf.WriteString(fmt.Sprintf(" 115200 %s\n", tty))
  139. }
  140. for _, tty := range []string{"ttyS0", "ttyS1", "ttyS2", "ttyS3", "ttyAMA0"} {
  141. if !strings.Contains(cmdline, fmt.Sprintf("console=%s", tty)) {
  142. continue
  143. }
  144. respawnConf.WriteString(gettyCmd)
  145. if strings.Contains(cmdline, fmt.Sprintf("rancher.autologin=%s", tty)) {
  146. respawnConf.WriteString(" --autologin rancher")
  147. }
  148. respawnConf.WriteString(fmt.Sprintf(" 115200 %s\n", tty))
  149. }
  150. respawnConf.WriteString("/usr/sbin/sshd -D")
  151. return respawnConf.String()
  152. }
  153. func writeRespawn() error {
  154. cmdline, err := ioutil.ReadFile("/proc/cmdline")
  155. if err != nil {
  156. return err
  157. }
  158. respawn := generateRespawnConf(string(cmdline))
  159. files, err := ioutil.ReadDir("/etc/respawn.conf.d")
  160. if err == nil {
  161. for _, f := range files {
  162. p := path.Join("/etc/respawn.conf.d", f.Name())
  163. content, err := ioutil.ReadFile(p)
  164. if err != nil {
  165. log.Errorf("Failed to read %s: %v", p, err)
  166. continue
  167. }
  168. respawn += fmt.Sprintf("\n%s", string(content))
  169. }
  170. } else if !os.IsNotExist(err) {
  171. log.Error(err)
  172. }
  173. return ioutil.WriteFile("/etc/respawn.conf", []byte(respawn), 0644)
  174. }
  175. func modifySshdConfig() error {
  176. sshdConfig, err := ioutil.ReadFile("/etc/ssh/sshd_config")
  177. if err != nil {
  178. return err
  179. }
  180. sshdConfigString := string(sshdConfig)
  181. for _, item := range []string{
  182. "UseDNS no",
  183. "PermitRootLogin no",
  184. "ServerKeyBits 2048",
  185. "AllowGroups docker",
  186. } {
  187. match, err := regexp.Match("^"+item, sshdConfig)
  188. if err != nil {
  189. return err
  190. }
  191. if !match {
  192. sshdConfigString += fmt.Sprintf("%s\n", item)
  193. }
  194. }
  195. return ioutil.WriteFile("/etc/ssh/sshd_config", []byte(sshdConfigString), 0644)
  196. }
  197. func setupSSH(cfg *config.CloudConfig) error {
  198. for _, keyType := range []string{"rsa", "dsa", "ecdsa", "ed25519"} {
  199. outputFile := fmt.Sprintf("/etc/ssh/ssh_host_%s_key", keyType)
  200. outputFilePub := fmt.Sprintf("/etc/ssh/ssh_host_%s_key.pub", keyType)
  201. if _, err := os.Stat(outputFile); err == nil {
  202. continue
  203. }
  204. saved, savedExists := cfg.Rancher.SSH.Keys[keyType]
  205. pub, pubExists := cfg.Rancher.SSH.Keys[keyType+"-pub"]
  206. if savedExists && pubExists {
  207. // TODO check permissions
  208. if err := util.WriteFileAtomic(outputFile, []byte(saved), 0600); err != nil {
  209. return err
  210. }
  211. if err := util.WriteFileAtomic(outputFilePub, []byte(pub), 0600); err != nil {
  212. return err
  213. }
  214. continue
  215. }
  216. cmd := exec.Command("bash", "-c", fmt.Sprintf("ssh-keygen -f %s -N '' -t %s", outputFile, keyType))
  217. if err := cmd.Run(); err != nil {
  218. return err
  219. }
  220. savedBytes, err := ioutil.ReadFile(outputFile)
  221. if err != nil {
  222. return err
  223. }
  224. pubBytes, err := ioutil.ReadFile(outputFilePub)
  225. if err != nil {
  226. return err
  227. }
  228. config.Set(fmt.Sprintf("rancher.ssh.keys.%s", keyType), string(savedBytes))
  229. config.Set(fmt.Sprintf("rancher.ssh.keys.%s-pub", keyType), string(pubBytes))
  230. }
  231. return os.MkdirAll("/var/run/sshd", 0644)
  232. }