Skip to content

fix: correct qcow2 streaming conversion to device#3

Merged
rossigee merged 1 commit intomasterfrom
fix/qcow2-stdout-redirect
Apr 6, 2026
Merged

fix: correct qcow2 streaming conversion to device#3
rossigee merged 1 commit intomasterfrom
fix/qcow2-stdout-redirect

Conversation

@rossigee
Copy link
Copy Markdown
Owner

@rossigee rossigee commented Apr 6, 2026

Summary

  • Bug 1 (nil panic): The qcow2 case assigned to a local qemuCmd variable instead of cmd, leaving cmd nil. The logging (cmd.Args) and execution (cmd.CombinedOutput()) lines below the switch would panic at runtime.
  • Bug 2 (data loss): Even with the variable name fixed, cmd.CombinedOutput() unconditionally sets both Stdout and Stderr, silently overriding the cmd.Stdout = deviceFile redirect — no data would reach the LVM volume.
  • Lint noctx: Missing //nolint:gosec,noctx on the new exec.Command call.
  • Lint errcheck: defer deviceFile.Close() did not check the error return.

Fix

  • Assign directly to cmd in the qcow2 case (not a local variable).
  • After the switch, check cmd.Stdout != nil to choose the execution path:
    • qcow2: cmd.Run() with a separate bytes.Buffer on cmd.Stderr so stdout continues flowing to the device file and stderr is still captured for error reporting.
    • raw/dd: unchanged cmd.CombinedOutput().
  • Use a checked defer closure for deviceFile.Close().
  • Add the required //nolint:gosec,noctx annotation.

Test plan

  • go build ./... passes
  • go test ./... passes (all packages)
  • golangci-lint run ./internal/lvm/... reports 0 issues
  • Integration: provision a qcow2-backed volume and verify the LVM block device contains the correct data

The previous change introduced two critical bugs:

1. Used a local `qemuCmd` variable instead of assigning to `cmd`,
   leaving `cmd` nil and causing a nil-pointer dereference at the
   logging and execution lines below the switch.

2. Even with the variable name corrected, `cmd.CombinedOutput()`
   overrides any prior `cmd.Stdout` assignment, so the device file
   redirect was silently discarded and no data would reach the volume.

Fix by:
- Assigning directly to `cmd` in the qcow2 case.
- Detecting whether stdout has been redirected after the switch and
  using `cmd.Run()` + a separate stderr buffer in that path, leaving
  `cmd.CombinedOutput()` for the raw/dd path.
- Using a checked defer for `deviceFile.Close()`.
- Adding the required `//nolint:gosec,noctx` annotation.
@rossigee rossigee merged commit 93ba886 into master Apr 6, 2026
1 check passed
@rossigee rossigee deleted the fix/qcow2-stdout-redirect branch April 6, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant