gokrazy/update.go
Michael Stapelberg f3445e01a9 fix switchRootPartition on non-PARTUUID installations
The first update always worked, but a subsequent update would not.

To manually switch an installation to PARTUUID, mount its boot partition and
replace the root= kernel parameter in cmdline.txt, like so:

/tmp/breakglass669384965 # mkdir boot
/tmp/breakglass669384965 # mount /dev/mmcblk0p1 boot
/tmp/breakglass669384965 # cat boot/cmdline.txt
console=ttyAMA0,115200 root=/dev/mmcblk0p2 init=/gokrazy/init elevator=deadline rootwait
/tmp/breakglass669384965 # sed -i 's,root=/dev/mmcblk0p,root=PARTUUID=471cad93-0,g' boot/cmdline.txt
/tmp/breakglass669384965 # cat boot/cmdline.txt
console=ttyAMA0,115200 root=PARTUUID=471cad93-02 init=/gokrazy/init elevator=deadline rootwait
/tmp/breakglass669384965 # umount boot
/tmp/breakglass669384965 # reboot

The PARTUUID= for your installation is printed by gokr-packer:
[…]
2020/05/01 10:05:34 write.go:366: writing MBR (LBAs: vmlinuz=51789, cmdline.txt=119561, PARTUUID=471cad93)
[…]
2020-05-01 10:06:17 +02:00

169 lines
4.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gokrazy
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/gokrazy/internal/fat"
"github.com/gokrazy/internal/rootdev"
)
var rootRe = regexp.MustCompile(`root=[^ ]+`)
func switchRootPartition(newRootPartition int) error {
f, err := os.OpenFile(rootdev.Partition(rootdev.Boot), os.O_RDWR, 0600)
if err != nil {
return err
}
defer f.Close()
rd, err := fat.NewReader(f)
if err != nil {
return err
}
offset, length, err := rd.Extents("/cmdline.txt")
if err != nil {
return err
}
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
b := make([]byte, length)
if _, err := f.Read(b); err != nil {
return err
}
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
rep := rootRe.ReplaceAllLiteral(b, []byte("root="+rootdev.PartitionCmdline(newRootPartition)))
if pad := length - int64(len(rep)); pad > 0 {
// The file content length can shrink when switching from PARTUUID= (the
// default) to /dev/mmcblk0p[23], on an older gokrazy installation.
// Because we overwrite the file in place and have no means to truncate
// it to a smaller length, we pad the command line with spaces instead.
// Note that we need to insert spaces before the trailing newline,
// otherwise the system wont boot:
rep = bytes.ReplaceAll(rep,
[]byte{'\n'},
append(bytes.Repeat([]byte{' '}, int(pad)), '\n'))
}
if _, err := f.Write(rep); err != nil {
return err
}
return f.Close()
}
func streamRequestTo(path string, r io.Reader) error {
f, err := os.OpenFile(path, os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
return f.Close()
}
func nonConcurrentUpdateHandler(dest string) func(http.ResponseWriter, *http.Request) {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "expected a PUT request", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
hash := sha256.New()
if err := streamRequestTo(dest, io.TeeReader(r.Body, hash)); err != nil {
log.Printf("updating %q failed: %v", dest, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%x", hash.Sum(nil))
}
}
func nonConcurrentSwitchHandler(newRootPartition int) func(http.ResponseWriter, *http.Request) {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
if err := switchRootPartition(newRootPartition); err != nil {
log.Printf("switching root partition to %q failed: %v", newRootPartition, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func initUpdate() error {
// The /update/features handler is used for negotiation of individual
// feature support (e.g. PARTUUID= support) between the packer and update
// target.
http.HandleFunc("/update/features", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "partuuid,")
})
http.HandleFunc("/update/mbr", nonConcurrentUpdateHandler(rootdev.BlockDevice()))
http.HandleFunc("/update/root", nonConcurrentUpdateHandler(rootdev.Partition(rootdev.InactiveRootPartition())))
http.HandleFunc("/update/switch", nonConcurrentSwitchHandler(rootdev.InactiveRootPartition()))
// bakery updates only the boot partition, which would reset the active root
// partition to 2.
updateHandler := nonConcurrentUpdateHandler(rootdev.Partition(rootdev.Boot))
http.HandleFunc("/update/boot", updateHandler)
http.HandleFunc("/update/bootonly", func(w http.ResponseWriter, r *http.Request) {
updateHandler(w, r)
if err := switchRootPartition(rootdev.ActiveRootPartition()); err != nil {
log.Printf("switching root partition to %d failed: %v", rootdev.ActiveRootPartition(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/reboot", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
go func() {
killSupervisedServices()
// give the HTTP response some time to be sent; allow processes some time to terminate
time.Sleep(1 * time.Second)
if err := syscall.Unmount("/perm", unix.MNT_FORCE); err != nil {
log.Printf("unmounting /perm failed: %v", err)
}
if err := reboot(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}()
})
return nil
}