update: use crc32 for checksumming when the client requests it

This is significantly faster than sha256 on the Raspberry Pi 4:
update speeds increase from ≈12 MB/s to ≈55 MB/s.
This commit is contained in:
Michael Stapelberg 2020-06-26 11:03:54 +02:00
parent 28292f8e00
commit f16a76357f

View File

@ -4,6 +4,8 @@ import (
"bytes"
"crypto/sha256"
"fmt"
"hash"
"hash/crc32"
"io"
"log"
"net/http"
@ -91,7 +93,13 @@ func nonConcurrentUpdateHandler(dest string) func(http.ResponseWriter, *http.Req
mu.Lock()
defer mu.Unlock()
hash := sha256.New()
var hash hash.Hash
switch r.Header.Get("X-Gokrazy-Update-Hash") {
case "crc32":
hash = crc32.NewIEEE()
default:
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)
@ -125,7 +133,7 @@ func initUpdate() error {
// 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,")
fmt.Fprintf(w, "partuuid,updatehash,")
})
http.HandleFunc("/update/mbr", nonConcurrentUpdateHandler(rootdev.BlockDevice()))
http.HandleFunc("/update/root", nonConcurrentUpdateHandler(rootdev.Partition(rootdev.InactiveRootPartition())))