Skip to content

Conversation

@vyasgun
Copy link
Contributor

@vyasgun vyasgun commented Apr 25, 2025

Summary by Sourcery

Add validation to reject qcow2 formatted block device images.

Bug Fixes:

Tests:

  • Add tests for the qcow2 block device validation.

@sourcery-ai
Copy link

sourcery-ai bot commented Apr 25, 2025

Reviewer's Guide by Sourcery

This pull request adds validation to detect qcow2 block devices used with VirtioBlk and returns an error if found, as vfkit does not support this format. It also includes tests for the new validation logic.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Implement validation to detect qcow2 images used as block devices.
  • Add a new function ValidBlkDevices to iterate through VM devices.
  • Check if a VirtioBlk device's image path points to a qcow2 file by reading the header.
  • Return an error if the file header matches the qcow2 magic number.
pkg/config/config.go
Integrate the block device validation into the VM configuration process.
  • Call ValidBlkDevices when creating the VM configuration.
  • Return an error if the validation fails.
cmd/vfkit/main.go
Add a test case for the new block device validation.
  • Create a test function TestVirtualMachine_ValidBlkDevices.
  • Use a temporary file with qcow2 header to test the error condition.
  • Use a temporary file with a non-qcow header to test the success condition.
pkg/config/config_test.go

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@openshift-ci openshift-ci bot requested review from lstocchi and praveenkumar April 25, 2025 10:54
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @vyasgun - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider defining the qcow2 magic number "QFI\xfb" as a constant for better readability and maintainability.
  • The type assertion for VirtioBlk can be combined with the switch statement (e.g., case dev := device.(*VirtioBlk):) for improved conciseness.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

return nil
}

func (vm *VirtualMachine) ValidBlkDevices() error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider using a type switch with an assignment and moving the file validation logic into its own helper function to improve code structure.

Try using a type switch with an assignment and move the file‐validation logic into its own helper. For example:

func (vm *VirtualMachine) ValidBlkDevices() error {
    for _, device := range vm.Devices {
        switch dev := device.(type) {
        case *VirtioBlk:
            if dev.ImagePath == "" {
                continue
            }
            if err := validateBlkDevice(dev.ImagePath); err != nil {
                return err
            }
        }
    }
    return nil
}

func validateBlkDevice(imgPath string) error {
    file, err := os.Open(imgPath)
    if err != nil {
        return fmt.Errorf("failed to open file %s: %v", imgPath, err)
    }
    defer file.Close()

    header := make([]byte, 4)
    if _, err := file.Read(header); err != nil {
        return fmt.Errorf("failed to read the header of file %s: %v", imgPath, err)
    }
    if string(header) == "QFI\xfb" {
        return fmt.Errorf("vfkit does not support qcow2 block devices")
    }
    return nil
}

This reduces redundant assertions and flattens the nested ifs, while keeping functionality intact.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good advice. Even better is to add a Validate() interface to devices so we can keep device related code in the same place.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great point, @nirs! Adding a Validate() method to a device interface would definitely be a cleaner and more scalable solution in the long run. It keeps the validation logic tightly coupled with the device implementation itself.

My suggestion aimed for a more localized improvement within the existing structure, but your interface-based approach is certainly preferable for future maintainability and extensibility. If you have the bandwidth to implement that, it would be an excellent enhancement!

return nil
}

func (vm *VirtualMachine) ValidBlkDevices() error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good advice. Even better is to add a Validate() interface to devices so we can keep device related code in the same place.

Copy link
Collaborator

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split this PR in at least 2 commits, one adding the Validate method to the VirtioDevice interface (+ValidateDevices), and a second commit adding the qcow2 check? I’d even move ValidateDevices to its own commit so that each commit does a single thing.


func (vm *VirtualMachine) ValidateDevices() error {
for _, device := range vm.Devices {
if err := device.Validate(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a number of calls to Validate in virtio.go, are they made redundant by this new method? or do we need both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either the Validate() calls at the end of FromOptions in some places can be removed or we can add dev.Validate to the end of all FromOptions. I have removed the redundant occurrences in this iteration.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern I have with this is that this weakens the FromOptions and AddDevicesFromCommandLine APIs, which are kind of public: I expect other go programs to be using github.com/crc-org/pkg/config, even if I’m not sure these specific methods will be widely used.
Before this change, they were returning an error when the config does not validate, after this change, all users of the API need to remember to do the validation themselves or they could get invalid device configuration.

I would keep the validation at the end of each FromOptions, or add the validation to either AddDevicesFromCmdLine or deviceFromCmdLine if we want to avoid the duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that makes sense. I will add validate back to FromOptions

Copy link
Collaborator

@nirs nirs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much nicer like this!

@vyasgun vyasgun force-pushed the pr/qcow2-error branch 6 times, most recently from a914f7f to 38b68ea Compare April 30, 2025 07:45
@cfergeau cfergeau moved this to Ready for review in Project planning: crc Apr 30, 2025

func (vm *VirtualMachine) ValidateDevices() error {
for _, device := range vm.Devices {
if err := device.Validate(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern I have with this is that this weakens the FromOptions and AddDevicesFromCommandLine APIs, which are kind of public: I expect other go programs to be using github.com/crc-org/pkg/config, even if I’m not sure these specific methods will be widely used.
Before this change, they were returning an error when the config does not validate, after this change, all users of the API need to remember to do the validation themselves or they could get invalid device configuration.

I would keep the validation at the end of each FromOptions, or add the validation to either AddDevicesFromCmdLine or deviceFromCmdLine if we want to avoid the duplication.

@vyasgun vyasgun force-pushed the pr/qcow2-error branch 3 times, most recently from 49c7338 to 04c0723 Compare May 15, 2025 07:58
@vyasgun
Copy link
Contributor Author

vyasgun commented May 15, 2025

/retest

@vyasgun
Copy link
Contributor Author

vyasgun commented May 15, 2025

Adding Validate() to FromOptions of VirtioBlkDev is causing the test to fail as the validation checks for the test file /foo/bar to be existent. Will update the test.

Copy link
Collaborator

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Add dev.Validate() back to FromOptions undoes what was done in Add ValidateDevices function to validate devices parsed from CmdLine , could you merge these 2 commits somehow?

vyasgun added 2 commits June 2, 2025 22:19
Added validation logic to `VirtioBlk` to detect and reject qcow2 image files
based on their magic header ("QFI\xfb").
Adds qemu-utils (for qemu-img) to the CI environment to support new tests
that validate block devices and reject unsupported qemu image formats.
@vyasgun vyasgun force-pushed the pr/qcow2-error branch 3 times, most recently from e37bed1 to f786434 Compare June 2, 2025 17:06
@vyasgun
Copy link
Contributor Author

vyasgun commented Jun 3, 2025

@cfergeau addressed the comments.

Copy link
Collaborator

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a few minor comments left

Added unit tests for the new `VirtioBlk.validate` method to ensure qcow2 images
are correctly rejected based on their magic header.

Updated existing VirtioBlk-related tests to create temporary image files on disk,
as the new validation logic checks for file existence and reads the header.
This ensures the tests pass with the added file-based validation in place.
@cfergeau
Copy link
Collaborator

cfergeau commented Jun 5, 2025

/lgtm
/approve

@openshift-ci
Copy link

openshift-ci bot commented Jun 5, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfergeau

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved label Jun 5, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit 4556fea into crc-org:main Jun 5, 2025
7 of 8 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in Project planning: crc Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Error out when a qcow2 image is detected

4 participants