2 * e2image.c --- Program which writes an image file backing up
3 * critical metadata for the filesystem.
5 * Copyright 2000 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
30 #include <sys/types.h>
32 #include <linux/ext2_fs.h>
34 #include "ext2fs/ext2fs.h"
35 #include "et/com_err.h"
36 #include "uuid/uuid.h"
38 #include "ext2fs/e2image.h"
40 #include "../version.h"
41 #include "nls-enable.h"
43 const char * program_name = "e2image";
44 char * device_name = NULL;
46 static void usage(void)
48 fprintf(stderr, _("Usage: %s device file\n"), program_name);
52 static void write_header(int fd, struct ext2_image_hdr *hdr)
54 char header_buf[4096];
57 if (lseek(fd, 0, SEEK_SET) < 0) {
58 perror("lseek while writing header");
61 memset(header_buf, 0, sizeof(header_buf));
64 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
66 actual = write(fd, header_buf, sizeof(header_buf));
68 perror("write header");
71 if (actual != sizeof(header_buf)) {
72 fprintf(stderr, _("short write (only %d bytes) for"
73 "writing image header"), actual);
79 int main (int argc, char ** argv)
87 struct ext2_image_hdr hdr;
90 setlocale(LC_MESSAGES, "");
91 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
92 textdomain(NLS_CAT_NAME);
94 fprintf (stderr, _("e2image %s, %s for EXT2 FS %s, %s\n"),
95 E2FSPROGS_VERSION, E2FSPROGS_DATE,
96 EXT2FS_VERSION, EXT2FS_DATE);
99 initialize_ext2_error_table();
100 while ((c = getopt (argc, argv, "r")) != EOF)
108 if (optind != argc - 2 )
110 device_name = argv[optind];
111 retval = ext2fs_open (device_name, open_flag, 0, 0,
112 unix_io_manager, &fs);
114 com_err (program_name, retval, _("while trying to open %s"),
116 printf(_("Couldn't find valid filesystem superblock.\n"));
120 fd = open(argv[optind+1], O_CREAT|O_TRUNC|O_RDWR, 0600);
122 com_err(program_name, errno, _("while trying to open %s"),
127 write_header(fd, NULL);
128 memset(&hdr, 0, sizeof(struct ext2_image_hdr));
130 hdr.offset_super = lseek(fd, 0, SEEK_CUR);
131 retval = ext2fs_image_super_write(fs, fd, 0);
133 com_err(program_name, retval, _("while writing superblock"));
137 hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
138 retval = ext2fs_image_inode_write(fs, fd, IMAGER_FLAG_SPARSEWRITE);
140 com_err(program_name, retval, _("while writing inode table"));
144 hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
145 retval = ext2fs_image_bitmap_write(fs, fd, 0);
147 com_err(program_name, retval, _("while writing block bitmap"));
151 hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
152 retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
154 com_err(program_name, retval, _("while writing inode bitmap"));