Patchwork [1/4] e2image: add offset switches

login
register
mail settings
Submitter Phillip Susi
Date Oct. 8, 2013, 3:56 p.m.
Message ID <1381247792-22508-2-git-send-email-psusi@ubuntu.com>
Download mbox | patch
Permalink /patch/281506/
State Superseded
Headers show

Comments

Phillip Susi - Oct. 8, 2013, 3:56 p.m.
Add -o and -O switches to specify the offset where the source
and destination filesystems start.  This is useful if you have
an image of a partitioned disk or wish to create one.

Signed-off-by: Phillip Susi <psusi@ubuntu.com>
---
 misc/e2image.8.in | 36 ++++++++++++++++++++++++++++++++++++
 misc/e2image.c    | 24 +++++++++++++++++++-----
 2 files changed, 55 insertions(+), 5 deletions(-)
Theodore Ts'o - Dec. 16, 2013, 4:32 a.m.
On Tue, Oct 08, 2013 at 11:56:29AM -0400, Phillip Susi wrote:
> Add -o and -O switches to specify the offset where the source
> and destination filesystems start.  This is useful if you have
> an image of a partitioned disk or wish to create one.
> 
> Signed-off-by: Phillip Susi <psusi@ubuntu.com>

Specifying a dest_offset only makes sense in raw mode, no?

	     		      	    	  - Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Phillip Susi - Dec. 16, 2013, 4:30 p.m.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 12/15/2013 11:32 PM, Theodore Ts'o wrote:
> On Tue, Oct 08, 2013 at 11:56:29AM -0400, Phillip Susi wrote:
>> Add -o and -O switches to specify the offset where the source and
>> destination filesystems start.  This is useful if you have an
>> image of a partitioned disk or wish to create one.
>> 
>> Signed-off-by: Phillip Susi <psusi@ubuntu.com>
> 
> Specifying a dest_offset only makes sense in raw mode, no?

I think it makes sense in qcow2 mode, but it seems it doesn't work and
isn't trivial to fix, so I'll disable it for now.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSryqNAAoJEI5FoCIzSKrwJi0H/jA0887Dku5kMrpga3fTJCf+
rjGe93qHoqf6DOprj+2XI4cTJOY572729QrxKqbtlicY5F7lbB/9JKXn4VNcFJEk
6+1iQHh5mmFMP9g2hWxQPgfm93UAUYlC/pWyQvwTeUjKBp+EPYmoUtrDIZYpc79S
eFIsFt2TwduQ3zcwwLLOx2knQNyic9e3mwPF0NeIXbByfAW6hhPHLh1eg4zbTcJH
GYQqNyjk0vS0qmW+CBfAxyH4eYk6JgXtmg8c+8eePwlQLpbIoYW84cs6+yWbAu1y
1tVeqi2YTuYk+kU0t5DEj5sHoYzSXTZCukO8G/cThrhEMVnG1pwNRH8egjoyyYo=
=Ty8d
-----END PGP SIGNATURE-----
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/misc/e2image.8.in b/misc/e2image.8.in
index 84b9729..86d3dfc 100644
--- a/misc/e2image.8.in
+++ b/misc/e2image.8.in
@@ -10,6 +10,14 @@  e2image \- Save critical ext2/ext3/ext4 filesystem metadata to a file
 [
 .B \-rsIQa
 ]
+[
+.B \-o
+.I source_offset
+]
+[
+.B \-O
+.I dest_offset
+]
 .I device
 .I image-file
 .SH DESCRIPTION
@@ -191,6 +199,34 @@  give an image that is suitable to use to clone the entire FS or
 for backup purposes.  Note that this option only works with the
 raw or QCOW2 formats.
 .PP
+.SH OFFSETS
+Normally a filesystem starts at the beginning of a partition, and
+.B e2image
+is run on the partition.  When working with image files, you don't
+have the option of using the partition device, so you can specify
+the offset where the filesystem starts directly with the
+.B \-o
+option.  Similarly the
+.B \-O
+option specifies the offset that should be seeked to in the destination
+before writing the filesystem.
+.PP
+For example, if you have a
+.B dd
+image of a whole hard drive that contains an ext2 fs in a partition
+starting at 1 MiB, you can clone that fs with:
+.PP
+.br
+\	\fBe2image \-aro 1048576 img /dev/sda1\fR
+.br
+.PP
+Or you can clone a fs into an image file, leaving room in the first
+MiB for a partition table with:
+.PP
+.br
+\	\fBe2image -arO 1048576 /dev/sda1 img\fR
+.br
+.PP
 .SH AUTHOR
 .B e2image
 was written by Theodore Ts'o (tytso@mit.edu).
diff --git a/misc/e2image.c b/misc/e2image.c
index 885a794..cf486c0 100644
--- a/misc/e2image.c
+++ b/misc/e2image.c
@@ -55,6 +55,7 @@  char * device_name = NULL;
 char all_data;
 char output_is_blk;
 /* writing to blk device: don't skip zeroed blocks */
+blk64_t source_offset, dest_offset;
 
 static void lseek_error_and_exit(int errnum)
 {
@@ -87,7 +88,7 @@  static int get_bits_from_size(size_t size)
 
 static void usage(void)
 {
-	fprintf(stderr, _("Usage: %s [-rsIQa] device image_file\n"),
+	fprintf(stderr, _("Usage: %s [-rsIQa] [-o source_offset] [-O dest_offset] device image_file\n"),
 		program_name);
 	exit (1);
 }
@@ -1269,7 +1270,7 @@  int main (int argc, char ** argv)
 	if (argc && *argv)
 		program_name = *argv;
 	add_error_table(&et_ext2_error_table);
-	while ((c = getopt(argc, argv, "rsIQa")) != EOF)
+	while ((c = getopt(argc, argv, "rsIQao:O:")) != EOF)
 		switch (c) {
 		case 'I':
 			flags |= E2IMAGE_INSTALL_FLAG;
@@ -1290,6 +1291,12 @@  int main (int argc, char ** argv)
 		case 'a':
 			all_data = 1;
 			break;
+		case 'o':
+			source_offset = strtoull(optarg, NULL, 0);
+			break;
+		case 'O':
+			dest_offset = strtoull(optarg, NULL, 0);
+			break;
 		default:
 			usage();
 		}
@@ -1317,9 +1324,11 @@  int main (int argc, char ** argv)
 			goto skip_device;
 		}
 	}
-
-	retval = ext2fs_open (device_name, open_flag, 0, 0,
-			      unix_io_manager, &fs);
+	char *options;
+	asprintf (&options, "offset=%llu", source_offset);
+	retval = ext2fs_open2 (device_name, options, open_flag, 0, 0,
+			       unix_io_manager, &fs);
+	free (options);
         if (retval) {
 		com_err (program_name, retval, _("while trying to open %s"),
 			 device_name);
@@ -1338,6 +1347,11 @@  skip_device:
 			exit(1);
 		}
 	}
+	if (dest_offset)
+		if (ext2fs_llseek (fd, dest_offset, SEEK_SET) < 0) {
+			perror("ext2fs_llseek");
+			exit(1);
+		}
 
 	if ((img_type & E2IMAGE_QCOW2) && (fd == 1)) {
 		com_err(program_name, 0, "QCOW2 image can not be written to "