Shop OBEX P1 Docs P2 Docs Learn Events
How to purposely corrupt a linux filesystem — Parallax Forums

How to purposely corrupt a linux filesystem


I would like to corrupt a Centos 5 or Centos 6 filesystem in such a way that upon booting the OS would detect that the filesystem is inconsistent and then drop to a command prompt to allow user to correct the filesystem manually.

I tried
a. powering off the computer abruptly at different times
b. left a file open after writing to it and then power off the computer

both didn't work as the OS considers the problems as minor and would then auto-correct them from the journal.

Comments

  • evanhevanh Posts: 15,187
    edited 2018-04-12 14:10
    It's not a data corruption method but changing the UUID number of the boot drive volume would probably do it.

    Recovering from this might be a challenge though. You'd have to get familiar with grub commands. Something I still haven't been motivated enough to do myself.


  • evanhevanh Posts: 15,187
    A bit of googling tells me it's easy to set an exact UUID number using tune2fs. Which means, from another boot partition/drive, it would be easy to change it and change it back again assuming you are just wanting a temporary boot fail. Which, in turn, means no need to jump through hoops learning the ins and outs of Grub.

  • I was thinking more towards simulating an actual corruption that may happen in normal usage of the filesystem.
    This is because I am working on a system that tries to recover from such a corruption without human intervention.
    I need a corrupted filesystem to let the system try recover it.
  • evanhevanh Posts: 15,187
    edited 2018-04-12 15:43
    Probably not a good idea to be playing with any operational partition at all then. Start with an empty partition you can keep reformatting to whatever suits. Volume mounting and unmounting should be all you need for checking of filesystem validity sequencing.

  • I was thinking more towards simulating an actual corruption that may happen in normal usage of the filesystem.
    This is because I am working on a system that tries to recover from such a corruption without human intervention.
    I need a corrupted filesystem to let the system try recover it.

    It depends on the storage medium.
    What is your medium?

    Theres always the option of putting it in the microwave for 1 second? lol. (dont do that)
  • My medium for Centos 5 is a Sandisk 8GB USB Key
    For Centos 6 is a Sandisk 16GB USB Key

    It has 2 partitions, which are /dev/sda1 mounted to /boot and
    /dev/sda2 mounted to /

    Normally it is the /dev/sda2 that gets corrupted because it is the one being used for writing.
    Any ideas on how to corrupt /dev/sda2 ?
  • evanhevanh Posts: 15,187
    edited 2018-04-13 03:14
    Normally it is the /dev/sda2 that gets corrupted because it is the one being used for writing.
    Any ideas on how to corrupt /dev/sda2 ?
    I think the idea is journaling is meant to handle any such data corruption. If it hasn't then the drive probably needs replaced.

    That said, disabling the journaling feature would do the trick I guess.

  • evanhevanh Posts: 15,187
    edited 2018-04-13 09:25
    Ie: Nerf the protection then mess with the volume metadata. I'm not sure of the outcome but would that be a satisfactory approach for your aims?

    EDIT: I suppose just arbitrary block writes to the partition, bypassing the volume's filesystem, will mess it up even with journaling enabled. Be warned, it won't be recoverable doing this method.

  • I suppose just arbitrary block writes to the partition, bypassing the volume's filesystem, will mess it up even with journaling enabled.
    Be warned, it won't be recoverable doing this method.


    Ok lets try this method.
    Do you have the specific commands to block write a small part of the partition?
    Sorry as i am not very good at linux low level commands.
  • evanhevanh Posts: 15,187
    edited 2018-04-13 13:25
    Here's an example I just hit a spare ext4 partition with. It wipes 4MB chunk, starting 10kB in. It made the drive unmountable until repaired with e2fsck. Impressively the journal contained enough to fully repair the damage:

    $ sudo dd if=/dev/zero of=/dev/sdh3 bs=1k seek=10 count=4k

    Not sure if "sudo" is used in Centos. It's for raising your privilege for the duration of the command issued. You have to enter your login password once. Nothing stopping you smashing any valid volume, mounted or not, so be careful to target the right one!

    "dd" is the block copy command.
    "if=/dev/zero" means source data of unlimited nulls. It can be any file. Defaults to console input if not set.
    "of=/dev/sdh3" means destination file, in this case my spare partition.
    "bs" for virtual block size used in the copying process.
    "seek" for starting virtual block number in destination.
    "count" for number of virtual blocks to copy.

Sign In or Register to comment.