From patchwork Fri May 25 12:42:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Blagodarenko X-Patchwork-Id: 920490 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="G/h7YUHA"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40smDv4TB8z9s02 for ; Fri, 25 May 2018 22:45:19 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934059AbeEYMoY (ORCPT ); Fri, 25 May 2018 08:44:24 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:56011 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933169AbeEYMoW (ORCPT ); Fri, 25 May 2018 08:44:22 -0400 Received: by mail-wm0-f67.google.com with SMTP id a8-v6so14027864wmg.5 for ; Fri, 25 May 2018 05:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=k5m9wsae9BEtuvATCUxbrBm15iXMuStSNKPQkyTPXPM=; b=G/h7YUHAWDwYfIJKnYBP9LPpTJvOg39+P7iSMWV8fDXuudGdjR0Uy9GeMWEabB6sQa tOCtzqfncyV7giDCU+CzQ3GYzv7Nee+0qUD+VpdKrfW8EUMkncfjXv/RffLEGxNwGOYR uqfRToyEMoQmjYmiHKl+SNPJXCwPZ7v3OAXFu2qY0HIAmYRd9VvgcM+x6IReVem1XdDu wDGHAteVKD/MKRghp9XkiIW3kiTCCIwEkfDCwLinMPZcL4VwxsM2D3Lr5OBQRsohJ/9t 3PzO8AGm53+jd8tSot/ZZ7Y+v3WE89Etbm510gHY01hh/boGdZDmfr6+vUBlLDST2wN2 HAIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=k5m9wsae9BEtuvATCUxbrBm15iXMuStSNKPQkyTPXPM=; b=FgJ9XwEKRTPdR2g3wJIjl7cjvd7cM4IoxPpOlREa5PaffSZ6Byqcheu8PsORfyvY63 lpFRRInX4TAqGjUoZ0LhvEHxBaPJbj6/8QJeaQknP3m3eYYROzNPll0NwZ/27q4P9tGk ZLJJ+MPhBLC15yU0Yz4scJ7PgrHtL8NWKO37Rb839h/cCt+0pp5Jk++2OX8B76LxlHfU y7up0wBT3RLcu11jWwJZ5WIoQQORBNGnXFFyaO0Sger9UfxYxitPYaJiJm8C6f0dUjGA dfWcqgROLQhIOunImnPPG5NjsNiTIt+Tr2d3Ok7OqVjrA5X3pdT3bGnLbmMFAy8eUXYE +48g== X-Gm-Message-State: ALKqPwc9KYzIEwpss8AHxhBsrDLksqxVLz3FzyXFbo21xptiYtOHpOo4 EPMhNJ+G0vymf4Pl+noS2ed1Xw== X-Google-Smtp-Source: AB8JxZoCLqKCsQHtrpQt7mVE1K3tg9yQeX04TECtP/+H3WZDWD4VsiqclEpwffx2aaWL255z7pi/oQ== X-Received: by 2002:a2e:9218:: with SMTP id k24-v6mr1628782ljg.70.1527252261165; Fri, 25 May 2018 05:44:21 -0700 (PDT) Received: from C02TN4C6HTD6.lan ([91.105.193.141]) by smtp.gmail.com with ESMTPSA id e7-v6sm4392662ljj.95.2018.05.25.05.44.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 05:44:20 -0700 (PDT) From: Artem Blagodarenko To: linux-ext4@vger.kernel.org Subject: [PATCH v4 2/4] e2image: add -b option to use supperblock backup Date: Fri, 25 May 2018 15:42:23 +0300 Message-Id: <20180525124225.31089-3-artem.blagodarenko@gmail.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180525124225.31089-1-artem.blagodarenko@gmail.com> References: <20180525124225.31089-1-artem.blagodarenko@gmail.com> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org e2image has no ability to use superblock backup to copy metadata. This feature can be useful if someone wants to make partition image and fix it using e2fsck utility. New -b option allows to pass superblock number, like e2fsck utility do. e2image doesn't change primary superblock and store is as is, so it can be fixed using e2fsck latter. Signed-off-by: Artem Blagodarenko Reviewed-by: Andreas Dilger --- misc/e2image.8.in | 33 +++++++++++++++++++++++++++++++++ misc/e2image.c | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/misc/e2image.8.in b/misc/e2image.8.in index a7bfdf24..c8dadc9d 100644 --- a/misc/e2image.8.in +++ b/misc/e2image.8.in @@ -12,6 +12,15 @@ e2image \- Save critical ext2/ext3/ext4 filesystem metadata to a file ] [ .B \-f +.B \-b +.I superblock +] +[ +.B \-B +.I blocksize +] +[ +.B \-fr ] .I device .I image-file @@ -167,6 +176,22 @@ the option will prevent analysis of problems related to hash-tree indexed directories. .PP +Option +.B \-b +.I superblock +can be used to get image from partition with broken primary superblock. +The partition is copied as is including broken primary superblock. +.PP +Option +.B \-B +.I blocksize +can be used to set superblock block size. Normally, e2fsck will search +for the superblock at various different block sizes in an attempt to find +the appropriate blocksize. This search can be fooled in some cases. This +option forces e2fsck to only try locating the superblock at a particular +blocksize. If the superblock is not found, e2fsck will terminate with a +fatal error. +.PP Note that this will work even if you substitute "/dev/hda1" for another raw disk image, or QCOW2 image previously created by .BR e2image . @@ -217,6 +242,14 @@ This can be useful to write a qcow2 image containing all data to a sparse image file where it can be loop mounted, or to a disk partition. Note that this may not work with qcow2 images not generated by e2image. .PP +Options +.B \-b +.I superblock +and +.B \-B +.I blocksize +can be used same way as for raw images. +.PP .SH INCLUDING DATA Normally .B e2image diff --git a/misc/e2image.c b/misc/e2image.c index 83ae6335..fa5f3b7a 100644 --- a/misc/e2image.c +++ b/misc/e2image.c @@ -104,7 +104,8 @@ static int get_bits_from_size(size_t size) static void usage(void) { - fprintf(stderr, _("Usage: %s [ -r|Q ] [ -f ] device image-file\n"), + fprintf(stderr, _("Usage: %s [ -r|Q ] [ -f ] [ -b superblock ] [ -B blocksize]" + "[ -fr ] device image-file\n"), program_name); fprintf(stderr, _(" %s -I device image-file\n"), program_name); fprintf(stderr, _(" %s -ra [ -cfnp ] [ -o src_offset ] " @@ -1256,7 +1257,8 @@ static void output_qcow2_meta_data_blocks(ext2_filsys fs, int fd) free_qcow2_image(img); } -static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags) +static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags, + blk64_t superblock) { struct process_block_struct pb; struct ext2_inode inode; @@ -1284,6 +1286,22 @@ static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags) } } + if (superblock) { + int j; + + ext2fs_mark_block_bitmap2(meta_block_map, superblock); + meta_blocks_count++; + + /* + * Mark the backup superblock descriptors + */ + for (j = 0; j < fs->desc_blocks; j++) { + ext2fs_mark_block_bitmap2(meta_block_map, + ext2fs_descriptor_block_loc2(fs, superblock, j)); + } + meta_blocks_count += fs->desc_blocks; + } + mark_table_blocks(fs); if (show_progress) fprintf(stderr, "%s", _("Scanning inodes...\n")); @@ -1463,6 +1481,8 @@ int main (int argc, char ** argv) int ignore_rw_mount = 0; int check = 0; struct stat st; + blk64_t superblock = 0; + int blocksize = 0; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); @@ -1476,8 +1496,14 @@ int main (int argc, char ** argv) if (argc && *argv) program_name = *argv; add_error_table(&et_ext2_error_table); - while ((c = getopt(argc, argv, "nrsIQafo:O:pc")) != EOF) + while ((c = getopt(argc, argv, "b:B:nrsIQafo:O:pc")) != EOF) switch (c) { + case 'b': + superblock = strtoull(optarg, NULL, 0); + break; + case 'B': + blocksize = strtoul(optarg, NULL, 0); + break; case 'I': flags |= E2IMAGE_INSTALL_FLAG; break; @@ -1529,6 +1555,11 @@ int main (int argc, char ** argv) "with raw or QCOW2 images.")); exit(1); } + if (superblock && !img_type) { + com_err(program_name, 0, "%s", _("-b option can only be used " + "with raw or QCOW2 images.")); + exit(1); + } if ((source_offset || dest_offset) && img_type != E2IMAGE_RAW) { com_err(program_name, 0, "%s", _("Offsets are only allowed with raw images.")); @@ -1579,8 +1610,9 @@ int main (int argc, char ** argv) } } sprintf(offset_opt, "offset=%llu", source_offset); - retval = ext2fs_open2(device_name, offset_opt, open_flag, 0, 0, - unix_io_manager, &fs); + retval = ext2fs_open2(device_name, offset_opt, open_flag, + superblock, blocksize, unix_io_manager, + &fs); if (retval) { com_err (program_name, retval, _("while trying to open %s"), device_name); @@ -1665,7 +1697,7 @@ skip_device: exit(1); } if (img_type) - write_raw_image_file(fs, fd, img_type, flags); + write_raw_image_file(fs, fd, img_type, flags, superblock); else write_image_file(fs, fd);