Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Jun 1999 13:48:45 +0000
From:      Niall Smart <niall@pobox.com>
To:        Josef Karthauser <joe@pavilion.net>
Cc:        hackers@freebsd.org
Subject:   Re: [DISKLABEL FRAGGED] Clues requested... ;)
Message-ID:  <376E42BD.117B32DA@pobox.com>
References:  <19990621083803.M95198@pavilion.net>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------6A03DA571A5C4CB5B0C068BC
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Josef Karthauser wrote:
> 
> Guess what... I've got a disk where the partition table and the disklabel has
> mysteriously disappeared!   Oops.
> 
> I've reconstructed the partition table, and now need to partition the disklabel.

Wow, you're probably the first person ever to do that, unlucky you.  Out
of sympathy I've written you this findsb program which of course I've
never had to use myself. Its not the most efficiently written program
ever
and since FreeBSD swap partitions don't have magic numbers it may be
slow,
but it seems to work, however I draw your attention to the disclaimer
in the C file. ;)

Regards,
Niall

niall% disklabel wd0s2 | sed -n '/fstype/,$p'
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:   131072        0    4.2BSD        0     0     0   # (Cyl.    0 -
8*)
  b:   262144   131072      swap                        # (Cyl.    8*-
24*)
  c:  4192965        0    unused        0     0         # (Cyl.    0 -
260)
  e:   819200   393216    4.2BSD        0     0     0   # (Cyl.   24*-
75*)
  f:  2980549  1212416    4.2BSD        0     0     0   # (Cyl.   75*-
260*)
niall% ./findsb /dev/wd0s2 0 
found superblock: offset=16384, fs_size=65536, fs_fsmnt=/
suggested disklabel entry:

    size   offset    fstype
  131072        0    4.2BSD

found superblock: offset=24576, fs_size=65536, fs_fsmnt=
suggested disklabel entry:

    size   offset    fstype
  131072       16    4.2BSD

^C
niall% ./findsb /dev/wd0s2 $[393216 * 512 - 8192 * 10] 
skipping 201244672 bytes
found superblock: offset=201342976, fs_size=409600, fs_fsmnt=/home
suggested disklabel entry:

    size   offset    fstype
  819200   393216    4.2BSD

found superblock: offset=201351168, fs_size=409600, fs_fsmnt=
suggested disklabel entry:

    size   offset    fstype
  819200   393232    4.2BSD

^C
--------------6A03DA571A5C4CB5B0C068BC
Content-Type: text/plain; charset=us-ascii;
 name="Makefile"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Makefile"

PROG	= findsb
CFLAGS	= -aout -static -W -Wall -Wmissing-prototypes -Wstrict-prototypes
MAN1	=

.include <bsd.prog.mk>

--------------6A03DA571A5C4CB5B0C068BC
Content-Type: text/plain; charset=us-ascii;
 name="findsb.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="findsb.c"

/*
 * Copyright (c) 1999 Niall Smart.  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 distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Niall Smart, niall@pobox.com.
 *
 * THIS SOFTWARE IS PROVIDED BY NIALL SMART ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL NIALL SMART BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS 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 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
*/


#include <sys/types.h>
#include <sys/param.h>
#include <ufs/ffs/fs.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>

int
main(int argc, char** argv)
{
	int		fd;
	int		ret;
	off_t		offset;
	u_quad_t	skip;
	char*		ptr;
	char		buf[SBSIZE];
	struct fs*	fs = (struct fs*)buf;

	if (argc != 3) {
		fprintf(stderr, "usage: %s device skip\n", argv[0]);
		exit(1);
	}
	
	if ( (fd = open(argv[1], O_RDONLY)) < 0) {
		perror("open");
		exit(1);
	}
	
	if ( (skip = strtouq(argv[2], &ptr, 0)) == QUAD_MAX || *ptr != '\0') {
		fprintf(stderr, "bad seek value: %s\n", argv[2]);
		exit(1);
	}

	if ( (skip % SBOFF) != 0)
		fprintf(stderr, "warning: %qu is not a multiple of SBOFF (%d)\n", skip, (int)SBOFF);;
	
	if (skip > 0) {
		fprintf(stderr, "skipping %qu bytes\n", skip);
		lseek(fd, skip, SEEK_SET);
	}

	while (1) {
		
		ret = read(fd, buf, sizeof(buf));
		
		if (ret < 0) {
			perror("read");
			exit(1);
		}
	
		/*
		 * based on checks in /sys/ufs/ffs/ffs_vfsops.c, line 478
		*/

		if (fs->fs_magic == FS_MAGIC &&
				fs->fs_bsize <= MAXBSIZE &&
				fs->fs_bsize >= (int32_t)sizeof(struct fs)) {
			
			offset = lseek(fd, 0, SEEK_CUR);

			printf("found superblock: offset=%qd, fs_size=%d, fs_fsmnt=%.*s\n",
					offset, fs->fs_size, MAXMNTLEN, fs->fs_fsmnt);
			
			printf("suggested disklabel entry:\n\n");
			printf("    size   offset    fstype\n");
			printf("%8d %8qd    4.2BSD\n\n", fs->fs_size * 2, (offset - 16384) / 512);
				

		}
	
	}
	
	printf("%d:%d\n", sizeof(struct fs), offsetof(struct fs, fs_magic));
	
	return 0;
}

--------------6A03DA571A5C4CB5B0C068BC--



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?376E42BD.117B32DA>