Answers: <p>It looks like that there is a name conflict:</p>
  
  <pre><code>The name of your virtual machine couldn't be set because VirtualBox
  is reporting another VM with that name already exists. Most of the
  time, this is because of an error with VirtualBox not cleaning up
  properly. To fix this, verify that no VMs with that name do exist
  (by opening the VirtualBox GUI). If they don't, then look at the
  folder in the error message from VirtualBox below and remove it
  if there isn't any information you need in there.
  </code></pre>
  
  <p>According to <a href="https://github.com/hashicorp/vagrant/issues/1817#issuecomment-20976959" rel="nofollow noreferrer">@xofer on Github</a> on has to set the <code>name</code>. However in the example you provided in conjunction with the logging, the issue seems to be caused by a VM that was created and still exists in Virtualbox. In other words one has to ensure that the VM has been removed or another name has to be defined to prevent this collision.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-25T08:52:19.270Z
Answers: 2019-12-25T08:52:19.270Z
Answers: 030
Body: <p>I'm building my Windows server 2012 vm with packer and when I'm doing a <code>vagrant up</code> it's failing because the folder name already exists…</p>
  
  <p>Here is my json file for packer</p>
  
  <pre><code>{
    "builders": [
      {
        "boot_wait": "2m",
        "communicator": "winrm",
        "disk_size": "{{user `disk_size`}}",
        "floppy_files": [
          "{{user `autounattend`}}",
          "./scripts/disable-winrm.ps1",
          "./scripts/enable-winrm.ps1"
        ],
        "guest_additions_mode": "disable",
        "guest_os_type": "Windows2012_64",
        "headless": "{{user `headless`}}",
        "iso_checksum": "{{user `iso_checksum`}}",
        "iso_checksum_type": "{{user `iso_checksum_type`}}",
        "iso_url": "{{user `iso_url`}}",
        "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
        "type": "virtualbox-iso",
        "vboxmanage": [
          [
            "modifyvm",
            "{{.Name}}",
            "--memory",
            "2048"
          ],
          [
            "modifyvm",
            "{{.Name}}",
            "--nic1",
            "NAT"
          ],
          [
            "modifyvm",
            "{{.Name}}",
            "--cpus",
            "2"
          ]
        ],
        "vm_name": "windows_2012_r2",
        "winrm_password": "vagrant",
        "winrm_timeout": "{{user `winrm_timeout`}}",
        "winrm_username": "vagrant"
      }
    ],
    "post-processors": [
      {
        "keep_input_artifact": false,
        "output": "vagrantenv/windows_2012_r2_{{.Provider}}.box",
        "type": "vagrant",
        "vagrantfile_template": "vagrantfile-windows_2012_r2.template"
      }
    ],
    "provisioners": [
      {
        "scripts": [
          "./scripts/vm-guest-tools.bat",
          "./scripts/enable-rdp.bat"
        ],
        "type": "windows-shell"
      }
    ],
    "variables": {
      "autounattend": "./answer_files/2012_r2/Autounattend.xml",
      "disk_size": "61440",
      "disk_type_id": "1",
      "headless": "true",
      "iso_checksum": "5b5e08c490ad16b59b1d9fab0def883a",
      "iso_checksum_type": "md5",
      "iso_url": "./iso/windows_2012_r2.iso",
      "winrm_timeout": "30m"
    }
  }
  
  </code></pre>
  
  <p>And my <code>post-processors</code> template</p>
  
  <pre><code># -*- mode: ruby -*-
  # vi: set ft=ruby :
  
  Vagrant.require_version "&gt;= 1.6.2"
  
  Vagrant.configure("2") do |config|
      config.vm.box = "windows_2012_r2"
      config.vm.communicator = "winrm"
  
      # Admin user name and password
      config.winrm.username = "vagrant"
      config.winrm.password = "vagrant"
  
      config.vm.guest = :windows
      config.windows.halt_timeout = 15
  
      config.vm.network :forwarded_port, guest: 3389, host: 3389, id: "rdp", auto_correct: true
  
      config.vm.provider :virtualbox do |v, override|
          #v.gui = true
          v.name = "windows_2012_r2"
          v.customize ["modifyvm", :id, "--memory", 2048]
          v.customize ["modifyvm", :id, "--cpus", 2]
          v.customize ["modifyvm", :id, "--vram", 128]
          v.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
          v.customize ["setextradata", "global", "GUI/SuppressMessages", "all" ]
          end
  end
  
  </code></pre>
  
  <p>This steps works porperly and I end up with a file <code>windows_2012_r2_virtualbox.box</code></p>
  
  <p>Then I run <code>vagrant init -f windows_2012_r2 windows_2012_r2_virtualbox.box</code></p>
  
  <p>and finally <code>vagrant up</code></p>
  
  <p>following this I'm getting the error:</p>
  
  <pre><code>Bringing machine 'default' up with 'virtualbox' provider...
  ==&gt; default: Box 'windows_2012_r2' could not be found. Attempting to find and install...
      default: Box Provider: virtualbox
      default: Box Version: &gt;= 0
  ==&gt; default: Box file was not detected as metadata. Adding it directly...
  ==&gt; default: Adding box 'windows_2012_r2' (v0) for provider: virtualbox
      default: Unpacking necessary files from: file:///D:/Dev/packer-windows/vagrantenv/windows_2012_r2_virtualbox.box
      default:
  ==&gt; default: Successfully added box 'windows_2012_r2' (v0) for 'virtualbox'!
  ==&gt; default: Importing base box 'windows_2012_r2'...
  ==&gt; default: Matching MAC address for NAT networking...
  ==&gt; default: Setting the name of the VM: windows_2012_r2
  The name of your virtual machine couldn't be set because VirtualBox
  is reporting another VM with that name already exists. Most of the
  time, this is because of an error with VirtualBox not cleaning up
  properly. To fix this, verify that no VMs with that name do exist
  (by opening the VirtualBox GUI). If they don't, then look at the
  folder in the error message from VirtualBox below and remove it
  if there isn't any information you need in there.
  
  VirtualBox error:
  
  VBoxManage.exe: error: Could not rename the directory 'D:\VM\VirtualBox\windows_2012_r2_1556359923004_28925' to 'D:\VM\VirtualBox\windows_2012_r2' to save the settings file (VERR_ALREADY_EXISTS)
  VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component SessionMachine, interface IMachine, callee IUnknown
  VBoxManage.exe: error: Context: "SaveSettings()" at line 3194 of file VBoxManageModifyVM.cpp
  
  </code></pre>
  
  <p>I notice that at the stage </p>
  
  <ul>
  <li><code>Successfully added box 'windows_2012_r2' (v0) for 'virtualbox'!</code> vagrant creates the folder <strong>windows_2012_r2</strong> in the virtualbox folder
  and then at this stage </li>
  <li><p><code>default: Importing base box 'windows_2012_r2'...</code> vagrant creates a folder <strong>windows_2012_r2_1556359923004_28925</strong> and try to rename it to <strong>windows_2012_r2</strong> but doesn't manage to it because it already exist.</p></li>
  <li><p><strong>windows_2012_r2</strong> folder has the <em>windows_2012_r2-disk001_2.vmdk</em> file  </p></li>
  <li><strong>windows_2012_r2_1556359923004_28925</strong> has windows_2012_r2_1556359923004_28925.vbox file</li>
  </ul>
  
  <p>I'm confused how vagrant manage the Virtualbox name when importing a packer created box why vagrant is renaming the folder name, and not using the already existing target folder?</p>
  
Tags: <vagrant><packer><virtualbox>
Title: Understanding Vagrant virtual machine import
UserDisplayName: Matthieu Ducorps
ThreadText: # Understanding Vagrant virtual machine import
  
  (Asked by: username_0 on 2019-04-28)
  
  I'm building my Windows server 2012 vm with packer and when I'm doing a `vagrant up` it's failing because the folder name already exists…
  
  Here is my json file for packer
  
  ```
  {
    "builders": [
      {
        "boot_wait": "2m",
        "communicator": "winrm",
        "disk_size": "{{user `disk_size`}}",
        "floppy_files": [
          "{{user `autounattend`}}",
          "./scripts/disable-winrm.ps1",
          "./scripts/enable-winrm.ps1"
        ],
        "guest_additions_mode": "disable",
        "guest_os_type": "Windows2012_64",
        "headless": "{{user `headless`}}",
        "iso_checksum": "{{user `iso_checksum`}}",
        "iso_checksum_type": "{{user `iso_checksum_type`}}",
        "iso_url": "{{user `iso_url`}}",
        "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
        "type": "virtualbox-iso",
        "vboxmanage": [
          [
            "modifyvm",
            "{{.Name}}",
            "--memory",
            "2048"
          ],
          [
            "modifyvm",
            "{{.Name}}",
            "--nic1",
            "NAT"
          ],
          [
            "modifyvm",
            "{{.Name}}",
            "--cpus",
            "2"
          ]
        ],
        "vm_name": "windows_2012_r2",
        "winrm_password": "vagrant",
        "winrm_timeout": "{{user `winrm_timeout`}}",
        "winrm_username": "vagrant"
      }
    ],
    "post-processors": [
      {
        "keep_input_artifact": false,
        "output": "vagrantenv/windows_2012_r2_{{.Provider}}.box",
        "type": "vagrant",
        "vagrantfile_template": "vagrantfile-windows_2012_r2.template"
      }
    ],
    "provisioners": [
      {
        "scripts": [
          "./scripts/vm-guest-tools.bat",
          "./scripts/enable-rdp.bat"
        ],
        "type": "windows-shell"
      }
    ],
    "variables": {
      "autounattend": "./answer_files/2012_r2/Autounattend.xml",
      "disk_size": "61440",
      "disk_type_id": "1",
      "headless": "true",
      "iso_checksum": "5b5e08c490ad16b59b1d9fab0def883a",
      "iso_checksum_type": "md5",
      "iso_url": "./iso/windows_2012_r2.iso",
      "winrm_timeout": "30m"
    }
  }
  
  ```
  
  And my `post-processors` template
  
  ```
  # -*- mode: ruby -*-
  # vi: set ft=ruby :
  
  Vagrant.require_version ">= 1.6.2"
  
  Vagrant.configure("2") do |config|
      config.vm.box = "windows_2012_r2"
      config.vm.communicator = "winrm"
  
      # Admin user name and password
      config.winrm.username = "vagrant"
      config.winrm.password = "vagrant"
  
      config.vm.guest = :windows
      config.windows.halt_timeout = 15
  
      config.vm.network :forwarded_port, guest: 3389, host: 3389, id: "rdp", auto_correct: true
  
      config.vm.provider :virtualbox do |v, override|
          #v.gui = true
          v.name = "windows_2012_r2"
          v.customize ["modifyvm", :id, "--memory", 2048]
          v.customize ["modifyvm", :id, "--cpus", 2]
          v.customize ["modifyvm", :id, "--vram", 128]
          v.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
          v.customize ["setextradata", "global", "GUI/SuppressMessages", "all" ]
          end
  end
  
  ```
  
  This steps works porperly and I end up with a file `windows_2012_r2_virtualbox.box`
  
  Then I run `vagrant init -f windows_2012_r2 windows_2012_r2_virtualbox.box`
  
  and finally `vagrant up`
  
  following this I'm getting the error:
  
  ```
  Bringing machine 'default' up with 'virtualbox' provider...
  ==> default: Box 'windows_2012_r2' could not be found. Attempting to find and install...
      default: Box Provider: virtualbox
      default: Box Version: >= 0
  ==> default: Box file was not detected as metadata. Adding it directly...
  ==> default: Adding box 'windows_2012_r2' (v0) for provider: virtualbox
      default: Unpacking necessary files from: file:///D:/Dev/packer-windows/vagrantenv/windows_2012_r2_virtualbox.box
      default:
  ==> default: Successfully added box 'windows_2012_r2' (v0) for 'virtualbox'!
  ==> default: Importing base box 'windows_2012_r2'...
  ==> default: Matching MAC address for NAT networking...
  ==> default: Setting the name of the VM: windows_2012_r2
  The name of your virtual machine couldn't be set because VirtualBox
  is reporting another VM with that name already exists. Most of the
  time, this is because of an error with VirtualBox not cleaning up
  properly. To fix this, verify that no VMs with that name do exist
  (by opening the VirtualBox GUI). If they don't, then look at the
  folder in the error message from VirtualBox below and remove it
  if there isn't any information you need in there.
  
  VirtualBox error:
  
  VBoxManage.exe: error: Could not rename the directory 'D:\VM\VirtualBox\windows_2012_r2_1556359923004_28925' to 'D:\VM\VirtualBox\windows_2012_r2' to save the settings file (VERR_ALREADY_EXISTS)
  VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component SessionMachine, interface IMachine, callee IUnknown
  VBoxManage.exe: error: Context: "SaveSettings()" at line 3194 of file VBoxManageModifyVM.cpp
  
  ```
  
  I notice that at the stage
  
  * `Successfully added box 'windows_2012_r2' (v0) for 'virtualbox'!` vagrant creates the folder **windows\_2012\_r2** in the virtualbox folder
    and then at this stage
  * `default: Importing base box 'windows_2012_r2'...` vagrant creates a folder **windows\_2012\_r2\_1556359923004\_28925** and try to rename it to **windows\_2012\_r2** but doesn't manage to it because it already exist.
  * **windows\_2012\_r2** folder has the *windows\_2012\_r2-disk001\_2.vmdk* file
  * **windows\_2012\_r2\_1556359923004\_28925** has windows\_2012\_r2\_1556359923004\_28925.vbox file
  
  I'm confused how vagrant manage the Virtualbox name when importing a packer created box why vagrant is renaming the folder name, and not using the already existing target folder?
  
  **Tags:** vagrant, packer, virtualbox
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-25. Score: 2 | Accepted answer
  
  It looks like that there is a name conflict:
  
  ```
  The name of your virtual machine couldn't be set because VirtualBox
  is reporting another VM with that name already exists. Most of the
  time, this is because of an error with VirtualBox not cleaning up
  properly. To fix this, verify that no VMs with that name do exist
  (by opening the VirtualBox GUI). If they don't, then look at the
  folder in the error message from VirtualBox below and remove it
  if there isn't any information you need in there.
  
  ```
  
  According to [@xofer on Github](https://github.com/hashicorp/vagrant/issues/1817#issuecomment-20976959) on has to set the `name`. However in the example you provided in conjunction with the logging, the issue seems to be caused by a VM that was created and still exists in Virtualbox. In other words one has to ensure that the VM has been removed or another name has to be defined to prevent this collision.
  
  
  ---
  
  