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)
[…]
This commit is contained in:
Michael Stapelberg 2020-05-01 09:59:33 +02:00
parent 3a6c5f85d2
commit f3445e01a9

View File

@ -1,6 +1,7 @@
package gokrazy
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
@ -46,6 +47,17 @@ func switchRootPartition(newRootPartition int) error {
}
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
}