【问题标题】:Vagrant VirtualBox second disk pathVagrant VirtualBox 第二个磁盘路径
【发布时间】:2014-01-29 18:58:57
【问题描述】:

我有 Vagrant + VirtualBox。

在我的 Vagrantfile 中有

config.vm.provider "virtualbox" do |v|
    v.customize [ "createhd", "--filename", "disk", "--size", 100000 ]
    v.customize [ 'storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "disk"]
end

当我启动 vagrant up 时,它会在 C:\HashiCorp\Vagrant\bin\disk

VBoxManage.exe: error: Could not find file for the medium 'C:\HashiCorp\Vagrant\bin\disk' (VERR_FILE_NOT_FOUND)

我希望磁盘与虚拟机的第一个磁盘一起存在 C:\Users\jma47\VirtualBox VMs\bin_build_1389371691

如何在 Vagrantfile 中执行此操作?

【问题讨论】:

    标签: virtualbox vagrant disk provider


    【解决方案1】:

    如果您为虚拟机定义名称,则可以这样做:

    # Try to make it work on both Windows and Linux
    if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
      vboxmanage_path = "C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe"
    else
      vboxmanage_path = "VBoxManage" # Assume it's in the path on Linux
    end
    
    Vagrant.configure(2) do |config|
      config.vm.box = "debian/wheezy64"
    
      config.vm.provider "virtualbox" do |vb|
        vb.name = "VM Name"
    
        # Get disk path
        line = `"#{vboxmanage_path}" list systemproperties`.split(/\n/).grep(/Default machine folder/).first
        vb_machine_folder = line.split(':', 2)[1].strip()
        second_disk = File.join(vb_machine_folder, vb.name, 'disk2.vdi')
    
        # Create and attach disk
        unless File.exist?(second_disk)
          vb.customize ['createhd', '--filename', second_disk, '--format', 'VDI', '--size', 60 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 0, '--device', 1, '--type', 'hdd', '--medium', second_disk]
      end
    end
    

    【讨论】:

    • 是否有机会让默认机器文件夹可移植,即也可以在 Windows 上工作?
    • 要在 Windows 中使用它,请从 cygwin 运行 vagrant up 命令。
    • 为了使其可移植更改此行:line = `vboxmanage list systemproperties`.split(/\n/).grep(/Default machine folder/).first
    • 我发现这不太奏效,因为 1) 我在路径中没有 VBoxManage 并且 2) 它在“C:\...”中的 : 处拆分路径。要解决这个问题:vboxmanage_path = "C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe" 然后 line = "#{vboxmanage_path}" list systemproperties.split(/\n/).grep(/Default machine folder/).first 然后 vb_machine_folder = line.split(':', 2)[1].strip()
    【解决方案2】:

    你需要在你的 Vagrantfile 中使用这样的东西:

    对于 Vagrant API v1:

    # Where to store the disk file
    disk = 'C:\Users\jma47\VirtualBox VMs\bin_build_1389371691\extra_disk.vdi'
    
    Vagrant::Config.run do |config|
      config.vm.box = 'base'
    
      config.vm.provider "virtualbox" do | v |
        unless File.exist?(disk)
          config.vm.customize ['createhd', '--filename', disk, '--size', 500 * 1024]
        end
        config.vm.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
      end
    end
    

    对于 Vagrant API v2:

    # Where to store the disk file
    disk = 'C:\Users\jma47\VirtualBox VMs\bin_build_1389371691\extra_disk.vdi'
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = 'base'
    
      config.vm.provider "virtualbox" do | p |
        unless File.exist?(disk)
          p.customize ['createhd', '--filename', disk, '--size', 1 * 1024]
        end
        p.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
      end
    end
    

    【讨论】:

    • 对于任何路过这里的人来说,“SATA 控制器”将根据盒子/操作系统的期望而改变。 “SATA”是另一种常见的选择。
    • ==> default: Box 'base' could not be found. Attempting to find and install...Couldn't open file /C:/Users/Chloe/workspace/project/base
    • @Chloe 将 base 替换为您想要开始的任何 vagrant base box,例如。 debian/wheezy64
    • 这是我自己生成的嵌入式系统。没有流浪者基地。 dev.qmp.cat/projects/qmp/wiki/Virtualbox
    • 我认为 Vagrant 需要一个盒子 - 但它们很容易从现有的 VM 中制作出来:vagrantup.com/docs/virtualbox/boxes.html
    【解决方案3】:

    “磁盘”参数应该是一个路径,Virtualbox需要它来存储第二个磁盘。

    使用绝对值,例如“c:\temp.disk”或“/tmp/disk.img”

    【讨论】:

    • 也不要在路径前加上多余的 './' - 我发现 'tmp/file.vdi' 与 './tmp/file.vdi' 相比存在问题,这本质上是相同的文件,但在全新安装时偶尔会导致 createhd 出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2018-09-01
    相关资源
    最近更新 更多