Wait mapper to be ready before formatting

This commit is contained in:
Ettore Di Giacinto 2022-10-15 16:19:36 +00:00
parent cabc24dc37
commit ce6ef0573b

19
main.go
View File

@ -19,6 +19,18 @@ import (
"github.com/urfave/cli"
)
func waitdevice(device string, attempts int) error {
for tries := 0; tries < attempts; tries++ {
sh("udevadm settle")
_, err := os.Lstat(device)
if !os.IsNotExist(err) {
return nil
}
time.Sleep(1 * time.Second)
}
return fmt.Errorf("no device found")
}
// TODO: Ask to discovery a pass to unlock. keep waiting until we get it and a timeout is exhausted with retrials (exp backoff)
func getPassword(b *block.Partition) (password string, err error) {
bus.Reload()
@ -110,6 +122,7 @@ func luksify(label string) error {
}
persistent = fmt.Sprintf("/dev/%s", persistent)
devMapper := fmt.Sprintf("/dev/mapper/%s", b.Name)
if err := createLuks(persistent, pass, "luks1"); err != nil {
return err
@ -119,7 +132,11 @@ func luksify(label string) error {
return err
}
out, err := sh(fmt.Sprintf("mkfs.ext4 %s -L %s", fmt.Sprintf("/dev/mapper/%s", b.Name), label))
if err := waitdevice(devMapper, 10); err != nil {
return err
}
out, err := sh(fmt.Sprintf("mkfs.ext4 %s -L %s", devMapper, label))
if err != nil {
return fmt.Errorf("err: %w, out: %s", err, out)