(This is in answer to an IRC question, but the answer is a bit longer than I can cover in IRC)
Can you read and write at the block level in a .vmdk file? I think the questioner was asking about writing a backup/restore type tool. Using only free software, qemu can do reads. You can attach qemu-nbd to a vmdk file and that will expose the logical blocks as NBD, and you can then read at the block level using libnbd:
#!/usr/bin/python3 import nbd h = nbd.NBD() h.connect_systemd_socket_activation( ["qemu-nbd", "-t", "/var/tmp/disk.vmdk"]) print("size = %d" % h.get_size()) buf = h.pread(512, 0)
$ ./qemu-test.py size = 1073741824
The example is in Python, but libnbd would let you do this from C or other languages just as easily.
While this works fine for reading, I wouldn’t necessarily be sure that writing is safe. The vmdk format is complex, baroque and only lightly documented, and the only implementation I’d trust is the one from VMware.
So as long as you’re prepared to use a bit of closed source software and agree with the (nasty) license, VDDK is the safer choice. You can isolate your own software from VDDK using our nbdkit plugin.
#!/usr/bin/python3 import nbd h = nbd.NBD() h.connect_command( ["nbdkit", "-s", "--exit-with-parent", "vddk", "libdir=/var/tmp/vmware-vix-disklib-distrib", "file=/var/tmp/disk.vmdk"]) print("size = %d" % h.get_size()) buf = h.pread(512, 0) h.pwrite(buf, 512)
I quite like how we’re using small tools and assembling them together into a pipeline in just a few lines of code:
┌─────────┬────────┐ ┌─────────┬────────┐ │ your │ libnbd │ NBD │ nbdkit │ VDDK │ │ program │ ●──────────────➤ │ │ └─────────┴────────┘ └─────────┴────────┘ disk.vmdk
One advantage of this approach is that it exposes the extents in the disk which you can iterate over using libnbd APIs. For a backup tool this would let you save the disk efficiently, or do change-block tracking.