fsmount.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package fsmount
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/rancher/os/config"
  6. "github.com/rancher/os/pkg/init/bootstrap"
  7. "github.com/rancher/os/pkg/log"
  8. "github.com/rancher/os/pkg/util"
  9. )
  10. const (
  11. tmpfsMagic int64 = 0x01021994
  12. ramfsMagic int64 = 0x858458f6
  13. )
  14. var (
  15. ShouldSwitchRoot bool
  16. )
  17. func MountOem(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  18. if err := mountConfigured("oem", cfg.Rancher.State.OemDev, cfg.Rancher.State.OemFsType, config.OemDir); err != nil {
  19. log.Debugf("Not mounting OEM: %v", err)
  20. } else {
  21. log.Infof("Mounted OEM: %s", cfg.Rancher.State.OemDev)
  22. }
  23. return cfg, nil
  24. }
  25. func MountBoot(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  26. if IsInitrd() {
  27. return cfg, nil
  28. }
  29. if err := mountConfigured("boot", cfg.Rancher.State.BootDev, cfg.Rancher.State.BootFsType, config.BootDir); err != nil {
  30. log.Debugf("Not mounting BOOT: %v", err)
  31. } else {
  32. log.Infof("Mounted BOOT: %s", cfg.Rancher.State.BootDev)
  33. }
  34. return cfg, nil
  35. }
  36. func mountConfigured(display, dev, fsType, target string) error {
  37. var err error
  38. if dev == "" {
  39. return nil
  40. }
  41. dev = util.ResolveDevice(dev)
  42. if dev == "" {
  43. return fmt.Errorf("Could not resolve device %q", dev)
  44. }
  45. if fsType == "auto" {
  46. fsType, err = util.GetFsType(dev)
  47. }
  48. if err != nil {
  49. return err
  50. }
  51. log.Debugf("FsType has been set to %s", fsType)
  52. log.Infof("Mounting %s device %s to %s", display, dev, target)
  53. return util.Mount(dev, target, fsType, "")
  54. }
  55. func mountState(cfg *config.CloudConfig) error {
  56. return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, config.StateDir)
  57. }
  58. func tryMountState(cfg *config.CloudConfig) error {
  59. err := mountState(cfg)
  60. if err == nil {
  61. return nil
  62. }
  63. log.Infof("Skipped an error when first mounting: %v", err)
  64. // If we failed to mount lets run bootstrap and try again
  65. if err := bootstrap.Bootstrap(cfg); err != nil {
  66. return err
  67. }
  68. return mountState(cfg)
  69. }
  70. func tryMountStateAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, bool, error) {
  71. if !IsInitrd() || cfg.Rancher.State.Dev == "" {
  72. return cfg, false, nil
  73. }
  74. if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil {
  75. return cfg, false, nil
  76. } else if err != nil {
  77. return cfg, false, err
  78. }
  79. return cfg, true, nil
  80. }
  81. func IsInitrd() bool {
  82. var stat syscall.Statfs_t
  83. syscall.Statfs("/", &stat)
  84. return int64(stat.Type) == tmpfsMagic || int64(stat.Type) == ramfsMagic
  85. }
  86. func MountStateAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  87. var err error
  88. cfg, ShouldSwitchRoot, err = tryMountStateAndBootstrap(cfg)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return cfg, nil
  93. }