summaryrefslogtreecommitdiff
path: root/build-aux/create_corrupted_file_system.sh
blob: 9f96f38215cac38b5fba5f94ee32160868b72e69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh

#    Copyright (C) 2014-2021 Assaf Gordon <assafgordon@gmail.com>
#
#    datamash I/O error testing module
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#    Written by Assaf Gordon.

BLOCK_SIZE=1024
IMGFILE=bad_disk.img
LOG=log.txt

die()
{
	BASE=$(basename "$0")
	## Print the STDOUT and STDERR we have so far
	cat $LOG >&2
	echo "$BASE: error: $@" >&2
	exit 1
}

# Reset log
echo > $LOG

## Ugly hack: Add common places where required programs might be.
## This script is run as a non-root user, so these directories
##  might not be on the $PATH
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin

for PROG in mkfs.ext3 debugfs ;
do
	which $PROG >/dev/null 2>&1 ||
		die "required program '$PROG' not found in \$PATH."
done

## Create data files, each 30000 bytes
seq 10000 | head -c 30000 > numbers.txt ||
        die "failed to create numbers.txt"
printf "%d bottles of beer on the wall\n" $(seq 2000 -1 2) |
	head -c 30000 > bottles.txt ||
        die "failed to create bottles.txt"

## Create ext3 file system image
dd if=/dev/zero of=$IMGFILE bs=1k count=128 1>$LOG 2>&1 || die "dd failed"
yes | mkfs.ext3 -b ${BLOCK_SIZE} $IMGFILE 1>$LOG 2>&1 ||
	die "mkfs.ext3 failed"

## Add two files and corrupt them
for FILE in numbers.txt bottles.txt ;
do
	## Add data file to file system
	echo "write ./$FILE $FILE"| debugfs -w $IMGFILE >$LOG 2>&1 ||
		die "failed to add $FILE to $IMGFILE"

	## Corrupt the file-system

	## Find the IND (indirect) block, which should contain pointers to
	## valid other blocks occupied by this file.
	INDBLOCK=$(echo "stat $FILE" |
			debugfs $IMGFILE 2>/dev/null |
			grep IND | perl -ne '/\(IND\):(\d+)/ && print $1')
	[ -z "$INDBLOCK" ] &&
		die "failed to find IND block for $FILE in $IMGFILE"

	## Mess-up the IND block, by overriting it with random data
	dd if=/dev/urandom bs=${BLOCK_SIZE} count=1 \
		of=$IMGFILE conv=notrunc seek=${INDBLOCK} >$LOG 2>&1 ||
			die "failed to override IND block $INDBLOCK"
done


## Verify that the file system image has errors in it
## (otherwise we failed to simulate errors)
fsck.ext3 -nf $IMGFILE >$LOG 2>&1 &&
	die "Failed to simulate bad file system (mkfs did not report errors)"

echo "
Done!

To use this file-systems, run:
  mkdir /tmp/badfs/
  sudo mount -o sync $IMGFILE /tmp/badfs/

To verify I/O errors, run:
  cat /tmp/badfs/numbers.txt > /dev/null
  cat /tmp/badfs/bottles.txt > /dev/null

"