imagedump.c 10.8 KB
Newer Older
Mike Hibler's avatar
all:  
Mike Hibler committed
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2003 University of Utah and the Flux Group.
Mike Hibler's avatar
all:  
Mike Hibler committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * All rights reserved.
 */

/*
 * Usage: imagedump <input file>
 *
 * Prints out information about an image.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <zlib.h>
#include <sys/stat.h>
#include "imagehdr.h"

static int detail = 0;
static int dumpmap = 0;
static int infd = -1;

static unsigned long long wasted;
static uint32_t sectinuse;
static uint32_t sectfree;

static void usage(void);
static void dumpfile(char *name, int fd);
33
static int dumpchunk(char *name, char *buf, int chunkno, int checkindex);
Mike Hibler's avatar
all:  
Mike Hibler committed
34

35
36
#define SECTOBYTES(s)	((unsigned long long)(s) * SECSIZE)

Mike Hibler's avatar
all:  
Mike Hibler committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
int
main(int argc, char **argv)
{
	int ch, version = 0;
	extern char build_info[];

	while ((ch = getopt(argc, argv, "dmv")) != -1)
		switch(ch) {
		case 'd':
			detail++;
			break;
		case 'm':
			dumpmap++;
			detail = 0;
			break;
		case 'v':
			version++;
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (version || detail) {
		fprintf(stderr, "%s\n", build_info);
		if (version)
			exit(0);
	}

	if (argc < 1)
		usage();

	while (argc > 0) {
		int isstdin = !strcmp(argv[0], "-");

		if (!isstdin) {
			if ((infd = open(argv[0], O_RDONLY, 0666)) < 0) {
				perror("opening input file");
				exit(1);
			}
		} else
			infd = fileno(stdin);

		dumpfile(isstdin ? "<stdin>" : argv[0], infd);

		if (!isstdin)
			close(infd);
		argc--;
		argv++;
	}
	exit(0);
}

static void
usage(void)
{
	fprintf(stderr, "usage: "
		"imagedump options <image filename> ...\n"
		" -v              Print version info and exit\n"
		" -d              Turn on progressive levels of detail\n");
	exit(1);
}	

static char chunkbuf[SUBBLOCKSIZE];
static unsigned int magic;
static unsigned long chunkcount;
static uint32_t nextsector;
107
static uint32_t fmax, fmin, franges, amax, amin, aranges;
108
static uint32_t adist[8]; /* <4k, <8k, <16k, <32k, <64k, <128k, <256k, >=256k */
Mike Hibler's avatar
all:  
Mike Hibler committed
109
110
111
112
113

static void
dumpfile(char *name, int fd)
{
	unsigned long long tbytes, dbytes, cbytes;
114
	int count, chunkno, checkindex = 1;
Mike Hibler's avatar
all:  
Mike Hibler committed
115
116
117
118
119
120
121
122
	off_t filesize;
	int isstdin;
	char *bp;

	isstdin = (fd == fileno(stdin));
	wasted = sectinuse = sectfree = 0;
	nextsector = 0;

123
124
125
	fmax = amax = 0;
	fmin = amin = ~0;
	franges = aranges = 0;
126
	memset(adist, 0, sizeof(adist));
127

Mike Hibler's avatar
all:  
Mike Hibler committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	if (!isstdin) {
		struct stat st;

		if (fstat(fd, &st) < 0) {
			perror(name);
			return;
		}
		if ((st.st_size % SUBBLOCKSIZE) != 0)
			printf("%s: WARNING: "
			       "file size not a multiple of chunk size\n",
			       name);
		filesize = st.st_size;
	} else
		filesize = 0;

	for (chunkno = 0; ; chunkno++) {
		bp = chunkbuf;

		if (isstdin)
			count = sizeof(chunkbuf);
		else {
			count = DEFAULTREGIONSIZE;
			if (lseek(infd, (off_t)chunkno*sizeof(chunkbuf),
				  SEEK_SET) < 0) {
				perror("seeking on zipped image");
				return;
			}
		}

		/*
		 * Parse the file one chunk at a time.  We read the entire
		 * chunk and hand it off.  Since we might be reading from
		 * stdin, we have to make sure we get the entire amount.
		 */
		while (count) {
			int cc;
			
			if ((cc = read(infd, bp, count)) <= 0) {
				if (cc == 0)
					goto done;
				perror("reading zipped image");
				return;
			}
			count -= cc;
			bp += cc;
		}
174
		if (chunkno == 0) {
Mike Hibler's avatar
all:  
Mike Hibler committed
175
176
177
178
179
180
181
182
183
184
185
			blockhdr_t *hdr = (blockhdr_t *)chunkbuf;

			magic = hdr->magic;
			if (magic < COMPRESSED_MAGIC_BASE ||
			    magic > COMPRESSED_MAGIC_CURRENT) {
				printf("%s: bad version %x\n", name, magic);
				return;
			}

			chunkcount = hdr->blocktotal;
			if ((filesize / SUBBLOCKSIZE) != chunkcount) {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
				if (chunkcount != 0) {
					if (isstdin)
						filesize = (off_t)chunkcount *
							SUBBLOCKSIZE;
					else
						printf("%s: WARNING: file size "
						       "inconsistant with "
						       "chunk count "
						       "(%lu != %lu)\n",
						       name,
						       (unsigned long)
						       (filesize/SUBBLOCKSIZE),
						       chunkcount);
				} else if (magic == COMPRESSED_V1) {
					printf("%s: WARNING: zero chunk count, "
					       "ignoring block fields\n",
Mike Hibler's avatar
all:  
Mike Hibler committed
202
					       name);
203
204
					checkindex = 0;
				}
Mike Hibler's avatar
all:  
Mike Hibler committed
205
206
207
208
209
210
			}

			printf("%s: %qu bytes, %lu chunks, version %d\n",
			       name, filesize,
			       (unsigned long)(filesize / SUBBLOCKSIZE),
			       hdr->magic - COMPRESSED_MAGIC_BASE + 1);
211
212
213
214
215
216
217
218
219
		} else if (chunkno == 1) {
			blockhdr_t *hdr = (blockhdr_t *)chunkbuf;

			/*
			 * If reading from stdin, we don't know til the
			 * second chunk whether blockindexes are being kept.
			 */
			if (isstdin && filesize == 0 && hdr->blockindex == 0)
				checkindex = 0;
Mike Hibler's avatar
all:  
Mike Hibler committed
220
		}
221
222

		if (dumpchunk(name, chunkbuf, chunkno, checkindex))
Mike Hibler's avatar
all:  
Mike Hibler committed
223
224
225
226
227
228
229
			break;
	}
 done:
	if (filesize == 0)
		filesize = (off_t)(chunkno + 1) * SUBBLOCKSIZE;

	cbytes = (unsigned long long)(filesize - wasted);
230
231
	dbytes = SECTOBYTES(sectinuse);
	tbytes = SECTOBYTES(sectinuse + sectfree);
Mike Hibler's avatar
all:  
Mike Hibler committed
232
233
234
235
236
237
238
239
240
241
242

	if (detail > 0)
		printf("\n");
	printf("  %qu bytes of overhead/wasted space (%5.2f%% of image file)\n",
	       wasted, (double)wasted / filesize * 100);
	printf("  %qu bytes of compressed data\n",
	       cbytes);
	printf("  %5.2fx compression of allocated data (%qu bytes)\n",
	       (double)dbytes / cbytes, dbytes);
	printf("  %5.2fx compression of total known disk size (%qu bytes)\n",
	       (double)tbytes / cbytes, tbytes);
243
244
245
246
247

	if (franges)
		printf("  %d free ranges: %qu/%qu/%qu ave/min/max size\n",
		       franges, SECTOBYTES(sectfree)/franges,
		       SECTOBYTES(fmin), SECTOBYTES(fmax));
248
	if (aranges) {
Mike Hibler's avatar
Mike Hibler committed
249
		int maxsz, i;
250

251
252
253
		printf("  %d allocated ranges: %qu/%qu/%qu ave/min/max size\n",
		       aranges, SECTOBYTES(sectinuse)/aranges,
		       SECTOBYTES(amin), SECTOBYTES(amax));
254
255
256
257
258
259
260
261
262
263
264
		printf("  size distribution:\n");
		maxsz = 4*SECSIZE;
		for (i = 0; i < 7; i++) {
			maxsz *= 2;
			if (adist[i])
				printf("    < %dk bytes: %d\n",
				       maxsz/1024, adist[i]);
		}
		if (adist[i])
			printf("    >= %dk bytes: %d\n", maxsz/1024, adist[i]);
	}
Mike Hibler's avatar
all:  
Mike Hibler committed
265
266
267
}

static int
268
dumpchunk(char *name, char *buf, int chunkno, int checkindex)
Mike Hibler's avatar
all:  
Mike Hibler committed
269
270
271
{
	blockhdr_t *hdr;
	struct region *reg;
272
	uint32_t count;
Mike Hibler's avatar
all:  
Mike Hibler committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	int i;

	hdr = (blockhdr_t *)buf;

	switch (hdr->magic) {
	case COMPRESSED_V1:
		reg = (struct region *)((struct blockhdr_V1 *)hdr + 1);
		break;
	case COMPRESSED_V2:
		reg = (struct region *)((struct blockhdr_V2 *)hdr + 1);
		break;
	default:
		printf("%s: bad magic (%x!=%x) in chunk %d\n",
		       name, hdr->magic, magic, chunkno);
		return 1;
	}
289
	if (checkindex && hdr->blockindex != chunkno) {
Mike Hibler's avatar
all:  
Mike Hibler committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
		printf("%s: bad chunk index (%d) in chunk %d\n",
		       name, hdr->blockindex, chunkno);
		return 1;
	}
	if (chunkcount && hdr->blocktotal != chunkcount) {
		printf("%s: bad chunkcount (%d!=%lu) in chunk %d\n",
		       name, hdr->blocktotal, chunkcount, chunkno);
		return 1;
	}
	if (hdr->size > (SUBBLOCKSIZE - hdr->regionsize)) {
		printf("%s: bad chunksize (%d > %d) in chunk %d\n",
		       name, hdr->size, SUBBLOCKSIZE-hdr->regionsize, chunkno);
		return 1;
	}
#if 1
	/* include header overhead */
	wasted += SUBBLOCKSIZE - hdr->size;
#else
	wasted += ((SUBBLOCKSIZE - hdr->regionsize) - hdr->size);
#endif

	if (detail > 0) {
		printf("  Chunk %d: %u compressed bytes, ",
		       chunkno, hdr->size);
		if (hdr->magic > COMPRESSED_V1) {
			printf("sector range [%u-%u], ",
			       hdr->firstsect, hdr->lastsect-1);
			if (hdr->reloccount > 0)
				printf("%d relocs, ", hdr->reloccount);
		}
		printf("%d regions\n", hdr->regioncount);
	}
	if (hdr->regionsize != DEFAULTREGIONSIZE)
		printf("  WARNING: "
		       "unexpected region size (%d!=%d) in chunk %d\n",
		       hdr->regionsize, DEFAULTREGIONSIZE, chunkno);

	for (i = 0; i < hdr->regioncount; i++) {
		if (detail > 1)
329
			printf("    Region %d: %u sectors [%u-%u]\n",
Mike Hibler's avatar
all:  
Mike Hibler committed
330
331
332
333
334
			       i, reg->size, reg->start,
			       reg->start + reg->size - 1);
		if (reg->start < nextsector)
			printf("    WARNING: chunk %d region %d "
			       "may overlap others\n", chunkno, i);
335
336
337
338
		if (reg->size == 0)
			printf("    WARNING: chunk %d region %d "
			       "zero-length region\n", chunkno, i);
		count = 0;
Mike Hibler's avatar
all:  
Mike Hibler committed
339
340
341
342
343
344
345
346
		if (hdr->magic > COMPRESSED_V1) {
			if (i == 0) {
				if (hdr->firstsect > reg->start)
					printf("    WARNING: chunk %d bad "
					       "firstsect value (%u>%u)\n",
					       chunkno, hdr->firstsect,
					       reg->start);
				else
347
					count = reg->start - hdr->firstsect;
Mike Hibler's avatar
all:  
Mike Hibler committed
348
			} else
349
				count = reg->start - nextsector;
Mike Hibler's avatar
all:  
Mike Hibler committed
350
351
352
353
354
355
			if (i == hdr->regioncount-1) {
				if (hdr->lastsect < reg->start + reg->size)
					printf("    WARNING: chunk %d bad "
					       "lastsect value (%u<%u)\n",
					       chunkno, hdr->lastsect,
					       reg->start + reg->size);
Mike Hibler's avatar
nits:    
Mike Hibler committed
356
357
358
359
360
361
362
363
364
				else {
					if (count > 0) {
						sectfree += count;
						if (count < fmin)
							fmin = count;
						if (count > fmax)
							fmax = count;
						franges++;
					}
365
366
					count = hdr->lastsect -
						(reg->start+reg->size);
Mike Hibler's avatar
nits:    
Mike Hibler committed
367
				}
Mike Hibler's avatar
all:  
Mike Hibler committed
368
369
			}
		} else
370
371
372
373
374
			count = reg->start - nextsector;
		if (count > 0) {
			sectfree += count;
			if (count < fmin)
				fmin = count;
Mike Hibler's avatar
nits:    
Mike Hibler committed
375
			if (count > fmax)
376
377
378
379
380
381
382
383
				fmax = count;
			franges++;
		}

		count = reg->size;
		sectinuse += count;
		if (count < amin)
			amin = count;
Mike Hibler's avatar
nits:    
Mike Hibler committed
384
		if (count > amax)
385
			amax = count;
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
		if (count < 8)
			adist[0]++;
		else if (count < 16)
			adist[1]++;
		else if (count < 32)
			adist[2]++;
		else if (count < 64)
			adist[3]++;
		else if (count < 128)
			adist[4]++;
		else if (count < 256)
			adist[5]++;
		else if (count < 512)
			adist[6]++;
		else
			adist[7]++;
402
		aranges++;
Mike Hibler's avatar
all:  
Mike Hibler committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

		if (dumpmap) {
			switch (hdr->magic) {
			case COMPRESSED_V1:
				if (reg->start - nextsector != 0)
					printf("F: [%08x-%08x]\n",
					       nextsector, reg->start-1);
				printf("A: [%08x-%08x]\n",
				       reg->start, reg->start + reg->size - 1);
				break;
			case COMPRESSED_V2:
				if (i == 0 && hdr->firstsect < reg->start)
					printf("F: [%08x-%08x]\n",
					       hdr->firstsect, reg->start-1);
				if (i != 0 && reg->start - nextsector != 0)
					printf("F: [%08x-%08x]\n",
					       nextsector, reg->start-1);
				printf("A: [%08x-%08x]\n",
				       reg->start, reg->start + reg->size - 1);
				if (i == hdr->regioncount-1 &&
				    reg->start+reg->size < hdr->lastsect)
					printf("F: [%08x-%08x]\n",
					       reg->start+reg->size,
					       hdr->lastsect-1);
				break;
			}
		}

		nextsector = reg->start + reg->size;
		reg++;
	}

	if (hdr->magic == COMPRESSED_V1)
		return 0;

	for (i = 0; i < hdr->reloccount; i++) {
		struct blockreloc *reloc = (struct blockreloc *)reg;
440
		uint32_t offset = SECTOBYTES(reloc->sector) + reloc->sectoff;
Mike Hibler's avatar
all:  
Mike Hibler committed
441
442
443
444
445
446
447
448

		if (reloc->sector < hdr->firstsect ||
		    reloc->sector >= hdr->lastsect)
			printf("    WARNING: "
			       "Reloc %d at %u not in chunk [%u-%u]\n", i,
			       reloc->sector, hdr->firstsect, hdr->lastsect-1);
		if (detail > 1)
			printf("    Reloc %d: %s [%u-%u] (sector %d)\n", i,
449
450
451
452
			       reloc->type == RELOC_FBSDDISKLABEL ?
			       "FBSDDISKLABEL" :
			       (reloc->type == RELOC_OBSDDISKLABEL ?
				"OBSDDISKLABEL" : "??"),
Mike Hibler's avatar
all:  
Mike Hibler committed
453
454
455
456
457
			       offset, offset + reloc->size, reloc->sector);
	}

	return 0;
}