I want to test a partition creation software like parted.

I found an old USB stick which crashes the program whenever I try to read it. In order to preserve this test case I need to copy the USB stick as exactly as possible, preserving IO errors.

I know it's probably impossible to make my HDD throw IO error when reading some bytes of that file, but maybe it can be emulated by some kind of a program which has whole USB stick contents hardcoded and remembers when to throw IO error?

I'm using Arch Linux and would prefer solution which can be adapted to it (possibly through some kind of VM or machine emulator).


For copying, use ddrescue, safecopy, or dd_rescue. They all keep a log of failed areas, as well as automatically retry until they can get the data out.

The general usage is:

  • ddrescue: (don't mix up with the older dd_rescue)

    ddrescue /dev/sdc3 sdc3.img sdc3.map

    If wanted, repeat with --retrim or `--try-again:

    ddrescue --try-again /dev/sdc3 sdc3.img sdc3.map

    Don't forget to specify the mapfile to record bad areas in.

  • safecopy:

    safecopy --stage1 /dev/sdc3 sdc3.img
    safecopy --stage2 /dev/sdc3 sdc3.img
    safecopy --stage3 /dev/sdc3 sdc3.img

    After stage 3, you'll have a stage3.badblocks file listing the individual bad sectors.

    If you want to retry, use:

    mv stage3.badblocks stage2.badblocks
    safecopy --stage3 /dev/sdc3 sdc3.img
  • dd_rescue: (not to be confused with the more-capable ddrescue)

    dd_rescue -o sdc3.badblocks /dev/sd3 sdc3.img

    (I've no idea if this tool supports resuming/retrying with a 2nd invocation, but at least it does output a badblocks log.)

These programs emphasize getting as much data out fast as they can, and keeping the slow recovery for later. So don't be surprised if "stage 1" will skip whole megabytes just because of a single bad block – it'll return to that in stage 2.

For emulating a bad disk, take the badblocks log you just got, and apply it to this post:

Use dmsetup to create a device backed by the "error" target. It will show up in /dev/mapper/<name>.

Page 7 of the Device mapper presentation (PDF) has exactly what you're looking for:

dmsetup create bad_disk << EOF
  0 8       linear /dev/sdb1 0
  8 1       error
  9 204791 linear /dev/sdb1 9

Or leave out the sdb1 parts to and put the "error" target as the device for blocks 0 - 8 (instead of sdb1) to make a pure error disk.

See also The Device Mapper appendix from "RHEL 5 Logical Volume Manager Administration".

— Peter Cordes, https://stackoverflow.com/questions/1870696/simulate-a-faulty-block-device-with-read-errors

(Converting a badblocks log or a ddrescue mapfile to dmsetup table syntax is left as an exercise to the reader...)

  • Like my answer, this answer too fails to tell the user how to preserve the disk errors. I'm actually unsure it's even possible. – djsmiley2k Feb 18 '17 at 20:40

dd can be told to continue when encountering errors, this will just write zeros in the spaces it can't read from the original disk...

dd bs=<blocksize here> conv=sync,noerror if=/dev/sdc123 of=/home/me/testFile

You could also try using ddrescue to try and actually recover the data however I'm unsure how well it'd work on a USB (flash based) disk.

WARNING - If you screw up the blocksize, you will screw up all data after the first non-read block - you want to set the blocksize to that of the device you're copying from.

  • I think this is the opposite of what the question asks. I think the question indicates that the cloned drive have the same errors as the original. – Ron Maupin Feb 18 '17 at 19:52
  • Yup, I agree @RonMaupin but figured I'd let them know about dd anyway. It won't attempt to 'recover' the bad data.... so any application which depends on said data will still get issues. – djsmiley2k Feb 18 '17 at 19:55
  • @RonMaupin is correct, copying with dd won't allow distinguishing between IO error and 0x0000, let alone emulating IO errors later. – styrofoam fly Feb 18 '17 at 20:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.