1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2022 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# fsck_ffs(8) false CLEAN claim scenario 31 32# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T150359.txt 33# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T150359.gz 34 35# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T172428.txt 36# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T172428.gz 37 38. ../default.cfg 39 40[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 41 42cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1 43 44set -e 45u1=$mdstart 46u2=$((mdstart + 1)) 47mp1=${mntpoint}$u1 48mp2=${mntpoint}$u2 49mkdir -p $mp1 $mp2 50log=$mp1/fsck9.sh.log 51diskimage=$mp1/fsck9.sh.diskimage 52backup=/tmp/fsck9.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz 53cleans=0 54reruns=0 55 56max=$((10 * 1024 * 1024)) 57newfs_flags='-U' 58 59mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1 60[ -c /dev/md$u1 ] && mdconfig -d -u $u1 61mdconfig -a -t swap -s 1g -u $u1 62newfs $newfs_flags -n /dev/md$u1 > /dev/null 63mount /dev/md$u1 $mp1 64 65[ -c /dev/md$u2 ] && mdconfig -d -u $u2 66dd if=/dev/zero of=$diskimage bs=$max count=1 status=none 67mdconfig -a -t vnode -f $diskimage -u $u2 68backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \ 69 tail -1 | sed 's/,//g'` 70newfs $newfs_flags -n md$u2 > /dev/null 71mount /dev/md$u2 $mp2 72[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2 73umount $mp2 74set +e 75 76chk() { 77 local i 78 79 clean=0 80 rerun=0 81 fsck_ffs -fy $1 > $log 2>&1 82 r=$? 83 if grep -qiE "super-?block.*failed" $log; then 84 for b in $backups; do 85 echo "fsck_ffs -b $b -fy $1" 86 fsck_ffs -b $b -fy $1 > $log 2>&1 87 r=$? 88 grep -qiE "super-?block.*failed" $log || 89 break 90 echo "Checking next SB" 91 done 92 usedasb=1 93 else 94 usedasb=0 95 fi 96 LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0 97 grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1 98 grep -q RERUN $log && rerun=1 99 [ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1" 100} 101 102cd $mp1 103clean=0 104s=0 105start=`date +%s` 106while [ $((`date +%s` - start)) -lt 300 ]; do 107 mount /dev/md$u2 $mp2 || break 108 if ! ls -lR $mp2 > /dev/null; then 109 s=102 110 echo "ls failed"; grep "core dumped" /var/log/messages | tail -1 111 break 112 fi 113 touch $mp2/`jot -rc 8 a z | tr -d '\n'` 114 while mount | grep -q "on $mp2 "; do umount $mp2; done 115 echo * | grep -q core && break 116 sync 117 mdconfig -d -u $u2 118 /tmp/flip -n 5 $diskimage 119 120 sync 121 if [ `stat -f%z $diskimage` -gt $max ]; then 122 ls -lh $diskimage 123 truncate -s $max $diskimage 124 else 125 gzip < $diskimage > $backup 126 fi 127 fsync $backup 128 sync 129 130 for i in `jot 5`; do 131 [ $i -gt 2 ] && echo "fsck run #$i" 132 chk $diskimage 133 [ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; } 134 [ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; } 135 [ -f fsck_ffs.core ] && 136 { cp -v $diskimage \ 137 /tmp/fsck_ffs.core.diskimage.`date +%Y%m%dT%H%M%S`; break 2; } 138 done 139 if [ $clean -eq 1 ]; then 140 fsck_ffs -fy $diskimage > $log 2>&1 141 if grep -q MODIFIED $log; then 142 echo "*** fsck of \"clean\" FS found more issues:" 143 cat $log 144 s=1 145 break 146 fi 147 fi 148 [ $clean -ne 1 ] && break 149 mdconfig -a -t vnode -f $diskimage -u $u2 150done 151sleep 2 # Wait for /dev to catch up 152[ -c /dev/md$u2 ] && r1=1 || r1=0 153for i in `jot 5`; do 154 mount | grep -q "on $mp2 " || break 155 umount $mp2 && break 156 sleep 2 157done 158mdconfig -d -u $u2 2>/dev/null # XXX when mount fails 159 160[ $s -eq 0 ] && rm -f $backup || echo "Preserved $backup due to status code $s" 161cd /tmp 162for i in `jot 5`; do 163 umount $mp1 && break 164 sleep 2 165done 166mdconfig -d -u $u1 167rm -f /tmp/flip 168exit $s 169