Skip to main content

Partial microSD Image

Instructions to partially clone the image from a microSD (or anything else), where the filesystem does not cover the entire disk.  For example, a 128GB microSD with a fresh RasPi image that only encompasses ~5.4 GB of space.

Run sudo fdisk -l to display partitioning details:

Disk /dev/mmcblk0: 119.38 GiB, 128177930240 bytes, 250347520 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: 0x1aa4489a

Device         Boot   Start      End Sectors  Size Id Type
/dev/mmcblk0p1         8192  1056767 1048576  512M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      1056768 10600447 9543680  4.6G 83 Linux

In this case, sector size of 512 bytes multiplied by the End sector is the total space used by these partitions.

10600447 * 512 bytes = 5.4GB

So divide that total size by whatever block size is preferred to use with dd, and round up to an integer:

(10600447 * 512 bytes)/4MB < 1400

Finally, run dd with count:

sudo dd if=/dev/mmcblk0 of=partial_image.img bs=4M status=progress count=1400

Check the image with gparted:

sudo gparted partial_image.img

Or, mount the partitions in the image using this technique:

$ sudo mount -o loop,offset=4194304,sizelimit=536870912 ./partial_image.img /mnt/mount_point/bootfs 
# offset: 512 bytes * 8192 sectors = 4194304 bytes
# sizelimit: 512 bytes * 1048576 sectors = 536870912 bytes

$ sudo mount -o loop,offset=541065216,sizelimit=4886364160 ./partial_image.img /mnt/mount_point/rootfs
# offset: 512 bytes * 1056768 sectors = 541065216 bytes 
# sizelimit: 512 bytes * 9543680 sectors = 4886364160 bytes

If it's just a single partition image, then you can get away with simply this:

sudo mount -o loop partial_image.img /mnt/mount_point/