【问题标题】:How do I provision a Vagrant box before it's been instantiated?如何在实例化之前配置 Vagrant 框?
【发布时间】:2018-03-29 11:57:28
【问题描述】:

我想从另一个 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 创建一个实例

我有这个 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.

流浪者 2.0.1

【问题讨论】:

    标签: vagrant virtualbox


    【解决方案1】:

    我发现我可以指定一个占位符框,并且可以在它启动之前将其换出,但它仍然需要复制千兆字节大小的图像,并且当您使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      相关资源
      最近更新 更多