Search code examples
linuxbashdd

dd - Backup MBR/partition table and first partition


I have been provided with a 3.8GB SD card image meant to be flashed to a 4GB SD card for booting a customized version of Raspian OS on the RaspBerry PI dev board. It has a first primary partition that is FAT32 that holds a bootloader, and another partition of a custom type that holds the OS.

I am able to boot the PI off the SD card with this image deployed on it, modify its contents while the board is running, then shut down the board.

I'd like to create my own disk image after I modify the contents of the card while it's booting. This would involve backing up the MBR, which I would attempt via:

dd if=/dev/sda of=~/Desktop/mbr.raw bs=512 count=1

I could then back up each partition one at a time to a separate file via:

dd if=/dev/sda1 of=~/Desktop/sda1.raw bs=1m
dd if=/dev/sda2 of=~/Desktop/sda2.raw bs=1m

Is there any way to concatenate these files into a single image, or safely script dd to extract all of their contents to a single file in the first place? The size of the bootloader and OS partitions may change in the future, but they will always be contiguous.


Solution

  • In the end, I did the following, which worked:

    1. Use fdisk -l /dev/sdc to list all partitions on the SD card. Note the block size (usually 512) and the "count" (ie: number of blocks occupied) by the first partition
    2. Define a variable, blks, as count+1.
    3. Issue the command: dd if=/dev/sdc of=~/my_image.img bs=512 count=${blks}