Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/e5d9fd068b0d86ab0098d614adfa2e7d to your computer and use it in GitHub Desktop.

Select an option

Save aont/e5d9fd068b0d86ab0098d614adfa2e7d to your computer and use it in GitHub Desktop.

How to Mount a Partition from a Disk Image Using an Offset

When working with disk images, it’s often necessary to access the contents of a specific partition inside the image. Tools like parted, jq, and mount make this process straightforward by letting you find partition start offsets and then mount them directly. Adding sizelimit= makes the loop mount safer and more explicit by constraining how much of the image is exposed to the mount operation.

Step 1: Extract Partition Offsets (and Sizes)

Use parted with JSON output to list all partitions and capture both their starting offsets and lengths (in bytes). The following command stores the results into a Bash array named PARTS, where each element contains start:size:

mapfile -t PARTS < <(parted --json 2025-05-13-raspios-bookworm-arm64-lite-f2fs.img unit B print 2>/dev/null \
  | jq -r '.disk.partitions[] | "\(.start):\(.size)"' \
  | sed 's/B//g')

Here’s what happens in this command:

  • parted --json ... unit B print shows partition information in bytes.
  • jq extracts both the starting byte (start) and the partition length (size).
  • sed 's/B//g' removes the trailing B character from both values.
  • mapfile -t PARTS < <(...) stores all start:size pairs into the Bash array PARTS.

Now:

  • PARTS[0] will look like 8388608:xxxxxx
  • PARTS[1] will look like yyyyyy:zzzzzz
  • etc.

To split a given entry into OFFSET and SIZELIMIT:

IFS=: read -r OFFSET SIZELIMIT <<< "${PARTS[0]}"
echo "offset=$OFFSET sizelimit=$SIZELIMIT"

Step 2: Mount the Partition (with offset= and sizelimit=)

Once you have both the start offset and the partition size, mount using both options:

sudo mount -o loop,offset=OFFSET,sizelimit=SIZELIMIT \
  2025-05-13-raspios-bookworm-arm64-lite-f2fs.img ./mnt

For example, if PARTS[0] expands to 8388608:536870912, you would use:

sudo mount -o loop,offset=8388608,sizelimit=536870912 \
  2025-05-13-raspios-bookworm-arm64-lite-f2fs.img ./mnt

In this example:

  • offset=8388608 tells mount to start reading the file system at the 8 MB mark (the start of the partition).
  • sizelimit=536870912 constrains the loop device to the partition’s length, preventing accidental reads into adjacent partitions.
  • ./mnt is the directory where the partition will be accessible.

Optional: Mount Partition N Directly

This mounts partition index N (0-based) by extracting offset and sizelimit from PARTS[N]:

N=0
IFS=: read -r OFFSET SIZELIMIT <<< "${PARTS[$N]}"
sudo mount -o loop,offset="$OFFSET",sizelimit="$SIZELIMIT" \
  2025-05-13-raspios-bookworm-arm64-lite-f2fs.img ./mnt

Why This Is Useful

This method is especially handy when:

  • You need to examine or modify files inside a Raspberry Pi OS image before flashing it.
  • You want to copy specific configuration files without booting the image.
  • You’re debugging or analyzing partitions in forensics or recovery tasks.

Using sizelimit= is a best practice in these workflows because it reduces the risk of mounting beyond the intended partition boundary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment