1
2
3
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
33
34
35
36
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
/*
* e2image.c --- Program which writes an image file backing up
* critical metadata for the filesystem.
*
* Copyright 2000, 2001 by Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <fcntl.h>
#include <grp.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern char *optarg;
extern int optind;
#endif
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ext2fs/ext2_fs.h"
#include "ext2fs/ext2fs.h"
#include "et/com_err.h"
#include "uuid/uuid.h"
#include "e2p/e2p.h"
#include "ext2fs/e2image.h"
#include "../version.h"
#include "nls-enable.h"
const char * program_name = "e2image";
char * device_name = NULL;
static void usage(void)
{
fprintf(stderr, _("Usage: %s [-r] device file\n"), program_name);
exit (1);
}
static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize)
{
char *header_buf;
int actual;
header_buf = malloc(blocksize);
if (!header_buf) {
fputs(_("Couldn't allocate header buffer\n"), stderr);
exit(1);
}
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek while writing header");
exit(1);
}
memset(header_buf, 0, blocksize);
if (hdr)
memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
actual = write(fd, header_buf, blocksize);
if (actual < 0) {
perror("write header");
exit(1);
}
if (actual != blocksize) {
fprintf(stderr, _("short write (only %d bytes) for "
"writing image header"), actual);
exit(1);
}
free(header_buf);
}
static void write_image_file(ext2_filsys fs, int fd)
{
struct ext2_image_hdr hdr;
struct stat st;
errcode_t retval;
write_header(fd, NULL, fs->blocksize);
memset(&hdr, 0, sizeof(struct ext2_image_hdr));
hdr.offset_super = lseek(fd, 0, SEEK_CUR);
retval = ext2fs_image_super_write(fs, fd, 0);
if (retval) {
com_err(program_name, retval, _("while writing superblock"));
exit(1);
}
hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
retval = ext2fs_image_inode_write(fs, fd,
(fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
if (retval) {
com_err(program_name, retval, _("while writing inode table"));
exit(1);
}
hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
retval = ext2fs_image_bitmap_write(fs, fd, 0);
if (retval) {
com_err(program_name, retval, _("while writing block bitmap"));
exit(1);
}
hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
if (retval) {
com_err(program_name, retval, _("while writing inode bitmap"));
exit(1);
}
hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE;
strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
strncat(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name));
hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
hdr.fs_blocksize = fs->blocksize;
if (stat(device_name, &st) == 0)
hdr.fs_device = st.st_rdev;
if (fstat(fd, &st) == 0) {
hdr.image_device = st.st_dev;
hdr.image_inode = st.st_ino;
}
memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
hdr.image_time = time(0);
write_header(fd, &hdr, fs->blocksize);
}
/*
* These set of functions are used to write a RAW image file.
*/
ext2fs_block_bitmap meta_block_map;
struct process_block_struct {
ext2_ino_t ino;
};
/*
* These subroutines short circuits ext2fs_get_blocks and
* ext2fs_check_directory; we use them since we already have the inode
* structure, so there's no point in letting the ext2fs library read
* the inode again.
*/
static ino_t stashed_ino = 0;
static struct ext2_inode *stashed_inode;
static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)),
ext2_ino_t ino,
blk_t *blocks)
{
int i;
if ((ino != stashed_ino) || !stashed_inode)
return EXT2_ET_CALLBACK_NOTHANDLED;
for (i=0; i < EXT2_N_BLOCKS; i++)
blocks[i] = stashed_inode->i_block[i];
return 0;
}
static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)),
ext2_ino_t ino)
{
if ((ino != stashed_ino) || !stashed_inode)
return EXT2_ET_CALLBACK_NOTHANDLED;
if (!LINUX_S_ISDIR(stashed_inode->i_mode))
return EXT2_ET_NO_DIRECTORY;
return 0;
}
static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
ext2_ino_t ino,
struct ext2_inode *inode)
{
if ((ino != stashed_ino) || !stashed_inode)
return EXT2_ET_CALLBACK_NOTHANDLED;
*inode = *stashed_inode;
return 0;
}
static void use_inode_shortcuts(ext2_filsys fs, int bool)
{
if (bool) {
fs->get_blocks = meta_get_blocks;
fs->check_directory = meta_check_directory;
fs->read_inode = meta_read_inode;
stashed_ino = 0;
} else {
fs->get_blocks = 0;
fs->check_directory = 0;
fs->read_inode = 0;
}
}
static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
blk_t *block_nr,
e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
blk_t ref_block EXT2FS_ATTR((unused)),
int ref_offset EXT2FS_ATTR((unused)),
void *priv_data EXT2FS_ATTR((unused)))
{
ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
return 0;
}
static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)),
blk_t *block_nr,
e2_blkcnt_t blockcnt,
blk_t ref_block EXT2FS_ATTR((unused)),
int ref_offset EXT2FS_ATTR((unused)),
void *priv_data EXT2FS_ATTR((unused)))
{
if (blockcnt < 0) {
ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
}
return 0;
}
static void mark_table_blocks(ext2_filsys fs)
{
blk_t block, b;
unsigned int i,j;
block = fs->super->s_first_data_block;
/*
* Mark primary superblock
*/
ext2fs_mark_block_bitmap(meta_block_map, block);
/*
* Mark the primary superblock descriptors
*/
for (j = 0; j < fs->desc_blocks; j++) {
ext2fs_mark_block_bitmap(meta_block_map,
ext2fs_descriptor_block_loc(fs, block, j));
}
for (i = 0; i < fs->group_desc_count; i++) {
/*
* Mark the blocks used for the inode table
*/
if (fs->group_desc[i].bg_inode_table) {
for (j = 0, b = fs->group_desc[i].bg_inode_table;
j < (unsigned) fs->inode_blocks_per_group;
j++, b++)
ext2fs_mark_block_bitmap(meta_block_map, b);
}
/*
* Mark block used for the block bitmap
*/
if (fs->group_desc[i].bg_block_bitmap) {
ext2fs_mark_block_bitmap(meta_block_map,
fs->group_desc[i].bg_block_bitmap);
}
/*
* Mark block used for the inode bitmap
*/
if (fs->group_desc[i].bg_inode_bitmap) {
ext2fs_mark_block_bitmap(meta_block_map,
fs->group_desc[i].bg_inode_bitmap);
}
block += fs->super->s_blocks_per_group;
}
}
/*
* This function returns 1 if the specified block is all zeros
*/
static int check_zero_block(char *buf, int blocksize)
{
char *cp = buf;
int left = blocksize;
while (left > 0) {
if (*cp++)
return 0;
left--;
}
return 1;
}
static void write_block(int fd, char *buf, int sparse_offset,
int blocksize, blk_t block)
{
int count;
errcode_t err;
if (sparse_offset) {
#ifdef HAVE_LSEEK64
if (lseek64(fd, sparse_offset, SEEK_CUR) < 0)
perror("lseek");
#else
if (lseek(fd, sparse_offset, SEEK_CUR) < 0)
perror("lseek");
#endif
}
if (blocksize) {
count = write(fd, buf, blocksize);
if (count != blocksize) {
if (count == -1)
err = errno;
else
err = 0;
com_err(program_name, err, "error writing block %d",
block);
}
}
}
static void output_meta_data_blocks(ext2_filsys fs, int fd)
{
errcode_t retval;
blk_t blk;
char buf[8192], zero_buf[8192];
int sparse = 0;
memset(zero_buf, 0, sizeof(zero_buf));
for (blk = 0; blk < fs->super->s_blocks_count; blk++) {
if ((blk >= fs->super->s_first_data_block) &&
ext2fs_test_block_bitmap(meta_block_map, blk)) {
retval = io_channel_read_blk(fs->io, blk, 1, buf);
if (retval) {
com_err(program_name, retval,
"error reading block %d", blk);
}
if ((fd != 1) && check_zero_block(buf, fs->blocksize))
goto sparse_write;
write_block(fd, buf, sparse, fs->blocksize, blk);
sparse = 0;
} else {
sparse_write:
if (fd == 1) {
write_block(fd, zero_buf, 0,
fs->blocksize, blk);
continue;
}
sparse += fs->blocksize;
if (sparse >= 1024*1024) {
write_block(fd, 0, sparse, 0, 0);
sparse = 0;
}
}
}
write_block(fd, zero_buf, sparse, 1, -1);
}
static void write_raw_image_file(ext2_filsys fs, int fd)
{
struct process_block_struct pb;
struct ext2_inode inode;
ext2_inode_scan scan;
ext2_ino_t ino;
errcode_t retval;
char * block_buf;
retval = ext2fs_allocate_block_bitmap(fs, "in-use block map",
&meta_block_map);
if (retval) {
com_err(program_name, retval, "while allocating block bitmap");
exit(1);
}
mark_table_blocks(fs);
retval = ext2fs_open_inode_scan(fs, 0, &scan);
if (retval) {
com_err(program_name, retval, _("while opening inode scan"));
exit(1);
}
block_buf = malloc(fs->blocksize * 3);
if (!block_buf) {
com_err(program_name, 0, "Can't allocate block buffer");
exit(1);
}
use_inode_shortcuts(fs, 1);
stashed_inode = &inode;
while (1) {
retval = ext2fs_get_next_inode(scan, &ino, &inode);
if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
continue;
if (retval) {
com_err(program_name, retval,
_("while getting next inode"));
exit(1);
}
if (ino == 0)
break;
if (!inode.i_links_count)
continue;
if (inode.i_file_acl) {
ext2fs_mark_block_bitmap(meta_block_map,
inode.i_file_acl);
}
if (!ext2fs_inode_has_valid_blocks(&inode))
continue;
stashed_ino = ino;
if (LINUX_S_ISDIR(inode.i_mode) ||
(LINUX_S_ISLNK(inode.i_mode) &&
ext2fs_inode_has_valid_blocks(&inode)) ||
ino == fs->super->s_journal_inum) {
retval = ext2fs_block_iterate2(fs, ino, 0,
block_buf, process_dir_block, &pb);
if (retval) {
com_err(program_name, retval,
"while iterating over inode %d",
ino);
exit(1);
}
} else {
if (inode.i_block[EXT2_IND_BLOCK] ||
inode.i_block[EXT2_DIND_BLOCK] ||
inode.i_block[EXT2_TIND_BLOCK]) {
retval = ext2fs_block_iterate2(fs,
ino, 0, block_buf,
process_file_block, &pb);
if (retval) {
com_err(program_name, retval,
"while iterating over %d", ino);
exit(1);
}
}
}
}
use_inode_shortcuts(fs, 0);
output_meta_data_blocks(fs, fd);
}
int main (int argc, char ** argv)
{
int c;
errcode_t retval;
ext2_filsys fs;
char *outfn;
int open_flag = 0;
int raw_flag = 0;
int fd = 0;
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
#endif
fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
E2FSPROGS_DATE);
if (argc && *argv)
program_name = *argv;
initialize_ext2_error_table();
while ((c = getopt (argc, argv, "r")) != EOF)
switch (c) {
case 'r':
raw_flag++;
break;
default:
usage();
}
if (optind != argc - 2 )
usage();
device_name = argv[optind];
outfn = argv[optind+1];
retval = ext2fs_open (device_name, open_flag, 0, 0,
unix_io_manager, &fs);
if (retval) {
com_err (program_name, retval, _("while trying to open %s"),
device_name);
fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
exit(1);
}
if (strcmp(outfn, "-") == 0)
fd = 1;
else {
#ifdef HAVE_OPEN64
fd = open64(outfn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
#else
fd = open(outfn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
#endif
if (fd < 0) {
com_err(program_name, errno,
_("while trying to open %s"), argv[optind+1]);
exit(1);
}
}
if (raw_flag)
write_raw_image_file(fs, fd);
else
write_image_file(fs, fd);
ext2fs_close (fs);
exit (0);
}
|