Mount a Raspberry Pi OS disk image with PowerShell Mount-DiskImage

I'd like to mount the first FAT partition of a standard Raspberry Pi OS disk image (the Lite version) to make some modifications and copy additional files into it for the first start of the Pi. After reading for a while, I found the Mount-DiskImage PowerShell cmdlet that sounds like it could do this. But it won't. When I try this command:

Mount-DiskImage -imagepath "C:\path\to\2021-10-30-raspios-bullseye-armhf-lite.img" -storagetype iso

I get this error:

Mount-DiskImage : Die Datei oder das Verzeichnis ist beschädigt und nicht lesbar.
In Zeile:1 Zeichen:1
+ Mount-DiskImage -imagepath "C:\path\to\2021-10-30-raspios- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_DiskImage:ROOT/Microsoft/.../MSFT_DiskImage) [Mount-DiskImage], CimE
   xception
    + FullyQualifiedErrorId : HRESULT 0x80070570,Mount-DiskImage

That means something like the image file is damaged/corrupted and not readable.

What am I doing wrong? Is this cmdlet capable of mounting the first partition of the image at all? If not, I need to continue my search for other solutions.


Solution 1:

Well, your image is not an ISO image – when you specify "ISO" as the type, Windows expects it to contain an actual ISO9660 or UDF filesystem, not an MBR partition table. (In other words, "ISO" means "something that makes sense as a virtual CD/DVD", it's not just a term for disk images in general.)

You may have better luck with -StorageType VHD, which instead attaches the image as a virtual HDD (and Windows will recognize any number of partitions in it, just like on a physical HDD). However, this type won't accept raw images – they have to be in the VHD or VHDX (Hyper-V) container format.

But because with VHD the issue is only with the container, not with the contents, it should be possible to easily convert an image from raw .img to .vhd for editing, then back to raw once done. The qemu-img tool can do both. Example instructions