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

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