Skip to content

Commit

Permalink
fix: Wait until partitions are recognized by the kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Dec 26, 2023
1 parent 678930a commit 39ee61a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"
)

const (
Expand Down Expand Up @@ -107,6 +108,26 @@ func (disk *Disk) Update() error {
return nil
}

// waitForNewPartition is called after creating a new partition in order to
// ensure the system is aware of it before proceeding.
func (disk *Disk) waitForNewPartition() error {
for {
output, err := OutputCommand("lsblk -nro NAME /dev/nvme0n1 | wc -l")
if err != nil {
return err
}

count, err := strconv.Atoi(strings.TrimSpace(output))
if err != nil {
return err
}

if count-1 != len(disk.Partitions) {
return nil
}
}
}

func (disk *Disk) LabelDisk(label DiskLabel) error {
labelDiskCmd := "parted -s %s mklabel %s"

Expand Down Expand Up @@ -157,6 +178,12 @@ func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int
return nil, fmt.Errorf("failed to create partition: %s", err)
}

// Wait until kernel is aware of new partition
err = target.waitForNewPartition()
if err != nil {
return nil, fmt.Errorf("failed to create partition: %s", err)
}

// Update partition list because we made changes to the disk
err = target.Update()
if err != nil {
Expand Down

0 comments on commit 39ee61a

Please sign in to comment.