move root device finding function to internal/rootdev

This commit is contained in:
Michael Stapelberg 2018-07-14 23:37:12 +02:00
parent d8206c507c
commit 9770d4408f
4 changed files with 16 additions and 28 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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),

View File

@ -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 {