diff --git a/toolkit/tools/imagegen/diskutils/diskutils.go b/toolkit/tools/imagegen/diskutils/diskutils.go index 0beab227883..a19fb8e2d65 100644 --- a/toolkit/tools/imagegen/diskutils/diskutils.go +++ b/toolkit/tools/imagegen/diskutils/diskutils.go @@ -682,13 +682,19 @@ func InitializeSinglePartition(diskDevPath string, partitionNumber int, partitionNumberStr := strconv.Itoa(partitionNumber) // There are two primary partition naming conventions: - // /dev/sdN style or /dev/loopNp style + // - /dev/sdN + // - /dev/loopNp // Detect the exact one we are using. - // Make sure we check for /dev/loopNp FIRST, since /dev/loop1 would generate /dev/loop11 as a partition - // device which may be a valid device. We want to select /dev/loop1p1 first. testPartDevPaths := []string{ fmt.Sprintf("%sp%s", diskDevPath, partitionNumberStr), - fmt.Sprintf("%s%s", diskDevPath, partitionNumberStr), + } + + // If disk path ends in a digit, then the 'p' style must be used. + // So, don't check the other style to avoid ambiguities. For example, /dev/loop1 vs. /dev/loop11. + // This is particularly relevant on Ubuntu, due to snap's use of loopback devices. + if !isDigit(diskDevPath[len(diskDevPath)-1]) { + devPath := fmt.Sprintf("%s%s", diskDevPath, partitionNumberStr) + testPartDevPaths = append(testPartDevPaths, devPath) } err = retry.Run(func() error { @@ -759,6 +765,10 @@ func InitializeSinglePartition(diskDevPath string, partitionNumber int, return } +func isDigit(c byte) bool { + return c >= '0' && c <= '9' +} + func setGptPartitionType(partition configuration.Partition, timeoutInSeconds, diskDevPath, partitionNumberStr string) (err error) { if supports, _ := PartedSupportsTypeCommand(); !supports { logger.Log.Warn("parted version <3.6 does not support the 'type' session command - skipping this operation")