Skip to content

Commit

Permalink
fix: Wait until partition is available
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Feb 3, 2024
1 parent 1335bcb commit a5d66c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 17 additions & 0 deletions core/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package albius

import (
"fmt"
"os"
"slices"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -291,3 +293,18 @@ func (part *Partition) SetLabel(label string) error {

return nil
}

// WaitUntilAvailable polls the specified partition until it is available.
// This is particularly useful to make sure a recently created or modified
// partition is recognized by the system.
func (part *Partition) WaitUntilAvailable() {
for {
_, err := os.Stat(part.Path)
if !os.IsNotExist(err) {
if uuid, err := part.GetUUID(); err != nil && uuid != "" {
return
}
}
time.Sleep(50 * time.Millisecond)
}
}
25 changes: 16 additions & 9 deletions core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
}
// lsblk seems to take a few milliseconds to update the partition's
// UUID, so we loop until it gives us one
uuid := ""
for uuid == "" {
uuid, _ = part.GetUUID()
part.WaitUntilAvailable()
uuid, err := part.GetUUID()
if err != nil {
return err
}
err = LuksOpen(part, fmt.Sprintf("luks-%s", uuid), luksPassword)
if err != nil {
Expand Down Expand Up @@ -282,9 +283,10 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
}
// lsblk seems to take a few milliseconds to update the partition's
// UUID, so we loop until it gives us one
uuid := ""
for uuid == "" {
uuid, _ = part.GetUUID()
part.WaitUntilAvailable()
uuid, err := part.GetUUID()
if err != nil {
return err
}
err = LuksOpen(part, fmt.Sprintf("luks-%s", uuid), password)
if err != nil {
Expand All @@ -310,6 +312,8 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
*/
case "pvcreate":
part := args[0].(string)
dummyPart := Partition{Path: part}
dummyPart.WaitUntilAvailable()
err := lvm.Pvcreate(part)
if err != nil {
return err
Expand Down Expand Up @@ -360,6 +364,8 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
pvs := []string{}
if len(args) > 1 {
for _, pv := range args[1].([]interface{}) {
dummyPart := Partition{Path: pv.(string)}
dummyPart.WaitUntilAvailable()
pvs = append(pvs, pv.(string))
}
}
Expand Down Expand Up @@ -585,9 +591,10 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
}
// lsblk seems to take a few milliseconds to update the partition's
// UUID, so we loop until it gives us one
uuid := ""
for uuid == "" {
uuid, _ = dummyPart.GetUUID()
dummyPart.WaitUntilAvailable()
uuid, err := dummyPart.GetUUID()
if err != nil {
return err
}
err = LuksOpen(&dummyPart, fmt.Sprintf("luks-%s", uuid), password)
if err != nil {
Expand Down

0 comments on commit a5d66c0

Please sign in to comment.