feat: add disk_format config with post-export disk conversion#176
Open
abdelhaleemahmed wants to merge 1 commit into
Open
feat: add disk_format config with post-export disk conversion#176abdelhaleemahmed wants to merge 1 commit into
abdelhaleemahmed wants to merge 1 commit into
Conversation
Add a new `disk_format` config field to the virtualbox-iso builder that
allows specifying the output disk format (VDI, VMDK, VHD). Default
remains VDI (the format VirtualBox creates internally).
VBoxManage export always converts VDI to VMDK (hardcoded in VirtualBox
C++ source: ApplianceImplExport.cpp). When disk_format is set to a
non-VMDK format, the export step now converts the VMDK back to the
requested format using VBoxManage clonemedium, and updates the OVF
references accordingly.
Changes:
- builder/virtualbox/common/step_export.go: Add DiskFormat field to
StepExport, add convertExportedDisk() method that runs after
VBoxManage export to convert VMDK to the requested format
- builder/virtualbox/iso/builder.go: Add DiskFormat config field with
validation (VDI/VMDK/VHD), pass it to StepExport
- builder/virtualbox/iso/step_create_disk.go: Use config.DiskFormat
instead of hardcoded "VDI"
- builder/virtualbox/iso/builder.hcl2spec.go: Add DiskFormat to HCL2 spec
Usage:
source "virtualbox-iso" "example" {
disk_format = "VDI"
# ... other config
}
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds a disk_format configuration option to the virtualbox-iso builder and wires it through disk creation and the export step, including a post-export conversion of the exported disk and OVF reference updates.
Changes:
- Add
disk_formatto the ISO builder config + HCL2 spec, with validation/defaulting. - Use
disk_formatduring disk creation (VBoxManage createhd --format ...). - Extend the export step to optionally convert the exported VMDK to another format and rewrite OVF references.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| builder/virtualbox/iso/step_create_disk.go | Use config.DiskFormat instead of hardcoded VDI when creating disks. |
| builder/virtualbox/iso/builder.hcl2spec.go | Add disk_format to the generated HCL2 schema. |
| builder/virtualbox/iso/builder.go | Add DiskFormat to config, default/validate it, and pass through to export step. |
| builder/virtualbox/common/step_export.go | Add post-export disk conversion + OVF reference rewrite logic. |
Files not reviewed (1)
- builder/virtualbox/iso/builder.hcl2spec.go: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+91
to
+100
| // If a non-VMDK disk format is requested, convert the exported VMDK | ||
| // to the requested format and update the OVF references. | ||
| diskFormat := strings.ToUpper(s.DiskFormat) | ||
| if diskFormat != "" && diskFormat != "VMDK" { | ||
| if err := s.convertExportedDisk(driver, ui, diskFormat); err != nil { | ||
| state.Put("error", err) | ||
| ui.Error(err.Error()) | ||
| return multistep.ActionHalt | ||
| } | ||
| } |
Comment on lines
+112
to
+119
| // Step A: Find the VMDK file in the output directory | ||
| pattern := filepath.Join(s.OutputDir, "*.vmdk") | ||
| matches, _ := filepath.Glob(pattern) | ||
| if len(matches) == 0 { | ||
| return fmt.Errorf("No VMDK file found in %s after export", s.OutputDir) | ||
| } | ||
| vmdkPath := matches[0] | ||
| vmdkBasename := filepath.Base(vmdkPath) |
Comment on lines
+134
to
+137
| // Step D: Delete the VMDK | ||
| if err := os.Remove(vmdkPath); err != nil { | ||
| ui.Message(fmt.Sprintf("Warning: could not delete VMDK: %s", err)) | ||
| } |
Comment on lines
+107
to
+111
| // convertExportedDisk converts the VMDK produced by VBoxManage export | ||
| // to the requested disk format (VDI, VHD) and updates the OVF file. | ||
| func (s *StepExport) convertExportedDisk(driver Driver, ui packersdk.Ui, diskFormat string) error { | ||
| ext := strings.ToLower(diskFormat) | ||
|
|
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.
Summary
disk_formatconfiguration field to thevirtualbox-isobuilderVBoxManage export(which always produces VMDK), automatically converts to the requested format usingVBoxManage clonemediumProblem
VBoxManage exportalways converts VDI disks to VMDK — this is hardcoded in VirtualBox's C++ source (ApplianceImplExport.cpp). Users who want VDI output (for discard/compact support) have no way to get it from the builder.Solution
Rather than trying to bypass VBoxManage export, this PR adds a post-export conversion step:
VBoxManage exportruns normally → produces correct OVF + VMDKdisk_formatis not VMDK,convertExportedDisk()runs:VBoxManage clonemedium disk.vmdk disk.vdi --format VDI.vmdkreferences in the OVF to.vdiUsage
Default is VDI (unchanged from current behavior where VirtualBox creates VDI internally). When set to VMDK, no conversion occurs (existing behavior).
Changes
builder/virtualbox/common/step_export.go— AddedDiskFormatfield toStepExport, addedconvertExportedDisk()methodbuilder/virtualbox/iso/builder.go— AddedDiskFormatconfig field with validation, pass to StepExportbuilder/virtualbox/iso/step_create_disk.go— Useconfig.DiskFormatinstead of hardcoded"VDI"builder/virtualbox/iso/builder.hcl2spec.go— AddedDiskFormatto HCL2 specTest plan
disk_format = "VDI"— VDI in outputvagrant upimports and boots successfullydisk_format = "VMDK"— no conversion occurs🤖 Generated with Claude Code