xref: /freebsd/tools/test/stress2/misc/fsck8.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
17f3383d2SPeter Holm#!/bin/sh
27f3383d2SPeter Holm
37f3383d2SPeter Holm#
4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
57f3383d2SPeter Holm#
67f3383d2SPeter Holm# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
77f3383d2SPeter Holm#
87f3383d2SPeter Holm# Redistribution and use in source and binary forms, with or without
97f3383d2SPeter Holm# modification, are permitted provided that the following conditions
107f3383d2SPeter Holm# are met:
117f3383d2SPeter Holm# 1. Redistributions of source code must retain the above copyright
127f3383d2SPeter Holm#    notice, this list of conditions and the following disclaimer.
137f3383d2SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
147f3383d2SPeter Holm#    notice, this list of conditions and the following disclaimer in the
157f3383d2SPeter Holm#    documentation and/or other materials provided with the distribution.
167f3383d2SPeter Holm#
177f3383d2SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
187f3383d2SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
197f3383d2SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207f3383d2SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
217f3383d2SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
227f3383d2SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
237f3383d2SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
247f3383d2SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
257f3383d2SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
267f3383d2SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
277f3383d2SPeter Holm# SUCH DAMAGE.
287f3383d2SPeter Holm#
297f3383d2SPeter Holm
307f3383d2SPeter Holm# fsck_ffs(8) disk image fuzz test. SB focus.
317f3383d2SPeter Holm
327f3383d2SPeter Holm# panic: wrong length 4098 for sectorsize 512
337f3383d2SPeter Holm# FreeBSD 14.0-CURRENT #0 main-n255602-51adf913e8815: Fri May 13 07:55:32 CEST 2022
347f3383d2SPeter Holm# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
357f3383d2SPeter Holm
367f3383d2SPeter Holm. ../default.cfg
377f3383d2SPeter Holm
387f3383d2SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
397f3383d2SPeter Holm
407f3383d2SPeter Holmcc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
417f3383d2SPeter Holm
427f3383d2SPeter Holmset -e
437f3383d2SPeter Holmu1=$mdstart
447f3383d2SPeter Holmu2=$((mdstart + 1))
457f3383d2SPeter Holmmp1=${mntpoint}$u1
467f3383d2SPeter Holmmp2=${mntpoint}$u2
477f3383d2SPeter Holmmkdir -p $mp1 $mp2
487f3383d2SPeter Holmlog=$mp1/fsck8.sh.log
497f3383d2SPeter Holmdiskimage=$mp1/fsck8.sh.diskimage
507f3383d2SPeter Holmbackup=/tmp/fsck8.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
517f3383d2SPeter Holmasbs=0
527f3383d2SPeter Holmcleans=0
537f3383d2SPeter Holmreruns=0
547f3383d2SPeter Holmwaccess=0
557f3383d2SPeter Holm
567f3383d2SPeter Holmmax=$((10 * 1024 * 1024))
577f3383d2SPeter Holm# UFS1 or UFS2 SU:
587f3383d2SPeter Holm[ `jot -r 1 0 1` -eq 0 ] && newfs_flags='-O 1' || newfs_flags='-O 2 -U'
597f3383d2SPeter Holm
607f3383d2SPeter Holmmount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
617f3383d2SPeter Holm[ -c /dev/md$u1 ] && mdconfig -d -u $u1
627f3383d2SPeter Holmmdconfig -a -t swap -s 1g -u $u1
637f3383d2SPeter Holmnewfs $newfs_flags -n /dev/md$u1 > /dev/null
647f3383d2SPeter Holmmount /dev/md$u1 $mp1
657f3383d2SPeter Holm
667f3383d2SPeter Holm[ -c /dev/md$u2 ] && mdconfig -d -u $u2
677f3383d2SPeter Holmdd if=/dev/zero of=$diskimage bs=$max count=1 status=none
687f3383d2SPeter Holmmdconfig -a -t vnode -f $diskimage -u $u2
697f3383d2SPeter Holmbackups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
707f3383d2SPeter Holm    tail -1 | sed 's/,//g'`
717f3383d2SPeter Holmnewfs $newfs_flags -n md$u2 > /dev/null
727f3383d2SPeter Holmmount /dev/md$u2 $mp2
737f3383d2SPeter Holm[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
747f3383d2SPeter Holmumount $mp2
757f3383d2SPeter Holmset +e
767f3383d2SPeter Holm
777f3383d2SPeter Holmchk() {
787f3383d2SPeter Holm	local i
797f3383d2SPeter Holm
807f3383d2SPeter Holm	clean=0
817f3383d2SPeter Holm	rerun=0
827f3383d2SPeter Holm	waccess=0
83dd894897SPeter Holm	timeout 5m fsck_ffs -fy $1 > $log 2>&1
847f3383d2SPeter Holm	r=$?
85dd894897SPeter Holm	if grep -qiE "super-?block.*failed" $log; then
867f3383d2SPeter Holm		for b in $backups; do
877f3383d2SPeter Holm			echo "Using alternate SB $b"
887f3383d2SPeter Holm			asbs=$((asbs + 1))
897f3383d2SPeter Holm			fsck_ffs -b $b -fy $1 > $log 2>&1
907f3383d2SPeter Holm			r=$?
91dd894897SPeter Holm			grep -qiE "super-?block.*failed" $log ||
927f3383d2SPeter Holm			   break
937f3383d2SPeter Holm		done
947f3383d2SPeter Holm		usedasb=1
957f3383d2SPeter Holm	else
967f3383d2SPeter Holm		usedasb=0
977f3383d2SPeter Holm	fi
987f3383d2SPeter Holm	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
997f3383d2SPeter Holm	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
1007f3383d2SPeter Holm	# For now regard a "was modified" as a cause for a rerun,
1017f3383d2SPeter Holm	# disregarding the "clean" claim.
1027f3383d2SPeter Holm	grep -Eq "WAS MODIFIED" $log && rerun=1
1037f3383d2SPeter Holm	grep -q RERUN $log && rerun=1
1047f3383d2SPeter Holm	grep -q "NO WRITE ACCESS" $log && waccess=1
1057f3383d2SPeter Holm	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
1067f3383d2SPeter Holm}
1077f3383d2SPeter Holm
1087f3383d2SPeter Holmcd $mp1
1097f3383d2SPeter Holmclean=0
1107f3383d2SPeter Holms=0
1117f3383d2SPeter Holmstart=`date +%s`
1127f3383d2SPeter Holmwhile [ $((`date +%s` - start)) -lt 300 ]; do
113dd894897SPeter Holm	mount /dev/md$u2 $mp2 || break
1147f3383d2SPeter Holm	ls -lR $mp2 > /dev/null || { s=102; echo "ls failed"; break; }
1157f3383d2SPeter Holm	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
1167f3383d2SPeter Holm	while mount | grep -q "on $mp2 "; do umount $mp2; done
1177f3383d2SPeter Holm	echo * | grep -q core && break
1187f3383d2SPeter Holm	sync
1197f3383d2SPeter Holm	mdconfig -d -u $u2
1207f3383d2SPeter Holm	# SBLOCK = 64k, SBLOCKSIZE = 8k
1217f3383d2SPeter Holm	/tmp/flip -n 5 -s $(((64 + 8) * 1024)) $diskimage
1227f3383d2SPeter Holm
1237f3383d2SPeter Holm	sync
1247f3383d2SPeter Holm	if [ `stat -f%z $diskimage` -gt $max ]; then
1257f3383d2SPeter Holm		ls -lh $diskimage
1267f3383d2SPeter Holm		truncate -s $max $diskimage
1277f3383d2SPeter Holm	else
1287f3383d2SPeter Holm		gzip < $diskimage > $backup
1297f3383d2SPeter Holm	fi
1307f3383d2SPeter Holm	fsync $backup
131dd894897SPeter Holm	sync
1327f3383d2SPeter Holm
1337f3383d2SPeter Holm	for i in `jot 5`; do
1347f3383d2SPeter Holm		[ $i -gt 2 ] && echo "fsck run #$i"
1357f3383d2SPeter Holm		chk $diskimage
1367f3383d2SPeter Holm		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
1377f3383d2SPeter Holm		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
1387f3383d2SPeter Holm		[ -f fsck_ffs.core ] &&
139dd894897SPeter Holm		    { cp -v $diskimage \
1407f3383d2SPeter Holm		        /tmp/fsck_ffs.core.diskimage.`date +%Y%m%dT%H%M%S`; break 2; }
1417f3383d2SPeter Holm	done
1427f3383d2SPeter Holm	[ $clean -ne 1 ] && break
1437f3383d2SPeter Holm	mdconfig -a -t vnode -f $diskimage -u $u2
1447f3383d2SPeter Holm	[ $r -ne 0 -a $clean -eq 1 ] &&
1457f3383d2SPeter Holm	    { echo "CLEAN && non zero exit code"; break; }
1467f3383d2SPeter Holm	[ $clean -eq 1 ] && continue
1477f3383d2SPeter Holm	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
1487f3383d2SPeter Holm	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
1497f3383d2SPeter Holm	break
1507f3383d2SPeter Holmdone
1517f3383d2SPeter Holmsleep 2	# Wait for /dev to catch up
1527f3383d2SPeter Holm[ -c /dev/md$u2 ] && r1=1 || r1=0
1537f3383d2SPeter Holmfor i in `jot 5`; do
1547f3383d2SPeter Holm	mount | grep -q "on $mp2 " || break
1557f3383d2SPeter Holm	umount $mp2 && break
1567f3383d2SPeter Holm	sleep 2
1577f3383d2SPeter Holmdone
1587f3383d2SPeter Holmmdconfig -d -u $u2 2>/dev/null # XXX when mount fails
1597f3383d2SPeter Holm
1607f3383d2SPeter Holmecho "$cleans cleans, $reruns reruns, $asbs alternate SBs."
1617f3383d2SPeter Holmif [ $clean -ne 1 ]; then
1627f3383d2SPeter Holm	echo "FS still not clean. Last fsck_ffs exit code was $r."
1637f3383d2SPeter Holm	echo =================
1647f3383d2SPeter Holm	cat $log
1657f3383d2SPeter Holm	echo =================
1667f3383d2SPeter Holm	cp -v $log /tmp
1677f3383d2SPeter Holm	[ $s -eq 0 ] && s=106
1687f3383d2SPeter Holmfi
169dd894897SPeter Holmecho * | grep -q core && { ls -l *.core; cp -v $log /tmp; exit 106; } ||
1707f3383d2SPeter Holm    rm -f $backup
1717f3383d2SPeter Holm[ $s -eq 101 ] && rm -f $backup	# mount error breakout
1727f3383d2SPeter Holmcd /tmp
1737f3383d2SPeter Holmfor i in `jot 5`; do
1747f3383d2SPeter Holm	umount $mp1 && break
1757f3383d2SPeter Holm	sleep 2
1767f3383d2SPeter Holmdone
1777f3383d2SPeter Holmmdconfig -d -u $u1
1787f3383d2SPeter Holmrm -f /tmp/flip
1797f3383d2SPeter Holmexit $s
180