Every operating system, has a way to mount file systems from devices or image files and include them in the file system. Under Linux the mount command instructs the operating system for an existing file system and associates it with a particular mount directory. Using the offset parameter of the mount command it’s possible to mount a Raspberry PI image to a directory and interact with it.
Default Partition Layout
Assuming that your Raspberry image contains a single installation of the Raspberry Pi OS, then it contains two partitions and a boot sector. Using the command:
fdisk -l image_name.img
The output should be similar to the following image:
Using those information and specifically the sector size and the start position of the Linux partition (Linux), the offset is easily calculated as:
offset = unit_size * start = 512 * 532480 =272629760
From the calculation we determine that the Linux file system is 272 629 760 bytes into the raspi.img file. Next, we create a mount point to any existing directory and mount the image, specifying the number of offset bytes to the Linux partition as follows:
mount -v -o offset=partition_offset -t ext4 image_name.img /mnt/img
Using the mountpi.sh script
The above process can be automated using the mountpi.sh script.
1) Clone the Git Repository
git clone https://github.com/novamostra/mountpi
2) Add Execution Permission
sudo chmod +x mountpi/mountpi.sh
3) Run the command as Super User
./mountpi/mountpi.sh -i image_file.img -m mount_dir
4) When finish making changes to the image safely unmount the image using the command:
./mountpi/mountpi.sh -i image_file.img -m mount_dir -u
Example
Resources
The script is open source and available on github