feat: add SWAP partition detection and cleanup before loop device detach#568
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the loop device teardown path in internal/image/imagedisc to proactively detect SWAP partitions on a loop device and disable them before detaching the loop device, aiming to avoid detach failures caused by active swap.
Changes:
- Added a pre-detach SWAP cleanup step in
LoopSetupDelete. - Implemented
lsblk -Jparsing to locate swap partitions under the target loop device. - Issued
swapofffor detected swap partitions and continued with detach regardless of swap cleanup outcome.
Comments suppressed due to low confidence (1)
internal/image/imagedisc/loopdev.go:66
LoopSetupDeletenow executeslsblk(and potentiallyswapoff) viashell.ExecCmdbeforelosetup -d. Existing unit tests forLoopSetupDelete(internal/image/imagedisc/loopdev_test.go) only mock thelosetup -d ...command, so they will fall back toDefaultExecutorand try to run reallsblk/swapoffduring tests. Update the tests/mocks to include the new commands (or refactor to make swap disabling injectable) so CI doesn't execute host commands.
func (loopDev *LoopDev) LoopSetupDelete(loopDevPath string) error {
// Handle SWAP partitions before detaching
if err := loopDev.disableSwapPartitions(loopDevPath); err != nil {
log.Warnf("Warning while disabling SWAP partitions on %s: %v", loopDevPath, err)
// Don't return error, try to continue with detach
}
cmd := fmt.Sprintf("losetup -d %s", loopDevPath)
if _, err := shell.ExecCmd(cmd, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to delete loop device %s: %v", loopDevPath, err)
return fmt.Errorf("failed to delete loop device %s: %w", loopDevPath, err)
samueltaripin
approved these changes
May 20, 2026
a7e6fcf to
d2f5d48
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merge Checklist
All boxes should be checked before merging the PR
Description
Any Newly Introduced Dependencies
How Has This Been Tested?
2026-05-19T11:36:11.361Z INFO imagedisc/loopdev.go:112 Found SWAP partition: /dev/loop32p2, disabling it
2026-05-19T11:36:11.378Z INFO imagedisc/loopdev.go:117 Successfully disabled SWAP on /dev/loop32p2
2026-05-19T11:36:11.383Z INFO rawmaker/rawmaker.go:133 Successfully detached loopback device: /dev/loop32
2026-05-19T11:36:11.383Z INFO display/display.go:21 Checking for image artifacts in: /home/user/riddhi/image-composer-tool/workspace/ubuntu-ubuntu24-x86_64/imagebuild/minimal