Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 27 Feb 1996 20:22:29 +0100
From:      "Christoph P. Kukulies" <kuku@gilberto.physik.rwth-aachen.de>
To:        freebsd-hackers@freefall.FreeBSD.org
Subject:   Re: help needed in recovering a disk
Message-ID:  <199602271922.UAA07342@gilberto.physik.rwth-aachen.de>

next in thread | raw e-mail | index | archive | help
Here is the program to scan the disk:

Originally I used 512 byte reads to scan for disklabel.
I had to change it to 2048 ti read in a whole struct fs.

======= disk.c======

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/param.h>
#define DKTYPENAMES
#include <sys/disklabel.h>
#include <ufs/ffs/fs.h>

/*
 */
#define DISKMAGIC	((u_long) 0x82564557)	/* The disk magic number */

main(argc,argv)
  int argc;
  char **argv;
{
	int             i;
	char *dev;
    struct fs *f;
    struct disklabel *dl;
	struct stat     sb;
	off_t           lseek();
    off_t			block=0;
	char            buf[2048];
	off_t           end;
	u_long         *l;
	off_t           offset = (off_t) 0;
	off_t           newoffset;
	int             fd, vd, n;
	if(argc==2)
		dev=argv[1];
    else
		dev="/dev/rwd0";
    printf("sizeof struct fs=%d\n",sizeof(struct fs));
    l = (u_long *) buf;
    f = (struct fs *) buf;
	dl = (struct disklabel *) buf;
	if ((fd = open(dev, O_RDONLY, 0)) == -1)
		fprintf(stderr,"error opening %s\n",dev),exit(1);
	while(read(fd, buf, 2048)>0) {
	  block+=2048;
	  printf("\r%qd",block);fflush(stdout);
	  if (*l == DISKMAGIC) {
		printf("\ndisklabel found at offset %qd (%qd MB/%qd)\n",
		       block-2048, (block-2048)/ (off_t) (1024), (block-2048) / 2048);
		printf("d_type=%d\n%s\nd_npartitions=%d\n", dl->d_type,
		       dl->d_typename,
		       dl->d_npartitions);
		for (i = 0; i < dl->d_npartitions; i++) {
			printf("\t%d\t%d\t%d\t%s\n",
			       dl->d_partitions[i].p_size,
			       dl->d_partitions[i].p_offset,
			       dl->d_partitions[i].p_fsize,
			       fstypenames[dl->d_partitions[i].p_fstype]);
		}
    }
    if(f->fs_magic == FS_MAGIC )
		printf("\nfs at block # %qd last mounted on %s f->fs_ncyl=%ld\n",(block-2048)/512,
		f->fs_fsmnt,f->fs_ncyl);
  }
}
--Chris Christoph P. U. Kukulies kuku@gil.physik.rwth-aachen.de



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