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/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. cfg := config.LoadConfig()
  40. d, _, err := util.Blkid("RANCHER_BOOT")
  41. if err != nil {
  42. log.Errorf("Failed to run blkid: %s", err)
  43. }
  44. if d != "" {
  45. partition = d
  46. baseName = filepath.Join(baseName, BootDir)
  47. } else {
  48. if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" {
  49. // try the rancher.state.dev setting
  50. partition = dev
  51. } else {
  52. d, _, err := util.Blkid("RANCHER_STATE")
  53. if err != nil {
  54. log.Errorf("Failed to run blkid: %s", err)
  55. }
  56. if d != "" {
  57. partition = d
  58. }
  59. }
  60. }
  61. cmd := exec.Command("lsblk", "-no", "pkname", partition)
  62. log.Debugf("Run(%v)", cmd)
  63. cmd.Stderr = os.Stderr
  64. // TODO: out can == "" - this is used to "detect software RAID" which is terrible
  65. if out, err := cmd.Output(); err == nil {
  66. device = "/dev/" + strings.TrimSpace(string(out))
  67. }
  68. }
  69. os.MkdirAll(baseName, 0755)
  70. cmd := exec.Command("mount", partition, baseName)
  71. //cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
  72. log.Debugf("mountdevice return2 -> d: %s, p: %s", device, partition)
  73. return device, partition, cmd.Run()
  74. }