go install github.com/hchargois/blocksan@latest
blocksan sanitizes a block device.
It basically does 3 things in a single command:
- Securely erase the block device
- Ensure it's working well and storing data correctly
- Benchmark the read and write speeds
The primary reason I wrote this tool is because I often recycle SD (or CFExpress) cards while buying or selling cameras; and I wanted to make sure the cards are in good condition, sufficiently fast, and that I'm not leaving any recoverable data on it.
It should also work fine on SSDs, however you may prefer to use the dedicated SATA/NVMe erase/sanitize commands which may be faster and ensure spare storage areas are erased too. You may still want to use blocksan in addition to verify integrity and speed of the device, though.
It may also be used to ensure you've not been scammed with one of those fake USB drives or SD cards that lie about their actual capacity.
The goal of this program is to erase data. To avoid mistakes, it warns you about the size of the device that's going to be erased and asks you for confirmation. But ultimately, if you do make a mistake, well... I hope you have backups.
Actually, even if you don't make a mistake, this program may erase all your data and eat your dog. I'm not giving any warranty.
The program does two passes, the first one writes data on the full device and the second one verifies it. The data that's written is pseudorandom and non-repeating so that the second pass can ensure that the device has actually stored it all correctly.
It uses direct IO to avoid any OS-level cache.
Note that direct IO is implemented through the github.com/ncw/directio library which aims to be portable, but blocksan has only been tested on Linux.
$ sudo blocksan /dev/sda
Opened /dev/sda of size 256060514304 bytes (238.5 GiB)
WARNING: This will irreversibly destroy all data on the device!
Type 'yes' to continue: yes
writing 100% |████████████████████| (238/238 GiB, 306 MiB/s)
Avg write speed: 306.0 MiB/s
P1 write speed: 58.5 MiB/s
P10 write speed: 325.2 MiB/s
verifying 100% |████████████████████| (238/238 GiB, 611 MiB/s)
Avg read speed: 612.1 MiB/s
P1 read speed: 450.6 MiB/s
P10 read speed: 509.5 MiB/s
✅ All good!
Verifying the data has failed, the device is shot, discard it (responsibly!)
$ sudo blocksan /dev/sde
Opened /dev/sde of size 63281561600 bytes (58.9 GiB)
WARNING: This will irreversibly destroy all data on the device!
Type 'yes' to continue: yes
writing 100% |████████████████████| (59/59 GiB, 128 MiB/s)
Avg write speed: 127.9 MiB/s
P1 write speed: 61.3 MiB/s
P10 write speed: 114.5 MiB/s
verifying 0% | | ( 0 B/59 GiB) [0s:0s]
❗ data mismatch