syslinux.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package install
  2. import (
  3. "bufio"
  4. "bytes"
  5. "html/template"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/rancher/os/pkg/log"
  11. )
  12. func syslinuxConfig(menu BootVars) error {
  13. log.Debugf("syslinuxConfig")
  14. filetmpl, err := template.New("syslinuxconfig").Parse(`{{define "syslinuxmenu"}}
  15. LABEL {{.Name}}
  16. LINUX ../vmlinuz-{{.Version}}-rancheros
  17. APPEND {{.KernelArgs}} {{.Append}}
  18. INITRD ../initrd-{{.Version}}-rancheros
  19. {{end}}
  20. TIMEOUT 20 #2 seconds
  21. DEFAULT RancherOS-current
  22. {{- range .Entries}}
  23. {{template "syslinuxmenu" .}}
  24. {{- end}}
  25. `)
  26. if err != nil {
  27. log.Errorf("syslinuxconfig %s", err)
  28. return err
  29. }
  30. cfgFile := filepath.Join(menu.BaseName, menu.BootDir+"syslinux/syslinux.cfg")
  31. log.Debugf("syslinuxConfig written to %s", cfgFile)
  32. f, err := os.Create(cfgFile)
  33. if err != nil {
  34. log.Errorf("Create(%s) %s", cfgFile, err)
  35. return err
  36. }
  37. err = filetmpl.Execute(f, menu)
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func ReadGlobalCfg(globalCfg string) (string, error) {
  44. append := ""
  45. buf, err := ioutil.ReadFile(globalCfg)
  46. if err != nil {
  47. return append, err
  48. }
  49. s := bufio.NewScanner(bytes.NewReader(buf))
  50. for s.Scan() {
  51. line := strings.TrimSpace(s.Text())
  52. if strings.HasPrefix(line, "APPEND") {
  53. append = strings.TrimSpace(strings.TrimPrefix(line, "APPEND"))
  54. }
  55. }
  56. return append, nil
  57. }
  58. func ReadSyslinuxCfg(currentCfg string) (string, string, error) {
  59. vmlinuzFile := ""
  60. initrdFile := ""
  61. // Need to parse currentCfg for the lines:
  62. // KERNEL ../vmlinuz-4.9.18-rancher^M
  63. // INITRD ../initrd-41e02e6-dirty^M
  64. buf, err := ioutil.ReadFile(currentCfg)
  65. if err != nil {
  66. return vmlinuzFile, initrdFile, err
  67. }
  68. DIST := filepath.Dir(currentCfg)
  69. s := bufio.NewScanner(bytes.NewReader(buf))
  70. for s.Scan() {
  71. line := strings.TrimSpace(s.Text())
  72. if strings.HasPrefix(line, "KERNEL") {
  73. vmlinuzFile = strings.TrimSpace(strings.TrimPrefix(line, "KERNEL"))
  74. vmlinuzFile = filepath.Join(DIST, filepath.Base(vmlinuzFile))
  75. }
  76. if strings.HasPrefix(line, "INITRD") {
  77. initrdFile = strings.TrimSpace(strings.TrimPrefix(line, "INITRD"))
  78. initrdFile = filepath.Join(DIST, filepath.Base(initrdFile))
  79. }
  80. }
  81. return vmlinuzFile, initrdFile, err
  82. }