Mount Raspberry Pi OS Image in Linux

Mount Raspberry Pi OS Image in Linux

Operating systems provides a way to mount file systems from devices or image files. It can be useful to view content of the OS image (.img). This tutorial shows how to mount Raspberry Pi OS image in Linux.

Mount OS image

If not already done, download the Raspberry Pi OS from the official website:

wget -qO raspios.img.xz

Extract OS image file from archive:

unxz raspios.img.xz

Check the partition table with fdisk command:

fdisk -l raspios.img

Output example:

Disk raspios.img: 1.88 GiB, 2017460224 bytes, 3940352 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7d5a2870

Device       Boot  Start     End Sectors  Size Id Type
raspios.img1        8192  532479  524288  256M  c W95 FAT32 (LBA)
raspios.img2      532480 3940351 3407872  1.6G 83 Linux

Using this information, the following data will be used for mounting Raspberry Pi OS image:

  • Sector size: 512
  • Start position of the img1 partition: 8192
  • Start position of the img2 partition: 532480
  • Number of sectors of the img1 partition: 524288
  • Number of sectors of the img2 partition: 3407872

Calculate mount offsets and assign to variables:

MOUNT_OFFSET_1=$(fdisk -l raspios.img | grep img1 | awk '{print $2 * 512}')
MOUNT_OFFSET_2=$(fdisk -l raspios.img | grep img2 | awk '{print $2 * 512}')

Calculate mount size limits and assign to variables:

MOUNT_SIZE_1=$(fdisk -l raspios.img | grep img1 | awk '{print $4 * 512}')
MOUNT_SIZE_2=$(fdisk -l raspios.img | grep img2 | awk '{print $4 * 512}')

Note: size limit option is necessary when mounting both partitions. The first partition will mount successfully. However, when mounting a second partition, you will get an error message "mount: … overlapping loop device exists". When mounting only one partition, the size limit option is optional.

Create mount point directories:

sudo mkdir -p /mnt/rpi/img1 /mnt/rpi/img2

Mount both partitions using the following commands:

sudo mount -o offset=$MOUNT_OFFSET_1,sizelimit=$MOUNT_SIZE_1 raspios.img /mnt/rpi/img1
sudo mount -o offset=$MOUNT_OFFSET_2,sizelimit=$MOUNT_SIZE_2 raspios.img /mnt/rpi/img2

Check that both partitions are mounted:

ls /mnt/rpi/img1
COPYING.linux             bcm2710-rpi-3-b.dtb       fixup.dat     kernel7l.img
LICENCE.broadcom          bcm2710-rpi-cm3.dtb       fixup4.dat    kernel8.img
ls /mnt/rpi/img2
bin   dev  home  lost+found  mnt  proc  run   srv  tmp  var
boot  etc  lib   media       opt  root  sbin  sys  usr

Unmount OS image

To unmount both partitions, run the following commands:

sudo umount /mnt/rpi/img1
sudo umount /mnt/rpi/img2

Delete mount point directories:

sudo rm -rf /mnt/rpi

Leave a Comment

Cancel reply

Your email address will not be published.