Skip to content

Commit

Permalink
Fix recursive partition iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Nov 5, 2023
1 parent 2bdf988 commit 741dc17
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions core/disk-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ type Partition struct {
// If the partition is LUKS-encrypted or an LVM volume, the logical volume
// opened in /dev/mapper will be a child of the physical partiiton in /dev.
// Otherwise, the partition will be a direct child of the block device, and
// therefore Parent will be nil.
// its Parent will be nil.
//
// The same logic applies for encrypted LVM volumes. When this is the case,
// the filesystem hirearchy is as follows:
//
// NAME FSTYPE
// -- sda1 LVM2_member
// |-- myVG-myLV crypto_LUKS
// |-- luks-volume btrfs
//
// In this case, the parent of "luks-volume" is "myVG-myLV", which,
// in turn, has "sda1" as parent. Since "sda1" is a physical partition,
// its parent is nil.
Parent *Partition
}

Expand Down Expand Up @@ -94,17 +106,15 @@ func iterChildren(childs *[]Children, result *[]Partition) {
Device: child.LogicalName,
})

detectedPartitions := len(*result)

currentPartitions := len(*result)
iterChildren(&child.Children, result)

if detectedPartitions == 0 {
return
}
detectedPartitions := len(*result) - currentPartitions

// Populate children's reference to parent
for i := 0; i < len(*result)-detectedPartitions; i++ {
(*result)[detectedPartitions+i].Parent = &(*result)[detectedPartitions-1]
for i := currentPartitions; i < len(*result); i++ {
if (*result)[i].Parent == nil {
(*result)[i].Parent = &(*result)[len(*result)-detectedPartitions-1]
}
}
}
}
Expand Down

0 comments on commit 741dc17

Please sign in to comment.