Flash SD Cards Safely with bmaptool and dd on Linux
Flash an SD card safely with bmaptool: check the image checksum, build a block map and avoid the wrong-device mistake that wipes disks with dd.
Contents
Flashing a disk image to an SD card with dd works, but one wrong device name can wipe your system disk. This guide shows how to confirm the target device, check the image checksum, build a block map with bmaptool and flash only the data that matters.
Why bmaptool instead of plain dd
bmaptool is a Python tool with two subcommands: create builds a block map (“bmap”) for an image, and copy writes an image to a file or block device using that map. Per the project, it writes only the mapped regions rather than the holes, so large images can be flashed a lot faster than with dd; the manual gives no benchmark. It also verifies checksums stored in the bmap while copying, and can read compressed images and remote URLs.
dd has none of this. It copies every byte you tell it to and trusts you completely.
Install bmaptool
On Debian and Ubuntu the package is bmap-tools. On other distributions, check your own package manager rather than assuming a name.
apt-get update
apt-get install -y bmap-tools
bmaptool --helpVerify the image checksum first
If the image publisher provides a SHA-256 file, check it before touching any device. A corrupted download is a common cause of boards that do not boot.
mkdir -p /tmp/flashdemo && cd /tmp/flashdemo
truncate -s 64M disk.img
printf 'hello image' | dd of=disk.img conv=notrunc status=none
sha256sum disk.img > disk.img.sha256
sha256sum -c disk.img.sha256Here the image is a stand-in file so the steps can run anywhere. With a real download, use the checksum file from the vendor instead of generating your own.
If you build the image yourself, the Buildroot guide shows how to make a minimal Linux image and boot it in QEMU before you write it to a card.
Create a block map
cd /tmp/flashdemo
bmaptool create -o disk.bmap disk.img
head -n 5 disk.bmapWithout -o, the map goes to standard output. By default bmaptool copy looks for the bmap next to the image, or you can pass --bmap.
Practice on a regular file
Copy to a plain file first to see how the workflow behaves without risk, then compare the result.
cd /tmp/flashdemo
bmaptool copy --bmap disk.bmap disk.img copy.img
cmp disk.img copy.img && echo identicalIdentify the SD card before writing
This is the step that prevents disasters. Run lsblk before inserting the card, insert it, and run it again. The new entry is your card. Compare size, model and removable flag against what you expect. See our lsblk, blkid and findmnt guide for the columns.
lsblk -o NAME,SIZE,MODEL,RM,TRAN,MOUNTPOINTSCard readers often show up as /dev/sdX or /dev/mmcblkN, but never assume. If any partition of the card is mounted, unmount it first.
sudo umount /dev/sdX?*Flash an SD card with bmaptool
Replace /dev/sdX with the whole-disk device you confirmed, not a partition. This overwrites the card.
sudo bmaptool copy --bmap disk.bmap disk.img /dev/sdXThe manual page says that for a block device bmaptool opens it in exclusive mode, so the copy fails if another process already has it open. That is a useful guard, but not a substitute for checking the device name. Avoid --no-verify, which the manual marks as not recommended.
bmaptool detects compression from the file extension (gzip, bzip2, xz, lzo, lz4 and zstd) and decompresses on the fly. A .bmap file with the same base name is looked up next to the image.
Flash with dd
If bmaptool or a bmap is not available, dd works, with care. This also overwrites the card.
sudo dd if=disk.img of=/dev/sdX bs=4M conv=fsync status=progress
syncif is the source and of the target; swapping or mistyping them destroys data. Run sync before removing the card. bmaptool copy --nobmap writes the whole image without a map if you want bmaptool’s behavior without one.
A short safety checklist
- Run
lsblkbefore and after inserting the card. - Confirm the size matches the card, and the device is not your system disk.
- Unmount any mounted partitions.
- Verify the image checksum.
- Double-check
of=or the final bmaptool argument before pressing Enter.
For a tool to see what your board prints once it boots, see Serial Console on Embedded Linux. To measure how long the board takes to boot after flashing, see Reduce Embedded Linux Boot Time.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.