install.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package install
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/log"
  9. "github.com/rancher/os/util"
  10. )
  11. const BootDir = "boot/"
  12. type MenuEntry struct {
  13. Name, BootDir, Version, KernelArgs, Append string
  14. }
  15. type BootVars struct {
  16. BaseName, BootDir string
  17. Timeout uint
  18. Fallback int
  19. Entries []MenuEntry
  20. }
  21. func MountDevice(baseName, device, partition string, raw bool) (string, string, error) {
  22. log.Debugf("mountdevice %s, raw %v", partition, raw)
  23. if partition == "" {
  24. if raw {
  25. log.Debugf("util.Mount (raw) %s, %s", partition, baseName)
  26. cmd := exec.Command("lsblk", "-no", "pkname", partition)
  27. log.Debugf("Run(%v)", cmd)
  28. cmd.Stderr = os.Stderr
  29. device := ""
  30. // TODO: out can == "" - this is used to "detect software RAID" which is terrible
  31. if out, err := cmd.Output(); err == nil {
  32. device = "/dev/" + strings.TrimSpace(string(out))
  33. }
  34. log.Debugf("mountdevice return -> d: %s, p: %s", device, partition)
  35. return device, partition, util.Mount(partition, baseName, "", "")
  36. }
  37. //rootfs := partition
  38. // Don't use ResolveDevice - it can fail, whereas `blkid -L LABEL` works more often
  39. d, _, err := util.Blkid("RANCHER_BOOT")
  40. if err != nil {
  41. log.Errorf("Failed to run blkid: %s", err)
  42. }
  43. if d != "" {
  44. partition = d
  45. baseName = filepath.Join(baseName, BootDir)
  46. } else {
  47. partition = GetStatePartition()
  48. }
  49. cmd := exec.Command("lsblk", "-no", "pkname", partition)
  50. log.Debugf("Run(%v)", cmd)
  51. cmd.Stderr = os.Stderr
  52. // TODO: out can == "" - this is used to "detect software RAID" which is terrible
  53. if out, err := cmd.Output(); err == nil {
  54. device = "/dev/" + strings.TrimSpace(string(out))
  55. }
  56. }
  57. os.MkdirAll(baseName, 0755)
  58. cmd := exec.Command("mount", partition, baseName)
  59. //cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
  60. log.Debugf("mountdevice return2 -> d: %s, p: %s", device, partition)
  61. return device, partition, cmd.Run()
  62. }
  63. func GetStatePartition() string {
  64. cfg := config.LoadConfig()
  65. if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" {
  66. // try the rancher.state.dev setting
  67. return dev
  68. }
  69. d, _, err := util.Blkid("RANCHER_STATE")
  70. if err != nil {
  71. log.Errorf("Failed to run blkid: %s", err)
  72. }
  73. return d
  74. }