diff --git a/toolkit/tools/imagegen/diskutils/diskutils.go b/toolkit/tools/imagegen/diskutils/diskutils.go index 5e441206b4c..0beab227883 100644 --- a/toolkit/tools/imagegen/diskutils/diskutils.go +++ b/toolkit/tools/imagegen/diskutils/diskutils.go @@ -492,7 +492,7 @@ func CreatePartitions(diskDevPath string, disk configuration.Disk, rootEncryptio return partDevPathMap, partIDToFsTypeMap, encryptedRoot, readOnlyRoot, err } - partFsType, err := FormatSinglePartition(partDevPath, partition) + partFsType, err := formatSinglePartition(diskDevPath, partDevPath, partition) if err != nil { err = fmt.Errorf("failed to format partition:\n%w", err) return partDevPathMap, partIDToFsTypeMap, encryptedRoot, readOnlyRoot, err @@ -782,12 +782,13 @@ func setGptPartitionType(partition configuration.Partition, timeoutInSeconds, di return } -// FormatSinglePartition formats the given partition to the type specified in the partition configuration -func FormatSinglePartition(partDevPath string, partition configuration.Partition, +// formatSinglePartition formats the given partition to the type specified in the partition configuration +func formatSinglePartition(diskDevPath string, partDevPath string, partition configuration.Partition, ) (fsType string, err error) { const ( - totalAttempts = 5 - retryDuration = time.Second + totalAttempts = 5 + retryDuration = time.Second + timeoutInSeconds = "5" ) fsType = partition.FsType @@ -803,14 +804,14 @@ func FormatSinglePartition(partDevPath string, partition configuration.Partition fsType = "vfat" } - mkfsArgs := []string{"-t", fsType} + mkfsArgs := []string{"--timeout", timeoutInSeconds, diskDevPath, "mkfs", "-t", fsType} mkfsArgs = append(mkfsArgs, mkfsOptions...) mkfsArgs = append(mkfsArgs, partDevPath) err = retry.Run(func() error { - _, stderr, err := shell.Execute("mkfs", mkfsArgs...) + _, stderr, err := shell.Execute("flock", mkfsArgs...) if err != nil { - logger.Log.Warnf("Failed to format partition using mkfs: %v", stderr) + logger.Log.Warnf("Failed to format partition using mkfs (and flock): %v", stderr) return err } diff --git a/toolkit/tools/pkg/imagecustomizerlib/shrinkfilesystems.go b/toolkit/tools/pkg/imagecustomizerlib/shrinkfilesystems.go index aa058dcf422..f444de5a1b1 100644 --- a/toolkit/tools/pkg/imagecustomizerlib/shrinkfilesystems.go +++ b/toolkit/tools/pkg/imagecustomizerlib/shrinkfilesystems.go @@ -85,9 +85,10 @@ func shrinkFilesystems(imageLoopDevice string, verityHashPartition *imagecustomi } // Shrink the file system with resize2fs -M - stdout, stderr, err := shell.Execute("resize2fs", "-M", partitionLoopDevice) + stdout, stderr, err := shell.Execute("flock", "--timeout", "5", imageLoopDevice, + "resize2fs", "-M", partitionLoopDevice) if err != nil { - return fmt.Errorf("failed to resize %s with resize2fs:\n%v", partitionLoopDevice, stderr) + return fmt.Errorf("failed to resize %s with resize2fs (and flock):\n%v", partitionLoopDevice, stderr) } // Find the new partition end value @@ -103,10 +104,11 @@ func shrinkFilesystems(imageLoopDevice string, verityHashPartition *imagecustomi } // Resize the partition with parted resizepart - _, stderr, err = shell.ExecuteWithStdin("yes" /*stdin*/, "parted", "---pretend-input-tty", - imageLoopDevice, "resizepart", strconv.Itoa(partitionNumber), end) + _, stderr, err = shell.ExecuteWithStdin("yes" /*stdin*/, "flock", "--timeout", "5", imageLoopDevice, + "parted", "---pretend-input-tty", imageLoopDevice, "resizepart", + strconv.Itoa(partitionNumber), end) if err != nil { - return fmt.Errorf("failed to resizepart %s with parted:\n%v", partitionLoopDevice, stderr) + return fmt.Errorf("failed to resizepart %s with parted (and flock):\n%v", partitionLoopDevice, stderr) } // Re-read the partition table