Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Wait for partition to become available #45

Merged
merged 6 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:

- name: Test
run: |
distrobox enter -r albius_test -- sudo go test -v ./...
distrobox enter -v -r albius_test -- sudo go test -v ./...
10 changes: 8 additions & 2 deletions core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,19 @@ func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int
return newPartition, nil
}

// GetPartition attempts to locate a partition by its number. For instance, partition 3
// will normally point to `/dev/sda3`, but this might not be the case if partitions have
// been deleted (see [Issue #44]). This function first checks if the desired partition is
// in the correct place, else it searches all partitions in target for the correct one.
//
// [Issue #44]: https://github.com/Vanilla-OS/Albius/issues/44
func (target *Disk) GetPartition(partNum int) *Partition {
// Happy path
// Happy path: No partitions are missing
if target.Partitions[partNum-1].Number == partNum {
return &target.Partitions[partNum-1]
}

// There are missing partition numbers, find partition manually
// Missing partition numbers, find correct partition manually
for _, part := range target.Partitions {
if part.Number == partNum {
return &part
Expand Down
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
2 changes: 1 addition & 1 deletion utils/create_test_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

distrobox-create -r -I -Y -ap "golang libbtrfs-dev libdevmapper-dev libgpgme-dev build-essential pkg-config lvm2 parted udev" -i ghcr.io/vanilla-os/vso:main albius_test
distrobox-create -r -I -Y -ap "golang libbtrfs-dev libdevmapper-dev libgpgme-dev build-essential pkg-config lvm2 parted udev" -i ghcr.io/vanilla-os/dev:main albius_test
Loading