Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 1 | /* Well, here's my linux version of findsuper. |
| 2 | * I'm sure you coulda done it faster. :) |
| 3 | * IMHO there isn't as much interesting data to print in the |
| 4 | * linux superblock as there is in the SunOS superblock--disk geometry is |
| 5 | * not there...and linux seems to update the dates in all the superblocks. |
| 6 | * SunOS doesn't ever touch the backup superblocks after the fs is created, |
| 7 | * as far as I can tell, so the date is more interesting IMHO and certainly |
| 8 | * marks which superblocks are backup ones. |
| 9 | * |
| 10 | * This still doesn't handle disks >2G. |
| 11 | * |
| 12 | * I wanted to add msdos support, but I couldn't make heads or tails |
| 13 | * of the kernel include files to find anything I could look for in msdos. |
| 14 | * |
| 15 | * Reading every block of a Sun partition is fairly quick. Doing the |
| 16 | * same under linux (slower hardware I suppose) just isn't the same. |
| 17 | * It might be more useful to default to reading the first (second?) block |
| 18 | * on each cyl; however, if the disk geometry is wrong, this is useless. |
| 19 | * But ya could still get the cyl size to print the numbers as cyls instead |
| 20 | * of blocks... |
| 21 | * |
| 22 | * run this as (for example) |
| 23 | * findsuper /dev/hda |
| 24 | * findsuper /dev/hda 437760 1024 (my disk has cyls of 855*512) |
| 25 | * |
| 26 | * I suppose the next step is to figgure out a way to determine if |
| 27 | * the block found is the first superblock somehow, and if so, build |
| 28 | * a partition table from the superblocks found... but this is still |
| 29 | * useful as is. |
| 30 | * |
| 31 | * Steve |
| 32 | * ssd@nevets.oau.org |
| 33 | * ssd@mae.engr.ucf.edu |
| 34 | */ |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <time.h> |
| 39 | |
| 40 | #include <linux/ext2_fs.h> |
| 41 | |
| 42 | |
| 43 | main(int argc, char *argv[]) |
| 44 | { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 45 | int i; |
| 46 | int skiprate=512; /* one sector */ |
| 47 | long sk=0; /* limited to 2G filesystems!! */ |
| 48 | FILE *f; |
| 49 | char *s; |
| 50 | time_t tm; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 51 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 52 | struct ext2_super_block ext2; |
| 53 | /* interesting fields: EXT2_SUPER_MAGIC |
| 54 | * s_blocks_count s_log_block_size s_mtime s_magic s_lastcheck */ |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 55 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 56 | if (argc<2) { |
| 57 | fprintf(stderr, |
| 58 | "Usage: findsuper device [skiprate [start]]\n"); |
| 59 | exit(1); |
| 60 | } |
| 61 | if (argc>2) |
| 62 | skiprate=atoi(argv[2]); |
| 63 | if (skiprate<512) { |
| 64 | fprintf(stderr, |
| 65 | "Do you really want to skip less than a sector??\n"); |
| 66 | exit(2); |
| 67 | } |
| 68 | if (argc>3) |
| 69 | sk=atol(argv[3]); |
| 70 | if (sk<0) { |
| 71 | fprintf(stderr,"Have to start at 0 or greater,not %ld\n",sk); |
| 72 | exit(1); |
| 73 | } |
| 74 | f=fopen(argv[1],"r"); |
| 75 | if (!f) { |
| 76 | perror(argv[1]); |
| 77 | exit(1); |
| 78 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 79 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 80 | /* Now, go looking for the superblock ! */ |
| 81 | printf(" thisoff block fs_blk_sz blksz last_mount\n"); |
| 82 | for (;!feof(f) && (i=fseek(f,sk,SEEK_SET))!= -1; sk+=skiprate){ |
| 83 | if (i=fread(&ext2,sizeof(ext2),1, f)!=1) { |
| 84 | perror("read failed"); |
| 85 | } else if (ext2.s_magic == EXT2_SUPER_MAGIC){ |
| 86 | tm = ext2.s_mtime; |
| 87 | s=ctime(&tm); |
| 88 | s[24]=0; |
| 89 | printf("%9ld %9ld %9ld %5ld %s\n",sk,sk/1024,ext2.s_blocks_count,ext2.s_log_block_size,s); |
| 90 | } |
| 91 | } |
| 92 | printf("Failed on %d at %ld\n", i, sk); |
| 93 | fclose(f); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 94 | } |