Re: fs corruption recovery?

From: H. Peter Anvin (hpa@zytor.com)
Date: Wed Jan 09 2002 - 00:20:31 EST


Followup to: <3C3BC0FC.30403@fit.edu>
By author: Kervin Pierre <kpierre@fit.edu>
In newsgroup: linux.dev.kernel
>
> Thanks for the reply,
>
> >Try "e2fsck -B 4096 -b 32768 <device>" instead.
> >
>
> e2fsck -B 4096 -b 32768 /dev/hdc1
>
> e2fsck 1.25 (20-Sep-2001)
> e2fsck: Attempt to read block from filesystem resulted in short read
> while trying to open /dev/hdc1
> Could this be a zero-length partition?
>
> Does ext keep a backup of that backup? :)
>
> Are there any other options?
>

You have bad media. You need to read the disk sector by sector, save
it elsewhere, write it to another drive, and then try to fsck it --
HOWEVER, DON'T FSCK THE ORIGINAL. Fsck can actually cause serious
damage if it guesses wrong.

The following is a small C program that I just threw together to read
sector by sector and dump the data on stdout. No guarantees
whatsoever:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#define SECTORSIZE 512

int read_sector(int fd, void *buf)
{
  char *p = buf;
  int bytes = SECTORSIZE;
  int rv;

  while ( bytes ) {
    if ( (rv = read(fd, p, bytes)) <= 0 ) {
      if ( rv < 0 || errno == EINTR )
        continue; /* Happens... */
      return 0; /* Error */
    }
    p += rv;
    bytes -= rv;
  }

  return 1; /* Success */
}

int main(int argc, char *argv[])
{
  unsigned long long count;
  unsigned long long n;
  int fd;
  char buffer[SECTORSIZE];

  if ( argc != 3 ) {
    fprintf(stderr, "Usage: %s device sectors\n", argv[0]);
    exit(1);
  }

  count = strtoull(argv[2], NULL, 0);
  
  fd = open(argv[1], O_RDONLY);
  if ( fd < 0 ) {
    perror(argv[1]);
    exit(1);
  }
  
  n = 0;
  while ( count-- ) {
    if ( !read_sector(fd, buffer) ) {
      fprintf(stderr, "Sector %llu: %s\n", n, strerror(errno));
      memset(buffer, 0, SECTORSIZE);
    }
    if ( fwrite(buffer, 1, SECTORSIZE, stdout) != SECTORSIZE ) {
      fprintf(stderr, "Sector %llu: output error: %s\n", n, strerror(errno));
      exit(1);
    }
    n++;
  }

  return 0;
}

        -hpa

-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<amsp@zytor.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Jan 15 2002 - 21:00:25 EST