Skip to content

Commit 2f92a78

Browse files
authored
Merge pull request #1578 from stgraber/main
incusd/device/disk: Better handle partitions
2 parents 0f2db18 + 3b6cb6c commit 2f92a78

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

internal/server/device/disk.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,7 @@ func (d *disk) getDiskLimits() (map[string]diskBlockLimit, error) {
22402240

22412241
// Build a list of all valid block devices
22422242
validBlocks := []string{}
2243+
parentBlocks := map[string]string{}
22432244

22442245
dents, err := os.ReadDir("/sys/class/block/")
22452246
if err != nil {
@@ -2248,10 +2249,13 @@ func (d *disk) getDiskLimits() (map[string]diskBlockLimit, error) {
22482249

22492250
for _, f := range dents {
22502251
fPath := filepath.Join("/sys/class/block/", f.Name())
2252+
2253+
// Ignore partitions.
22512254
if util.PathExists(fmt.Sprintf("%s/partition", fPath)) {
22522255
continue
22532256
}
22542257

2258+
// Only select real block devices.
22552259
if !util.PathExists(fmt.Sprintf("%s/dev", fPath)) {
22562260
continue
22572261
}
@@ -2261,7 +2265,37 @@ func (d *disk) getDiskLimits() (map[string]diskBlockLimit, error) {
22612265
return nil, err
22622266
}
22632267

2264-
validBlocks = append(validBlocks, strings.TrimSuffix(string(block), "\n"))
2268+
// Add the block to the list.
2269+
blockIdentifier := strings.TrimSuffix(string(block), "\n")
2270+
validBlocks = append(validBlocks, blockIdentifier)
2271+
2272+
// Look for partitions.
2273+
subDents, err := os.ReadDir(fPath)
2274+
if err != nil {
2275+
return nil, err
2276+
}
2277+
2278+
for _, sub := range subDents {
2279+
// Skip files.
2280+
if !sub.IsDir() {
2281+
continue
2282+
}
2283+
2284+
// Select partitions.
2285+
if !util.PathExists(filepath.Join(fPath, sub.Name(), "partition")) {
2286+
continue
2287+
}
2288+
2289+
// Get the block identifier for the partition.
2290+
partition, err := os.ReadFile(filepath.Join(fPath, sub.Name(), "dev"))
2291+
if err != nil {
2292+
return nil, err
2293+
}
2294+
2295+
// Add the partition to the map.
2296+
partitionIdentifier := strings.TrimSuffix(string(partition), "\n")
2297+
parentBlocks[partitionIdentifier] = blockIdentifier
2298+
}
22652299
}
22662300

22672301
// Process all the limits
@@ -2310,6 +2344,9 @@ func (d *disk) getDiskLimits() (map[string]diskBlockLimit, error) {
23102344
if slices.Contains(validBlocks, block) {
23112345
// Straightforward entry (full block device)
23122346
blockStr = block
2347+
} else if parentBlocks[block] != "" {
2348+
// Known partition.
2349+
blockStr = parentBlocks[block]
23132350
} else {
23142351
// Attempt to deal with a partition (guess its parent)
23152352
fields := strings.SplitN(block, ":", 2)

0 commit comments

Comments
 (0)