Mount archives

Notes published the
6 - 7 minutes to read, 1382 words

Why mounting archives

Graphical archive managers, like WinRAR, Ark, 7-Zip, and many others, provide an explorer-like view of the content of the archive. It is possible to navigate the content of a .zip, .rar, .iso, or .tar file without copying the content on the disk, eventually extract single files, and maybe even modify them on the fly in the archive.

Some file managers, like explorer.exe, Midnight Commander, Dolphin, and others, even support some archives out-of-the-box, as if they were simple folders.

While such programs (and the integrated functionality in file managers) are extremely practical when dealing interactively with archives, it has the disadvantage that it is not possible to use external tools that can be used on files.

It is not, for example, possible to use find to find a file inside an archive, cat to display the content, or grep to search the content of a specific file.

There are alternate tools that can deal with it, but one needs to invest some time, eventually, install them on the system, and last but not least, it might not be possible to change or adapt existing programs or scripts so easily to support archives.

In such cases, as long as we do not want to modify an archive, the simplest solution is to extract the content in a temporary directory, do whatever we want to do with the content, and remove the temporary directory at the end.

While it works, it has the disadvantage that when working with big archives, we are going to extract everything, even if we need to interact only with a small subset of the archive: it is not efficient. A better approach would be to extract only the needed files, but it is not always easy to foresee which are needed; an alternate approach would be to extract them lazily, on demand.

Just like it is possible to mount folders, it is also possible to mount archives, and a sufficiently smart implementation could extract the files on demand.

iso images

ISO images are probably the most straightforward example.

When a disc is inserted into the drive (and mounted), the content is "freely" accessible to any program, and files and folders are visible to any program.

But when creating a .iso image, which is (or should be) a one-to-one copy of the disc, the content is stored in an opaque blob.

Linux

On GNU/Linux systems, it is possible to use mount as an administrator

# mount iso file (as admin)
mount -o loop "$iso_file" "$mount_folder";

# unmount iso file
umount "$mount_folder";

The probably most popular alternative is to use fuseiso, as it does not require administrator rights.

The usage is straightforward

# mount iso file
fuseiso "$iso_file" "$empty_mount_folder";

# unmount iso file
fusermount -u "$empty_mount_folder";
# or
umount "$empty_mount_folder";

Contrary to mount, using fuseiso with a non-empty directory leads to an error.

While the main advantage of using fuseiso is that it does not require administrator rights, it is problematic if one wants to use the content of the virtual disc file with other users, because those won’t be able to access the content.

The output of ls as another user (even root) would look like

> sudo ls -lah /tmp | grep iso
ls: cannot access '/tmp/iso': Permission denied
d?????????  ? ?    ?       ?            ? iso

As the filesystem is virtual and mounted by a given user, other users will not be able to access it.

There are other ways for mounting iso files, some of them are listed at the debian wiki.

Windows

Since Windows 10, it is possible to mount iso files directly from explorer.exe. It is also possible to unmount it by "ejecting" the disc from explorer.exe as if it were a real disc.

The same operation is possible from PowerShell (according to the documentation 🗄️, on some systems administrator rights might be required):

# mount
$MountedDisk = Mount-DiskImage -ImagePath absolute/path/to/iso

# get the drive letter
($MountedDisk | Get-Volume).DriveLetter

# unmount
Dismount-DiskImage -ImagePath absolute/path/to/iso

# in case the same file has been mounted multiple times, prefer
# following command:
Dismount-DiskImage -DevicePath $mountedDisk.DevicePath

On older Windows versions one needs to resort to third-party tools or extract the content of the archive.

zip and other archives

.zip archives are probably the most common archive types in the wild as it is supported out-of-the-box on most Windows versions.

On GNU/Linux systems it is possible to mount them similarly like .iso files with fuse-zip:

# mount zip file
fuse-zip "$zip_file" "$empty_mount_folder";

# unmount zip file
fusermount -u "$empty_mount_folder";
# or
umount "$empty_mount_folder";

Another program able to mount zip archives is archivemount

# mount zip file
archivemount "$zip_file" "$empty_mount_folder";

# unmount zip file
fusermount -u "$empty_mount_folder";
# or
umount "$empty_mount_folder";

Similar to fuse-zip, it is possible to modify or add content, but (to my surprise), the zip archive got converted to a tar archive 😲. Alone for this unexpected side-effect, one should nearly always prefer to use the option -o readonly with archivemount, to avoid the conversion.

In the case of fuse-zip, a filesystem is mounted as read-only with -o ro.

In the case of compressed archives, the performance question is legitimate.

Especially with archives containing one big file, it might be faster to uncompress the content and access directly to the file, instead of opening it from the archive manager.

PERFORMANCE

On a small archives fuse-zip have the same performance with commonly used virtual filesystems like KIO, Gnome GVFS, mc vfs, unpackfs, avfs, fuse-j-zip. But on large archives with many file (like zipped Linux kernel sources) fuse-zip have the greatest speed. You can download test suite from the web-site and make sure that it is true.

fuse-zip project page

There is a performance comparison table for fuse-zip, but I do not know how practical or actual it is.

vhd images

A .vhd file (and .vhdx) is a file format that represents a virtual hard disk. It is a format used by some virtualization platforms, like Microsoft Virtual PC, VirtualBox, and possibly others.

Inside the virtual machine, a virtual hard disk is mounted and accessed transparently, but outside of the virtual machine, it is an opaque binary blob.

Linux

ON GNU/Linux systems it is possible to use guestmount:

guestmount --add "$vhdx_file" --ro -i "$empty_mount_folder"

fusermount -u "$empty_mount_folder";
# or
umount "$empty_mount_folder";

Windows

Windows supports .vhd files out-of-the-box.

# mount
$MountedDisk = Mount-DiskImage -ImagePath absolute/path/to/vhd

# get the drive letter
($MountedDisk | Get-Volume).DriveLetter

# unmount
Dismount-DiskImage -ImagePath absolute/path/to/vhd

# in case the same file has been mounted multiple times, prefer
# following command:
Dismount-DiskImage -DevicePath $mountedDisk.DevicePath

With -Access ReadOnly it is possible to mount the virtual disk as read-only, otherwise, do no set the parameter os use -Access ReadWrite.

Apparently, I need administrator privileges for mounting virtual hard drives, but not for mounting .iso files, the error is

Mount-DiskImage : A required privilege is not held by the client.
At line:1 char:1
+ Mount-DiskImage -ImagePath C:\Users\fekir\file.vhdx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_DiskImage ...torageType = 3):ROOT/Microsoft/.../MSFT_DiskImage) [Mount-DiskImage], CimException
    + FullyQualifiedErrorId : HRESULT 0x80070522,Mount-DiskImage

I am not sure why one file type can be mounted as a normal user, and the other not.

Also note that even if the .vhdx is mounted as administrator, it can be ejected from explorer.exe without administrator rights. Thus either explorer.exe can perform some administrative operation without invoking UAC, or Mount-DiskImage and explorer.exe are doing something slightly differently.

What about Mount-VHD

Mount-VHD 🗄️ is another cmdlet of PowerShell for mounting virtual disks, but it requires the Hyper-V module. Since it is disabled on my systems (it causes issues with VirtualBox), I was not able to test it, but according to the documentation, it works similarly to Mount-DiskImage:

Mount-VHD -Path absolute/path/to/vhdx

Dismount-VHD -Path absolute/path/to/vhdx

I am not sure what advantages Mount-VHD provides over Mount-DiskImage, and why one needs to enable the Hyper-V module.


Do you want to share your opinion? Or is there an error, some parts that are not clear enough?

You can contact me anytime.