install.go 2.2 KB

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