grub.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package install
  2. import (
  3. "html/template"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/rancher/os/log"
  8. )
  9. func RunGrub(baseName, device string) error {
  10. log.Debugf("installGrub")
  11. //grub-install --boot-directory=${baseName}/boot ${device}
  12. cmd := exec.Command("grub-install", "--boot-directory="+baseName+"/boot", device)
  13. if err := cmd.Run(); err != nil {
  14. log.Errorf("%s", err)
  15. return err
  16. }
  17. return nil
  18. }
  19. func grubConfig(menu BootVars) error {
  20. log.Debugf("grubConfig")
  21. filetmpl, err := template.New("grub2config").Parse(`{{define "grub2menu"}}menuentry "{{.Name}}" {
  22. set root=(hd0,msdos1)
  23. linux /{{.bootDir}}vmlinuz-{{.Version}}-rancheros {{.KernelArgs}} {{.Append}}
  24. initrd /{{.bootDir}}initrd-{{.Version}}-rancheros
  25. }
  26. {{end}}
  27. set default="0"
  28. set timeout="{{.Timeout}}"
  29. {{if .Fallback}}set fallback={{.Fallback}}{{end}}
  30. {{- range .Entries}}
  31. {{template "grub2menu" .}}
  32. {{- end}}
  33. `)
  34. if err != nil {
  35. log.Errorf("grub2config %s", err)
  36. return err
  37. }
  38. cfgFile := filepath.Join(menu.BaseName, menu.BootDir+"grub/grub.cfg")
  39. log.Debugf("grubConfig written to %s", cfgFile)
  40. f, err := os.Create(cfgFile)
  41. if err != nil {
  42. return err
  43. }
  44. err = filetmpl.Execute(f, menu)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func PvGrubConfig(menu BootVars) error {
  51. log.Debugf("pvGrubConfig")
  52. filetmpl, err := template.New("grublst").Parse(`{{define "grubmenu"}}
  53. title RancherOS {{.Version}}-({{.Name}})
  54. root (hd0)
  55. kernel /${bootDir}vmlinuz-{{.Version}}-rancheros {{.KernelArgs}} {{.Append}}
  56. initrd /${bootDir}initrd-{{.Version}}-rancheros
  57. {{end}}
  58. default 0
  59. timeout {{.Timeout}}
  60. {{if .Fallback}}fallback {{.Fallback}}{{end}}
  61. hiddenmenu
  62. {{- range .Entries}}
  63. {{template "grubmenu" .}}
  64. {{- end}}
  65. `)
  66. if err != nil {
  67. log.Errorf("pv grublst: %s", err)
  68. return err
  69. }
  70. cfgFile := filepath.Join(menu.BaseName, menu.BootDir+"grub/menu.lst")
  71. log.Debugf("grubMenu written to %s", cfgFile)
  72. f, err := os.Create(cfgFile)
  73. if err != nil {
  74. log.Errorf("Create(%s) %s", cfgFile, err)
  75. return err
  76. }
  77. err = filetmpl.Execute(f, menu)
  78. if err != nil {
  79. log.Errorf("execute %s", err)
  80. return err
  81. }
  82. return nil
  83. }