Subject: Re: install/25138: 2.0: sysinst upgrade broken: fsck_ffs bails out
To: Hubert Feyrer <hubert@feyrer.de>
From: Darrin B. Jewell <dbj@netbsd.org>
List: netbsd-bugs
Date: 04/12/2004 18:16:06
--=-=-=

"Darrin B. Jewell" <dbj@netbsd.org> writes:

> "Darrin B. Jewell" <dbj@netbsd.org> writes:
> > I also plan to create a simple shell script test for the existence of
> > the botched superblock problem.
> 
> For reference, here's a script that will do such a test.

After some feedback from perry, here's an updated script.


--=-=-=
Content-Type: application/x-sh
Content-Disposition: attachment; filename=checksb.sh
Content-Description: shell script to test for botched superblock conditions

#!/bin/sh

# This shell script extracts the `ffs superblock' of the file[s] listed
# on its command line and tests for the following condition:
# ((fs_magic == FS_UFS1_MAGIC) || fs_magic == FS_UFS1_MAGIC_SWAPPED) &&
# (fs_sbsize == fs_maxbsize) && !(fs_old_flags & FS_FLAGS_UPDATED)
#
# exit status is based on status of last filesystem checked:
#   0 for botched superblock
#   1 for filesystem does not appear to be ffs1 filesystem
#   3 for ok fslevel 3 filesystem
#   4 for ok fslevel 4 filesystem
#   255 on usage error
#
# dbj@netbsd.org 2004-04-12T18:15:06-0400

#debug=1
verbose=1


check_part()
{
  # The following are 'cat -v' representations of the ffs1 magic number:
  fsmagicn="^@^A^YT"  # 0x00011954 FS_UFS1_MAGIC
  fsmagics="T^Y^A^@"  # 0x54190100 FS_UFS1_MAGIC_SWAPPED

  # In each of the foo=`dd | dd | cat -v` pipelines below, the first dd command reads in
  # the superblock using block aligned i/o so that it works on a raw character device.
  # The second dd extracts the exact field from the superblock that we with to
  # read.  The data is put through cat -v to avoid having binary data in shell strings.

  if [ ! -z "${verbose}" ]; then
    echo -n "Checking $1 ... " 1>&2
  fi

  # First we extract the superblock magic number field:
  magic="`(dd if="$1" bs=8192 skip=1 count=1 | dd bs=1 count=4 skip=1372 | cat -v) 2> /dev/null`"
  
  # First we check if the magic number is valid either swapped or unswapped:
  if [ "${magic}" = "${fsmagicn}" -o "${magic}" = "${fsmagics}" ]; then

    # Then we read fs_bsize, fs_maxbsize and fs_old_flags fields from the disk:
    bsize="`(dd if="$1" bs=8192 skip=1 count=1 | dd bs=1 count=4 skip=48 | cat -v) 2> /dev/null`"
    maxbsize="`(dd if="$1" bs=8192 skip=1 count=1 | dd bs=1 count=4 skip=860 | cat -v) 2> /dev/null`"
    oldflags="`(dd if="$1" bs=8192 skip=1 count=1 | dd bs=1 count=1 skip=211 | cat -v) 2> /dev/null`"

    if [ ! -z "${debug}" ]; then
      echo "$1: magic=${magic} bsize=${bsize} maxbsize=${maxbsize} oldflags=${oldflags}" 1>&2
    fi

    # Compare the fs_bsize with fs_maxbsize to see if they are the same
    if [ "${bsize}" = "${maxbsize}" ]; then

      # Now check to see if the high bit of fs_old_flags is set.
      case "${oldflags}" in

        # Since the shell variable is the cat -v output, the
        # high bit is indicated in the variable with the prefix M-
        M-*)
        if [ ! -z "${verbose}" ]; then
          echo "file system looks ok at fslevel 4." 1>&2
          return 4
        fi
        ;;

        # if the high bit of fs_old_flags is not set, then there is a problem
 	*)
        if [ ! -z "${verbose}" ]; then
          echo "file system has botched superblock upgrade." 1>&2
        fi
        return 0
        ;;
      esac

    fi # [ "${bsize}" = "${maxbsize}" ]

  else # ! [ "${magic}" = "${fsmagicn}" -o "${magic}" = "${fsmagics}" ]
    if [ ! -z "${verbose}" ]; then
      echo "does not appear to be an ffs1 filesystem." 1>&2
    fi
    return 1
  fi  # ! [ "${magic}" = "${fsmagicn}" -o "${magic}" = "${fsmagics}" ]
  if [ ! -z "${verbose}" ]; then
    echo "file system looks ok at fslevel 3." 1>&2
  fi
  return 3
}

if [ -z "$1" ]; then
  echo "usage: checksb rawpart" 1>&2
  echo "For example: checksb /dev/rwd0a" 1>&2
  exit 255
fi

for p in ${1+"$@"}; do
  check_part "$p"
done
exit

--=-=-=--