From 9770d4408faed953da50b73d6f985c94faa0fa30 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 14 Jul 2018 23:37:12 +0200 Subject: [PATCH] move root device finding function to internal/rootdev --- mount.go | 4 +++- reboot_amd64.go | 5 +++-- status.go | 3 ++- update.go | 32 ++++++++------------------------ 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/mount.go b/mount.go index 944ee30..1140eae 100644 --- a/mount.go +++ b/mount.go @@ -6,6 +6,8 @@ import ( "log" "os" "syscall" + + "github.com/gokrazy/internal/rootdev" ) // mountCompat deals with old FAT root file systems, to cover the case where @@ -92,7 +94,7 @@ func mountfs() error { } } - dev := mustFindRootDevice() + "4" + dev := rootdev.MustFind() + "4" if err := syscall.Mount(dev, "/perm", "ext4", 0, ""); err != nil { log.Printf("Could not mount permanent storage partition %s: %v", dev, err) } diff --git a/reboot_amd64.go b/reboot_amd64.go index f6cc644..bdcf246 100644 --- a/reboot_amd64.go +++ b/reboot_amd64.go @@ -8,6 +8,7 @@ import ( "syscall" "unsafe" + "github.com/gokrazy/internal/rootdev" "golang.org/x/sys/unix" ) @@ -23,7 +24,7 @@ func kexecReboot() error { return err } - if err := syscall.Mount(mustFindRootDevice()+"1", tmpdir, "vfat", 0, ""); err != nil { + if err := syscall.Mount(rootdev.MustFind()+"1", tmpdir, "vfat", 0, ""); err != nil { return err } @@ -36,7 +37,7 @@ func kexecReboot() error { if err != nil { return err } - rep := rootRe.ReplaceAllLiteral(cmdline, []byte("root="+mustFindRootDevice()+inactiveRootPartition)) + rep := rootRe.ReplaceAllLiteral(cmdline, []byte("root="+rootdev.MustFind()+inactiveRootPartition)) // NUL-terminate cmdline cmdlinebuf := make([]byte, len(rep)+1) copy(cmdlinebuf, rep) diff --git a/status.go b/status.go index 928e2a2..99b4c00 100644 --- a/status.go +++ b/status.go @@ -15,6 +15,7 @@ import ( "time" "github.com/gokrazy/gokrazy/internal/bundled" + "github.com/gokrazy/internal/rootdev" "golang.org/x/sys/unix" ) @@ -160,7 +161,7 @@ func initStatus(services []*service) { Hostname string }{ Services: services, - PermDev: mustFindRootDevice() + "4", + PermDev: rootdev.MustFind() + "4", PermUsed: int64(st.Bsize) * int64(st.Blocks-st.Bfree), PermAvail: int64(st.Bsize) * int64(st.Bavail), PermTotal: int64(st.Bsize) * int64(st.Blocks), diff --git a/update.go b/update.go index 35fb484..78a5841 100644 --- a/update.go +++ b/update.go @@ -17,33 +17,17 @@ import ( "golang.org/x/sys/unix" "github.com/gokrazy/internal/fat" + "github.com/gokrazy/internal/rootdev" ) var ( - rootRe = regexp.MustCompile(`root=/dev/(?:mmcblk0p|sda)([2-3])`) - rootDeviceRe = regexp.MustCompile(`root=(/dev/(?:mmcblk0p|sda))`) + rootRe = regexp.MustCompile(`root=/dev/(?:mmcblk0p|sda)([2-3])`) inactiveRootPartition string ) -// mustFindRootDevice returns the device from which gokrazy was booted. It is -// safe to append a partition number to the resulting string. mustFindRootDevice -// works once /proc is mounted. -func mustFindRootDevice() string { - cmdline, err := ioutil.ReadFile("/proc/cmdline") - if err != nil { - panic(err) - } - - matches := rootDeviceRe.FindStringSubmatch(string(cmdline)) - if len(matches) != 2 { - panic(fmt.Sprintf("mustFindRootDevice: kernel command line %q did not match %v", string(cmdline), rootRe)) - } - return matches[1] -} - func switchRootPartition(newRootPartition string) error { - f, err := os.OpenFile(mustFindRootDevice()+"1", os.O_RDWR, 0600) + f, err := os.OpenFile(rootdev.MustFind()+"1", os.O_RDWR, 0600) if err != nil { return err } @@ -67,7 +51,7 @@ func switchRootPartition(newRootPartition string) error { return err } - rep := rootRe.ReplaceAllLiteral(b, []byte("root="+mustFindRootDevice()+newRootPartition)) + rep := rootRe.ReplaceAllLiteral(b, []byte("root="+rootdev.MustFind()+newRootPartition)) if _, err := f.Write(rep); err != nil { return err } @@ -151,13 +135,13 @@ func initUpdate() error { return fmt.Errorf("root partition %q (from %q) is unexpectedly neither 2 nor 3", rootPartition, matches[0]) } - http.HandleFunc("/update/boot", nonConcurrentUpdateHandler(mustFindRootDevice()+"1")) - http.HandleFunc("/update/mbr", nonConcurrentUpdateHandler(strings.TrimSuffix(mustFindRootDevice(), "p"))) - http.HandleFunc("/update/root", nonConcurrentUpdateHandler(mustFindRootDevice()+inactiveRootPartition)) + http.HandleFunc("/update/boot", nonConcurrentUpdateHandler(rootdev.MustFind()+"1")) + http.HandleFunc("/update/mbr", nonConcurrentUpdateHandler(strings.TrimSuffix(rootdev.MustFind(), "p"))) + http.HandleFunc("/update/root", nonConcurrentUpdateHandler(rootdev.MustFind()+inactiveRootPartition)) http.HandleFunc("/update/switch", nonConcurrentSwitchHandler(inactiveRootPartition)) // bakery updates only the boot partition, which would reset the active root // partition to 2. - updateHandler := nonConcurrentUpdateHandler(mustFindRootDevice() + "1") + updateHandler := nonConcurrentUpdateHandler(rootdev.MustFind() + "1") http.HandleFunc("/update/bootonly", func(w http.ResponseWriter, r *http.Request) { updateHandler(w, r) if err := switchRootPartition(rootPartition); err != nil {