syslinux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. )
  12. var (
  13. part = `label: dos
  14. label-id: 0xadc9f7b1
  15. device: /dev/nbd0
  16. unit: sectors
  17. /dev/nbd0p1 : start= 2048, size= 102400, type=83, bootable
  18. /dev/nbd0p2 : start= 104448, size= 6187008, type=39
  19. `
  20. syslinuxconfig = []byte(`
  21. TIMEOUT 50
  22. DEFAULT vesamenu.c32
  23. #DEFAULT 0
  24. MENU TITLE Harvey OS
  25. PROMPT 0
  26. ONTIMEOUT 0
  27. LABEL 0
  28. TEXT HELP
  29. Boot Harvey kernel with local filesystem
  30. ENDTEXT
  31. MENU LABEL Harvey OS (Local FS)
  32. KERNEL mboot.c32
  33. APPEND ../harvey service=cpu nobootprompt=local!#S/sdE0/ maxcores=1024 nvram=/boot/nvram nvrlen=512 nvroff=0 acpiirq=1 mouseport=ps2 vgasize=1024x768x24 monitor=vesa
  34. LABEL 1
  35. TEXT HELP
  36. Boot Harvey kernel with remote filesystem (10.0.2.2)
  37. ENDTEXT
  38. MENU LABEL Harvey OS (Remote FS)
  39. KERNEL mboot.c32
  40. APPEND ../harvey service=cpu nobootprompt=tcp maxcores=1024 fs=10.0.2.2 auth=10.0.2.2 nvram=/boot/nvram nvrlen=512 nvroff=0 acpiirq=1 mouseport=ps2 vgasize=1024x768x24 monitor=vesa
  41. LABEL 2
  42. TEXT HELP
  43. Install harvey on image
  44. ENDTEXT
  45. MENU LABEL Harvey OS (Install)
  46. KERNEL mboot.c32
  47. APPEND ../harvey service=cpu nobootprompt=tcp maxcores=1024 fs=10.0.2.2 auth=10.0.2.2 nvram=/boot/nvram nvrlen=512 nvroff=0 acpiirq=1 mouseport=ps2 vgasize=1024x768x24 monitor=vesa onstartup=/util/img/prepfs
  48. `)
  49. )
  50. type command struct {
  51. *exec.Cmd
  52. in io.Reader
  53. }
  54. func gencmds(imgType string) []command {
  55. imgFilename := "harvey." + imgType
  56. return []command{
  57. {Cmd: exec.Command("qemu-img", "create", "-f", imgType, imgFilename, "3G")},
  58. {Cmd: exec.Command("chown", os.ExpandEnv("$SUDO_USER"), imgFilename)},
  59. {Cmd: exec.Command("chgrp", os.ExpandEnv("$SUDO_USER"), imgFilename)},
  60. {Cmd: exec.Command("modprobe", "nbd", "max_part=63")},
  61. {Cmd: exec.Command("qemu-nbd", "-c", "/dev/nbd0", "-f", imgType, imgFilename)},
  62. {Cmd: exec.Command("dd", "conv=notrunc", "bs=440", "count=1", "if=img/syslinux-bios/mbr.bin", "of=/dev/nbd0")},
  63. {Cmd: exec.Command("sfdisk", "/dev/nbd0"), in: bytes.NewBufferString(part)},
  64. {Cmd: exec.Command("mkfs.ext4", "/dev/nbd0p1")},
  65. {Cmd: exec.Command("mkdir", "-p", "disk/")},
  66. {Cmd: exec.Command("mount", "/dev/nbd0p1", "disk/")},
  67. {Cmd: exec.Command("extlinux", "-i", "disk/")},
  68. {Cmd: exec.Command("cp", "../sys/src/9/amd64/harvey.32bit", "disk/harvey")},
  69. {Cmd: exec.Command("ls", "-l", "disk")},
  70. }
  71. }
  72. func detach() {
  73. log.Println("Detaching")
  74. if err := exec.Command("umount", "disk").Run(); err != nil {
  75. log.Print(err)
  76. }
  77. if err := exec.Command("qemu-nbd", "-d", "/dev/nbd0").Run(); err != nil {
  78. log.Print(err)
  79. }
  80. }
  81. func main() {
  82. imgType := flag.String("imgtype", "raw", "Type of image to create (raw, qcow2)")
  83. flag.Parse()
  84. if *imgType != "qcow2" && *imgType != "raw" {
  85. log.Println("Invalid imgtype")
  86. return
  87. }
  88. if os.Getuid() != 0 {
  89. log.Println("You have to run me as root")
  90. return
  91. }
  92. defer detach()
  93. setupcmds := gencmds(*imgType)
  94. for _, c := range setupcmds {
  95. c.Stdin, c.Stdout, c.Stderr = c.in, os.Stdout, os.Stderr
  96. if err := c.Run(); err != nil {
  97. log.Printf("%v: %v", c.Cmd, err)
  98. return
  99. }
  100. }
  101. fi, err := ioutil.ReadDir("img/syslinux-bios/syslinux")
  102. if err != nil {
  103. log.Print(err)
  104. return
  105. }
  106. if err := os.Mkdir("disk/syslinux", 0777); err != nil {
  107. log.Print(err)
  108. }
  109. for _, f := range fi {
  110. log.Printf("Process %v", f)
  111. b, err := ioutil.ReadFile(filepath.Join("img/syslinux-bios/syslinux", f.Name()))
  112. if err != nil {
  113. log.Print(err)
  114. return
  115. }
  116. if err := ioutil.WriteFile(filepath.Join("disk/syslinux/", f.Name()), b, 0666); err != nil {
  117. log.Print(err)
  118. return
  119. }
  120. }
  121. if err := ioutil.WriteFile(filepath.Join("disk/syslinux/", "syslinux.cfg"), syslinuxconfig, 0666); err != nil {
  122. log.Print(err)
  123. return
  124. }
  125. }