2

I am going to be testing user flows that use USB mounting/unmounting and file placement on a USB on a Linux based machine. One of our use cases is to notify the user if the USB device they have plugged in is corrupt or unreadable.

Is there a way to purposefully corrupt/break a USB device via command line so that we have one to test a "corrupt device" scenario?

Ideally, if the USB device can mount, but not be readable/writable this is what I am looking for.

8
  • I'm looking for a software one to corrupt the USB, but then I want the USB hardware itself to remain corrupt so that while testing other software, we have something we cause use for this use case. Ideally if there was some command line actions I can do on the USB drive while plugged in to corrupt it. By hardware do you mean something like "pull out the USB while writing files" or something like that?
    – Wimateeka
    Commented Mar 15, 2022 at 12:12
  • You might be able to do something with the device mapper using a target_type of either error or flakey. I haven't done it myself though.
    – user516667
    Commented Mar 15, 2022 at 12:43
  • @Martin that could be a great idea for this use case. Commented Mar 15, 2022 at 12:57
  • @Martin can you expand on how this would work? I've never used the device_mapper before and would need more detailed steps to do what you are suggesting
    – Wimateeka
    Commented Mar 15, 2022 at 13:20
  • 1
    I mentioned that I'd not done it, so I have been trying for the last hour or so. I don't think it will match your usage case since it creates an erroring dm device, but doesn't map it onto the USB stick. I also think that the "flakey" map won't work because the flakeyness will be in the device mapper and not on the USB. Sorry for the red herring. I think you'll have to fall back on roaima's suggestion below.
    – user516667
    Commented Mar 15, 2022 at 17:48

1 Answer 1

1

A hardware solution would almost certainly be off-topic here.

Software solutions. It's difficult to suggest anything that will offer a partial breakage. Here are some suggestions that may or may not be useful:

  1. Erase the USB. Insert the stick and verify that a mount didn't succeed.

  2. Format the USB as ext2. Mount the USB. Remove it. Unmount the filesystem. Possibly attempt to unmount the left-overs. What I would expect to happen when you insert this stick is that the mount will fail because a filesystem check (fsck) will be required.

  3. Format the USB as ext4. Set the read-only flag on the filesystem according to the instructions at Mark an ext4 filesystem as read-only. When you mount this stick you should find a read-only filesystem.

  4. Format the USB as ext2, ext3 or ext4. Fill the filesystem with files, large and small (cp -a /{bin,etc,lib} /mnt/usb for example, for a USB mounted on /mnt/usb). Unmount the filesystem. Wipe the USB from about 2MB onwards (dd bs=1M seek=2 if=/dev/zero >/dev/sdX, for a USB device /dev/sdX). When you next mount this filesystem you should find that (most of) the directory structure looks plausible but that files are corrupt.

I can't give you a recommendation for generating a read error from a USB stick. Perhaps pull the stick midway through it being used. Or synthesise that in your software.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .