4

I'm looking for a way to corrupt an ext3 partition safely, so that it can be recovered by fsck on next boot, for testing purposes. Thanks!

4
  • 6
    Are you just trying to force a fsck on reboot? There are safer, easier and better ways to do that.
    – MadHatter
    Jul 26, 2013 at 7:12
  • No, I'm not looking for force fsck using "touch /forcefsck" or something. I just want to see how it behaves when the file system is actually corrupt. And I don't worry about data loss in test environment. Thanks!
    – shad
    Jul 26, 2013 at 7:23
  • 1
    I'd like to emphasize the "seek=10000" that @MadHatter just wrote, please don't omit it from your testing or you're screwed without a full disk backup. You should probably add this as an answer?
    – pauska
    Jul 26, 2013 at 8:09
  • 1
    OK, I've added it as an answer and deleted it as a comment.
    – MadHatter
    Jul 26, 2013 at 9:42

2 Answers 2

6

Use dd to write data from /dev/zero on top of the raw device. Start with a few bytes, e2fsck -p, repeat, increasing the count until you trash something expensive.

dd if=/dev/zero bs=1 count=10 of=/dev/sda1 seek=10000

You increase count= to write more. The seek= is designed to pass over the first 10k of /dev/sda1 so you don't trash the superblock or any of the vital structures at the top of the filesystem. There's nothing wrong with trashing the superblock (given that this is test data), but fsck won't be able to autorecover from loss of the superblock so you'll have to manually point it to a backup superblock. You specifically ask about what the reboot fsck can recover from, so you should know that the -p flag puts fsck into the boot-time "fix what you can safely fix" mode.

If you do this on a data partition, you can avoid the reboot cost, and just keep the trash-fsck cycle going.

0
3

You could overwrite the first superblock, for ext3 it would be

dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/<filesystem to corrupt>

That's a fun thing to recover from. You also don't need to reboot just use fsck interactively.

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