Hi Kazukiyo,

This is certainly possible.  I've enclosed a hack from Niall Smart that should
generated enough information to for you to reconstruct it.   I'm working on a
general solution to this for inclusion FreeBSD as shipped, but it's at home
and I'm at work, that said it's Niall's basic code saved my harddisk a few weeks
ago :)

Joe

----- Forwarded message from Niall Smart <[EMAIL PROTECTED]> -----

Date: Mon, 21 Jun 1999 13:48:45 +0000
From: Niall Smart <[EMAIL PROTECTED]>
Organization: Trinity Commerce
X-Mailer: Mozilla 4.6 [en] (X11; I; FreeBSD 3.2-STABLE i386)
X-Accept-Language: en
To: Josef Karthauser <[EMAIL PROTECTED]>
CC: [EMAIL PROTECTED]
Subject: Re: [DISKLABEL FRAGGED] Clues requested... ;)

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
PROG    = findsb
CFLAGS  = -aout -static -W -Wall -Wmissing-prototypes -Wstrict-prototypes
MAN1    =

.include <bsd.prog.mk>

/*
 * 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, [EMAIL PROTECTED]
 *
 * 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;
}


----- End forwarded message -----

-- 
Josef Karthauser        FreeBSD: How many times have you booted today?
Technical Manager       Viagra for your server (http://www.uk.freebsd.org)
Pavilion Internet plc.  [[EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED]]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to