summaryrefslogtreecommitdiffstats
path: root/misc/e2fuzz.sh
blob: 389f2ca4fd98956688142423eb641b915eb54e52 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash

# Test harness to fuzz a filesystem over and over...
# Copyright (C) 2014 Oracle.

DIR=/tmp
PASSES=10000
SZ=32m
SCRIPT_DIR="$(dirname "$0")"
FEATURES="has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize,64bit,metadata_csum,bigalloc,sparse_super2,inline_data"
BLK_SZ=4096
INODE_SZ=256
EXTENDED_OPTS="discard"
EXTENDED_FSCK_OPTIONS=""
RUN_FSCK=1
OVERRIDE_PATH=1
HAS_FUSE2FS=0
USE_FUSE2FS=0
MAX_FSCK=10
SRCDIR=/etc
test -x "${SCRIPT_DIR}/fuse2fs" && HAS_FUSE2FS=1

print_help() {
	echo "Usage: $0 OPTIONS"
	echo "-b:	FS block size is this. (${BLK_SZ})"
	echo "-B:	Corrupt this many bytes per run."
	echo "-d:	Create test files in this directory. (${DIR})"
	echo "-E:	Extended mke2fs options."
	echo "-f:	Do not run e2fsck after each pass."
	echo "-F:	Extended e2fsck options."
	echo "-I:	Create inodes of this size. (${INODE_SZ})"
	echo "-n:	Run this many passes. (${PASSES})"
	echo "-O:	Create FS with these features."
	echo "-p:	Use system's mke2fs/e2fsck/tune2fs tools."
	echo "-s:	Create FS images of this size. (${SZ})"
	echo "-S:	Copy files from this dir. (${SRCDIR})"
	echo "-x:	Run e2fsck at most this many times. (${MAX_FSCK})"
	test "${HAS_FUSE2FS}" -gt 0 && echo "-u:	Use fuse2fs instead of the kernel."
	exit 0
}

GETOPT="d:n:s:O:I:b:B:E:F:fpx:S:"
test "${HAS_FUSE2FS}" && GETOPT="${GETOPT}u"

while getopts "${GETOPT}" opt; do
	case "${opt}" in
	"B")
		E2FUZZ_ARGS="${E2FUZZ_ARGS} -b ${OPTARG}"
		;;
	"d")
		DIR="${OPTARG}"
		;;
	"n")
		PASSES="${OPTARG}"
		;;
	"s")
		SZ="${OPTARG}"
		;;
	"O")
		FEATURES="${FEATURES},${OPTARG}"
		;;
	"I")
		INODE_SZ="${OPTARG}"
		;;
	"b")
		BLK_SZ="${OPTARG}"
		;;
	"E")
		EXTENDED_OPTS="${OPTARG}"
		;;
	"F")
		EXTENDED_FSCK_OPTS="-E ${OPTARG}"
		;;
	"f")
		RUN_FSCK=0
		;;
	"p")
		OVERRIDE_PATH=0
		;;
	"u")
		USE_FUSE2FS=1
		;;
	"x")
		MAX_FSCK="${OPTARG}"
		;;
	"S")
		SRCDIR="${OPTARG}"
		;;
	*)
		print_help
		;;
	esac
done

if [ "${OVERRIDE_PATH}" -gt 0 ]; then
	PATH="${SCRIPT_DIR}:${SCRIPT_DIR}/../e2fsck/:${PATH}"
	export PATH
fi

TESTDIR="${DIR}/tests/"
TESTMNT="${DIR}/mnt/"
BASE_IMG="${DIR}/e2fuzz.img"

cat > /tmp/mke2fs.conf << ENDL
[defaults]
        base_features = ${FEATURES}
        default_mntopts = acl,user_xattr,block_validity
        enable_periodic_fsck = 0
        blocksize = ${BLK_SZ}
        inode_size = ${INODE_SZ}
        inode_ratio = 4096
	cluster_size = $((BLK_SZ * 2))
	options = ${EXTENDED_OPTS}
ENDL
MKE2FS_CONFIG=/tmp/mke2fs.conf
export MKE2FS_CONFIG

# Set up FS image
echo "+ create fs image"
umount "${TESTDIR}"
umount "${TESTMNT}"
rm -rf "${TESTDIR}"
rm -rf "${TESTMNT}"
mkdir -p "${TESTDIR}"
mkdir -p "${TESTMNT}"
rm -rf "${BASE_IMG}"
truncate -s "${SZ}" "${BASE_IMG}"
mke2fs -F -v "${BASE_IMG}"
if [ $? -ne 0 ]; then
	exit $?
fi

# Populate FS image
echo "+ populate fs image"
modprobe loop
mount "${BASE_IMG}" "${TESTMNT}" -o loop
if [ $? -ne 0 ]; then
	exit $?
fi
SRC_SZ="$(du -ks "${SRCDIR}" | awk '{print $1}')"
FS_SZ="$(( $(stat -f "${TESTMNT}" -c '%a * %S') / 1024 ))"
NR="$(( (FS_SZ * 4 / 10) / SRC_SZ ))"
if [ "${NR}" -lt 1 ]; then
	NR=1
fi
echo "+ make ${NR} copies"
seq 1 "${NR}" | while read nr; do
	cp -pRdu "${SRCDIR}" "${TESTMNT}/test.${nr}" 2> /dev/null
done
umount "${TESTMNT}"
e2fsck -fn "${BASE_IMG}"
if [ $? -ne 0 ]; then
	echo "fsck failed??"
	exit 1
fi

# Run tests
echo "+ run test"
ret=0
seq 1 "${PASSES}" | while read pass; do
	echo "+ pass ${pass}"
	PASS_IMG="${TESTDIR}/e2fuzz-${pass}.img"
	FSCK_IMG="${TESTDIR}/e2fuzz-${pass}.fsck"
	FUZZ_LOG="${TESTDIR}/e2fuzz-${pass}.fuzz.log"
	OPS_LOG="${TESTDIR}/e2fuzz-${pass}.ops.log"

	echo "++ corrupt image"
	cp "${BASE_IMG}" "${PASS_IMG}"
	if [ $? -ne 0 ]; then
		exit $?
	fi
	tune2fs -L "e2fuzz-${pass}" "${PASS_IMG}"
	e2fuzz -v "${PASS_IMG}" ${E2FUZZ_ARGS} > "${FUZZ_LOG}"
	if [ $? -ne 0 ]; then
		exit $?
	fi

	echo "++ mount image"
	if [ "${USE_FUSE2FS}" -gt 0 ]; then
		"${SCRIPT_DIR}/fuse2fs" "${PASS_IMG}" "${TESTMNT}"
		res=$?
	else
		mount "${PASS_IMG}" "${TESTMNT}" -o loop
		res=$?
	fi

	if [ "${res}" -eq 0 ]; then
		echo "+++ ls -laR"
		ls -laR "${TESTMNT}/test.1/" > /dev/null 2> "${OPS_LOG}"

		echo "+++ cat files"
		find "${TESTMNT}/test.1/" -type f -size -1048576k -print0 | xargs -0 cat > /dev/null 2>> "${OPS_LOG}"

		echo "+++ expand"
		find "${TESTMNT}/" -type f 2> /dev/null | head -n 50000 | while read f; do
			attr -l "$f" > /dev/null 2>> "${OPS_LOG}"
			if [ -f "$f" -a -w "$f" ]; then
				dd if=/dev/zero bs="${BLK_SZ}" count=1 >> "$f" 2>> "${OPS_LOG}"
			fi
			mv "$f" "$f.longer" > /dev/null 2>> "${OPS_LOG}"
		done
		sync

		echo "+++ create files"
		cp -pRdu "${SRCDIR}" "${TESTMNT}/test.moo" 2>> "${OPS_LOG}"
		sync

		echo "+++ remove files"
		rm -rf "${TESTMNT}/test.moo" 2>> "${OPS_LOG}"

		umount "${TESTMNT}"
		res=$?
		if [ "${res}" -ne 0 ]; then
			ret=1
			break
		fi
		sync
		test "${USE_FUSE2FS}" -gt 0 && sleep 2
	fi
	if [ "${RUN_FSCK}" -gt 0 ]; then
		cp "${PASS_IMG}" "${FSCK_IMG}"
		pass_img_sz="$(stat -c '%s' "${PASS_IMG}")"

		seq 1 "${MAX_FSCK}" | while read fsck_pass; do
			echo "++ fsck pass ${fsck_pass}: $(which e2fsck) -fy ${FSCK_IMG} ${EXTENDED_FSCK_OPTS}"
			FSCK_LOG="${TESTDIR}/e2fuzz-${pass}-${fsck_pass}.log"
			e2fsck -fy "${FSCK_IMG}" ${EXTENDED_FSCK_OPTS} > "${FSCK_LOG}" 2>&1
			res=$?
			echo "++ fsck returns ${res}"
			if [ "${res}" -eq 0 ]; then
				exit 0
			elif [ "${fsck_pass}" -eq "${MAX_FSCK}" ]; then
				echo "++ fsck did not fix in ${MAX_FSCK} passes."
				exit 1
			fi
			if [ "${res}" -gt 0 -a \
			     "$(grep 'Memory allocation failed' "${FSCK_LOG}" | wc -l)" -gt 0 ]; then
				echo "++ Ran out of memory, get more RAM"
				exit 0
			fi
			if [ "${res}" -gt 0 -a \
			     "$(grep 'Could not allocate block' "${FSCK_LOG}" | wc -l)" -gt 0 -a \
			     "$(dumpe2fs -h "${FSCK_IMG}" | grep '^Free blocks:' | awk '{print $3}')0" -eq 0 ]; then
				echo "++ Ran out of space, get a bigger image"
				exit 0
			fi
			if [ "${fsck_pass}" -gt 1 ]; then
				diff -u "${TESTDIR}/e2fuzz-${pass}-$((fsck_pass - 1)).log" "${FSCK_LOG}"
				if [ $? -eq 0 ]; then
					echo "++ fsck makes no progress"
					exit 2
				fi
			fi

			fsck_img_sz="$(stat -c '%s' "${FSCK_IMG}")"
			if [ "${fsck_img_sz}" -ne "${pass_img_sz}" ]; then
				echo "++ fsck image size changed"
				exit 3
			fi
		done
		fsck_loop_ret=$?
		if [ "${fsck_loop_ret}" -gt 0 ]; then
			break;
		fi
	fi

	echo "+++ check fs for round 2"
	FSCK_LOG="${TESTDIR}/e2fuzz-${pass}-round2.log"
	e2fsck -fn "${FSCK_IMG}" ${EXTENDED_FSCK_OPTS} >> "${FSCK_LOG}" 2>&1
	res=$?
	if [ "${res}" -ne 0 ]; then
		echo "++++ fsck failed."
		exit 1
	fi

	echo "++ mount image (2)"
	mount "${FSCK_IMG}" "${TESTMNT}" -o loop
	res=$?

	if [ "${res}" -eq 0 ]; then
		echo "+++ ls -laR (2)"
		ls -laR "${TESTMNT}/test.1/" > /dev/null 2> "${OPS_LOG}"

		echo "+++ cat files (2)"
		find "${TESTMNT}/test.1/" -type f -size -1048576k -print0 | xargs -0 cat > /dev/null 2>> "${OPS_LOG}"

		echo "+++ expand (2)"
		find "${TESTMNT}/" -type f 2> /dev/null | head -n 50000 | while read f; do
			attr -l "$f" > /dev/null 2>> "${OPS_LOG}"
			if [ -f "$f" -a -w "$f" ]; then
				dd if=/dev/zero bs="${BLK_SZ}" count=1 >> "$f" 2>> "${OPS_LOG}"
			fi
			mv "$f" "$f.longer" > /dev/null 2>> "${OPS_LOG}"
		done
		sync

		echo "+++ create files (2)"
		cp -pRdu "${SRCDIR}" "${TESTMNT}/test.moo" 2>> "${OPS_LOG}"
		sync

		echo "+++ remove files (2)"
		rm -rf "${TESTMNT}/test.moo" 2>> "${OPS_LOG}"

		umount "${TESTMNT}"
		res=$?
		if [ "${res}" -ne 0 ]; then
			ret=1
			break
		fi
		sync
		test "${USE_FUSE2FS}" -gt 0 && sleep 2

		echo "+++ check fs (2)"
		e2fsck -fn "${FSCK_IMG}" >> "${FSCK_LOG}" 2>&1
		res=$?
		if [ "${res}" -ne 0 ]; then
			echo "++ fsck failed."
			exit 1
		fi
	else
		echo "++ mount(2) failed with ${res}"
		exit 1
	fi
	rm -rf "${FSCK_IMG}" "${PASS_IMG}" "${FUZZ_LOG}" "${TESTDIR}"/e2fuzz*.log
done

exit $ret