Skip to content

Commit

Permalink
Toolkit: Add missing flock calls. (microsoft#10804)
Browse files Browse the repository at this point in the history
When making changes to partitions or filesystems, it is recommended to take a file lock over the disk block device as this informs the host OS that you are making changes and that it should avoid scanning or changing the device until you are done. While most of the relevant operations are covered, there a few places that are missing the lock. For example, when calling `mkfs` or `resize2fs`.
  • Loading branch information
cwize1 authored and durgajagadeesh committed Dec 31, 2024
1 parent e7063cd commit 3b67cec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 9 additions & 8 deletions toolkit/tools/imagegen/diskutils/diskutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
12 changes: 7 additions & 5 deletions toolkit/tools/pkg/imagecustomizerlib/shrinkfilesystems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3b67cec

Please sign in to comment.