Browse Source

syslinux.go can create raw or qcow2 images

Signed-off-by: Graham MacDonald <grahamamacdonald@gmail.com>
Graham MacDonald 5 years ago
parent
commit
0b125fb3a8
5 changed files with 99 additions and 47 deletions
  1. 4 0
      rc/bin/cpurc
  2. 1 1
      util/GO9PIMG
  3. 2 2
      util/build-img.sh
  4. 28 3
      util/img/prepfs
  5. 64 41
      util/syslinux.go

+ 4 - 0
rc/bin/cpurc

@@ -83,5 +83,9 @@ if(test -e /cfg/$sysname/cpustart)
 echo 'key proto=p9sk1 dom=harvey user=harvey !password=harvey' >/mnt/factotum/ctl
 aux/listen1 -t tcp!*!6666 /bin/cpu -R &
 
+if(test -e /env/onstartup) {
+	eval $onstartup
+}
+
 # echo `{date} $sysname >>/sys/log/boot
 exit ''

+ 1 - 1
util/GO9PIMG

@@ -37,7 +37,7 @@ hostfwd=tcp::1666-:1666,\
 hostfwd=tcp::5356-:5356,\
 hostfwd=tcp::17013-:17013 \
 -object filter-dump,id=f1,netdev=user.0,file=/tmp/vm0.pcap \
--drive id=disk,file=$HARVEY/harvey.qcow2,if=none \
+-drive id=disk,file=$HARVEY/util/harvey.qcow2,if=none \
 -device ich9-ahci,id=ahci \
 -device ide-drive,drive=disk,bus=ahci.0 \
 $*

+ 2 - 2
util/build-img.sh

@@ -13,14 +13,14 @@ echo "Creating harvey image $DEST"
 rm -f $DEST
 
 guestfish <<EOF
-disk-create $DEST qcow2 1G
+disk-create $DEST qcow2 3G
 add-drive $DEST
 
 launch
 
 part-init /dev/sda mbr
 part-add /dev/sda p 2048 104447
-part-add /dev/sda p 104448 1992704
+part-add /dev/sda p 104448 6291455
 part-set-mbr-id /dev/sda 2 57
 mke2fs /dev/sda1
 part-set-bootable /dev/sda 1 true

+ 28 - 3
util/img/prepfs

@@ -1,13 +1,17 @@
 #!/bin/rc
 venti=127.0.0.1
 
+echo prepfs: prep
 disk/prep -w -a^(nvram fossil arenas bloom isect) /dev/sdE0/plan9
 
+echo prepfs: venti
 venti/fmtarenas arenas0 /dev/sdE0/arenas
 venti/fmtisect isect0 /dev/sdE0/isect
 venti/fmtindex /util/img/venti.conf
 venti/conf -w /dev/sdE0/arenas < /util/img/venti.conf
 venti/venti -c /dev/sdE0/arenas
+
+echo prepfs: fossil
 fossil/flfmt /dev/sdE0/fossil
 fossil/conf -w /dev/sdE0/fossil < /util/img/fossil.conf
 fossil/fossil -f /dev/sdE0/fossil
@@ -15,6 +19,27 @@ fossil/fossil -f /dev/sdE0/fossil
 mount -c /srv/fossil /n/fossil
 
 cd /n/fossil
-mkdir x
-cd x
-dircp /root/amd64 .
+
+echo prepfs: cp cfg
+mkdir -p cfg/$sysname
+dircp /cfg/example cfg/$sysname
+
+echo prepfs: cp amd64
+mkdir amd64
+dircp /root/amd64 amd64
+echo prepfs: cp lib
+mkdir lib
+dircp /root/lib lib
+echo prepfs: cp rc
+mkdir rc
+dircp /root/rc rc
+echo prepfs: cp adm
+mkdir adm
+dircp /root/adm adm
+echo prepfs: cp sys/lib
+mkdir -p sys/lib
+dircp /sys/lib sys/lib
+echo prepfs: cp sys/man
+mkdir sys/man
+dircp /sys/man sys/man
+mkdir sys/log

+ 64 - 41
util/syslinux.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"bytes"
+	"flag"
 	"io"
 	"io/ioutil"
 	"log"
@@ -11,63 +12,72 @@ import (
 )
 
 var (
-	setupcmds = []struct {
-		*exec.Cmd
-		in io.Reader
-	}{
-		{Cmd: exec.Command("qemu-img", "create", "-f", "qcow2", "harvey.qcow2", "3G")},
-		{Cmd: exec.Command("modprobe", "nbd", "max_part=63")},
-		{Cmd: exec.Command("qemu-nbd", "-c", "/dev/nbd0", "harvey.qcow2")},
-		{Cmd: exec.Command("dd", "conv=notrunc", "bs=440", "count=1", "if=img/syslinux-bios/mbr.bin", "of=/dev/nbd0")},
-		{Cmd: exec.Command("sfdisk", "/dev/nbd0"), in: bytes.NewBufferString(part)},
-		{Cmd: exec.Command("mkfs.ext4", "/dev/nbd0p1")},
-		{Cmd: exec.Command("mount", "/dev/nbd0p1", "disk/")},
-		{Cmd: exec.Command("extlinux", "-i", "disk/")},
-		{Cmd: exec.Command("cp", "../sys/src/9/amd64/harvey.32bit", "disk/harvey")},
-		{Cmd: exec.Command("ls", "-l", "disk")},
-	}
 	part = `label: dos
 label-id: 0xadc9f7b1
 device: /dev/nbd0
 unit: sectors
 
-/dev/nbd0p1 : start=        2048, size=     6289408, type=83, bootable
+/dev/nbd0p1 : start=        2048, size=      102400, type=83, bootable
+/dev/nbd0p2 : start=      104448, size=     6187008, type=39
 `
-	syslinuxconfig = []byte(`# syslinux.cfg
-#TIMEOUT = Time to wait to autoboot in 1/10 secs. zero (0) disables the timeout 
-#PROMPT = one (1) displays the prompt only, zero (0) will not display the prompt
-#ONTIMEOUT = The default menu label to automatically boot at timeout (selected after timeout)
-#DISPLAY = Optional menu text if not using LABEL Entries
-
-TIMEOUT 1000
+	syslinuxconfig = []byte(`
+TIMEOUT 50
 DEFAULT vesamenu.c32
-#MENU BACKGROUND /backgroundimages/your_image_here.png
+#DEFAULT 0
 MENU TITLE Harvey OS
 PROMPT 0
-ONTIMEOUT 1
-#DISPLAY Menu.txt
-
-#Display help text when F1 pressed on Syslinux Menu
-F1 help.txt
+ONTIMEOUT 0
 
 LABEL 0
-MENU LABEL Reboot
 TEXT HELP
-Restart the computer
+Boot Harvey kernel with local filesystem
 ENDTEXT
-COM32 reboot.c32
+MENU LABEL Harvey OS (Local FS)
+KERNEL mboot.c32
+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
 
 LABEL 1
 TEXT HELP
-Boot Harvey kernel
+Boot Harvey kernel with remote filesystem (10.0.2.2)
 ENDTEXT
-MENU LABEL Harvey OS
+MENU LABEL Harvey OS (Remote FS)
 KERNEL mboot.c32
-APPEND ../harvey
+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
 
+LABEL 2
+TEXT HELP
+Install harvey on image
+ENDTEXT
+MENU LABEL Harvey OS (Install)
+KERNEL mboot.c32
+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
 `)
 )
 
+type command struct {
+	*exec.Cmd
+	in io.Reader
+}
+
+func gencmds(imgType string) []command {
+	imgFilename := "harvey." + imgType
+	return []command{
+		{Cmd: exec.Command("qemu-img", "create", "-f", imgType, imgFilename, "3G")},
+		{Cmd: exec.Command("chown", os.ExpandEnv("$SUDO_USER"), imgFilename)},
+		{Cmd: exec.Command("chgrp", os.ExpandEnv("$SUDO_USER"), imgFilename)},
+		{Cmd: exec.Command("modprobe", "nbd", "max_part=63")},
+		{Cmd: exec.Command("qemu-nbd", "-c", "/dev/nbd0", "-f", imgType, imgFilename)},
+		{Cmd: exec.Command("dd", "conv=notrunc", "bs=440", "count=1", "if=img/syslinux-bios/mbr.bin", "of=/dev/nbd0")},
+		{Cmd: exec.Command("sfdisk", "/dev/nbd0"), in: bytes.NewBufferString(part)},
+		{Cmd: exec.Command("mkfs.ext4", "/dev/nbd0p1")},
+		{Cmd: exec.Command("mkdir", "-p", "disk/")},
+		{Cmd: exec.Command("mount", "/dev/nbd0p1", "disk/")},
+		{Cmd: exec.Command("extlinux", "-i", "disk/")},
+		{Cmd: exec.Command("cp", "../sys/src/9/amd64/harvey.32bit", "disk/harvey")},
+		{Cmd: exec.Command("ls", "-l", "disk")},
+	}
+}
+
 func detach() {
 	log.Println("Detaching")
 	if err := exec.Command("umount", "disk").Run(); err != nil {
@@ -79,23 +89,33 @@ func detach() {
 }
 
 func main() {
+	imgType := flag.String("imgtype", "raw", "Type of image to create (raw, qcow2)")
+	flag.Parse()
+	if *imgType != "qcow2" && *imgType != "raw" {
+		log.Println("Invalid imgtype")
+		return
+	}
 
 	if os.Getuid() != 0 {
-		log.Fatalf("You have to run me as root")
+		log.Println("You have to run me as root")
+		return
 	}
 
 	defer detach()
 
+	setupcmds := gencmds(*imgType)
 	for _, c := range setupcmds {
 		c.Stdin, c.Stdout, c.Stderr = c.in, os.Stdout, os.Stderr
 		if err := c.Run(); err != nil {
-			log.Fatalf("%v: %v", c, err)
+			log.Printf("%v: %v", c.Cmd, err)
+			return
 		}
 	}
 
 	fi, err := ioutil.ReadDir("img/syslinux-bios/syslinux")
 	if err != nil {
-		log.Fatal(err)
+		log.Print(err)
+		return
 	}
 	if err := os.Mkdir("disk/syslinux", 0777); err != nil {
 		log.Print(err)
@@ -104,13 +124,16 @@ func main() {
 		log.Printf("Process %v", f)
 		b, err := ioutil.ReadFile(filepath.Join("img/syslinux-bios/syslinux", f.Name()))
 		if err != nil {
-			log.Fatal(err)
+			log.Print(err)
+			return
 		}
 		if err := ioutil.WriteFile(filepath.Join("disk/syslinux/", f.Name()), b, 0666); err != nil {
-			log.Fatal(err)
+			log.Print(err)
+			return
 		}
 	}
 	if err := ioutil.WriteFile(filepath.Join("disk/syslinux/", "syslinux.cfg"), syslinuxconfig, 0666); err != nil {
-		log.Fatal(err)
+		log.Print(err)
+		return
 	}
 }