【问题标题】:How to get the VM's directory on Vagrant如何在 Vagrant 上获取 VM 的目录
【发布时间】:2019-03-02 04:23:52
【问题描述】:

我需要根据一些外部配置在我的 VirtualBox VM 上创建一些额外的磁盘,但这些磁盘必须是独立的,我的意思是,在 VM 的目录下。我成功地做到了,但是强制使用 VM 名称(使其可预测),但是,由于某些限制,我无法在我的环境中强制使用该名称。有些人会同时在不同的分支和不同的目录中工作,对于这种特殊情况,强制命名不是一个好主意,它需要是动态的。任何解决此问题的想法将不胜感激。

Vagrant.require_version ">= 1.9.7"

Vagrant.configure('2') do |config|

  config.vm.define "mylinux-vm", autostart: false do |this|

    this.vm.box = "ubuntu/xenial64"
    this.vm.hostname = "my-linuxvm"
    this.vm.provider :virtualbox do |vb|
      vb.memory = 2048; 
      vb.cpus = 2

      # What is the directory where this VM is going to be created?
      # I need to create other disk files under the VM directory
      # is going to be something like:
      #    $HOME/VirtualBox VMs/<current_dir_name>_mylinux-vm_<timestamp>
      # How can I get this directory?
    end
  end
end

【问题讨论】:

    标签: ruby vagrant virtualbox provisioning vagrantfile


    【解决方案1】:

    您可以通过执行以下操作来设置该文件夹的名称,并在您的 Vagrantfile 中引用它:

    vb_memory = 2048
    vb_cpus = 2
    vb_name = "mylinuxvm-foobar-mem#{vb_memory}-cpu#{vb_cpus}"
    
    ...
    
    this.vm.provider :virtualbox do |vb|
      vb.memory = vb_memory; 
      vb.cpus = vb_cpus
      vb.name = vb_name  
      # Setting the above would create a directory like this : 
      # $HOME/VirtualBox VMs/mylinuxvm-foobar-mem2048-cpu2
    end
    

    这将使文件夹的名称保持唯一,您也可以在整个 Vagrantfile 中将其称为 vb_name

    【讨论】:

    • 我使用这种方法使它工作:强制使用 VM 名称(使其可预测),但是,由于某些限制,我无法在我的环境中执行此操作
    • 请详细说明您所指的限制。
    • 限制是 我不能强制使用名称,因为最终有人可能希望在他们的工作站上运行配置任务,如果他们已经有一个带有VirtualBox 上的名称,我需要它是动态的。
    • 好的,你的意思是你的名字必须是独一无二的,而且是可预测的,对吗?
    • 独一无二,是的。只要我有办法从某个地方读取此属性,我就会对此感到高兴。
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多