Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Discussions related to using VirtualBox on Linux hosts.
Post Reply
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

Hi,

I want to store an image of actual hardware HDDs containing a standards Ubuntu installation (no VM, an actual physical drive bootable in a physical pc).
So I do convert the drive to vdi:

Code: Select all

sudo qemu-img convert -O vdi /dev/sdh xxx.vdi -p
This works fine an is in fact bootable in virtualbox. But now I want to compact the image:

Code: Select all

vboximg-mount -i xxx.vdi /media/me/backup/
zerofree -v /media/me/backup/vol0
This works too.
But then!

Code: Select all

vboxmanage modifymedium --compact xxx.vdi
This fails with

VBoxManage: error: Failed to lock media when compacting 'xxx.vdi'
VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MediumWrap, interface IMedium, callee nsISupports
VBoxManage: error: Context: "Compact(pProgress.asOutParam())" at line 791 of file VBoxManageDisk.cpp
VBoxManage: error: Failed to compact medium!


Why? What does this error even mean? Of course, the VM is turned off + shutdown and no snapshots exist.

I am on Ubuntu 20.04 with latest vbox 6.1.36.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by mpack »

"Invalid object state" means to me that the VM is still running, though perhaps suspended, snapshotted etc. All of which would disable messing with the low level structure of the VDI file.

Or is the drive created with a fixed size? Fixed size drives can't be compacted. I also don't recognize the use of qemu-img for this task, e.g. the fact that you gave the file a .vdi extension doesn't mean it's really a VDI. That question could be settled using VBoxManage showmediuminfo.

Try cloning the VDI using CloneVDI, and click the "Compact" option. If you did it this way to begin with then in fact there was no need to run zerofree - provided CloneVDI recognizes the filesystem being used (meaning it has to be ext4 and no LVM). CloneVDI will also complain if the extension doesn't match the format.
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

$ VBoxManage showmediuminfo xxx.vdi
UUID: xxx
Parent UUID: base
State: locked read
Type: normal (base)
Location: xxx.vdi
Storage format: VDI
Format variant: dynamic default
Capacity: 953869 MBytes
Size on disk: 943631 MBytes
Encryption: disabled
Property: AllocationBlockSize=1048576
read locked...
It cant really be a VM issue, since I initially created it from a disk image, and it never touched a VM.

What's the recommended way to clone a physical drive to VDI format? ConeVDI? It's cloning now, let's see.
Is there another clean way to clear the readlock flag?
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by scottgus1 »

edsch wrote:BoxManage showmediuminfo xxx.vdi
UUID: xxx
....
Location: xxx.vdi
You almost got all the hackable info obfuscated......

But you left the most important item we need:
edsch wrote:Property: AllocationBlockSize=1048576
Get ready for a bumpy ride:

ALL YOUR BASE ARE BELONG TO US!!!

:lol:

But seriously, disk names and Virtualbox UUIDs are not in any sense things that can let bad guys into your PC. They're virtual: they don't actually exist.

*************************
To determine the reason for the read lock, please also run and post the output of the VM this vdi is attached to:

vboxmanage showvminfo "VM name"
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by mpack »

"Read lock" is not a property of the VM or the VDI, it would be because the file is marked read-only on the host for some reason. E.g. tagged with the read-only attribute or in a folder that your user doesn't own. A CloneVDI clone can't end up read-only or it couldn't have been created.
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

Hack me then! 8)

It's not attached to any VM, I gave the entire showmediuminfo output.

How can I find out what is causing the read-lock? It's def not file permissions. I have other VDIs with the same file permissions right next to it in the same dir that I can compact, though these are single-partition images rather than full-disk images.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by mpack »

As I said, it's not a feature of VirtualBox, so that makes it a host OS issue and beyond our scope. Usually it's because some running app still has ownership, e.g. your disk image viewing/mounting tool perhaps.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by fth0 »

Sorry that I have to chime in here to get the thread back on track:

In contrast to the MediumType, which can have values like MediumType_Readonly, the MediumState is a VirtualBox-internal state to synchronize medium accesses by different VirtualBox components.

VBoxManage showmediuminfo returning "State: locked read" (MediumState_LockedRead) means that some VirtualBox component (*) has already locked the medium for read access. I'd suggest to reboot the host and try the VBoxManage showmediuminfo and VBoxManage modifymedium commands before any other VirtualBox component (e.g. the VirtualBox Manager) is started.

(*) In theory, it could be a race condition in VBoxManage itself, because VBoxManage starts the VBoxSVC process that reads information from all media in the media registry.
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

Thanks! I thought so.

I cant reboot the machine right now to see if some lock is held somewhere. But cloning the vdi with CloneVDI and compact option a) worked and b) compacted the disk as expected. State is now "created" and all works as expected.

Therefore we can consider this issue solved. I will report back in the coming days whether a reboot "unlocked" the old vdi file.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by fth0 »

Thanks for reporting back! :)

CloneVDI (by mpack) worked because it is not part of VirtualBox, which is an advantage in this case.
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

I accidentally deleted the bad vdi before reboot. But the space wasnt freed, so a lock was indeed held somewhere. Upon reboot I got a bunch of IO errors too. After the reboot all is well now. Not sure what went wrong exactly though.
edsch
Posts: 8
Joined: 25. Nov 2013, 15:57

Re: Compact a full-disk vdi image VBOX_E_INVALID_OBJECT_STATE

Post by edsch »

After some investigation I think this happened:

I accidentally used gnome-disk-image-mounter on the vdi, which silently failed to mount the disk, but nevertheless attached it as a loopback device in /dev/loopXY. This held the lock and gave the IO errors on reboot. Correct way is to use vboximg-mount.
Post Reply