### Install MinGW build Source: https://github.com/libyal/libvmdk/wiki/Building Install the compiled library and tools to the system. ```bash sudo make install ``` -------------------------------- ### Build and Install with GCC Source: https://github.com/libyal/libvmdk/wiki/Building Standard commands to compile and install the library using autotools. ```bash ./configure make ``` ```bash sudo make install ``` ```bash ./configure --prefix=/usr ``` ```bash sudo ldconfig ``` -------------------------------- ### Build RedHat Packages Source: https://github.com/libyal/libvmdk/wiki/Building Commands to install dependencies, build RPMs, and install the library. ```bash dnf install rpm-build zlib-devel fuse-devel python3-devel ``` ```bash mv libvmdk-alpha-.tar.gz libvmdk-.tar.gz rpmbuild -ta libvmdk-.tar.gz ``` ```bash ~/rpmbuild/RPMS//libvmdk--1..rpm ~/rpmbuild/RPMS//libvmdk-devel--1..rpm ~/rpmbuild/RPMS//libvmdk-python3--1..rpm ~/rpmbuild/RPMS//libvmdk-tools--1..rpm ~/rpmbuild/SRPMS/libvmdk--1.src.rpm ``` ```bash sudo rpm -ivh libvmdk--1..rpm ``` -------------------------------- ### Install Build Dependencies Source: https://github.com/libyal/libvmdk/wiki/Building Commands to install required build tools on various platforms. ```bash sudo apt install git autoconf automake autopoint libtool pkg-config ``` ```bash sudo dnf install git autoconf automake gettext-devel libtool pkg-config ``` ```bash sudo port install git autoconf automake gettext libtool pkgconfig ``` -------------------------------- ### Build Python Bindings with setup.py Source: https://github.com/libyal/libvmdk/wiki/Building Commands to build and install Python bindings directly. ```bash python setup.py build sudo python setup.py install ``` ```bash python setup.py --help ``` -------------------------------- ### Build Debian Packages Source: https://github.com/libyal/libvmdk/wiki/Building Commands to install dependencies, build the package, and install the resulting .deb files. ```bash sudo apt install autotools-dev build-essential debhelper dh-autoreconf dh-python fakeroot pkg-config zlib1g-dev libfuse-dev python3-dev python3-setuptools ``` ```bash cp -rf dpkg debian dpkg-buildpackage -rfakeroot ``` ```bash libvmdk_-1_.deb libvmdk-dev_-1_.deb libvmdk-python3_-1_.deb libvmdk-tools_-1_.deb ``` ```bash sudo dpkg -i libvmdk_-1_.deb ``` -------------------------------- ### Build Debian Packages Source: https://context7.com/libyal/libvmdk/llms.txt Generates installable .deb packages from the source code. ```bash # Install build dependencies sudo apt install autotools-dev build-essential debhelper dh-autoreconf \ dh-python fakeroot pkg-config zlib1g-dev libfuse-dev python3-dev python3-setuptools # Prepare and build packages cp -rf dpkg debian dpkg-buildpackage -rfakeroot # Install generated packages sudo dpkg -i ../libvmdk_*_amd64.deb sudo dpkg -i ../libvmdk-tools_*_amd64.deb sudo dpkg -i ../libvmdk-python3_*_amd64.deb ``` -------------------------------- ### Build macOS Packages Source: https://github.com/libyal/libvmdk/wiki/Building Commands to build, install, and package libvmdk for macOS. ```bash ./configure --prefix=/usr/local --enable-python --with-pyprefix make ``` ```bash make install DESTDIR=$PWD/tmp ``` ```bash ./configure --prefix=$PWD/tmp --enable-python --with-pyprefix ``` ```bash otool -LT tmp/usr/lib/libvmdk.1.dylib ``` ```bash $PWD/tmp/ ``` ```bash mkdir -p $PWD/tmp/usr/share/doc/libvmdk cp AUTHORS COPYING COPYING.LESSER NEWS README $PWD/tmp/usr/share/doc/libvmdk ``` ```bash pkgbuild --root $PWD/tmp --identifier com.github.libyal.libvmdk --version --ownership recommended ../libvmdk-.pkg ``` ```bash hdiutil create ../libvmdk-.dmg -srcfolder ../libvmdk-.pkg -fs HFS+ ``` -------------------------------- ### Check FUSE support in configure Source: https://github.com/libyal/libvmdk/wiki/Mounting Example output indicating missing FUSE support. ```text Building: ... FUSE support: no ``` -------------------------------- ### Configure Python for specific macOS version Source: https://github.com/libyal/libvmdk/wiki/Building Specify include and library paths for a custom Python installation on macOS. ```bash CFLAGS=-I/Library/Frameworks/Python.framework/Versions/2.7/include/ \ LDFLAGS=-L/Library/Frameworks/Python.framework/Versions/2.7/lib/ \ ./configure --enable-python --with-pyprefix ``` -------------------------------- ### Python VMDK File Access with pyvmdk Source: https://context7.com/libyal/libvmdk/llms.txt Opens and reads from a VMDK file using pyvmdk. Demonstrates getting library version, opening a handle, reading metadata, and performing basic read operations with seek. ```python import pyvmdk # Get library version print(f"pyvmdk version: {pyvmdk.get_version()}") # Create and open handle vmdk_handle = pyvmdk.handle() vmdk_handle.open("virtual_disk.vmdk") vmdk_handle.open_extent_data_files() # Get disk metadata print(f"Media size: {vmdk_handle.media_size} bytes") print(f"Media size: {vmdk_handle.media_size / (1024**3):.2f} GB") # Read first 512 bytes (boot sector) data = vmdk_handle.read_buffer_at_offset(512, 0) print(f"Read {len(data)} bytes from offset 0") # Check for MBR signature if data[510:512] == b'\x55\xaa': print("Found MBR boot signature") # Seek and read sequentially vmdk_handle.seek(1048576) # Seek to 1MB offset data = vmdk_handle.read(512) print(f"Read {len(data)} bytes at current offset") # Get current position print(f"Current offset: {vmdk_handle.get_offset()}") # Close handle (optional - garbage collection will close it) vmdk_handle.close() ``` -------------------------------- ### MSYS-MinGW Linker Error Example Source: https://github.com/libyal/libvmdk/wiki/Troubleshooting This linker error in MSYS-MinGW suggests an issue with finding a library. Reverting to a previous version of gettext might resolve this. ```text libtool: link: cannot find the library `/home/keith/staged/mingw32/lib/libiconv. la' or unhandled argument `/home/keith/staged/mingw32/lib/libiconv.la' ``` -------------------------------- ### Visual Studio Run-time Error Example Source: https://github.com/libyal/libvmdk/wiki/Troubleshooting This run-time error on Windows, related to KERNEL32.DLL, often occurs when running an executable built with Visual Studio 2010 or later on an unsupported Windows version. ```text The procedure entry point DecodePointer could not be located in the dynamic link library KERNEL32.DLL ``` -------------------------------- ### Configure libvmdk for wide character support Source: https://github.com/libyal/libvmdk/wiki/C-development Example of configuring libvmdk with wide character string support during compilation. This is typically done via the configure script or by defining specific preprocessor macros on Windows. ```bash ./configure --enable-wide-character-type=yes ``` -------------------------------- ### GCC Compilation Error Example Source: https://github.com/libyal/libvmdk/wiki/Troubleshooting This error indicates a failure during the compilation process using GCC. Examine the config.log file for specific error messages, often marked with ': error: '. ```text error: command 'gcc' failed with exit status 1 ``` ```text libclocale_locale.c:358:7: error: 'LOCALE_NAME_USER_DEFAULT' undeclared (first use in this function) ``` -------------------------------- ### Get VMDK Media Size and Disk Type Source: https://context7.com/libyal/libvmdk/llms.txt Retrieves the media size in bytes and the disk type (e.g., sparse, flat) of a VMDK file. Requires libvmdk initialization and handle opening. ```c #include #include #include const char* get_disk_type_string(int disk_type) { switch (disk_type) { case LIBVMDK_DISK_TYPE_MONOLITHIC_SPARSE: return "Monolithic Sparse"; case LIBVMDK_DISK_TYPE_MONOLITHIC_FLAT: return "Monolithic Flat"; case LIBVMDK_DISK_TYPE_SPARSE_2GB_EXTENT: return "Split Sparse (2GB extents)"; case LIBVMDK_DISK_TYPE_FLAT_2GB_EXTENT: return "Split Flat (2GB extents)"; case LIBVMDK_DISK_TYPE_STREAM_OPTIMIZED: return "Stream Optimized"; case LIBVMDK_DISK_TYPE_VMFS_SPARSE: return "VMFS Sparse"; default: return "Unknown"; } } int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_error_t *error = NULL; size64_t media_size = 0; int disk_type = 0; uint32_t content_id = 0; libvmdk_handle_initialize(&handle, &error); libvmdk_handle_open(handle, "virtual_disk.vmdk", LIBVMDK_OPEN_READ, &error); libvmdk_handle_open_extent_data_files(handle, &error); /* Get media size in bytes */ if (libvmdk_handle_get_media_size(handle, &media_size, &error) == 1) { printf("Media size: %llu bytes (%.2f GB)\n", (unsigned long long)media_size, (double)media_size / (1024 * 1024 * 1024)); } /* Get disk type */ if (libvmdk_handle_get_disk_type(handle, &disk_type, &error) == 1) { printf("Disk type: %s\n", get_disk_type_string(disk_type)); } /* Get content identifier */ if (libvmdk_handle_get_content_identifier(handle, &content_id, &error) == 1) { printf("Content ID: 0x%08x\n", content_id); } libvmdk_handle_close(handle, NULL); libvmdk_handle_free(&handle, NULL); return EXIT_SUCCESS; } ``` -------------------------------- ### MSYS-MinGW Gettext Version Revert Commands Source: https://github.com/libyal/libvmdk/wiki/Troubleshooting Commands to remove the current gettext package and install a specific previous version in MSYS-MinGW. ```bash mingw-get remove gettext mingw-get install gettext=18.3.1-1 ``` -------------------------------- ### Prepare Git Source Source: https://github.com/libyal/libvmdk/wiki/Building Commands to clone and prepare the development source code for building. ```bash git clone https://github.com/libyal/libvmdk.git cd libvmdk/ ./synclibs.sh ./autogen.sh ``` -------------------------------- ### Configure Visual Studio Project Settings Source: https://github.com/libyal/libvmdk/wiki/Building Steps to configure the solution platform and toolset for 64-bit compilation in Visual Studio. ```text Configuration manager -> Active solution platform ``` ```text ``` ```text Configuration Properties -> General -> Platform Toolset ``` -------------------------------- ### Convert Visual Studio Solution with msvscpp-convert Source: https://github.com/libyal/libvmdk/wiki/Building Automate the conversion of Visual Studio 2008 solution files to 2010 format with x64 support. ```bash msvscpp-convert.py --extend-with-x64 --output-format 2010 msvscpp\libvmdk.sln ``` -------------------------------- ### Build libvmdk with MSBuild Source: https://github.com/libyal/libvmdk/wiki/Building Use MSBuild from the command line to build the libvmdk solution. Ensure Visual Studio environment variables are set first. ```bash C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat msbuild msvscpp\libvmdk.sln /p:Configuration=Release;Platform=Win32 ``` -------------------------------- ### Build with MinGW Source: https://github.com/libyal/libvmdk/wiki/Building Standard commands to build libvmdk using MinGW tools. ```bash mingw32-configure --prefix=/opt/local/i386-mingw32 --enable-winapi=yes mingw32-make ``` ```bash ./configure --host=i386-mingw32 --prefix=/opt/local/i386-mingw32 --enable-winapi=yes make ``` -------------------------------- ### Prepare Git Source on Windows Source: https://github.com/libyal/libvmdk/wiki/Building Commands to clone and prepare the source code using PowerShell on Windows. ```powershell git clone https://github.com/libyal/libvmdk.git cd libvmdk\ .\synclibs.ps1 .\synczlib.ps1 .\autogen.ps1 ``` -------------------------------- ### Configure Build Options Source: https://github.com/libyal/libvmdk/wiki/Building Commands to enable specific build features like debug output or static libraries. ```bash ./configure --enable-verbose-output --enable-debug-output ``` ```bash ./configure --enable-shared=no ``` -------------------------------- ### Handle Delta Links (Snapshots) in C Source: https://context7.com/libyal/libvmdk/llms.txt Demonstrates how to link a snapshot handle to a base disk handle to read merged data from a snapshot chain. Requires initializing handles for both files and setting the parent-child relationship. ```c #include #include #include int main(int argc, char *argv[]) { libvmdk_handle_t *base_handle = NULL; libvmdk_handle_t *snapshot_handle = NULL; libvmdk_error_t *error = NULL; uint32_t parent_content_id = 0; uint8_t parent_filename[512]; size_t parent_filename_size; /* Initialize base disk handle */ libvmdk_handle_initialize(&base_handle, &error); libvmdk_handle_open(base_handle, "base_disk.vmdk", LIBVMDK_OPEN_READ, &error); libvmdk_handle_open_extent_data_files(base_handle, &error); /* Initialize snapshot handle */ libvmdk_handle_initialize(&snapshot_handle, &error); libvmdk_handle_open(snapshot_handle, "snapshot-000001.vmdk", LIBVMDK_OPEN_READ, &error); libvmdk_handle_open_extent_data_files(snapshot_handle, &error); /* Check if snapshot has a parent reference */ if (libvmdk_handle_get_parent_content_identifier( snapshot_handle, &parent_content_id, &error) == 1) { printf("Parent content ID: 0x%08x\n", parent_content_id); } /* Get parent filename from descriptor */ if (libvmdk_handle_get_utf8_parent_filename_size( snapshot_handle, &parent_filename_size, &error) == 1) { if (libvmdk_handle_get_utf8_parent_filename( snapshot_handle, parent_filename, parent_filename_size, &error) == 1) { printf("Parent filename: %s\n", parent_filename); } } /* Link snapshot to its parent - reads from parent for unchanged sectors */ if (libvmdk_handle_set_parent_handle(snapshot_handle, base_handle, &error) != 1) { fprintf(stderr, "Unable to set parent handle.\n"); libvmdk_error_fprint(error, stderr); libvmdk_error_free(&error); } else { printf("Snapshot linked to parent successfully.\n"); /* Now reads from snapshot_handle will return merged data */ uint8_t buffer[512]; ssize_t read_count = libvmdk_handle_read_buffer_at_offset( snapshot_handle, buffer, 512, 0, &error); printf("Read %zd bytes from snapshot chain.\n", read_count); } /* Cleanup - close snapshot first, then parent */ libvmdk_handle_close(snapshot_handle, NULL); libvmdk_handle_free(&snapshot_handle, NULL); libvmdk_handle_close(base_handle, NULL); libvmdk_handle_free(&base_handle, NULL); return EXIT_SUCCESS; } ``` -------------------------------- ### Enable Python bindings Source: https://github.com/libyal/libvmdk/wiki/Building Include this flag during configuration to build the Python bindings. ```bash ./configure --enable-python ``` -------------------------------- ### Configure static executables Source: https://github.com/libyal/libvmdk/wiki/Building Enable static executable generation during the configure process. ```bash ./configure --enable-static-executables=yes ``` -------------------------------- ### Mount as loop device on Linux Source: https://github.com/libyal/libvmdk/wiki/Mounting Mounts the exposed device file as a loop device. Requires the byte offset of the file system. ```bash mount -o loop,ro,offset=${OFFSET} /mnt/fuse/vmdk1 /mnt/file_system ``` -------------------------------- ### MinGW build script Source: https://github.com/libyal/libvmdk/wiki/Building A shell script for manual cross-compilation configuration with MinGW. ```bash #!/bin/sh CC=/opt/local/bin/i386-mingw32-gcc CXX=/opt/local/bin/i386-mingw32-g++ AR=/opt/local/bin/i386-mingw32-ar OBJDUMP=/opt/local/bin/i386-mingw32-objdump RANLIB=/opt/local/bin/i386-mingw32-ranlib STRIP=/opt/local/bin/i386-mingw32-strip MINGWFLAGS="-mwin32 -mconsole -march=i586 " CFLAGS="$MINGWFLAGS" CXXFLAGS="$MINGWFLAGS" CC=$CC CXX=$CXX AR=$AR OBJDUMP=$OBJDUMP RANLIB=$RANLIB STRIP=$STRIP ./configure --host=i586-mingw32msvc --prefix=/opt/local/i386-mingw32 --enable-winapi=yes CC=$CC CXX=$CXX AR=$AR OBJDUMP=$OBJDUMP RANLIB=$RANLIB STRIP=$STRIP CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" make ``` -------------------------------- ### C Error Handling with libvmdk Source: https://context7.com/libyal/libvmdk/llms.txt Demonstrates comprehensive error handling using libvmdk, including printing detailed error messages and backtraces to stderr. Also shows how to capture errors as a string and set up verbose logging to a file. ```c #include #include #include int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_error_t *error = NULL; char error_string[512]; libvmdk_handle_initialize(&handle, &error); /* Attempt to open non-existent file */ if (libvmdk_handle_open(handle, "nonexistent.vmdk", LIBVMDK_OPEN_READ, &error) != 1) { /* Print error to stderr */ fprintf(stderr, "Error opening file:\n"); libvmdk_error_fprint(error, stderr); /* Print backtrace for detailed debugging */ fprintf(stderr, "\nBacktrace:\n"); libvmdk_error_backtrace_fprint(error, stderr); /* Alternatively, get error as string */ if (libvmdk_error_sprint(error, error_string, 512) > 0) { printf("\nError string: %s\n", error_string); } /* Always free error when done */ libvmdk_error_free(&error); } /* Enable verbose output to stderr for debugging */ libvmdk_notify_set_verbose(1); libvmdk_notify_set_stream(stderr, NULL); /* Or log to a file */ if (libvmdk_notify_stream_open("vmdk_debug.log", &error) != 1) { fprintf(stderr, "Could not open log file.\n"); libvmdk_error_free(&error); } libvmdk_handle_free(&handle, NULL); /* Close notification stream */ libvmdk_notify_stream_close(NULL); return EXIT_SUCCESS; } ``` -------------------------------- ### Troubleshoot permission denied error Source: https://github.com/libyal/libvmdk/wiki/Mounting Commands to resolve permission issues with /etc/fuse.conf. ```text fusermount – failed to open /etc/fuse.conf – Permission denied ``` ```bash sudo addgroup fuse ``` ```bash sudo chmod o+r /etc/fuse.conf ``` -------------------------------- ### Access pyvmdk help Source: https://github.com/libyal/libvmdk/wiki/Python-development Displays help information for the pyvmdk module and handle class. ```python import pyvmdk help(pyvmdk) help(pyvmdk.handle) ``` -------------------------------- ### Iterate extent descriptors in C Source: https://context7.com/libyal/libvmdk/llms.txt Demonstrates how to open a VMDK handle and iterate through its extent descriptors to retrieve metadata. Ensure libvmdk is properly initialized and handles are freed after use. ```c #include #include #include const char* get_extent_type_string(int extent_type) { switch (extent_type) { case LIBVMDK_EXTENT_TYPE_FLAT: return "Flat"; case LIBVMDK_EXTENT_TYPE_SPARSE: return "Sparse"; case LIBVMDK_EXTENT_TYPE_VMFS_FLAT: return "VMFS Flat"; case LIBVMDK_EXTENT_TYPE_VMFS_SPARSE: return "VMFS Sparse"; case LIBVMDK_EXTENT_TYPE_ZERO: return "Zero"; default: return "Unknown"; } } int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_extent_descriptor_t *extent_descriptor = NULL; libvmdk_error_t *error = NULL; int number_of_extents = 0; int extent_index; int extent_type; off64_t extent_offset; size64_t extent_size; uint8_t filename[256]; size_t filename_size; libvmdk_handle_initialize(&handle, &error); libvmdk_handle_open(handle, "virtual_disk.vmdk", LIBVMDK_OPEN_READ, &error); /* Get number of extents */ if (libvmdk_handle_get_number_of_extents(handle, &number_of_extents, &error) != 1) { fprintf(stderr, "Unable to get number of extents.\n"); goto cleanup; } printf("Number of extents: %d\n\n", number_of_extents); /* Iterate through each extent */ for (extent_index = 0; extent_index < number_of_extents; extent_index++) { if (libvmdk_handle_get_extent_descriptor( handle, extent_index, &extent_descriptor, &error) != 1) { fprintf(stderr, "Unable to get extent descriptor %d.\n", extent_index); continue; } printf("Extent %d:\n", extent_index); /* Get extent type */ if (libvmdk_extent_descriptor_get_type( extent_descriptor, &extent_type, &error) == 1) { printf(" Type: %s\n", get_extent_type_string(extent_type)); } /* Get extent range (offset and size) */ if (libvmdk_extent_descriptor_get_range( extent_descriptor, &extent_offset, &extent_size, &error) == 1) { printf(" Offset: %lld\n", (long long)extent_offset); printf(" Size: %llu bytes\n", (unsigned long long)extent_size); } /* Get extent filename */ if (libvmdk_extent_descriptor_get_utf8_filename_size( extent_descriptor, &filename_size, &error) == 1 && filename_size > 0) { if (libvmdk_extent_descriptor_get_utf8_filename( extent_descriptor, filename, filename_size, &error) == 1) { printf(" Filename: %s\n", filename); } } libvmdk_extent_descriptor_free(&extent_descriptor, NULL); printf("\n"); } cleanup: if (extent_descriptor != NULL) libvmdk_extent_descriptor_free(&extent_descriptor, NULL); libvmdk_handle_close(handle, NULL); libvmdk_handle_free(&handle, NULL); return EXIT_SUCCESS; } ``` -------------------------------- ### Mount on macOS Source: https://github.com/libyal/libvmdk/wiki/Mounting Uses hdiutil to attach the device file on macOS. ```bash hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount /mnt/fuse/vmdk1 ``` -------------------------------- ### Run Tool with GDB Debugger (GCC) Source: https://github.com/libyal/libvmdk/wiki/Troubleshooting Launches a crashing tool under the GDB debugger to inspect its behavior. Replace 'vmdkinfo' and 'image.vmdk' with the actual tool and file. ```bash gdb --ex r --args vmdkinfo image.vmdk ``` -------------------------------- ### Build libvmdk from Source Source: https://context7.com/libyal/libvmdk/llms.txt Compiles the library with Python bindings and FUSE support using the GNU Compiler Collection. ```bash # Install dependencies (Debian/Ubuntu) sudo apt install build-essential autoconf automake autopoint \ libtool pkg-config zlib1g-dev libfuse-dev python3-dev # Clone and prepare source git clone https://github.com/libyal/libvmdk.git cd libvmdk ./synclibs.sh ./autogen.sh # Configure with Python bindings ./configure --enable-python # Or configure with all options ./configure --enable-python --enable-verbose-output --enable-debug-output # Build make # Install sudo make install sudo ldconfig # Verify installation vmdkinfo -V python3 -c "import pyvmdk; print(pyvmdk.get_version())" ``` -------------------------------- ### Initialize libvmdk handle Source: https://github.com/libyal/libvmdk/wiki/C-development Allocates and initializes a handle structure for libvmdk operations. The handle argument must be NULL. The error argument is optional. ```c #include #include #include libvmdk_error_t *error = NULL; libvmdk_handle_t *handle = NULL; if( libvmdk_handle_initialize(&handle, &error) != 1 ) { fprintf(stderr, "Unable to initialize handle.\n"); libvmdk_error_free(&error); exit(EXIT_FAILURE); } ``` ```c libvmdk_handle_initialize(&handle, NULL); ``` -------------------------------- ### Set WINVER for MinGW Source: https://github.com/libyal/libvmdk/wiki/Building Manually set the Windows version to support WINAPI functions. ```bash CFLAGS=-DWINVER=0x0501 ./configure --host=i386-mingw32 --enable-winapi=yes ``` -------------------------------- ### Open VMDK handle by path Source: https://github.com/libyal/libvmdk/wiki/Python-development Opens a VMDK image using a file path and opens associated extent data files. ```python vmdk_handle = pyvmdk.handle() vmdk_handle.open("image.vmdk") vmdk_handle.open_extent_data_files() ... vmdk_handle.close() ``` -------------------------------- ### Mount with root access Source: https://github.com/libyal/libvmdk/wiki/Mounting Passes the allow_root option to the FUSE subsystem. ```bash vmdkmount -X allow_root image.vmdk /mnt/fuse ``` -------------------------------- ### Initialize and Free libvmdk Handle Source: https://context7.com/libyal/libvmdk/llms.txt Initialize a handle before performing operations and free it to release resources. The handle must be NULL prior to initialization. ```c #include #include #include int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_error_t *error = NULL; /* Initialize handle - handle must be NULL before initialization */ if (libvmdk_handle_initialize(&handle, &error) != 1) { fprintf(stderr, "Unable to initialize handle.\n"); libvmdk_error_fprint(error, stderr); libvmdk_error_free(&error); return EXIT_FAILURE; } printf("Handle initialized successfully.\n"); /* Free handle - also closes if open */ if (libvmdk_handle_free(&handle, &error) != 1) { fprintf(stderr, "Unable to free handle.\n"); libvmdk_error_fprint(error, stderr); libvmdk_error_free(&error); return EXIT_FAILURE; } return EXIT_SUCCESS; } ``` -------------------------------- ### Extract Source Distribution Source: https://github.com/libyal/libvmdk/wiki/Building Commands to download and extract the source distribution package. ```text libvmdk-alpha-.tar.gz ``` ```bash tar xfv libvmdk-alpha-.tar.gz ``` ```text libvmdk- ``` -------------------------------- ### MinGW WINAPI error Source: https://github.com/libyal/libvmdk/wiki/Building Error message indicating an incompatible WINVER setting. ```text #error WINAPI file open function for Windows 2000 or earlier NOT implemented yet ``` -------------------------------- ### Open libvmdk handle for reading Source: https://github.com/libyal/libvmdk/wiki/C-development Opens a VMDK file for reading using the provided handle and filename. Supports narrow and wide character strings for filenames. Returns 1 on success, -1 on error. ```c filename = "image.vmdk"; if( libvmdk_handle_open(handle, filename, LIBVMDK_OPEN_READ, &error) != 1 ) { fprintf(stderr, "Unable to open handle.\n" ); libvmdk_handle_free(&handle, NULL); libvmdk_error_free(&error); exit(EXIT_FAILURE); } ``` -------------------------------- ### Open VMDK handle using file-like objects Source: https://github.com/libyal/libvmdk/wiki/Python-development Opens a VMDK handle by manually providing file-like objects for the image and its extents. ```python file_object = open("image.vmdk", "rb") vmdk_handle = pyvmdk.handle() vmdk_handle.open_file_object(file_object) base_directory = os.path.dirname(filename) extent_data_files = [] for extent_descriptor in vmdk_handle.extent_descriptors: extent_data_filename = extent_descriptor.filename _, path_separator, filename = extent_data_filename.rpartition("/") if not path_separator: _, path_separator, filename = extent_data_filename.rpartition("\\") if not path_separator: filename = extent_data_filename extent_data_file_path = os.path.join(base_directory, filename) if not os.path.exists(extent_data_file_path): break extent_data_files.append(extent_data_file_path) if len(extent_data_files) != vmdk_handle.number_of_extents: raise RuntimeError("Unable to locate all extent data files.") file_objects = [] for extent_data_file_path in extent_data_files: file_object = open(extent_data_file_path, "rb") file_objects.append(file_object) vmdk_handle.open_extent_data_files_file_objects(file_objects) ... vmdk_handle.close() ``` -------------------------------- ### Troubleshooting MinGW WINVER issues Source: https://github.com/libyal/libvmdk/wiki/Building If you encounter 'GetLocaleInfoEx' or 'LOCALE_NAME_USER_DEFAULT' undeclared errors with MinGW, try setting WINVER to 0x0501. ```c libclocale_locale.c: In function 'libclocal_local_get_decimal_point': libclocale_locale.c:357:2: warning implicit declaration of function 'GetLocaleInfoEx' [-Wimplicit-function-declaration] libclocale_locale.c:358:7: error: 'LOCALE_NAME_USER_DEFAULT' undeclared (first use in this function) libclocale_locale.c:358:7: note: each undeclared identifier is reported only once for every function it appears in ``` -------------------------------- ### Analyze VMDK with pyvmdk and pytsk3 Source: https://context7.com/libyal/libvmdk/llms.txt Wraps a pyvmdk handle to provide a file-like interface for pytsk3, enabling partition and file system analysis. ```python import pyvmdk import pytsk3 class VmdkImgInfo(pytsk3.Img_Info): """Wrapper to use pyvmdk handle with pytsk3.""" def __init__(self, vmdk_handle): self._vmdk_handle = vmdk_handle super(VmdkImgInfo, self).__init__( url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL) def close(self): self._vmdk_handle.close() def read(self, offset, size): self._vmdk_handle.seek(offset) return self._vmdk_handle.read(size) def get_size(self): return self._vmdk_handle.media_size # Open VMDK file vmdk_handle = pyvmdk.handle() vmdk_handle.open("forensic_image.vmdk") vmdk_handle.open_extent_data_files() # Create pytsk3 image info wrapper img_info = VmdkImgInfo(vmdk_handle) # Calculate partition offset (e.g., 63 sectors * 512 bytes for legacy MBR) partition_offset = 63 * 512 # Open file system try: fs_info = pytsk3.FS_Info(img_info, offset=partition_offset) print(f"File system type: {fs_info.info.ftype}") print(f"Block size: {fs_info.info.block_size}") # List root directory root_dir = fs_info.open_dir(path="/") print("\nRoot directory contents:") for entry in root_dir: if entry.info.name.name not in [b".", b".."]: name = entry.info.name.name.decode("utf-8", errors="replace") print(f" {name}") except Exception as e: print(f"Error opening file system: {e}") img_info.close() ``` -------------------------------- ### Enable Verbose and Debug Output in libvmdk Source: https://github.com/libyal/libvmdk/wiki/Building Add these definitions to common\config_winapi.h to enable verbose and debug output for troubleshooting. ```c #define HAVE_VERBOSE_OUTPUT 1 #define HAVE_DEBUG_OUTPUT 1 ``` -------------------------------- ### Define LIBVMDK_DLL_IMPORT for DLL usage Source: https://github.com/libyal/libvmdk/wiki/Building Before including , define LIBVMDK_DLL_IMPORT to use the DLL. Ensure dependent DLLs like zlib.dll are available. ```c #define LIBVMDK_DLL_IMPORT ``` -------------------------------- ### Cygwin build artifacts Source: https://github.com/libyal/libvmdk/wiki/Building Expected output files after a successful Cygwin build. ```text libvmdk/.libs/cygvmdk-0.dll ``` ```text vmdktools/.libs/vmdkinfo.exe vmdktools/.libs/vmdkmount.exe ``` -------------------------------- ### MinGW build artifacts Source: https://github.com/libyal/libvmdk/wiki/Building Expected output files after a successful MinGW build. ```text libvmdk/.libs/libvmdk-1.dll ``` ```text vmdktools/.libs/vmdkinfo.exe vmdktools/.libs/vmdkmount.exe ``` -------------------------------- ### Mount VMDK as File System with vmdkmount Source: https://context7.com/libyal/libvmdk/llms.txt Exposes raw disk content as a device file for mounting or analysis on Linux, macOS, and Windows. ```bash # Create mount point mkdir -p /mnt/vmdk # Mount VMDK image (Linux/macOS with FUSE) vmdkmount virtual_disk.vmdk /mnt/vmdk # The raw disk content is now available as: # /mnt/vmdk/vmdk1 # List mounted content ls -la /mnt/vmdk/ # total 0 # dr-xr-xr-x 2 root root 0 Jan 1 00:00 . # drwxr-xr-x 3 root root 4096 Jan 1 00:00 .. # -r--r--r-- 1 root root 21474836480 Jan 1 00:00 vmdk1 # Mount a partition from the virtual disk (Linux) # First find partition offset using fdisk or mmls sudo mount -o loop,ro,offset=1048576 /mnt/vmdk/vmdk1 /mnt/partition # Or on macOS, attach as disk image hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount /mnt/vmdk/vmdk1 # Enable root access to mount point vmdkmount -X allow_root virtual_disk.vmdk /mnt/vmdk # Unmount when done (Linux) fusermount -u /mnt/vmdk # Or umount /mnt/vmdk # Windows usage (with Dokan) vmdkmount virtual_disk.vmdk X: # Raw content available at X:\VMDK1 ``` -------------------------------- ### Configure FUSE for root access Source: https://github.com/libyal/libvmdk/wiki/Mounting Settings required in /etc/fuse.conf to allow root access to mount points. ```text /etc/fuse.conf ``` ```text user_allow_other ``` -------------------------------- ### Display VMDK Metadata with vmdkinfo Source: https://context7.com/libyal/libvmdk/llms.txt Retrieves disk type, media size, and extent configuration from a VMDK file. ```bash # Basic usage - show VMDK metadata vmdkinfo virtual_disk.vmdk # Example output: # vmdkinfo 20251220 # # VMware Virtual Disk information: # Disk type: monolithicSparse # Media size: 21474836480 bytes # # Extent descriptor(s): # 0: type: sparse, offset: 0, size: 21474836480, filename: virtual_disk-flat.vmdk # Enable verbose output for debugging vmdkinfo -v virtual_disk.vmdk # Show version information vmdkinfo -V # Show help vmdkinfo -h ``` -------------------------------- ### Mount a VMDK image Source: https://github.com/libyal/libvmdk/wiki/Mounting Mounts a VMDK image to a specified directory or drive letter. ```bash vmdkmount image.vmdk /mnt/fuse ``` ```bash vmdkmount image.vmdk x: ``` -------------------------------- ### Python VMDK File Objects with pyvmdk Source: https://context7.com/libyal/libvmdk/llms.txt Opens VMDK files using Python file objects for more control over I/O. Demonstrates opening descriptor and extent data files as file objects and associating them with the pyvmdk handle. ```python import os import pyvmdk filename = "virtual_disk.vmdk" base_directory = os.path.dirname(os.path.abspath(filename)) # Open descriptor file as file object file_object = open(filename, "rb") vmdk_handle = pyvmdk.handle() vmdk_handle.open_file_object(file_object) # Collect extent data file paths extent_data_files = [] for extent_descriptor in vmdk_handle.extent_descriptors: extent_filename = extent_descriptor.filename # Extract just the filename from path _, path_sep, fname = extent_filename.rpartition("/") if not path_sep: _, path_sep, fname = extent_filename.rpartition("\\") if not path_sep: fname = extent_filename extent_path = os.path.join(base_directory, fname) if not os.path.exists(extent_path): raise RuntimeError(f"Extent file not found: {extent_path}") extent_data_files.append(extent_path) # Verify all extents found if len(extent_data_files) != vmdk_handle.number_of_extents: raise RuntimeError("Unable to locate all extent data files") # Open extent files as file objects file_objects = [open(path, "rb") for path in extent_data_files] vmdk_handle.open_extent_data_files_file_objects(file_objects) # Now the handle is ready for reading print(f"Media size: {vmdk_handle.media_size} bytes") data = vmdk_handle.read_buffer_at_offset(512, 0) vmdk_handle.close() # Note: close() does not close the file objects; close them manually for fo in file_objects: fo.close() file_object.close() ``` -------------------------------- ### Access the exposed device file Source: https://github.com/libyal/libvmdk/wiki/Mounting The path to the RAW storage media data provided by the mount. ```text /mnt/fuse/vmdk1 ``` ```text X:\VMDK1 ``` -------------------------------- ### Open and Close VMDK Files Source: https://context7.com/libyal/libvmdk/llms.txt Open a VMDK descriptor file and its associated extent data files. Ensure proper error handling and cleanup using the provided handle functions. ```c #include #include #include int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_error_t *error = NULL; const char *filename = "virtual_disk.vmdk"; if (libvmdk_handle_initialize(&handle, &error) != 1) { fprintf(stderr, "Unable to initialize handle.\n"); goto on_error; } /* Open VMDK descriptor file for reading */ if (libvmdk_handle_open(handle, filename, LIBVMDK_OPEN_READ, &error) != 1) { fprintf(stderr, "Unable to open: %s\n", filename); goto on_error; } /* Open extent data files - assumes they are in the same directory */ if (libvmdk_handle_open_extent_data_files(handle, &error) != 1) { fprintf(stderr, "Unable to open extent data files.\n"); goto on_error; } printf("VMDK file opened successfully.\n"); /* Close the VMDK handle - returns 0 on success */ if (libvmdk_handle_close(handle, &error) != 0) { fprintf(stderr, "Unable to close handle.\n"); goto on_error; } libvmdk_handle_free(&handle, NULL); return EXIT_SUCCESS; on_error: if (error != NULL) { libvmdk_error_fprint(error, stderr); libvmdk_error_free(&error); } if (handle != NULL) { libvmdk_handle_free(&handle, NULL); } return EXIT_FAILURE; } ``` -------------------------------- ### Read Data from VMDK Source: https://context7.com/libyal/libvmdk/llms.txt Reads raw disk data from a VMDK file at specified offsets. Requires libvmdk initialization and handle opening. Error handling is omitted for brevity. ```c #include #include #include #include int main(int argc, char *argv[]) { libvmdk_handle_t *handle = NULL; libvmdk_error_t *error = NULL; uint8_t buffer[512]; ssize_t read_count; off64_t offset; /* Initialize and open handle (error handling omitted for brevity) */ libvmdk_handle_initialize(&handle, &error); libvmdk_handle_open(handle, "virtual_disk.vmdk", LIBVMDK_OPEN_READ, &error); libvmdk_handle_open_extent_data_files(handle, &error); /* Read first sector (512 bytes) at offset 0 */ read_count = libvmdk_handle_read_buffer_at_offset( handle, buffer, 512, 0, &error); if (read_count == -1) { fprintf(stderr, "Unable to read from VMDK.\n"); libvmdk_error_fprint(error, stderr); libvmdk_error_free(&error); } else { printf("Read %zd bytes from offset 0.\n", read_count); /* Check for MBR signature */ if (buffer[510] == 0x55 && buffer[511] == 0xAA) { printf("Found MBR boot signature.\n"); } } /* Seek to a specific offset and read sequentially */ offset = libvmdk_handle_seek_offset(handle, 1048576, SEEK_SET, &error); if (offset != -1) { read_count = libvmdk_handle_read_buffer(handle, buffer, 512, &error); printf("Read %zd bytes from offset %lld.\n", read_count, (long long)offset); } /* Get current offset */ if (libvmdk_handle_get_offset(handle, &offset, &error) == 1) { printf("Current offset: %lld\n", (long long)offset); } libvmdk_handle_close(handle, NULL); libvmdk_handle_free(&handle, NULL); return EXIT_SUCCESS; } ``` -------------------------------- ### Integrate pyvmdk with pytsk3 Source: https://github.com/libyal/libvmdk/wiki/Python-development Wraps a pyvmdk handle to be used as an image source for pytsk3. ```python import pytsk3 ``` ```python class vmdk_Img_Info(pytsk3.Img_Info): def __init__(self, vmdk_handle): self._vmdk_handle = vmdk_handle super(vmdk_Img_Info, self).__init__( url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL) def close(self): self._vmdk_handle.close() def read(self, offset, size): self._vmdk_handle.seek(offset) return self._vmdk_handle.read(size) def get_size(self): return self._vmdk_handle.get_media_size() vmdk_handle = pyvmdk.handle() vmdk_handle.open("image.vmdk") vmdk_handle.open_extent_data_files() img_info = vmdk_Img_Info(vmdk_handle) fs_info = pytsk3.FS_Info(img_info, offset=63 * 512) ``` -------------------------------- ### Troubleshoot Debian Build Errors Source: https://github.com/libyal/libvmdk/wiki/Building Common error message and the expected location for the upstream tarball. ```text dpkg-source: error: can't build with source format '3.0 (quilt)': no upstream tarball' found at ../libvmdk-.orig.tar.{bz2,gz,lzma,xz} ``` ```text ../libvmdk-.orig.tar.gz ``` -------------------------------- ### Run Automated Tests Source: https://github.com/libyal/libvmdk/wiki/Testing Execute the automated tests for the libvmdk package. This command requires a bash shell environment. ```bash make check ``` -------------------------------- ### Retrieve pyvmdk version Source: https://github.com/libyal/libvmdk/wiki/Python-development Returns the libvmdk version as a Unicode string. ```python pyvmdk.get_version() ``` -------------------------------- ### Run Automated Tests with Valgrind Source: https://github.com/libyal/libvmdk/wiki/Testing Execute the automated tests with Valgrind to detect memory leaks. This process will significantly impact test execution speed. ```bash make check CHECK_WITH_VALGRIND=1; ```