blob: b03c8aaac0e6f4d50c99ac47839d4f0ae8bca51f [file] [log] [blame]
Theodore Ts'o74a74d22000-12-09 14:33:29 +00001/*
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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04009 *
Theodore Ts'o74a74d22000-12-09 14:33:29 +000010 * Well, here's my linux version of findsuper.
Theodore Ts'o7f88b041997-04-26 14:48:50 +000011 * 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'o7f88b041997-04-26 14:48:50 +000019 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040021 *
Theodore Ts'o7f88b041997-04-26 14:48:50 +000022 * 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 Dilgerd6903ec2001-10-01 15:38:14 -060041 *
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'o16fa86b2003-04-14 20:40:49 -040053 *
54 * %Begin-Header%
55 * This file may be redistributed under the terms of the GNU Public
56 * License.
57 * %End-Header%
Theodore Ts'o7f88b041997-04-26 14:48:50 +000058 */
59
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000060/*
61 * Documentation addendum added by Andreas dwguest@win.tue.nl/aeb@cwi.nl
Theodore Ts'oefc6f622008-08-27 23:07:54 -040062 *
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000063 * The program findsuper is a utility that scans a disk and finds
Theodore Ts'obb145b02005-06-20 08:35:27 -040064 * copies of ext2 superblocks (by checking for the ext2 signature).
Theodore Ts'oefc6f622008-08-27 23:07:54 -040065 *
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000066 * For each superblock found, it prints the offset in bytes, the
Theodore Ts'obb145b02005-06-20 08:35:27 -040067 * offset in 1024-byte blocks, the size of the ext2 partition in fs
Andreas Dilgerd6903ec2001-10-01 15:38:14 -060068 * blocks, the filesystem blocksize (in bytes), the block group number
69 * (always 0 for older ext2 systems), and a timestamp (s_mtime).
Theodore Ts'oefc6f622008-08-27 23:07:54 -040070 *
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000071 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040074 *
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000075 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040081 *
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000082 */
83
Andreas Dilgerd6903ec2001-10-01 15:38:14 -060084#define _FILE_OFFSET_BITS 64
Theodore Ts'oe2423cc1999-06-18 00:51:31 +000085
Theodore Ts'o7f88b041997-04-26 14:48:50 +000086#include <stdio.h>
87#include <stdlib.h>
Andreas Dilgerd6903ec2001-10-01 15:38:14 -060088#include <string.h>
89#include <unistd.h>
90#include <errno.h>
91#include <fcntl.h>
Theodore Ts'o7f88b041997-04-26 14:48:50 +000092#include <time.h>
93
Theodore Ts'o54c637d2001-05-14 11:45:38 +000094#include "ext2fs/ext2_fs.h"
JP Abgralle0ed7402014-03-19 19:08:39 -070095#include "ext2fs/ext2fs.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096#include "nls-enable.h"
Theodore Ts'o7f88b041997-04-26 14:48:50 +000097
Andreas Dilgerd6903ec2001-10-01 15:38:14 -060098#undef DEBUG
Theodore Ts'o7f88b041997-04-26 14:48:50 +000099
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600100#ifdef DEBUG
101#define WHY(fmt, arg...) { printf("\r%Ld: " fmt, sk, ##arg) ; continue; }
102#else
103#define WHY(fmt, arg...) { continue; }
104#endif
105
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400106static void usage(void)
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400107{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108 fprintf(stderr,
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400109 _("Usage: findsuper device [skipbytes [startkb]]\n"));
110 exit(1);
111}
112
113
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600114int main(int argc, char *argv[])
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000115{
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000116 int skiprate=512; /* one sector */
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600117 loff_t sk=0, skl=0;
118 int fd;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000119 char *s;
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600120 time_t tm, last = time(0);
121 loff_t interval = 1024 * 1024;
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400122 int c, print_jnl_copies = 0;
123 const char * device_name;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000124 struct ext2_super_block ext2;
125 /* interesting fields: EXT2_SUPER_MAGIC
126 * s_blocks_count s_log_block_size s_mtime s_magic s_lastcheck */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000127
128#ifdef ENABLE_NLS
129 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500130 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000131 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
132 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -0700133 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000134#endif
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400135
136 while ((c = getopt (argc, argv, "j")) != EOF) {
137 switch (c) {
138 case 'j':
139 print_jnl_copies++;
140 break;
141 default:
142 usage();
143 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000144 }
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400145
146 if (optind == argc)
147 usage();
148
149 device_name = argv[optind++];
150
151 if (optind < argc) {
152 skiprate = strtol(argv[optind], &s, 0);
153 if (s == argv[optind]) {
154 fprintf(stderr,_("skipbytes should be a number, not %s\n"), s);
155 exit(1);
156 }
157 optind++;
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600158 }
159 if (skiprate & 0x1ff) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000160 fprintf(stderr,
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600161 _("skipbytes must be a multiple of the sector size\n"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000162 exit(2);
163 }
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400164 if (optind < argc) {
165 sk = skl = strtoll(argv[optind], &s, 0) << 10;
166 if (s == argv[optind]) {
167 fprintf(stderr,
168 _("startkb should be a number, not %s\n"), s);
169 exit(1);
170 }
171 optind++;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000172 }
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600173 if (sk < 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700174 fprintf(stderr, _("startkb should be positive, not %llu\n"),sk);
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600175 exit(1);
176 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400177
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400178 fd = open(device_name, O_RDONLY);
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600179 if (fd < 0) {
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400180 perror(device_name);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000181 exit(1);
182 }
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400183
Theodore Ts'o598ff012005-12-09 19:16:40 -0500184 /* Now, go looking for the superblock! */
JP Abgralle0ed7402014-03-19 19:08:39 -0700185 printf(_("starting at %llu, with %u byte increments\n"), sk, skiprate);
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400186 if (print_jnl_copies)
187 printf(_("[*] probably superblock written in the ext3 "
188 "journal superblock,\n\tso start/end/grp wrong\n"));
189 printf(_("byte_offset byte_start byte_end fs_blocks blksz grp last_mount_time sb_uuid label\n"));
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600190 for (; lseek64(fd, sk, SEEK_SET) != -1 &&
191 read(fd, &ext2, 512) == 512; sk += skiprate) {
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400192 static unsigned char last_uuid[16] = "blah";
193 unsigned long long bsize, grpsize;
194 int jnl_copy, sb_offset;
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600195
196 if (sk && !(sk & (interval - 1))) {
197 time_t now, diff;
198
199 now = time(0);
200 diff = now - last;
201
202 if (diff > 0) {
203 s = ctime(&now);
204 s[24] = 0;
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400205 printf("\r%11Lu: %8LukB/s @ %s", sk,
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600206 (((sk - skl)) / diff) >> 10, s);
207 fflush(stdout);
208 }
209 if (diff < 5)
210 interval <<= 1;
211 else if (diff > 20)
212 interval >>= 1;
213 last = now;
214 skl = sk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000215 }
Theodore Ts'oe2423cc1999-06-18 00:51:31 +0000216 if (ext2.s_magic != EXT2_SUPER_MAGIC)
217 continue;
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400218 if (ext2.s_log_block_size > 6)
219 WHY("log block size > 6 (%u)\n", ext2.s_log_block_size);
JP Abgralle0ed7402014-03-19 19:08:39 -0700220 if (ext2fs_r_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500221 WHY("r_blocks_count > blocks_count (%u > %u)\n",
JP Abgralle0ed7402014-03-19 19:08:39 -0700222 ext2fs_r_blocks_count(&ext2),
223 ext2fs_blocks_count(&ext2));
224 if (ext2fs_free_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500225 WHY("free_blocks_count > blocks_count\n (%u > %u)\n",
JP Abgralle0ed7402014-03-19 19:08:39 -0700226 ext2fs_free_blocks_count(&ext2),
227 ext2fs_blocks_count(&ext2));
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600228 if (ext2.s_free_inodes_count > ext2.s_inodes_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500229 WHY("free_inodes_count > inodes_count (%u > %u)\n",
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600230 ext2.s_free_inodes_count, ext2.s_inodes_count);
231
Theodore Ts'oe2423cc1999-06-18 00:51:31 +0000232 tm = ext2.s_mtime;
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400233 s = ctime(&tm);
234 s[24] = 0;
235 bsize = 1 << (ext2.s_log_block_size + 10);
236 grpsize = bsize * ext2.s_blocks_per_group;
237 if (memcmp(ext2.s_uuid, last_uuid, sizeof(last_uuid)) == 0 &&
238 ext2.s_rev_level > 0 && ext2.s_block_group_nr == 0) {
239 jnl_copy = 1;
240 } else {
241 jnl_copy = 0;
242 memcpy(last_uuid, ext2.s_uuid, sizeof(last_uuid));
243 }
244 if (ext2.s_block_group_nr == 0 || bsize == 1024)
245 sb_offset = 1024;
246 else
247 sb_offset = 0;
248 if (jnl_copy && !print_jnl_copies)
249 continue;
250 printf("\r%11Lu %11Lu%s %11Lu%s %9u %5Lu %4u%s %s %02x%02x%02x%02x %s\n",
251 sk, sk - ext2.s_block_group_nr * grpsize - sb_offset,
252 jnl_copy ? "*":" ",
JP Abgralle0ed7402014-03-19 19:08:39 -0700253 sk + ext2fs_blocks_count(&ext2) * bsize -
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400254 ext2.s_block_group_nr * grpsize - sb_offset,
JP Abgralle0ed7402014-03-19 19:08:39 -0700255 jnl_copy ? "*" : " ", ext2fs_blocks_count(&ext2), bsize,
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400256 ext2.s_block_group_nr, jnl_copy ? "*" : " ", s,
257 ext2.s_uuid[0], ext2.s_uuid[1],
258 ext2.s_uuid[2], ext2.s_uuid[3], ext2.s_volume_name);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000259 }
Andreas Dilger42fef8d2006-08-06 00:56:37 -0400260 printf(_("\n%11Lu: finished with errno %d\n"), sk, errno);
Andreas Dilgerd6903ec2001-10-01 15:38:14 -0600261 close(fd);
262
263 return errno;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000264}