如何在实例化之前配置Vagrant框?

时间:2018-03-28 22:20:16

标签: vagrant virtualbox

我想从另一个Vagrant框的输出中创建一个Vagrant框。我想在Vagrant中做到这一点。我想运行vagrant initvagrant up,并执行以下步骤:

  1. 运行此命令以将.bin转换为.vdi vboxmanage convertfromraw --format vdi ../build/qMp_3.2.1-Clearance_VirtualBox_x86_factory_20180325-0702.bin qmp-nycmesh-3.2.1.vdi
  2. 根据.vdi
  3. 创建实例

    我有这个Vagrantfile,但是1)它在尝试创建框之前没有执行shell脚本,2)它试图下载一个框而不是使用给定的VDI。

    Vagrant.configure("2") do |config|
      config.vm.provision "shell", inline: "echo Hello"
      #config.vm.box = "nycmesh-qmp-openwrt"
      config.vm.network "public_network"
      config.vm.network "private_network", type: "dhcp"
      config.vm.provider "virtualbox" do |vb|
        vb.memory = "64"
        vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "../build/qMp_3.2.1-Clearance_VirtualBox_x86_factory_20170406-2203.vdi"]
        #vb.customize ["modifyvm", :id, "--ostype", "Linux26"]
      end
    end
    

    使用config.vm.box,它会给出

    $ vagrant up
    ==> default: Box 'nycmesh-qmp-openwrt' could not be found. Attempting to find and install...
        default: Downloading: nycmesh-qmp-openwrt
    An error occurred while downloading the remote file. ...
    Couldn't open file .../nycmesh-qmp-openwrt
    

    评论config.vm.box给出了

    $ vagrant up
    vm:
    * A box must be specified.
    

    Vagrant 2.0.1

1 个答案:

答案 0 :(得分:0)

我发现我可以指定一个占位符框,并且可以在启动之前将其交换掉,但它仍然需要复制千兆字节大小的图像,当您使用vagrant destroy时它不会删除占位符图像。这不太理想。

我意识到Vagrantfile只是Ruby,所以我能够编写VDI创建脚本。我无法删除占位符图像,因为没有钩子可以在正在创建的实例和交换磁盘映像之间插入。

Vagrant.configure("2") do |config|
  latest_bin = `ls -t ../build/*.bin | head -1`.strip
  #latest_bin = Dir.glob('../build/*.bin').sort{ |a,b| File.new(a).stat <=> File.new(b).stat }.last
  vdi_file = 'nycmesh-qmp-openwrt.vdi'
  system "vboxmanage convertfromraw --format vdi #{latest_bin} #{vdi_file}" unless File.exist?(vdi_file)
  config.vm.box = "centos/7"
  config.vm.network "public_network"
  config.vm.network "private_network", type: "dhcp"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "64"
    # add the newly created build disk firmware
    #vb.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 0, '--device', 0, '--type', 'hdd', '--medium', "none"]
    vb.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 0, '--device', 0, '--type', 'hdd', '--medium', "nycmesh-qmp-openwrt.vdi"]
    vb.customize ["modifyvm", :id, "--ostype", "Linux26"]
  end
  config.vm.provision "shell", inline: "" do
    # delete the placeholder dummy hdd image. there should be a better way.
    # NO WAY TO GET THE DUMMY HDD FILE NAME AFTER THE INSTANCE IS CREATED AND BEFORE THE NEW VDI IS INSTALLED!
    # id = File.read(".vagrant/machines/default/virtualbox/id") 
    # hdd_file = `vboxmanage showvminfo #{id}`.split(/\n/).grep(/IDE \(0, 0\): /).first.split(/ /)[3]
    # puts hdd_file
    # File.delete(hdd_file) if hdd_file.index 'centos-7-1-1.x86_64.vmdk'
  end
end