Accessing blocks of harddisk

Discussion about using the VirtualBox API, Tutorials, Samples.

Accessing blocks of harddisk

Postby rshaw » 20. Feb 2013, 17:47

Hello,

I am developing an application using VirtualBox's API and I am looking for the best way of modifying a medium through VirtualBox. I need to do this after a snapshot is created but I don't see anywhere in the API that the sectors on the disk can actually be accessed. Is this not posssible? I can't go through the original medium file because I can't change the original file. I was hoping to let VirtualBox handle this by using its differencing files in a similar fashion to just booting up the VM.

To be clear, I am looking for a way to get a handle on a differenced disk so I can read the harddisk and make any changes necessary while never actually interacting with the original file, only through virtualbox.

The only way I can think of is attaching the medium, booting up VirtualBox to a live cd, and scripting what changes need to be run. Then automatically shutting down signaling that my changes are complete. I'd rather a more programmatic appropach to this problem.
rshaw
 
Posts: 15
Joined: 11. Feb 2013, 17:04

Re: Accessing blocks of harddisk

Postby noteirak » 20. Feb 2013, 21:51

Moved to API section

The API contains the IFile interface, which is only extended by the IGuestFile interface, so AFAIK you can only read from files in the guest, but you cannot read files from the host...
But if your code is running from the host itself, you can just open the file in read yourself and get the full path from IMedium
Hyperbox - Virtual Infrastructure Manager - https://kamax.io/hbox/
Manage your VirtualBox infrastructure the free way!
noteirak
Site Moderator
 
Posts: 5198
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7

Re: Accessing blocks of harddisk

Postby rshaw » 20. Feb 2013, 23:47

noteirak wrote:Moved to API section

The API contains the IFile interface, which is only extended by the IGuestFile interface, so AFAIK you can only read from files in the guest, but you cannot read files from the host...
But if your code is running from the host itself, you can just open the file in read yourself and get the full path from IMedium


Can you clarify this? I'm not sure I understand. Are you saying this is or is not possible by using VirtualBox's API? It looks like you can modify arbitrary files with the IFile interface, but what if I wanted to modify the MBR? What if the machine is off? The only way I can see getting a handle on IFile is through creating an IGuestSession through https://www.virtualbox.org/sdkref/inter ... 77ddf2dc19 - for that to work credentials to the guest are also needed.
rshaw
 
Posts: 15
Joined: 11. Feb 2013, 17:04

Re: Accessing blocks of harddisk

Postby noteirak » 21. Feb 2013, 00:39

Looking at the API, the only things you can get via the API are files and directories from the guest via a IGuestSession, and that's it. You cannot get MBR or things like that.
Hyperbox - Virtual Infrastructure Manager - https://kamax.io/hbox/
Manage your VirtualBox infrastructure the free way!
noteirak
Site Moderator
 
Posts: 5198
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7

Re: Accessing blocks of harddisk

Postby klaus » 21. Feb 2013, 12:22

To answer the original question: it is NOT possible to read or modify the contents of a disk image through the VirtualBox API in any way. Neither on a sector not on a file level. So far there was no demand for such image hacking. You'd have to explain much more what you want to achieve to create a plausible use case for a new API feature. We have many high level image manipulation operations (merging images, copying data between image chains, ...), so I want to be sure that this isn't covered already.
We'd have a C library which allows access to disk images of any format supported by VirtualBox (which is used by the hard disk device emulations), so using that would be a possible short term workaround.
klaus
Oracle Corporation
 
Posts: 714
Joined: 10. May 2007, 14:57

Re: Accessing blocks of harddisk

Postby rshaw » 21. Feb 2013, 16:08

klaus wrote:To answer the original question: it is NOT possible to read or modify the contents of a disk image through the VirtualBox API in any way. Neither on a sector not on a file level. So far there was no demand for such image hacking. You'd have to explain much more what you want to achieve to create a plausible use case for a new API feature. We have many high level image manipulation operations (merging images, copying data between image chains, ...), so I want to be sure that this isn't covered already.
We'd have a C library which allows access to disk images of any format supported by VirtualBox (which is used by the hard disk device emulations), so using that would be a possible short term workaround.


Hi Klaus, thank you for your reply.

I am working on creating a sandbox enviornment (of sorts) and have a need to access arbitrary portions of the harddisk, or better still, expose it as a virtual block device (sort of how xmount can expose E01 files as raw dds). This device could then be programmatically modified by the host, with vbox taking care of the correct translation into the backing store (vdi, vmdk, iSCSI, etc). Some of the IGuestSession controls are quite useful, but the use case (both needing credentials to the guest and extensions being installed) are unbeleivably rare. A perfect example of something I'd need is modifying the registry on a windows guest whose power is currently off. Or reading details about the MBR to try to provide automated information output to the end-user. VMware's API includes what I need (if you'd like to take a look at a function prototype I'm envisioning) ( http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.vddk.pg.doc%2FvddkFunctions.6.4.html ) but their product is not usable due to licensing issues.

EDIT: As a follow up. Do you have an extremely short example of what using that C lib might look like? Perhaps something like just reading a single sector of a drive given a vm name?
rshaw
 
Posts: 15
Joined: 11. Feb 2013, 17:04

Re: Accessing blocks of harddisk

Postby klaus » 21. Feb 2013, 18:41

The disk image library is much lower level, it doesn't know about VM configs, disk attachments etc. - it only handles images, and it'd be your job to feed it the right information (e.g. an image chain corresponding to the current disk state of a VM). There's no direct connection to the high level API.
Example code is e.g. in https://www.virtualbox.org/browser/vbox ... ditool.cpp and the header file describing the library interface functions is in https://www.virtualbox.org/browser/vbox ... /VBox/vd.h
So far I saw no convincing reason for doing image hacking on the host, in my opinion the far more flexible and more well-defined way of doing such manipulations is creating another VM which has the same disks attached, but is running off a different medium (other hard disk or DVD), and has the necessary equipment to do the intended surgery (if necessary controlled from the host through guest additions and the corresponding guest execute interface).
Several people asked for tools to make virtual disks accessible on the host, but this is extremely complicated, requires a very different implementation on every host OS (a curse if one wants to have a cross-platform product) and is only interesting for a tiny fraction of the user base. Besides functionality limitations, e.g. someone on a Windows host won't be able to access the latest and greatest Linux filesystems and vice versa.
klaus
Oracle Corporation
 
Posts: 714
Joined: 10. May 2007, 14:57

Re: Accessing blocks of harddisk

Postby rshaw » 21. Feb 2013, 20:47

I will accept the answer of modifying the disk through the virtual machine, however I would like to point out that tasks which require frequent change or update can be difficult to implement with a bootable image attempting to do such things.

Sure, you could implement a Puppet like system that will push commands to slaves at bootup, but now you're creating a networked virtual infrastructure that may be difficult to manage / maintain. Being able to modify disk data while the machine is offline would be an enormously powerful utility.

Thanks for your comments, suggestions and help!
rshaw
 
Posts: 15
Joined: 11. Feb 2013, 17:04

Re: Accessing blocks of harddisk

Postby klaus » 21. Feb 2013, 20:57

I deliberately pointed to the "guest exec" functionality as part of the guest additions, to avoid dragging in any networking issues.
Also - regarding the disk image mounting tool: if someone decides that it's important for him and starts an implementation we'll of course gladly accept any contribution. It's just not very high on our todo list (which may change without prior notice, as usual).
klaus
Oracle Corporation
 
Posts: 714
Joined: 10. May 2007, 14:57


Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 1 guest