【问题标题】:Vagrant - set the location of the virtual hard drive for VirtualBoxVagrant - 为 VirtualBox 设置虚拟硬盘的位置
【发布时间】:2016-03-20 12:15:15
【问题描述】:

我在D:\vagrant\precise32\02-lamp\目录中执行了以下命令(在 Windows 上,使用 Git Bash)

$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
$ vagrant init precise32
$ vagrant up

注意。我没有更改原始的 Vagrantfile。

我认为目录D:\vagrant\precise32\02-lamp\ 将是VDI 类文件的位置,但事实并非如此。工作目录用作共享文件夹

我找到了 Vagrant 盒子的位置 C:\Users\USER\.vagrant.d\boxes\precise32\0\virtualbox

根据Where is Vagrant saving changes to the VM,我在VirtualBox GUI 中找到了虚拟硬盘文件的位置。这是

C:\Users\USER\VirtualBox VMs\02-lamp_default_1458429875795_57100\

我不想把这个文件放在系统驱动器C:\,而是放在数据驱动器D:\。 如何设置这样的 vagrant 配置?

【问题讨论】:

    标签: vagrant vagrantfile


    【解决方案1】:

    对于 VirtualBox,您可以通过 GUI 的“首选项”对话框更改默认机器文件夹的位置。

    This guide,虽然已经使用了几年,但工作正常,我上周关注了如何将现有的 vagrant/VirtualBox 驱动器移动到新位置。

    编辑

    我已经引用了上述链接/指南中的步骤,以供后代使用:

    • ~/.vagrant.d 移动到外部驱动器。我把它改名为vagrant_home 所以 不用 ls -a 我也能看到。

    • VAGRANT_HOME 设置为 /path/to/drive/vagrant_home~/.bash_profile

    • 打开 VirtualBox 应用程序,打开首选项,并将其默认机器文件夹设置为 /path/to/drive/VirtualBox VMs

    • 关闭 VirtualBox。

    • 移动你的 VirtualBox VMs 文件夹到驱动器。重新打开 VirtualBox。你会看到的 您的虚拟机被列为“不可访问”。从列表中删除它们。

    • 对于 外部驱动器上VirtualBox VMs 文件夹中的每个 VM,浏览 到Finder中的文件夹并双击.vbox文件以恢复它 到 VirtualBox 管理器。 (还有比这更简单的方法吗?)

    • 最后,移动你创建的任何现有 Vagrant 目录 vagrant init(这些是每个目录中都有一个Vagrantfile)到 外部驱动器。由于这些目录只存储您的元数据 可以将它们留在您的主驱动器上,但最好保留所有内容 在一起,这样你就可以很容易地将整个驱动器插入另一个 机器并从那里启动您的虚拟机。

    【讨论】:

    • 对我不起作用。我将文件夹复制到 D:\Vbox,但出现错误无法注册硬盘 'D:\vbox\02-lamp_default_1458429875795_57100\box-disk1.vmdk' {fbc6d122-8c9e-4e6d-9bcc-a466203f1968},因为 UUID {fbc6d122-8c9e-4e6d-9bcc-a466203f1968} 的硬盘 'C:\Users\USER\VirtualBox VMs\02-lamp_default_1458429875795_57100\box-disk1.vmdk' 已经存在。
    • @mwloda 遵循指南 - 您需要从 vbox gui 中删除所有机器,然后双击 *.vbox 并等待几秒钟以重新添加它们 :) 我昨天才这样做,它有效。
    • 我从 VirtualBox GUI 中删除了 VM,但是当我尝试打开 .vbox 文件时,我收到了关于带有 UUID 的现有硬盘的相同消息。看起来虽然在 GUI 中单击“删除”选项,但它仍然记得原来的 Box。 编辑。主机重启有帮助
    • @mwloda 那么问题现在解决了吗?
    • 好吧,我认为可以在创建 VM 之前在 Vagrantfile 中设置正确的位置。您的解决方案有效,现在即使不重新启动,计算机也奇怪1. 使用vagrant up 创建虚拟机。 2.打开Oracle VirtualBox Manager,查看原位置Settings/Storage/StorageTree/Controller:SATA。控制器3.Oracle VirtualBox Manager 中删除 VM,4. 将整个 VM 目录 C:\Users\USER\VirtualBox VMs\02-lamp_default 移动到新位置。 5. 双击 *.vbox 文件,打开 Oracle VirtualBox Manager6. vagrant 又起来了
    【解决方案2】:

    当您总是想在导入期间更改 Virtualbox 创建 VM 的位置时,也可以通过 CLI 执行此操作(因为 Virtualbox 通常希望将它们放在一个位置,而不是像 VMware 那样跟踪它们在磁盘上的任何位置这样做)。

    请注意,通过 GUI 或 CLI 更改此设置不会移动现有虚拟机,它只会设置一个新路径以供下一台导入/创建的机器使用。如果您有想要移动的现有机器,您可以关闭并关闭 Virtualbox 的所有实例,然后从 cmd/shell 窗口使用mv /old/path /new/path 或将文件夹剪切并粘贴到 GUI 中的新位置,然后更改machinefolder 到该路径并打开 Virtualbox,它应该会检测到所有现有的虚拟机。

    如果您有大量用户需要将虚拟机路径移出其主目录以避免自动备份大型文件,则使用 CLI 可以更轻松地编写脚本/自动化。虚拟机的“最佳”位置在一定程度上取决于您的系统,但/usr/local/ 可能是在 macOS 或 Linux 上创建新文件夹的好位置。

    # Look at the current path
    
    vboxmanage list systemproperties | grep machine
    
    # Output (commented for easier copying and pasting of commands)
    # Default machine folder:          /Users/<YourUser>/VirtualBox VMs
    
    # Set it to a different folder in your home aka ~
    # If you user has access to the path and can create files/folders, then
    # the folder doesn't need to exist beforehand, Virtualbox will create it
    
    vboxmanage setproperty machinefolder ~/VirtualMachines
    
    # No output produced
    
    vboxmanage list systemproperties | grep machine
    
    # Output (commented for easier copying and pasting of commands)
    # Default machine folder:          /Users/<YourUser>/VirtualMachines
    

    您也可以将其设置为家庭以外的文件夹,但这通常需要创建文件夹并在 Virtualbox 可以使用之前修复权限。

    # [Optional] Only needed if moving out of the home directory to
    # a place the user doesn't have permission to access by default
    sudo mkdir -p /usr/local/VirtualMachines && \
    sudo chown -R ${USER} /usr/local/VirtualMachines
    
    # If you add : like this `${USER}:` to the above, instead of 
    # setting the group to admin or wheel it will use the user's default group
    # which can be seen by running `id -g`
    vboxmanage setproperty machinefolder /usr/local/VirtualMachines
    
    # No output produced
    
    vboxmanage list systemproperties | grep machine
    
    # Output (commented for easier copying and pasting of commands)
    # Default machine folder:          /usr/local/VirtualMachines
    

    如果您改变主意,可以轻松将其设置回默认值,但您需要自己将虚拟机重新移回。

    vboxmanage setproperty machinefolder default
    
    vboxmanage list systemproperties | grep machine
    
    # Output (commented for easier copying and pasting of commands)
    # Default machine folder:          /Users/<YourUser>/VirtualBox VMs
    

    【讨论】:

      【解决方案3】:

      对于外部驱动器上 VirtualBox VMs 文件夹中的每个 VM,在 Finder 中浏览到其文件夹,然后双击 .vbox 文件将其恢复到 VirtualBox Manager。 (还有比这更简单的方法吗?)

      有一个更简单的方法... 进入 VirtualBox Manager GUI,点击 Machine > Add 并找到您想要添加回来的 .vbox。

      【讨论】:

        猜你喜欢
        • 2013-07-15
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-14
        相关资源
        最近更新 更多