e2image.c revision fef2b38d8e5b475655771f8208265a8fa9246601
1/* 2 * e2image.c --- Program which writes an image file backing up 3 * critical metadata for the filesystem. 4 * 5 * Copyright 2000, 2001 by Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * License. 10 * %End-Header% 11 */ 12 13#define _LARGEFILE_SOURCE 14#define _LARGEFILE64_SOURCE 15 16#include <fcntl.h> 17#include <grp.h> 18#ifdef HAVE_GETOPT_H 19#include <getopt.h> 20#else 21extern char *optarg; 22extern int optind; 23#endif 24#include <pwd.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <time.h> 29#include <unistd.h> 30#include <fcntl.h> 31#include <errno.h> 32#include <sys/stat.h> 33#include <sys/types.h> 34 35#include "ext2fs/ext2_fs.h" 36#include "ext2fs/ext2fs.h" 37#include "et/com_err.h" 38#include "uuid/uuid.h" 39#include "e2p/e2p.h" 40#include "ext2fs/e2image.h" 41 42#include "../version.h" 43#include "nls-enable.h" 44 45const char * program_name = "e2image"; 46char * device_name = NULL; 47 48static void usage(void) 49{ 50 fprintf(stderr, _("Usage: %s [-rsI] device image_file\n"), 51 program_name); 52 exit (1); 53} 54 55static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize) 56{ 57 char *header_buf; 58 int actual; 59 60 header_buf = malloc(blocksize); 61 if (!header_buf) { 62 fputs(_("Couldn't allocate header buffer\n"), stderr); 63 exit(1); 64 } 65 66 if (lseek(fd, 0, SEEK_SET) < 0) { 67 perror("lseek while writing header"); 68 exit(1); 69 } 70 memset(header_buf, 0, blocksize); 71 72 if (hdr) 73 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr)); 74 75 actual = write(fd, header_buf, blocksize); 76 if (actual < 0) { 77 perror("write header"); 78 exit(1); 79 } 80 if (actual != blocksize) { 81 fprintf(stderr, _("short write (only %d bytes) for " 82 "writing image header"), actual); 83 exit(1); 84 } 85 free(header_buf); 86} 87 88static void write_image_file(ext2_filsys fs, int fd) 89{ 90 struct ext2_image_hdr hdr; 91 struct stat st; 92 errcode_t retval; 93 94 write_header(fd, NULL, fs->blocksize); 95 memset(&hdr, 0, sizeof(struct ext2_image_hdr)); 96 97 hdr.offset_super = lseek(fd, 0, SEEK_CUR); 98 retval = ext2fs_image_super_write(fs, fd, 0); 99 if (retval) { 100 com_err(program_name, retval, _("while writing superblock")); 101 exit(1); 102 } 103 104 hdr.offset_inode = lseek(fd, 0, SEEK_CUR); 105 retval = ext2fs_image_inode_write(fs, fd, 106 (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0); 107 if (retval) { 108 com_err(program_name, retval, _("while writing inode table")); 109 exit(1); 110 } 111 112 hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR); 113 retval = ext2fs_image_bitmap_write(fs, fd, 0); 114 if (retval) { 115 com_err(program_name, retval, _("while writing block bitmap")); 116 exit(1); 117 } 118 119 hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR); 120 retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP); 121 if (retval) { 122 com_err(program_name, retval, _("while writing inode bitmap")); 123 exit(1); 124 } 125 126 hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE; 127 strcpy(hdr.magic_descriptor, "Ext2 Image 1.0"); 128 gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname)); 129 strncat(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name) - 1); 130 hdr.fs_blocksize = fs->blocksize; 131 132 if (stat(device_name, &st) == 0) 133 hdr.fs_device = st.st_rdev; 134 135 if (fstat(fd, &st) == 0) { 136 hdr.image_device = st.st_dev; 137 hdr.image_inode = st.st_ino; 138 } 139 memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid)); 140 141 hdr.image_time = time(0); 142 write_header(fd, &hdr, fs->blocksize); 143} 144 145/* 146 * These set of functions are used to write a RAW image file. 147 */ 148ext2fs_block_bitmap meta_block_map; 149ext2fs_block_bitmap scramble_block_map; /* Directory blocks to be scrambled */ 150 151struct process_block_struct { 152 ext2_ino_t ino; 153 int is_dir; 154}; 155 156/* 157 * These subroutines short circuits ext2fs_get_blocks and 158 * ext2fs_check_directory; we use them since we already have the inode 159 * structure, so there's no point in letting the ext2fs library read 160 * the inode again. 161 */ 162static ino_t stashed_ino = 0; 163static struct ext2_inode *stashed_inode; 164 165static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)), 166 ext2_ino_t ino, 167 blk_t *blocks) 168{ 169 int i; 170 171 if ((ino != stashed_ino) || !stashed_inode) 172 return EXT2_ET_CALLBACK_NOTHANDLED; 173 174 for (i=0; i < EXT2_N_BLOCKS; i++) 175 blocks[i] = stashed_inode->i_block[i]; 176 return 0; 177} 178 179static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)), 180 ext2_ino_t ino) 181{ 182 if ((ino != stashed_ino) || !stashed_inode) 183 return EXT2_ET_CALLBACK_NOTHANDLED; 184 185 if (!LINUX_S_ISDIR(stashed_inode->i_mode)) 186 return EXT2_ET_NO_DIRECTORY; 187 return 0; 188} 189 190static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 191 ext2_ino_t ino, 192 struct ext2_inode *inode) 193{ 194 if ((ino != stashed_ino) || !stashed_inode) 195 return EXT2_ET_CALLBACK_NOTHANDLED; 196 *inode = *stashed_inode; 197 return 0; 198} 199 200static void use_inode_shortcuts(ext2_filsys fs, int bool) 201{ 202 if (bool) { 203 fs->get_blocks = meta_get_blocks; 204 fs->check_directory = meta_check_directory; 205 fs->read_inode = meta_read_inode; 206 stashed_ino = 0; 207 } else { 208 fs->get_blocks = 0; 209 fs->check_directory = 0; 210 fs->read_inode = 0; 211 } 212} 213 214static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)), 215 blk_t *block_nr, 216 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 217 blk_t ref_block EXT2FS_ATTR((unused)), 218 int ref_offset EXT2FS_ATTR((unused)), 219 void *priv_data EXT2FS_ATTR((unused))) 220{ 221 struct process_block_struct *p; 222 223 p = (struct process_block_struct *) priv_data; 224 225 ext2fs_mark_block_bitmap(meta_block_map, *block_nr); 226 if (scramble_block_map && p->is_dir && blockcnt >= 0) 227 ext2fs_mark_block_bitmap(scramble_block_map, *block_nr); 228 return 0; 229} 230 231static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)), 232 blk_t *block_nr, 233 e2_blkcnt_t blockcnt, 234 blk_t ref_block EXT2FS_ATTR((unused)), 235 int ref_offset EXT2FS_ATTR((unused)), 236 void *priv_data EXT2FS_ATTR((unused))) 237{ 238 if (blockcnt < 0) { 239 ext2fs_mark_block_bitmap(meta_block_map, *block_nr); 240 } 241 return 0; 242} 243 244static void mark_table_blocks(ext2_filsys fs) 245{ 246 blk_t first_block, b; 247 unsigned int i,j; 248 249 first_block = fs->super->s_first_data_block; 250 /* 251 * Mark primary superblock 252 */ 253 ext2fs_mark_block_bitmap(meta_block_map, first_block); 254 255 /* 256 * Mark the primary superblock descriptors 257 */ 258 for (j = 0; j < fs->desc_blocks; j++) { 259 ext2fs_mark_block_bitmap(meta_block_map, 260 ext2fs_descriptor_block_loc(fs, first_block, j)); 261 } 262 263 for (i = 0; i < fs->group_desc_count; i++) { 264 /* 265 * Mark the blocks used for the inode table 266 */ 267 if (fs->group_desc[i].bg_inode_table) { 268 for (j = 0, b = fs->group_desc[i].bg_inode_table; 269 j < (unsigned) fs->inode_blocks_per_group; 270 j++, b++) 271 ext2fs_mark_block_bitmap(meta_block_map, b); 272 } 273 274 /* 275 * Mark block used for the block bitmap 276 */ 277 if (fs->group_desc[i].bg_block_bitmap) { 278 ext2fs_mark_block_bitmap(meta_block_map, 279 fs->group_desc[i].bg_block_bitmap); 280 } 281 282 /* 283 * Mark block used for the inode bitmap 284 */ 285 if (fs->group_desc[i].bg_inode_bitmap) { 286 ext2fs_mark_block_bitmap(meta_block_map, 287 fs->group_desc[i].bg_inode_bitmap); 288 } 289 } 290} 291 292/* 293 * This function returns 1 if the specified block is all zeros 294 */ 295static int check_zero_block(char *buf, int blocksize) 296{ 297 char *cp = buf; 298 int left = blocksize; 299 300 while (left > 0) { 301 if (*cp++) 302 return 0; 303 left--; 304 } 305 return 1; 306} 307 308static void write_block(int fd, char *buf, int sparse_offset, 309 int blocksize, blk_t block) 310{ 311 int count; 312 errcode_t err; 313 314 if (sparse_offset) { 315#ifdef HAVE_LSEEK64 316 if (lseek64(fd, sparse_offset, SEEK_CUR) < 0) 317 perror("lseek"); 318#else 319 if (lseek(fd, sparse_offset, SEEK_CUR) < 0) 320 perror("lseek"); 321#endif 322 } 323 if (blocksize) { 324 count = write(fd, buf, blocksize); 325 if (count != blocksize) { 326 if (count == -1) 327 err = errno; 328 else 329 err = 0; 330 com_err(program_name, err, "error writing block %u", 331 block); 332 exit(1); 333 } 334 } 335} 336 337int name_id[256]; 338 339static void scramble_dir_block(ext2_filsys fs, blk_t blk, char *buf) 340{ 341 char *p, *end, *cp; 342 struct ext2_dir_entry_2 *dirent; 343 int rec_len, id, len; 344 345 end = buf + fs->blocksize; 346 for (p = buf; p < end-8; p += rec_len) { 347 dirent = (struct ext2_dir_entry_2 *) p; 348 rec_len = dirent->rec_len; 349#ifdef WORDS_BIGENDIAN 350 rec_len = ext2fs_swab16(rec_len); 351#endif 352#if 0 353 printf("rec_len = %d, name_len = %d\n", rec_len, dirent->name_len); 354#endif 355 if (rec_len < 8 || (rec_len % 4) || 356 (p+rec_len > end)) { 357 printf("Corrupt directory block %lu: " 358 "bad rec_len (%d)\n", (unsigned long) blk, 359 rec_len); 360 rec_len = end - p; 361#ifdef WORDS_BIGENDIAN 362 dirent->rec_len = ext2fs_swab16(rec_len); 363#endif 364 continue; 365 } 366 if (dirent->name_len + 8 > rec_len) { 367 printf("Corrupt directory block %lu: " 368 "bad name_len (%d)\n", (unsigned long) blk, 369 dirent->name_len); 370 dirent->name_len = rec_len - 8; 371 continue; 372 } 373 cp = p+8; 374 len = rec_len - dirent->name_len - 8; 375 if (len > 0) 376 memset(cp+dirent->name_len, 0, len); 377 if (dirent->name_len==1 && cp[0] == '.') 378 continue; 379 if (dirent->name_len==2 && cp[0] == '.' && cp[1] == '.') 380 continue; 381 382 memset(cp, 'A', dirent->name_len); 383 len = dirent->name_len; 384 id = name_id[len]++; 385 while ((len > 0) && (id > 0)) { 386 *cp += id % 26; 387 id = id / 26; 388 cp++; 389 len--; 390 } 391 } 392} 393 394static void output_meta_data_blocks(ext2_filsys fs, int fd) 395{ 396 errcode_t retval; 397 blk_t blk; 398 char *buf, *zero_buf; 399 int sparse = 0; 400 401 buf = malloc(fs->blocksize); 402 if (!buf) { 403 com_err(program_name, ENOMEM, "while allocating buffer"); 404 exit(1); 405 } 406 zero_buf = malloc(fs->blocksize); 407 if (!zero_buf) { 408 com_err(program_name, ENOMEM, "while allocating buffer"); 409 exit(1); 410 } 411 memset(zero_buf, 0, fs->blocksize); 412 for (blk = 0; blk < fs->super->s_blocks_count; blk++) { 413 if ((blk >= fs->super->s_first_data_block) && 414 ext2fs_test_block_bitmap(meta_block_map, blk)) { 415 retval = io_channel_read_blk(fs->io, blk, 1, buf); 416 if (retval) { 417 com_err(program_name, retval, 418 "error reading block %u", blk); 419 } 420 if (scramble_block_map && 421 ext2fs_test_block_bitmap(scramble_block_map, blk)) 422 scramble_dir_block(fs, blk, buf); 423 if ((fd != 1) && check_zero_block(buf, fs->blocksize)) 424 goto sparse_write; 425 write_block(fd, buf, sparse, fs->blocksize, blk); 426 sparse = 0; 427 } else { 428 sparse_write: 429 if (fd == 1) { 430 write_block(fd, zero_buf, 0, 431 fs->blocksize, blk); 432 continue; 433 } 434 sparse += fs->blocksize; 435 if (sparse >= 1024*1024) { 436 write_block(fd, 0, sparse, 0, 0); 437 sparse = 0; 438 } 439 } 440 } 441 if (sparse) 442 write_block(fd, zero_buf, sparse-1, 1, -1); 443 free(zero_buf); 444 free(buf); 445} 446 447static void write_raw_image_file(ext2_filsys fs, int fd, int scramble_flag) 448{ 449 struct process_block_struct pb; 450 struct ext2_inode inode; 451 ext2_inode_scan scan; 452 ext2_ino_t ino; 453 errcode_t retval; 454 char * block_buf; 455 456 retval = ext2fs_allocate_block_bitmap(fs, "in-use block map", 457 &meta_block_map); 458 if (retval) { 459 com_err(program_name, retval, "while allocating block bitmap"); 460 exit(1); 461 } 462 463 if (scramble_flag) { 464 retval = ext2fs_allocate_block_bitmap(fs, "scramble block map", 465 &scramble_block_map); 466 if (retval) { 467 com_err(program_name, retval, 468 "while allocating scramble block bitmap"); 469 exit(1); 470 } 471 } 472 473 mark_table_blocks(fs); 474 475 retval = ext2fs_open_inode_scan(fs, 0, &scan); 476 if (retval) { 477 com_err(program_name, retval, _("while opening inode scan")); 478 exit(1); 479 } 480 481 block_buf = malloc(fs->blocksize * 3); 482 if (!block_buf) { 483 com_err(program_name, 0, "Can't allocate block buffer"); 484 exit(1); 485 } 486 487 use_inode_shortcuts(fs, 1); 488 stashed_inode = &inode; 489 while (1) { 490 retval = ext2fs_get_next_inode(scan, &ino, &inode); 491 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) 492 continue; 493 if (retval) { 494 com_err(program_name, retval, 495 _("while getting next inode")); 496 exit(1); 497 } 498 if (ino == 0) 499 break; 500 if (!inode.i_links_count) 501 continue; 502 if (inode.i_file_acl) { 503 ext2fs_mark_block_bitmap(meta_block_map, 504 inode.i_file_acl); 505 } 506 if (!ext2fs_inode_has_valid_blocks(&inode)) 507 continue; 508 509 stashed_ino = ino; 510 pb.ino = ino; 511 pb.is_dir = LINUX_S_ISDIR(inode.i_mode); 512 if (LINUX_S_ISDIR(inode.i_mode) || 513 (LINUX_S_ISLNK(inode.i_mode) && 514 ext2fs_inode_has_valid_blocks(&inode)) || 515 ino == fs->super->s_journal_inum) { 516 retval = ext2fs_block_iterate2(fs, ino, 0, 517 block_buf, process_dir_block, &pb); 518 if (retval) { 519 com_err(program_name, retval, 520 "while iterating over inode %u", 521 ino); 522 exit(1); 523 } 524 } else { 525 if (inode.i_block[EXT2_IND_BLOCK] || 526 inode.i_block[EXT2_DIND_BLOCK] || 527 inode.i_block[EXT2_TIND_BLOCK]) { 528 retval = ext2fs_block_iterate2(fs, 529 ino, 0, block_buf, 530 process_file_block, &pb); 531 if (retval) { 532 com_err(program_name, retval, 533 "while iterating over inode %u", ino); 534 exit(1); 535 } 536 } 537 } 538 } 539 use_inode_shortcuts(fs, 0); 540 output_meta_data_blocks(fs, fd); 541 free(block_buf); 542} 543 544static void install_image(char *device, char *image_fn, int raw_flag) 545{ 546 errcode_t retval; 547 ext2_filsys fs; 548 int open_flag = EXT2_FLAG_IMAGE_FILE; 549 int fd = 0; 550 io_manager io_ptr; 551 io_channel io, image_io; 552 553 if (raw_flag) { 554 com_err(program_name, 0, "Raw images cannot be installed"); 555 exit(1); 556 } 557 558#ifdef CONFIG_TESTIO_DEBUG 559 io_ptr = test_io_manager; 560 test_io_backing_manager = unix_io_manager; 561#else 562 io_ptr = unix_io_manager; 563#endif 564 565 retval = ext2fs_open (image_fn, open_flag, 0, 0, 566 io_ptr, &fs); 567 if (retval) { 568 com_err (program_name, retval, _("while trying to open %s"), 569 image_fn); 570 exit(1); 571 } 572 573 retval = ext2fs_read_bitmaps (fs); 574 if (retval) { 575 com_err(program_name, retval, "error reading bitmaps"); 576 exit(1); 577 } 578 579 580 fd = open(image_fn, O_RDONLY); 581 if (fd < 0) { 582 perror(image_fn); 583 exit(1); 584 } 585 586 retval = io_ptr->open(device, IO_FLAG_RW, &io); 587 if (retval) { 588 com_err(device, 0, "while opening device file"); 589 exit(1); 590 } 591 592 image_io = fs->io; 593 594 ext2fs_rewrite_to_io(fs, io); 595 596 if (lseek(fd, fs->image_header->offset_inode, SEEK_SET) < 0) { 597 perror("lseek"); 598 exit(1); 599 } 600 601 retval = ext2fs_image_inode_read(fs, fd, 0); 602 if (retval) { 603 com_err(image_fn, 0, "while restoring the image table"); 604 exit(1); 605 } 606 607 ext2fs_close (fs); 608 exit (0); 609} 610 611int main (int argc, char ** argv) 612{ 613 int c; 614 errcode_t retval; 615 ext2_filsys fs; 616 char *image_fn; 617 int open_flag = 0; 618 int raw_flag = 0; 619 int install_flag = 0; 620 int scramble_flag = 0; 621 int fd = 0; 622 623#ifdef ENABLE_NLS 624 setlocale(LC_MESSAGES, ""); 625 setlocale(LC_CTYPE, ""); 626 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 627 textdomain(NLS_CAT_NAME); 628#endif 629 fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION, 630 E2FSPROGS_DATE); 631 if (argc && *argv) 632 program_name = *argv; 633 add_error_table(&et_ext2_error_table); 634 while ((c = getopt (argc, argv, "rsI")) != EOF) 635 switch (c) { 636 case 'r': 637 raw_flag++; 638 break; 639 case 's': 640 scramble_flag++; 641 break; 642 case 'I': 643 install_flag++; 644 break; 645 default: 646 usage(); 647 } 648 if (optind != argc - 2 ) 649 usage(); 650 device_name = argv[optind]; 651 image_fn = argv[optind+1]; 652 653 if (install_flag) { 654 install_image(device_name, image_fn, raw_flag); 655 exit (0); 656 } 657 658 retval = ext2fs_open (device_name, open_flag, 0, 0, 659 unix_io_manager, &fs); 660 if (retval) { 661 com_err (program_name, retval, _("while trying to open %s"), 662 device_name); 663 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout); 664 exit(1); 665 } 666 667 if (strcmp(image_fn, "-") == 0) 668 fd = 1; 669 else { 670#ifdef HAVE_OPEN64 671 fd = open64(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600); 672#else 673 fd = open(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600); 674#endif 675 if (fd < 0) { 676 com_err(program_name, errno, 677 _("while trying to open %s"), argv[optind+1]); 678 exit(1); 679 } 680 } 681 682 if (raw_flag) 683 write_raw_image_file(fs, fd, scramble_flag); 684 else 685 write_image_file(fs, fd); 686 687 ext2fs_close (fs); 688 remove_error_table(&et_ext2_error_table); 689 exit (0); 690} 691