将 Box 添加到 Vagrant

Adding Box to Vagrant

我创建了一个用于 Hyper-V 提供程序的新框。当我尝试添加框时,它死于以下内容:

PS C:\> vagrant box add sbx-misc-app01 .\img-misc-w2k16.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'sbx-misc-app01' (v0) for provider:
    box: Unpacking necessary files from: file://C:/img-misc-w2k16.box
    box:
C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:156:in `block (3 levels) in add': undefined method `to_sym' for nil:NilClass (NoMethodError)
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:461:in `with_temp_dir'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:134:in `block (2 levels) in add'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:461:in `with_temp_dir'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:115:in `block inadd' from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:448:in `block in with_collection_lock'
        from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:447:in `with_collection_lock'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/box_collection.rb:104:in `add'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/builtin/box_add.rb:357:in `box_add'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/builtin/box_add.rb:146:in `add_direct'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/builtin/box_add.rb:120:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/plugins/commands/box/command/add.rb:78:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/plugins/commands/box/command/root.rb:66:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/cli.rb:46:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/lib/vagrant/environment.rb:269:in `cli'
        from C:/HashiCorp/Vagrant/embedded/gems/2.0.3/gems/vagrant-2.0.3/bin/vagrant:151:in `<main>'

问题:

  1. 是否有元数据文件参考?我已经搜索过了。我确定它就在我面前,但我找不到它。
  2. 为什么我会收到消息 "box: Box file was not detected as metadata."?我的存档中有一个 metadata.json 文件。
  3. WTF 有 "undefined method `to_sym'" 错误?

环境...

盒子里的东西...

C:\img-misc-w2k16\Virtual 硬盘
C:\img-misc-w2k16\Virtual 机器
C:\img-misc-w2k16\metadata.json
C:\img-misc-w2k16\Virtual 硬 Disks\img-misc-w2k16.vhdx
C:\img-misc-w2k16\Virtual Machines\AB56D8F5-4F71-4CCD-97E6-237713B79FD8..... C:\img-misc-w2k16\Virtual Machines\AB56D8F5-4F71-4CCD-97E6-237713B79FD8.....

metadata.json 的内容...

{
    "name": "foo/windowsServer2016x64",
    "description": "This box contains Windows Server 2016 64-bit standard edition.",
    "versions": [
      {
        "version": "1.0.0",
        "providers": [
          {
            "name": "hyperv"
          }
        ]
      }
    ]
  }

似乎对盒子的不同方面有些混淆。

所以取自 https://www.vagrantup.com/docs/boxes/format.html

Box File - This is a compressed (tar, tar.gz, zip) file that is specific to a single provider and can contain anything. Vagrant core does not ever use the contents of this file. Instead, they are passed to the provider. Therefore, a VirtualBox box file has different contents from a VMware box file and so on.

Box Catalog Metadata - This is a JSON document (typically exchanged during interactions with HashiCorp's Vagrant Cloud) that specifies the name of the box, a description, available versions, available providers, and URLs to the actual box files (next component) for each provider and version. If this catalog metadata does not exist, a box file can still be added directly, but it will not support versioning and updating.

文件 C:\img-misc-w2k16\metadata.json 是 box 文件的一部分 - 它不同于 box 目录元数据(如果您要分发您的 box 则使用)但您共享的内容是 box 目录元数据。

进一步阅读

Within the archive, Vagrant does expect a single file: metadata.json. This is a JSON file that is completely unrelated to the above box catalog metadata component; there is only one metadata.json per box file (inside the box file), whereas one catalog metadata JSON document can describe multiple versions of the same box, potentially spanning multiple providers.

metadata.json must contain at least the "provider" key with the provider the box is for. Vagrant uses this to verify the provider of the box. For example, if your box was for VirtualBox, the metadata.json would look like this:

{ "provider": "virtualbox" }

If there is no metadata.json file or the file does not contain valid JSON with at least a "provider" key, then Vagrant will error when adding the box, because it cannot verify the provider.

所以当你打包你的盒子时,metadata.json的内容应该是

{
  "provider": "hyperv"
}

一旦你替换了你的文件的内容,你就可以重新打包这个盒子,你应该可以从这个盒子运行一个虚拟机。