Raspberry Pi Image Mount

Written by Luke Arntz on December 28, 2023
[ linux ] [ disk ] [ image ] [ fdisk ] [ rpi4 ] [ rpi ] [ raspberrypi ] [ debian ]

Contents

Overview

Wanted to write this up because it’s not something I do regularly, and I always have to lookup how to find the start of the root partition. I specially only do this for my Raspberry Pi’s, but this should work with any image that is configured similarly.

The goal is to download an OS image, in this case Debian, and mount the image locally to add a private SSH key.

Mounting the Image

  1. Download an OS image (here I’m using the Debian tested images for RPi images), and decompress it.

  2. Find the root partition offset using fdisk.

    > fdisk -l 20220808_raspi_4_bookworm.img
    
    Disk 20220808_raspi_4_bookworm.img: 1.95 GiB, 2097152000 bytes, 4096000 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: 0xf7e489e2
    
    Device                         Boot  Start     End Sectors  Size Id Type
    20220808_raspi_4_bookworm.img1        8192  819199  811008  396M  c W95 FAT32 (LBA)
    20220808_raspi_4_bookworm.img2      819200 4095999 3276800  1.6G 83 Linux
    
  3. Multiply the start sector by the sector size. In this case that is 512 * 819200 = 419430400

  4. Mount the image using the offset.

    sudo mount -o loop,offset=419430400 20220808_raspi_4_bookworm.img ./tmp/
    
  5. Copy ssh keys to root user.

    sudo chmod -R 600 root/.ssh
    # paste public key into file or change this to cp another authorized_keys file
    sudo vi root/.ssh/authorized_keys
    sudo mkdir root/.ssh
    
  6. Unmount image

    sudo umount ./tmp
    

Related Articles

Top