Blame SOURCES/e2fsprogs-1.44.6-e2image-add-b-and-B-options-to-specify-where-to-find.patch

0ab26d
From c86892cb20129e3925fb16c228fb7433332371ce Mon Sep 17 00:00:00 2001
0ab26d
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
0ab26d
Date: Wed, 6 Mar 2019 11:52:13 -0500
0ab26d
Subject: [PATCH 3/4] e2image: add -b and -B options to specify where to find
0ab26d
 the superblock
0ab26d
0ab26d
e2image has no ability to use superblock backup to copy metadata.
0ab26d
This feature can be useful if someone wants to make partition
0ab26d
image and fix it using e2fsck utility.
0ab26d
0ab26d
New -b option allows to pass superblock number, like e2fsck utility does.
0ab26d
e2image doesn't change primary superblock and store it as is, so
0ab26d
it can be fixed using e2fsck latter. Option -B allows setting
0ab26d
superblock size.
0ab26d
0ab26d
Signed-off-by: Artem Blagodarenko <c17828@cray.com>
0ab26d
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
0ab26d
---
0ab26d
 misc/e2image.8.in | 33 +++++++++++++++++++++++++++++++++
0ab26d
 misc/e2image.c    | 43 +++++++++++++++++++++++++++++++++++++------
0ab26d
 2 files changed, 70 insertions(+), 6 deletions(-)
0ab26d
0ab26d
diff --git a/misc/e2image.8.in b/misc/e2image.8.in
0ab26d
index a7bfdf24..bbbb57ae 100644
0ab26d
--- a/misc/e2image.8.in
0ab26d
+++ b/misc/e2image.8.in
0ab26d
@@ -12,6 +12,15 @@ e2image \- Save critical ext2/ext3/ext4 filesystem metadata to a file
0ab26d
 ]
0ab26d
 [
0ab26d
 .B \-f
0ab26d
+.B \-b
0ab26d
+.I superblock
0ab26d
+]
0ab26d
+[
0ab26d
+.B \-B
0ab26d
+.I blocksize
0ab26d
+]
0ab26d
+[
0ab26d
+.B \-fr
0ab26d
 ]
0ab26d
 .I device
0ab26d
 .I image-file
0ab26d
@@ -167,6 +176,22 @@ the
0ab26d
 option will prevent analysis of problems related to hash-tree indexed
0ab26d
 directories.
0ab26d
 .PP
0ab26d
+Option
0ab26d
+.B \-b
0ab26d
+.I superblock
0ab26d
+can be used to get image from partition with broken primary superblock.
0ab26d
+The partition is copied as-is including broken primary superblock.
0ab26d
+.PP
0ab26d
+Option
0ab26d
+.B \-B
0ab26d
+.I blocksize
0ab26d
+can be used to set superblock block size. Normally, e2fsck will search
0ab26d
+for the superblock at various different block sizes in an attempt to find
0ab26d
+the appropriate blocksize. This search can be fooled in some cases.  This
0ab26d
+option forces e2fsck to only try locating the superblock at a particular
0ab26d
+blocksize. If the superblock is not found, e2fsck will terminate with a
0ab26d
+fatal error.
0ab26d
+.PP
0ab26d
 Note that this will work even if you substitute "/dev/hda1" for another raw
0ab26d
 disk image, or QCOW2 image previously created by
0ab26d
 .BR e2image .
0ab26d
@@ -217,6 +242,14 @@ This can be useful to write a qcow2 image containing all data to a
0ab26d
 sparse image file where it can be loop mounted, or to a disk partition.
0ab26d
 Note that this may not work with qcow2 images not generated by e2image.
0ab26d
 .PP
0ab26d
+Options
0ab26d
+.B \-b
0ab26d
+.I superblock
0ab26d
+and
0ab26d
+.B \-B
0ab26d
+.I blocksize
0ab26d
+can be used same way as for raw images.
0ab26d
+.PP
0ab26d
 .SH INCLUDING DATA
0ab26d
 Normally
0ab26d
 .B e2image
0ab26d
diff --git a/misc/e2image.c b/misc/e2image.c
0ab26d
index 9e21d0db..3c881fee 100644
0ab26d
--- a/misc/e2image.c
0ab26d
+++ b/misc/e2image.c
0ab26d
@@ -104,7 +104,8 @@ static int get_bits_from_size(size_t size)
0ab26d
 
0ab26d
 static void usage(void)
0ab26d
 {
0ab26d
-	fprintf(stderr, _("Usage: %s [ -r|Q ] [ -f ] device image-file\n"),
0ab26d
+	fprintf(stderr, _("Usage: %s [ -r|Q ] [ -f ] [ -b superblock ] [ -B blocksize]"
0ab26d
+			  "[ -fr ] device image-file\n"),
0ab26d
 		program_name);
0ab26d
 	fprintf(stderr, _("       %s -I device image-file\n"), program_name);
0ab26d
 	fprintf(stderr, _("       %s -ra  [  -cfnp  ] [ -o src_offset ] "
0ab26d
@@ -1267,7 +1268,8 @@ static void output_qcow2_meta_data_blocks(ext2_filsys fs, int fd)
0ab26d
 	free_qcow2_image(img);
0ab26d
 }
0ab26d
 
0ab26d
-static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags)
0ab26d
+static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags,
0ab26d
+				 blk64_t superblock)
0ab26d
 {
0ab26d
 	struct process_block_struct	pb;
0ab26d
 	struct ext2_inode		inode;
0ab26d
@@ -1295,6 +1297,22 @@ static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags)
0ab26d
 		}
0ab26d
 	}
0ab26d
 
0ab26d
+	if (superblock) {
0ab26d
+		int j;
0ab26d
+
0ab26d
+		ext2fs_mark_block_bitmap2(meta_block_map, superblock);
0ab26d
+		meta_blocks_count++;
0ab26d
+
0ab26d
+		/*
0ab26d
+		 * Mark the backup superblock descriptors
0ab26d
+		 */
0ab26d
+		for (j = 0; j < fs->desc_blocks; j++) {
0ab26d
+			ext2fs_mark_block_bitmap2(meta_block_map,
0ab26d
+			ext2fs_descriptor_block_loc2(fs, superblock, j));
0ab26d
+		}
0ab26d
+		meta_blocks_count += fs->desc_blocks;
0ab26d
+	}
0ab26d
+
0ab26d
 	mark_table_blocks(fs);
0ab26d
 	if (show_progress)
0ab26d
 		fprintf(stderr, "%s", _("Scanning inodes...\n"));
0ab26d
@@ -1474,6 +1492,8 @@ int main (int argc, char ** argv)
0ab26d
 	int ignore_rw_mount = 0;
0ab26d
 	int check = 0;
0ab26d
 	struct stat st;
0ab26d
+	blk64_t superblock = 0;
0ab26d
+	int blocksize = 0;
0ab26d
 
0ab26d
 #ifdef ENABLE_NLS
0ab26d
 	setlocale(LC_MESSAGES, "");
0ab26d
@@ -1487,8 +1507,14 @@ int main (int argc, char ** argv)
0ab26d
 	if (argc && *argv)
0ab26d
 		program_name = *argv;
0ab26d
 	add_error_table(&et_ext2_error_table);
0ab26d
-	while ((c = getopt(argc, argv, "nrsIQafo:O:pc")) != EOF)
0ab26d
+	while ((c = getopt(argc, argv, "b:B:nrsIQafo:O:pc")) != EOF)
0ab26d
 		switch (c) {
0ab26d
+		case 'b':
0ab26d
+			superblock = strtoull(optarg, NULL, 0);
0ab26d
+			break;
0ab26d
+		case 'B':
0ab26d
+			blocksize = strtoul(optarg, NULL, 0);
0ab26d
+			break;
0ab26d
 		case 'I':
0ab26d
 			flags |= E2IMAGE_INSTALL_FLAG;
0ab26d
 			break;
0ab26d
@@ -1540,6 +1566,11 @@ int main (int argc, char ** argv)
0ab26d
 						 "with raw or QCOW2 images."));
0ab26d
 		exit(1);
0ab26d
 	}
0ab26d
+	if (superblock && !img_type) {
0ab26d
+		com_err(program_name, 0, "%s", _("-b option can only be used "
0ab26d
+						 "with raw or QCOW2 images."));
0ab26d
+		exit(1);
0ab26d
+	}
0ab26d
 	if ((source_offset || dest_offset) && img_type != E2IMAGE_RAW) {
0ab26d
 		com_err(program_name, 0, "%s",
0ab26d
 			_("Offsets are only allowed with raw images."));
0ab26d
@@ -1590,8 +1621,8 @@ int main (int argc, char ** argv)
0ab26d
 		}
0ab26d
 	}
0ab26d
 	sprintf(offset_opt, "offset=%llu", source_offset);
0ab26d
-	retval = ext2fs_open2(device_name, offset_opt, open_flag, 0, 0,
0ab26d
-			      unix_io_manager, &fs);
0ab26d
+	retval = ext2fs_open2(device_name, offset_opt, open_flag,
0ab26d
+			      superblock, blocksize, unix_io_manager, &fs);
0ab26d
         if (retval) {
0ab26d
 		com_err (program_name, retval, _("while trying to open %s"),
0ab26d
 			 device_name);
0ab26d
@@ -1681,7 +1712,7 @@ skip_device:
0ab26d
 		exit(1);
0ab26d
 	}
0ab26d
 	if (img_type)
0ab26d
-		write_raw_image_file(fs, fd, img_type, flags);
0ab26d
+		write_raw_image_file(fs, fd, img_type, flags, superblock);
0ab26d
 	else
0ab26d
 		write_image_file(fs, fd);
0ab26d
 
0ab26d
-- 
0ab26d
2.20.1
0ab26d