Theodore Ts'o | 74a74d2 | 2000-12-09 14:33:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * findsuper --- quick hacked up program to find ext2 superblocks. |
| 3 | * |
| 4 | * This is a hack, and really shouldn't be installed anywhere. If you |
| 5 | * need a program which does this sort of functionality, please try |
| 6 | * using gpart program. |
| 7 | * |
| 8 | * Portions Copyright 1998-2000, Theodore Ts'o. |
| 9 | * |
Theodore Ts'o | 74a74d2 | 2000-12-09 14:33:29 +0000 | [diff] [blame] | 10 | * Well, here's my linux version of findsuper. |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 11 | * I'm sure you coulda done it faster. :) |
| 12 | * IMHO there isn't as much interesting data to print in the |
| 13 | * linux superblock as there is in the SunOS superblock--disk geometry is |
| 14 | * not there...and linux seems to update the dates in all the superblocks. |
| 15 | * SunOS doesn't ever touch the backup superblocks after the fs is created, |
| 16 | * as far as I can tell, so the date is more interesting IMHO and certainly |
| 17 | * marks which superblocks are backup ones. |
| 18 | * |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 19 | * I wanted to add msdos support, but I couldn't make heads or tails |
| 20 | * of the kernel include files to find anything I could look for in msdos. |
| 21 | * |
| 22 | * Reading every block of a Sun partition is fairly quick. Doing the |
| 23 | * same under linux (slower hardware I suppose) just isn't the same. |
| 24 | * It might be more useful to default to reading the first (second?) block |
| 25 | * on each cyl; however, if the disk geometry is wrong, this is useless. |
| 26 | * But ya could still get the cyl size to print the numbers as cyls instead |
| 27 | * of blocks... |
| 28 | * |
| 29 | * run this as (for example) |
| 30 | * findsuper /dev/hda |
| 31 | * findsuper /dev/hda 437760 1024 (my disk has cyls of 855*512) |
| 32 | * |
| 33 | * I suppose the next step is to figgure out a way to determine if |
| 34 | * the block found is the first superblock somehow, and if so, build |
| 35 | * a partition table from the superblocks found... but this is still |
| 36 | * useful as is. |
| 37 | * |
| 38 | * Steve |
| 39 | * ssd@nevets.oau.org |
| 40 | * ssd@mae.engr.ucf.edu |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 41 | * |
| 42 | * Additional notes by Andreas Dilger <adilger@turbolinux.com>: |
| 43 | * - fixed to support > 2G devices by using lseek64 |
| 44 | * - add reliability checking for the superblock to avoid random garbage |
| 45 | * - add adaptive progress meter |
| 46 | * |
| 47 | * It _should_ also handle signals and tell you the ending block, so |
| 48 | * that you can resume at a later time, but it doesn't yet... |
| 49 | * |
| 50 | * Note that gpart does not appear to find all superblocks that aren't aligned |
| 51 | * with the start of a possible partition, so it is not useful in systems |
| 52 | * with LVM or similar setups which don't use fat partition alignment. |
Theodore Ts'o | 16fa86b | 2003-04-14 20:40:49 -0400 | [diff] [blame] | 53 | * |
| 54 | * %Begin-Header% |
| 55 | * This file may be redistributed under the terms of the GNU Public |
| 56 | * License. |
| 57 | * %End-Header% |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 60 | /* |
| 61 | * Documentation addendum added by Andreas dwguest@win.tue.nl/aeb@cwi.nl |
| 62 | * |
| 63 | * The program findsuper is a utility that scans a disk and finds |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 64 | * copies of ext2 superblocks (by checking for the ext2 signature |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 65 | * |
| 66 | * For each superblock found, it prints the offset in bytes, the |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 67 | * offset in 1024-byte blocks, the size of ext2 partition in fs |
| 68 | * blocks, the filesystem blocksize (in bytes), the block group number |
| 69 | * (always 0 for older ext2 systems), and a timestamp (s_mtime). |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 70 | * |
| 71 | * This program can be used to retrieve partitions that have been |
| 72 | * lost. The superblock for block group 0 is found 1 block (2 |
| 73 | * sectors) after the partition start. |
| 74 | * |
| 75 | * For new systems that have a block group number in the superblock it |
| 76 | * is immediately clear which superblock is the first of a partition. |
| 77 | * For old systems where no group numbers are given, the first |
| 78 | * superblock can be recognised by the timestamp: all superblock |
| 79 | * copies have the creation time in s_mtime, except the first, which |
| 80 | * has the last time e2fsck or tune2fs wrote to the filesystem. |
| 81 | * |
| 82 | */ |
| 83 | |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 84 | #define _FILE_OFFSET_BITS 64 |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 85 | |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 86 | #include <stdio.h> |
| 87 | #include <stdlib.h> |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 88 | #include <string.h> |
| 89 | #include <unistd.h> |
| 90 | #include <errno.h> |
| 91 | #include <fcntl.h> |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 92 | #include <time.h> |
| 93 | |
Theodore Ts'o | 54c637d | 2001-05-14 11:45:38 +0000 | [diff] [blame] | 94 | #include "ext2fs/ext2_fs.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 95 | #include "nls-enable.h" |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 96 | |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 97 | #undef DEBUG |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 98 | |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 99 | #ifdef DEBUG |
| 100 | #define WHY(fmt, arg...) { printf("\r%Ld: " fmt, sk, ##arg) ; continue; } |
| 101 | #else |
| 102 | #define WHY(fmt, arg...) { continue; } |
| 103 | #endif |
| 104 | |
| 105 | int main(int argc, char *argv[]) |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 106 | { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 107 | int skiprate=512; /* one sector */ |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 108 | loff_t sk=0, skl=0; |
| 109 | int fd; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 110 | char *s; |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 111 | time_t tm, last = time(0); |
| 112 | loff_t interval = 1024 * 1024; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 113 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 114 | struct ext2_super_block ext2; |
| 115 | /* interesting fields: EXT2_SUPER_MAGIC |
| 116 | * s_blocks_count s_log_block_size s_mtime s_magic s_lastcheck */ |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 117 | |
| 118 | #ifdef ENABLE_NLS |
| 119 | setlocale(LC_MESSAGES, ""); |
Theodore Ts'o | 14308a5 | 2002-03-05 03:26:52 -0500 | [diff] [blame] | 120 | setlocale(LC_CTYPE, ""); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 121 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 122 | textdomain(NLS_CAT_NAME); |
| 123 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 124 | if (argc<2) { |
| 125 | fprintf(stderr, |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 126 | _("Usage: findsuper device [skipbytes [startkb]]\n")); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 127 | exit(1); |
| 128 | } |
| 129 | if (argc>2) |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 130 | skiprate = strtol(argv[2], &s, 0); |
| 131 | if (s == argv[2]) { |
| 132 | fprintf(stderr,_("skiprate should be a number, not %s\n"), s); |
| 133 | exit(1); |
| 134 | } |
| 135 | if (skiprate & 0x1ff) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 136 | fprintf(stderr, |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 137 | _("skipbytes must be a multiple of the sector size\n")); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 138 | exit(2); |
| 139 | } |
| 140 | if (argc>3) |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 141 | sk = skl = strtoll(argv[3], &s, 0) << 10; |
| 142 | if (s == argv[3]) { |
| 143 | fprintf(stderr,_("startkb should be a number, not %s\n"), s); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 144 | exit(1); |
| 145 | } |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 146 | if (sk < 0) { |
| 147 | fprintf(stderr,_("startkb should be positive, not %Ld\n"), sk); |
| 148 | exit(1); |
| 149 | } |
| 150 | fd = open(argv[1], O_RDONLY); |
| 151 | if (fd < 0) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 152 | perror(argv[1]); |
| 153 | exit(1); |
| 154 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 155 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 156 | /* Now, go looking for the superblock ! */ |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 157 | printf(_("starting at %Ld, with %d byte increments\n"), sk, skiprate); |
| 158 | printf(_(" thisoff block fs_blk_sz blksz grp last_mount\n")); |
| 159 | for (; lseek64(fd, sk, SEEK_SET) != -1 && |
| 160 | read(fd, &ext2, 512) == 512; sk += skiprate) { |
| 161 | |
| 162 | if (sk && !(sk & (interval - 1))) { |
| 163 | time_t now, diff; |
| 164 | |
| 165 | now = time(0); |
| 166 | diff = now - last; |
| 167 | |
| 168 | if (diff > 0) { |
| 169 | s = ctime(&now); |
| 170 | s[24] = 0; |
| 171 | printf("\r%14Ld: %8LdkB/s @ %s", sk, |
| 172 | (((sk - skl)) / diff) >> 10, s); |
| 173 | fflush(stdout); |
| 174 | } |
| 175 | if (diff < 5) |
| 176 | interval <<= 1; |
| 177 | else if (diff > 20) |
| 178 | interval >>= 1; |
| 179 | last = now; |
| 180 | skl = sk; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 181 | } |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 182 | if (ext2.s_magic != EXT2_SUPER_MAGIC) |
| 183 | continue; |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 184 | if (ext2.s_log_block_size > 4) |
| 185 | WHY("log block size > 4 (%d)\n", ext2.s_log_block_size); |
| 186 | if (ext2.s_r_blocks_count > ext2.s_blocks_count) |
| 187 | WHY("r_blocks_count > blocks_count (%d > %d)\n", |
| 188 | ext2.s_r_blocks_count, ext2.s_blocks_count); |
| 189 | if (ext2.s_free_blocks_count > ext2.s_blocks_count) |
| 190 | WHY("free_blocks_count > blocks_count\n (%d > %d)\n", |
| 191 | ext2.s_free_blocks_count, ext2.s_blocks_count); |
| 192 | if (ext2.s_free_inodes_count > ext2.s_inodes_count) |
| 193 | WHY("free_inodes_count > inodes_count (%d > %d)\n", |
| 194 | ext2.s_free_inodes_count, ext2.s_inodes_count); |
| 195 | |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 196 | tm = ext2.s_mtime; |
| 197 | s=ctime(&tm); |
| 198 | s[24]=0; |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 199 | printf("\r%14Ld %9Ld %9d %5d %4d %s\n", |
| 200 | sk, sk >> 10, ext2.s_blocks_count, |
| 201 | 1 << (ext2.s_log_block_size + 10), |
Theodore Ts'o | e2423cc | 1999-06-18 00:51:31 +0000 | [diff] [blame] | 202 | ext2.s_block_group_nr, s); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 203 | } |
Andreas Dilger | d6903ec | 2001-10-01 15:38:14 -0600 | [diff] [blame] | 204 | printf(_("\n%14Ld: finished with errno %d\n"), sk, errno); |
| 205 | close(fd); |
| 206 | |
| 207 | return errno; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 208 | } |