VirtualBox on the Command Line: Renaming a Virtual Hard Drive

The invisible Programmer Antagonist who lives in my head, who is an expert in all technologies including VirtualBox and knows all Best Practices under the sun, asks me “Why do you want to rename a virtual hard drive? Have you considered that the renaming of hard drive is not a feature of VirtualBox for a reason?” The Programmer Antagonist inside my head doesn’t actually know the reason because he’s self-constructed and I personally don’t know why it’s a bad idea, but I give developers the benefit of the doubt so “surely there is one”; feel free to provide your ideas. Needless to say, though, this inner critic has made me drag my feet about making this, my last of the “VirtualBox on the Command Line” posts (for now – this covers the scope of the stuff I’ve messed around with). I feel nervous about sharing because it’s a hack, but at the same time, hacks can often save the day, especially if it’s as a result of a feature that is not built in but you still need to use (I’ve also had to develop similar hacks for my company’s version of redmine for features the devs have probably left out of the UI for good reason – like removing watchers that are not myself from tickets and deleting write-lock comments that won’t get emailed to stakeholders as a result of the write-lock.)

The truth is, a few months back I ran into a pretty good use case for renaming hard drives. I cracked into a pre-configured VirtualBox system and found a set of machines with names akin to these:


When I listed the hard drives, I’d get something like this:


Really, I’ve just made up some names here and introduced some arbitrary inconsistencies, but take a look at these and you get an idea that there was supposed to be a consistent naming scheme to these machines and hard drives and that the current names clearly don’t follow it.

Consistency is pretty easy to add to the machines themselves because VM renaming is easy. Not so with renaming the hard drives. And the other problem with renaming the hard drives isn’t just for consistency’s sake – when I dug deeper, I found configurations between machine and hard drive that were doomed to confuse any person who tried to administer the system in the future. By this I mean that a machine like stageweb_a would be pointing to the hard drive named stageweb-d.some_subnet instead of the logical stageweb_a hard drive. So things were bonkers.

Anyhow, let’s say we want to rename a hard drive from “horrible_name.vdi” to “excellent_name.vdi”. ┬áIts parent machine (the VM) has already been renamed to “excellent_machine”.

Start by shutting down the parent machine and detaching the hard drive:

VBoxManage -q controlvm excellent_name poweroff

VBoxManage -q storageattach excellent_name --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium none

Now, clone the hard drive and give the cloned hard drive the new name. This is the “hack” for renaming.

VBoxManage clonehd horrible_name.vdi excellent_name.vdi --format VDI --remember

(The –remember option “opens” the hard drive and otherwise registers it with the VirtualBox system on the host machine.)

Now, mount the new hard drive to the host machine, and restart the host machine:

VBoxManage -q storageattach excellent_name --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium excellent_name.vdi

VBoxHeadless -s excellent_name &

Confirm that you can access the machine with the cloned hard drive attached. If this is the case, you can remove the old hard drive:

VBoxManage -q closemedium disk horrible_name.vdi

rm /home/addie/.VirtualBox/HardDrives/horrible_name.vdi

(Obviously, you’d be in your own home directory here and not mine.)

This is clearly not perfect, but given everything I’ve documented so far, it’s actually pretty repetitive of stuff that’s already been done. Still, it’s worth it to document the actual process just to show how the renaming of a virtual hard drive is possible – albeit by cloning, which preserves the content but not unique identifiers like the UUID. For me, the content is more than enough, so this has worked for me in cleaning up my VirtualBox systems.

Thanks for reading and hope these commands have been of some help!

This entry was posted in Today I Learned... and tagged , , . Bookmark the permalink.

4 Responses to VirtualBox on the Command Line: Renaming a Virtual Hard Drive

  1. Vbox user says:

    I came across this page, and your site, when I was trying to find a command built into vbox for renaming drive images.

    What I have found is; after you release the drive if you rename the file, and then reattach everything works fine. Cloning disks just takes soooo long.

    Thanks for pointing me in the right direction.

    • addie says:

      Thanks for the tip – you’re totally correct, the renaming / reattaching is definitely a more straightforward route. Glad I was able to point you in the right direction; it can be frustrating that learning these things requires a little bit of experimentation, but I’m glad you’ve shared your more efficient approach.

    • Alain says:

      My two cents.
      Only releasing the drive from the vm is not sufficient, as when reattaching it with the new vdi file name, VBox will see an UUID conflict. After it has been released, the drive must be closed with the closemedium command, so it is “forgotten” by VBox. Only then the vdi file can be renamed and the drive reattached.
      All this can be achieved through the VBox GUI and the Virtual Media Manager GUI.

  2. Martin Bring says:

    Thanks, I needed to do just that.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>