Subject: Re: Find super-block backups
To: =?ISO-8859-1?Q?Johan_Kr=FCger-Haglert?= <aliquis@link-net.org>
From: Herb Peyerl <hpeyerl@beer.org>
List: netbsd-help
Date: 01/02/2006 10:01:48
On Jan 2, 2006, at 9:59 AM, Johan Kr=FCger-Haglert wrote:
> I have (had) a NetBSD 2.x machine using a 40GB drive and ffsv2.
> However something failed and I get errors like these on the console:

I wrote this in 1995 after I had a minor 'incident'.  I have no idea =20
whether it's still applicable and may not compile without some =20
changes.....

No warranty blah blah blah....

/*
* Copyright (c) 1995 Herb Peyerl <hpeyerl@novatel.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the =20
distribution.
* 3. All advertising materials mentioning features or use of this =20
software
*    must display the following acknowledgement:
*      This product includes software developed by Herb Peyerl.
* 4. The name of Herb Peyerl may not be used to endorse or promote =20
products
*    derived from this software without specific prior written =20
permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED =20
WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE =20
DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES =20
(INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS =20
OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE =20
USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: scour.c,v 1.7 1999/06/06 15:27:54 hpeyerl Exp $
*/
/*
* Frightening program to scour a disk and search for superblocks and
* print out the associated filesystem sizes and disk offsets of the
* filesystems.
*
* Useful when you "accidentally" lose your disklabel.
*
* Lots of assumptions are made:
*    Filesystems are placed on block boundaries.
*    If we find FS_MAGIC and the correct number of SPC then we have a =20=

valid
*        superblock.
*    That the filesystems were at some point mounted on a machine and =20=

that
*        the location of the mountpoint is present.  Take out the if=20
(strlen)
*        if you don't like this assumption.
*    We don't back-calculate the location and size of the swap partition
*        because that would entail making another assumption.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>

/*
* Change the SPC to be the Sectors/Cylinder attribute. (sectors * heads)
* (This should either be passed in as an argument or felched out of the
* kernel somewhere...  Probably read the disklabel.
*/
#define SPC 780

main(argc, argv)
         int argc;
         char *argv[];
{
         char buf[BBSIZE];
         struct fs *fs;
         FILE *fp;
         int x;
         int boffset;

         if (argc < 2)
                 errx(1, "Need device argument.");

         if ((fp =3D fopen(argv[1], "r")) =3D=3D NULL)
                 err(1, "fopen");

         fs =3D (struct fs *)buf;
         boffset =3D 0;
         while (!feof(fp)) {
                 fread(buf, BBSIZE, 1, fp);
                 boffset +=3D BBSIZE;
                 for(x=3D0; x+sizeof(struct fs) < BBSIZE; x++) {
                         fs =3D (struct fs *)(buf + x);
                         if (fs->fs_magic =3D=3D FS_MAGIC && fs->fs_spc =20=

=3D=3D SPC) {
                                 if (strlen(fs->fs_fsmnt) =3D=3D 0)
                                         continue;
                                 printf ("[%s]:diskoffset->[%ld] =20
fs_size->[%ld]\n",
                                         fs->fs_fsmnt,
                                         (boffset/512)+(x/512) - 32,
                                         fs->fs_size);
                         }
                 }
         }
}