Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add Hyper-V configuration versions information in box.xml during vagrant post processor. #23

Open
hc-github-team-packer opened this issue Jul 7, 2021 · 4 comments

Comments

@hc-github-team-packer
Copy link

This issue was originally opened by @fsdrw08 in hashicorp/packer#11129 and has been migrated to this repository. The original issue description is below.


Description

Please add Hyper-V configuration versions information in box.xml during vagrant post processor.
I found that even if we specify the configuration_version (https://www.packer.io/docs/builders/hyperv/iso#configuration_version) in packer template, with vagrant post-processor to build a vagrant box (which means as expect, it should be a lower configuration version Hyper-V VM box). But Vagrant up this box will still create a VM in hyper-V with the latest configuration version .

After investigate Vagrant's related HYPER-V VM create script, I found that Vagrant will primary use the information from box.xml (under .vagrant.d\boxes\ <vm name>\0\hyperv\Virtual Machines) as VM parameter to create a new VM with PowerShell CMDLET New-VM. And if we don't specify the -Version (VM configuration version) parameter in this New-VM command, it will create the VM with the as latest as the host support version. e.g. in Win10 21H1, this command will create a 9.0 version VM without -version parameter.

My Request:

  • First, in packer vagrant post processor part:
    Add Hyper-V configuration versions information in box.xml during vagrant post processor.

  • After first part, in vagrant up process part:
    Improve vagrant hyper-v plug in script module "VagrantVM.psm" to let vagrant up a hyper-v vm box with it's own configuration version.

Use Case(s)

The purpose use case is vagrant up a Hyper-V VM box as what the box original configuration version is.

Potential configuration

  • Add Hyper-V configuration versions information in box.xml during vagrant post processor. The expect result like
    <configuration>
    <properties>
    ...
    <version type="string">8.0</version>
    </properties>
    ...
    </configuration>

  • After box.xml improve, in vagrant up process part:
    add some script snippet to detect the box xml version node in https://github.com/hashicorp/vagrant/blob/5b501a3fb05ed0ab16cf10991b3df9d231edb5cf/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 's New-VagrantVMXML function
    something like insert below code in line 241
    $Version = [string]($VMConfig.configuration.properties.version."#text")
    # Version parameter in PS4 so validate before using
    if((Get-Command Hyper-V\New-VM).Parameters.Keys.Contains("version")) {
    $NewVMConfig.Version = $Version
    }

Potential References

@horvaro
Copy link

horvaro commented Aug 31, 2021

@fsdrw08
I am facing the same issue and I found a trace to what we could do, but I cant get past it.

On some builds it will create a Box which will start as configuration version 8.0 and some will start as 9.0.
I found that the information about which configuration version Hyper-V should use is only in the *.vmcx files.
On 'vagrant up', Vagrant searches for any configuration files in the 'Virtual Machines' folder. It will stop on the first valid one (no matter if it is a box.xml or *.vmcx).
https://github.com/hashicorp/vagrant/blob/5b501a3fb05ed0ab16cf10991b3df9d231edb5cf/plugins/providers/hyperv/action/import.rb#L31

*.vmcx are named after the UUID of the VM, which is randomly generated.
So: Some builds from packer create a Box with a structure inside the 'Virtual Machines' Folder like this:

  • 'box.xml'
  • 'F1C7C5C3-F7EA-45CA-8B87-119864E08A35.vmcx'
  • 'F1C7C5C3-F7EA-45CA-8B87-119864E08A35.vmrs'

And some will be like this:

  • '29A77F80-839B-488E-8233-9F2E16FE4E86.vmcx'
  • '29A77F80-839B-488E-8233-9F2E16FE4E86.vmrs'
  • 'box.xml'

Notice that some vmcx files start with a number and will be further up in a Folder listing.

Packer will read all artifact names inside the builder plugin and will return a list of files for the post-processors.
(https://github.com/hashicorp/packer-plugin-hyperv/blob/main/builder/hyperv/common/artifact.go#L27)
This means, renaming the VMCX files to start with a number or '_' in a post-processing step wont work, because packer already knows which files need to be exported.
The only thing left would be to set the range of the UUIDs which Hyper-V creates and let it only create ones that start with a number. And that's where I am stuck ...

@fsdrw08
Copy link

fsdrw08 commented Aug 31, 2021

@fsdrw08
I am facing the same issue and I found a trace to what we could do, but I cant get past it.

On some builds it will create a Box which will start as configuration version 8.0 and some will start as 9.0.
I found that the information about which configuration version Hyper-V should use is only in the *.vmcx files.
On 'vagrant up', Vagrant searches for any configuration files in the 'Virtual Machines' folder. It will stop on the first valid one (no matter if it is a box.xml or *.vmcx).
https://github.com/hashicorp/vagrant/blob/5b501a3fb05ed0ab16cf10991b3df9d231edb5cf/plugins/providers/hyperv/action/import.rb#L31

*.vmcx are named after the UUID of the VM, which is randomly generated.
So: Some builds from packer create a Box with a structure inside the 'Virtual Machines' Folder like this:

  • 'box.xml'
  • 'F1C7C5C3-F7EA-45CA-8B87-119864E08A35.vmcx'
  • 'F1C7C5C3-F7EA-45CA-8B87-119864E08A35.vmrs'

And some will be like this:

  • '29A77F80-839B-488E-8233-9F2E16FE4E86.vmcx'
  • '29A77F80-839B-488E-8233-9F2E16FE4E86.vmrs'
  • 'box.xml'

Notice that some vmcx files start with a number and will be further up in a Folder listing.

Packer will read all artifact names inside the builder plugin and will return a list of files for the post-processors.
(https://github.com/hashicorp/packer-plugin-hyperv/blob/main/builder/hyperv/common/artifact.go#L27)
This means, renaming the VMCX files to start with a number or '_' in a post-processing step wont work, because packer already knows which files need to be exported.
The only thing left would be to set the range of the UUIDs which Hyper-V creates and let it only create ones that start with a number. And that's where I am stuck ...

I notice this issue in these days too, I think it should make a improvement in vagrant side, vagrant should take vmcx config file as primary than the box.xml file

aaronknister pushed a commit to aaronknister/packer-plugin-vagrant that referenced this issue Mar 16, 2022
…icorp#23)

Bumps [github.com/hashicorp/packer-plugin-sdk](https://github.com/hashicorp/packer-plugin-sdk) from 0.2.0 to 0.2.1.
- [Release notes](https://github.com/hashicorp/packer-plugin-sdk/releases)
- [Changelog](https://github.com/hashicorp/packer-plugin-sdk/blob/main/CHANGELOG.md)
- [Commits](hashicorp/packer-plugin-sdk@v0.2.0...v0.2.1)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@saily
Copy link

saily commented Jun 24, 2024

Hi, any update on this topic?

@fsdrw08
Copy link

fsdrw08 commented Jun 24, 2024

now I use terraform hyperv provider instead of Vagrant, Vagrant is dead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants