recovery.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package recovery
  2. import (
  3. "github.com/rancher/os/config"
  4. "github.com/rancher/os/pkg/compose"
  5. "github.com/rancher/os/pkg/init/docker"
  6. "github.com/rancher/os/pkg/log"
  7. "github.com/rancher/os/pkg/netconf"
  8. "github.com/rancher/os/pkg/sysinit"
  9. composeConfig "github.com/docker/libcompose/config"
  10. "github.com/docker/libcompose/yaml"
  11. )
  12. var (
  13. // TODO: move this into the os-config file so it can be customised.
  14. recoveryDockerService = composeConfig.ServiceConfigV1{
  15. Image: config.OsBase,
  16. Command: yaml.Command{
  17. "ros",
  18. "recovery-init",
  19. },
  20. Labels: map[string]string{
  21. config.DetachLabel: "false",
  22. config.ScopeLabel: "system",
  23. },
  24. LogDriver: "json-file",
  25. Net: "host",
  26. Uts: "host",
  27. Pid: "host",
  28. Ipc: "host",
  29. Privileged: true,
  30. Volumes: []string{
  31. "/dev:/host/dev",
  32. "/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt.rancher",
  33. "/lib/modules:/lib/modules",
  34. "/lib/firmware:/lib/firmware",
  35. "/usr/bin/ros:/usr/bin/ros:ro",
  36. "/usr/bin/ros:/usr/bin/cloud-init-save",
  37. "/usr/bin/ros:/usr/bin/respawn:ro",
  38. "/usr/share/ros:/usr/share/ros:ro",
  39. "/var/lib/rancher:/var/lib/rancher",
  40. "/var/lib/rancher/conf:/var/lib/rancher/conf",
  41. "/var/run:/var/run",
  42. },
  43. }
  44. )
  45. func LoadRecoveryConsole(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  46. if cfg.Rancher.Recovery {
  47. Recovery(nil)
  48. }
  49. return cfg, nil
  50. }
  51. func Recovery(initFailure error) {
  52. if initFailure != nil {
  53. log.Errorf("RancherOS has failed to boot: %v", initFailure)
  54. }
  55. log.Info("Launching recovery console")
  56. var recoveryConfig config.CloudConfig
  57. recoveryConfig.Rancher.Defaults = config.Defaults{
  58. Network: netconf.NetworkConfig{
  59. DNS: netconf.DNSConfig{
  60. Nameservers: []string{
  61. "8.8.8.8",
  62. "8.8.4.4",
  63. },
  64. },
  65. },
  66. }
  67. recoveryConfig.Rancher.BootstrapDocker = config.DockerConfig{
  68. EngineOpts: config.EngineOpts{
  69. Bridge: "none",
  70. StorageDriver: "overlay2",
  71. Restart: &[]bool{false}[0],
  72. Graph: "/var/lib/recovery-docker",
  73. Group: "root",
  74. Host: []string{"unix:///var/run/system-docker.sock"},
  75. UserlandProxy: &[]bool{false}[0],
  76. },
  77. }
  78. _, err := docker.Start(&recoveryConfig)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. _, err = config.ChainCfgFuncs(&recoveryConfig,
  83. []config.CfgFuncData{
  84. {"loadSystemImages", sysinit.LoadBootstrapImages},
  85. {"recovery console", recoveryServices},
  86. })
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. }
  91. func recoveryServices(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  92. _, err := compose.RunServiceSet("recovery", cfg, map[string]*composeConfig.ServiceConfigV1{
  93. "recovery": &recoveryDockerService,
  94. })
  95. return nil, err
  96. }