Skip to content

Commit

Permalink
fix: Handle possible missing partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Feb 3, 2024
1 parent 3fd01a8 commit 75ca3dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
16 changes: 16 additions & 0 deletions core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,19 @@ func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int

return newPartition, nil
}

func (target *Disk) GetPartition(partNum int) *Partition {
// Happy path
if target.Partitions[partNum-1].Number == partNum {
return &target.Partitions[partNum-1]
}

// There are missing partition numbers, find partition manually
for _, part := range target.Partitions {
if part.Number == partNum {
return &part
}
}

return nil
}
28 changes: 14 additions & 14 deletions core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
if err != nil {
return err
}
err = disk.Partitions[partNum-1].RemovePartition()
err = disk.GetPartition(partNum).RemovePartition()
if err != nil {
return err
}
Expand All @@ -182,7 +182,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
if err != nil {
return err
}
err = disk.Partitions[partNum-1].ResizePartition(partNewSize)
err = disk.GetPartition(partNum).ResizePartition(partNewSize)
if err != nil {
return err
}
Expand All @@ -203,11 +203,11 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
if err != nil {
return err
}
err = disk.Partitions[partNum-1].SetLabel(partNewName)
err = disk.GetPartition(partNum).SetLabel(partNewName)
if err != nil {
return err
}
err = disk.Partitions[partNum-1].NamePartition(partNewName)
err = disk.GetPartition(partNum).NamePartition(partNewName)
if err != nil {
return err
}
Expand All @@ -226,7 +226,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
if err != nil {
return err
}
err = disk.Partitions[partNum-1].SetPartitionFlag(args[1].(string), args[2].(bool))
err = disk.GetPartition(partNum).SetPartitionFlag(args[1].(string), args[2].(bool))
if err != nil {
return err
}
Expand All @@ -245,14 +245,14 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
return err
}
filesystem := args[1].(string)
disk.Partitions[partNum-1].Filesystem = PartitionFs(filesystem)
err = MakeFs(&disk.Partitions[partNum-1])
disk.GetPartition(partNum).Filesystem = PartitionFs(filesystem)
err = MakeFs(disk.GetPartition(partNum))
if err != nil {
return err
}
if len(args) == 3 {
label := args[2].(string)
err := disk.Partitions[partNum-1].SetLabel(label)
err := disk.GetPartition(partNum).SetLabel(label)
if err != nil {
return err
}
Expand All @@ -274,9 +274,9 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
}
filesystem := args[1].(string)
password := args[2].(string)
part := disk.Partitions[partNum-1]
part := disk.GetPartition(partNum)
part.Filesystem = PartitionFs(filesystem)
err = LuksFormat(&part, password)
err = LuksFormat(part, password)
if err != nil {
return err
}
Expand All @@ -286,17 +286,17 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
for uuid == "" {
uuid, _ = part.GetUUID()
}
err = LuksOpen(&part, fmt.Sprintf("luks-%s", uuid), password)
err = LuksOpen(part, fmt.Sprintf("luks-%s", uuid), password)
if err != nil {
return err
}
err = LUKSMakeFs(&part)
err = LUKSMakeFs(part)
if err != nil {
return err
}
if len(args) == 4 {
label := args[3].(string)
err := LUKSSetLabel(&part, label)
err := LUKSSetLabel(part, label)
if err != nil {
return err
}
Expand Down Expand Up @@ -942,7 +942,7 @@ func (recipe *Recipe) SetupMountpoints() error {
disk = diskCache[diskName]
}

err = disk.Partitions[part-1].Mount(baseRoot + mnt.Target)
err = disk.GetPartition(part).Mount(baseRoot + mnt.Target)
if err != nil {
return err
}
Expand Down

0 comments on commit 75ca3dc

Please sign in to comment.