I want to intentionally corrupt a file on an ext4 filesystem for testing. Is there a way to corrupt it by, say, leaving a file open and then rebooting?
Do you want to be able to recover? Or how corrupt are we talking?
I would use
dd, a block transfer utility.
dd if=/dev/urandom of=/dev/sdWV count=X bs=Y seek=Z
sdWV is the device you wish to corrupt
- sdW will be the block device
- V will be the partition number
X is the number of bocks you want to write
Y is the size of each block
Z is the place on the partition where you want to start corrupting data
Make sure you make a backup before you do any corruption.
Here, the image source will be
/dev/urandom, a stream that will just generate random data, overwriting anything and everything on your device.
If you omit the
count directive, it will continue writing until the device is full up.
Heh, sorry about the corruption to your current running system - your post title and body differ very much. Corrupting a filesystem and corrupting a file are pretty different...
If you want to corrupt the file, you can still use dd, but output overwriting an existing file - not the actual block device.
~ > echo "this is a test file that is about to be corrupted" > test_file ~ > cat test_file this is a test file that is about to be corrupted ~ > dd if=/dev/urandom of=test_file bs=8 count=1 seek=1 1+0 records in 1+0 records out 8 bytes (8 B) copied, 0.007185 s, 1.1 kB/s ~ > cat test_file this is \o♣▒▒▒_C~ >