Skip site navigation (1)Skip section navigation (2)
Date:      23 Mar 1998 16:33:40 -0500
From:      Andrew Heybey <ath@niksun.com>
To:        Narvi <narvi@haldjas.folklore.ee>
Cc:        FreeBSD-Hackers@FreeBSD.ORG
Subject:   Re: Finding an FS on the disk
Message-ID:  <85afahf5xn.fsf@stiegl.niksun.com>
In-Reply-To: Narvi's message of Mon, 23 Mar 1998 22:48:07 %2B0200 (EET)
References:  <Pine.BSF.3.96.980323224325.13652D-100000@haldjas.folklore.ee>

next in thread | previous in thread | raw e-mail | index | archive | help
Here is some code I wrote when I had a similar problem under SunOS.  I
just modified it to compile and (apparently) work under FreeBSD.  Do
something like:

./find_super /dev/rwd0c

and it will print something like:

Found possible superblock at sector 16:
size 65536, bsize 8192, fsize 1024
Found possible superblock at sector 32:
size 65536, bsize 8192, fsize 1024
... superblocks (& backups) from /var, /usr, etc.

You have to figure out a) whether you just got unlucky and there
happened to be an FS_MAGIC at the beggining of a sector of otherwise
random data (which is why my program prints out size, bsize & fsize
for sanity checking), b) which superblock is the one at the beginning
of the file system you care about and c) how to recover your data once
you have passed a) and b).

Good luck.

andrew

==================== cut here (MIME? what's that?) ==================
/* Hunt for file system superblocks on disk.
 * Andrew Heybey, ath@niksun.com
 *
 * This code is placed in the public domain.  No warantee.
 *
 * $Id: find_super.c,v 1.2 1998/03/23 21:28:48 ath Exp $
 */

#include <sys/param.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ufs/ffs/fs.h>

#define BUFSIZE (512*1024)

main(argc, argv)
  int argc;
  char *argv[];
{
    char *dev = argv[1];
    int fd;
    int res;
    FILE *out = stdout;
    u_long sect = 0;
    char buf[BUFSIZE];
    int cnt, limit;
    struct fs *fs;

    fd = open (dev, O_RDONLY);
    if (fd < 0)  {
	perror ("open");
	exit (1);
    }

    for (;;)  {
	res = read (fd, buf, sizeof (buf));
	if (res != sizeof (buf))
	    break;
	for (cnt = 0; cnt < BUFSIZE; cnt += 512, sect++)  {
	    fs = (struct fs *)&buf[cnt];

	    if (fs->fs_magic != FS_MAGIC)
		continue;

	    fprintf (out, "Found possible superblock at sector %u:\n", sect);

	    fprintf (out, "size %ld, bsize %ld, fsize %ld\n",
		     fs->fs_size, fs->fs_bsize, fs->fs_fsize);
	}
    }

    if (res < 0)  {
	perror ("read");
    }

    close (fd);
}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?85afahf5xn.fsf>