2 * e2image.c --- Program which writes an image file backing up
3 * critical metadata for the filesystem.
5 * Copyright 2000, 2001 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
30 #include <sys/types.h>
32 #include "ext2fs/ext2_fs.h"
33 #include "ext2fs/ext2fs.h"
34 #include "et/com_err.h"
35 #include "uuid/uuid.h"
37 #include "ext2fs/e2image.h"
39 #include "../version.h"
40 #include "nls-enable.h"
42 const char * program_name = "e2image";
43 char * device_name = NULL;
45 static void usage(void)
47 fprintf(stderr, _("Usage: %s device file\n"), program_name);
51 static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize)
56 header_buf = malloc(blocksize);
58 fprintf(stderr, _("Couldn't allocate header buffer\n"));
62 if (lseek(fd, 0, SEEK_SET) < 0) {
63 perror("lseek while writing header");
66 memset(header_buf, 0, blocksize);
69 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
71 actual = write(fd, header_buf, blocksize);
73 perror("write header");
76 if (actual != blocksize) {
77 fprintf(stderr, _("short write (only %d bytes) for"
78 "writing image header"), actual);
85 int main (int argc, char ** argv)
93 struct ext2_image_hdr hdr;
97 setlocale(LC_MESSAGES, "");
98 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
99 textdomain(NLS_CAT_NAME);
101 fprintf (stderr, _("e2image %s, %s for EXT2 FS %s, %s\n"),
102 E2FSPROGS_VERSION, E2FSPROGS_DATE,
103 EXT2FS_VERSION, EXT2FS_DATE);
105 program_name = *argv;
106 initialize_ext2_error_table();
107 while ((c = getopt (argc, argv, "r")) != EOF)
115 if (optind != argc - 2 )
117 device_name = argv[optind];
118 retval = ext2fs_open (device_name, open_flag, 0, 0,
119 unix_io_manager, &fs);
121 com_err (program_name, retval, _("while trying to open %s"),
123 printf(_("Couldn't find valid filesystem superblock.\n"));
127 fd = open(argv[optind+1], O_CREAT|O_RDWR, 0600);
129 com_err(program_name, errno, _("while trying to open %s"),
134 write_header(fd, NULL, fs->blocksize);
135 memset(&hdr, 0, sizeof(struct ext2_image_hdr));
137 hdr.offset_super = lseek(fd, 0, SEEK_CUR);
138 retval = ext2fs_image_super_write(fs, fd, 0);
140 com_err(program_name, retval, _("while writing superblock"));
144 hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
145 retval = ext2fs_image_inode_write(fs, fd, IMAGER_FLAG_SPARSEWRITE);
147 com_err(program_name, retval, _("while writing inode table"));
151 hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
152 retval = ext2fs_image_bitmap_write(fs, fd, 0);
154 com_err(program_name, retval, _("while writing block bitmap"));
158 hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
159 retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
161 com_err(program_name, retval, _("while writing inode bitmap"));
165 hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE;
166 strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
167 gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
168 strncat(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name));
169 hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
170 hdr.fs_blocksize = fs->blocksize;
172 if (stat(device_name, &st) == 0)
173 hdr.fs_device = st.st_rdev;
175 if (fstat(fd, &st) == 0) {
176 hdr.image_device = st.st_dev;
177 hdr.image_inode = st.st_ino;
179 memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
181 hdr.image_time = time(0);
182 write_header(fd, &hdr, fs->blocksize);