1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2023 Peter Holm <pho@FreeBSD.org> 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27# SUCH DAMAGE. 28# 29 30# Variation of fsck9.sh with smaller disk size and newfs options '', 'U' and 'O1' 31# fsck_ffs core dump seen 32 33. ../default.cfg 34 35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 36 37cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1 38 39set -e 40prog=$(basename "$0" .sh) 41u1=$mdstart 42u2=$((mdstart + 1)) 43mp1=${mntpoint}$u1 44mp2=${mntpoint}$u2 45mkdir -p $mp1 $mp2 46log=$mp1/$prog.sh.log 47diskimage=$mp1/$prog.sh.diskimage 48backup=/tmp/$prog.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz 49cleans=0 50reruns=0 51 52# Pick a random newfs flag 53newfs_flags=$(echo "" "-U" "-O1" | awk -v N=`jot -r 1 1 3` '{print $N}') 54[ $# -eq 1 ] && newfs_flags="$1" # or use script argument 55max=$((512 * 1024)) 56 57mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1 58[ -c /dev/md$u1 ] && mdconfig -d -u $u1 59mdconfig -a -t swap -s 1g -u $u1 60newfs $newfs_flags -n /dev/md$u1 > /dev/null 61mount /dev/md$u1 $mp1 62 63[ -c /dev/md$u2 ] && mdconfig -d -u $u2 64dd if=/dev/zero of=$diskimage bs=$max count=1 status=none 65mdconfig -a -t vnode -f $diskimage -u $u2 66backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \ 67 tail -1 | sed 's/,//g'` 68echo "newfs $newfs_flags -n md$u2" 69newfs $newfs_flags -n md$u2 > /dev/null 70[ "$newfs_flags" = "" ] && tunefs -n disable md$u2 71set +e 72 73chk() { 74 local i 75 76 clean=0 77 rerun=0 78 fsck_ffs -fy $1 > $log 2>&1 79 r=$? 80 if grep -qiE "super-?block.*failed" $log; then 81 for b in $backups; do 82 echo "fsck_ffs -b $b -fy $1" 83 fsck_ffs -b $b -fy $1 > $log 2>&1 84 r=$? 85 grep -qiE "super-?block.*failed" $log || 86 break 87 echo "Checking next SB" 88 done 89 usedasb=1 90 else 91 usedasb=0 92 fi 93 LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0 94 grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1 95 grep -q RERUN $log && rerun=1 96 [ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1" 97} 98 99cd $mp1 100clean=0 101errors=0 102s=0 103start=`date +%s` 104while [ $((`date +%s` - start)) -lt 300 ]; do 105 mount /dev/md$u2 $mp2 || break 106 if ! ls -lR $mp2 > /dev/null; then 107 s=102 108 echo "ls failed"; grep "core dumped" /var/log/messages | tail -1 109 break 110 fi 111 rm -f $mp2/???????? 112 touch $mp2/`jot -rc 8 a z | tr -d '\n'` 113 mkdir -p $mp2/dir/dir/dir/dir 114 echo "abc" > $mp2/dir/dir/dir/dir/f 115 while mount | grep -q "on $mp2 "; do umount $mp2; done 116 echo * | grep -q core && break 117 mdconfig -d -u $u2 118 119 # Introduce 5 random single bit errors to the file system image 120 /tmp/flip -n 5 $diskimage 121 122 sync; sleep .1 123 if [ `stat -f%z $diskimage` -gt $max ]; then 124 ls -lh $diskimage 125 truncate -s $max $diskimage 126 else 127 gzip < $diskimage > $backup 128 fi 129 fsync $backup 130 sync; sleep .1 131 132 for i in `jot 5`; do 133 [ $i -gt 2 ] && echo "fsck run #$i" 134 chk $diskimage 135 [ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; } 136 [ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; } 137 if [ -f fsck_ffs.core ]; then 138 tstamp=`date +%Y%m%dT%H%M%S` 139 gzip < $backup > /tmp/fsck_ffs.core.diskimage.$tstamp 140 gzip < fsck_ffs.core > /tmp/fsck_ffs.core.$tstamp 141 break 2 142 fi 143 done 144 if [ $clean -eq 1 ]; then 145 fsck_ffs -fy $diskimage > $log 2>&1 146 if grep -q MODIFIED $log; then 147 echo "*** fsck of \"clean\" FS found more issues:" 148 cat $log 149 errors=$((errors + 1)) 150 s=1 151 break 152 fi 153 fi 154 [ $clean -ne 1 ] && break 155 mdconfig -a -t vnode -f $diskimage -u $u2 156done 157for i in `jot 5`; do 158 mount | grep -q "on $mp2 " || break 159 umount $mp2 && break 160 sleep 2 161done 162mdconfig -l | grep -q $u2 && mdconfig -d -u $u2 163 164[ $s -eq 0 ] && rm -f $backup || echo "Preserved $backup due to status code $s" 165cd /tmp 166for i in `jot 5`; do 167 umount $mp1 && break 168 sleep 2 169done 170mdconfig -d -u $u1 171rm -f /tmp/flip 172exit $s 173