Vagrant VirtualBox第二个磁盘路径

7

我带有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目录下寻找"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中实现这一目标?

3个回答

11

如果你为虚拟机定义一个名称,就可以做到这一点:

# 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上也能工作? - Kombajn zbożowy
1
要在Windows中使用它,请从Cygwin运行您的vagrant up命令。 - Jepper
1
为了使其可移植,请更改此行:line = \vboxmanage list systemproperties`.split(/\n/).grep(/Default machine folder/).first`。 - Chloe
我发现这个方法不太可行,因为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() - EM0

8
你需要在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

2
对于其他路过的人,“SATA控制器”将根据盒子/操作系统的期望而改变。 “SATA”是另一个常见选项。 - nelsonda
==> default: Box 'base' could not be found. Attempting to find and install... and Couldn't open file /C:/Users/Chloe/workspace/project/base - Chloe
@Chloe 用你想要的任何vagrant基础盒子替换base,例如debian/wheezy64 - Duncan Lock
这是我自己生成的嵌入式系统。没有Vagrant基础。http://dev.qmp.cat/projects/qmp/wiki/Virtualbox - Chloe
我认为Vagrant需要一个盒子 - 但是你可以很容易地从现有的虚拟机自己制作一个:https://www.vagrantup.com/docs/virtualbox/boxes.html - Duncan Lock
在我的 Centos/7 系统上,使用 IDE 替代 SATA 控制器 对我很有效。通常情况下,您也可以使用 VirtualBox GUI 在 设置->存储 菜单中查找控制器的名称(在访问之前需要关闭虚拟机)。 - Dirk

0

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

使用绝对路径,例如"c:\temp.disk"或者"/tmp/disk.img"。


1
不要在路径前面加上多余的'./',我曾经遇到过一个问题,即'tmp/file.vdi'与'./tmp/file.vdi'之间存在问题,尽管它们实际上是相同的文件,但在干净安装期间使用vagrant up时会偶尔出现createhd问题。 - Steve Grove

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接