簡體   English   中英

如何在實例化之前准備一個Vagrant框?

[英]How do I provision a Vagrant box before it's been instantiated?

我想從另一個Vagrant框的輸出中創建一個Vagrant框。 我想在Vagrant內部進行。 我想運行vagrant initvagrant up ,並執行以下步驟:

  1. 運行此命令以從.bin轉換為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 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM