Patchwork [v2] e2image: Print a warning if running over a mounted filesystem

login
register
mail settings
Submitter Carlos Maiolino
Date Sept. 27, 2013, 3:33 p.m.
Message ID <1380296025-28937-1-git-send-email-cmaiolino@redhat.com>
Download mbox | patch
Permalink /patch/278610/
State Superseded
Headers show

Comments

Carlos Maiolino - Sept. 27, 2013, 3:33 p.m.
Several users use to run e2image over a mounted RW filesystem, providing
inconsistent, useless e2images for debugging purposes.
This patch adds a warning in such cases, notifying the user and also adds a
force option making e2image able to run in such cases.
Also print a warning when not using qcow or raw formats but allows image
creation to proceed, since, such images might be used for metadata backup
purposes.

Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
---
 misc/e2image.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
Eric Sandeen - Sept. 27, 2013, 4:37 p.m.
On 9/27/13 10:33 AM, Carlos Maiolino wrote:
> Several users use to run e2image over a mounted RW filesystem, providing
> inconsistent, useless e2images for debugging purposes.
> This patch adds a warning in such cases, notifying the user and also adds a
> force option making e2image able to run in such cases.
> Also print a warning when not using qcow or raw formats but allows image
> creation to proceed, since, such images might be used for metadata backup
> purposes.
> 
> Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>

manpage updates, please :)

My only other nitpick would be that a comment somewhere before:

+		if (img_type && !ignore_rw_mount) {
+			fprintf(stderr, "Use -f option if you really want to "
+					"do that.\n");

to explain why that condition requires -f, but the other (!img_type)
does not, would be helpful to future code-readers.

Thanks,
-Eric

> ---
>  misc/e2image.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/misc/e2image.c b/misc/e2image.c
> index 885a794..248399d 100644
> --- a/misc/e2image.c
> +++ b/misc/e2image.c
> @@ -87,7 +87,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 [-rsIQaf] device image_file\n"),
>  		program_name);
>  	exit (1);
>  }
> @@ -1252,9 +1252,11 @@ int main (int argc, char ** argv)
>  	int open_flag = EXT2_FLAG_64BITS;
>  	int img_type = 0;
>  	int flags = 0;
> +	int mount_flags = 0;
>  	int qcow2_fd = 0;
>  	int fd = 0;
>  	int ret = 0;
> +	int ignore_rw_mount = 0;
>  	struct stat st;
>  
>  #ifdef ENABLE_NLS
> @@ -1269,7 +1271,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, "rsIQaf")) != EOF)
>  		switch (c) {
>  		case 'I':
>  			flags |= E2IMAGE_INSTALL_FLAG;
> @@ -1290,6 +1292,9 @@ int main (int argc, char ** argv)
>  		case 'a':
>  			all_data = 1;
>  			break;
> +		case 'f':
> +			ignore_rw_mount = 1;
> +			break;
>  		default:
>  			usage();
>  		}
> @@ -1305,6 +1310,21 @@ int main (int argc, char ** argv)
>  	device_name = argv[optind];
>  	image_fn = argv[optind+1];
>  
> +	ext2fs_check_if_mounted(device_name, &mount_flags);
> +
> +	if ((mount_flags & EXT2_MF_MOUNTED) &&
> +	   !(mount_flags & EXT2_MF_READONLY)) {
> +		fprintf(stderr, "\nWarning: Running e2image on a mounted "
> +				"RW filesystem can result in an inconsistent "
> +				"image which will not be useful for "
> +				"debugging purposes.\n");
> +		if (img_type && !ignore_rw_mount) {
> +			fprintf(stderr, "Use -f option if you really want to "
> +					"do that.\n");
> +			exit(1);
> +		}
> +	}
> +
>  	if (flags & E2IMAGE_INSTALL_FLAG) {
>  		install_image(device_name, image_fn, img_type);
>  		exit (0);
> 

--
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
Carlos Maiolino - Sept. 27, 2013, 6:10 p.m.
Hi Eric,

> manpage updates, please :)
> 

I'm writing a manpage patch to talk about it, I'm just waiting until everybody
accepts this patch and I can write a manpage patch to the proper fix (instead of
need to re-write both, patch and manpage).

About the comment, yes, I agree, doing that and sending a V3

Thanks for the review

> My only other nitpick would be that a comment somewhere before:
> 
> +		if (img_type && !ignore_rw_mount) {
> +			fprintf(stderr, "Use -f option if you really want to "
> +					"do that.\n");
> 
> to explain why that condition requires -f, but the other (!img_type)
> does not, would be helpful to future code-readers.
> 
> Thanks,
> -Eric
> 
> > ---
> >  misc/e2image.c | 24 ++++++++++++++++++++++--
> >  1 file changed, 22 insertions(+), 2 deletions(-)
> > 
> > diff --git a/misc/e2image.c b/misc/e2image.c
> > index 885a794..248399d 100644
> > --- a/misc/e2image.c
> > +++ b/misc/e2image.c
> > @@ -87,7 +87,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 [-rsIQaf] device image_file\n"),
> >  		program_name);
> >  	exit (1);
> >  }
> > @@ -1252,9 +1252,11 @@ int main (int argc, char ** argv)
> >  	int open_flag = EXT2_FLAG_64BITS;
> >  	int img_type = 0;
> >  	int flags = 0;
> > +	int mount_flags = 0;
> >  	int qcow2_fd = 0;
> >  	int fd = 0;
> >  	int ret = 0;
> > +	int ignore_rw_mount = 0;
> >  	struct stat st;
> >  
> >  #ifdef ENABLE_NLS
> > @@ -1269,7 +1271,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, "rsIQaf")) != EOF)
> >  		switch (c) {
> >  		case 'I':
> >  			flags |= E2IMAGE_INSTALL_FLAG;
> > @@ -1290,6 +1292,9 @@ int main (int argc, char ** argv)
> >  		case 'a':
> >  			all_data = 1;
> >  			break;
> > +		case 'f':
> > +			ignore_rw_mount = 1;
> > +			break;
> >  		default:
> >  			usage();
> >  		}
> > @@ -1305,6 +1310,21 @@ int main (int argc, char ** argv)
> >  	device_name = argv[optind];
> >  	image_fn = argv[optind+1];
> >  
> > +	ext2fs_check_if_mounted(device_name, &mount_flags);
> > +
> > +	if ((mount_flags & EXT2_MF_MOUNTED) &&
> > +	   !(mount_flags & EXT2_MF_READONLY)) {
> > +		fprintf(stderr, "\nWarning: Running e2image on a mounted "
> > +				"RW filesystem can result in an inconsistent "
> > +				"image which will not be useful for "
> > +				"debugging purposes.\n");
> > +		if (img_type && !ignore_rw_mount) {
> > +			fprintf(stderr, "Use -f option if you really want to "
> > +					"do that.\n");
> > +			exit(1);
> > +		}
> > +	}
> > +
> >  	if (flags & E2IMAGE_INSTALL_FLAG) {
> >  		install_image(device_name, image_fn, img_type);
> >  		exit (0);
> > 
>

Patch

diff --git a/misc/e2image.c b/misc/e2image.c
index 885a794..248399d 100644
--- a/misc/e2image.c
+++ b/misc/e2image.c
@@ -87,7 +87,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 [-rsIQaf] device image_file\n"),
 		program_name);
 	exit (1);
 }
@@ -1252,9 +1252,11 @@  int main (int argc, char ** argv)
 	int open_flag = EXT2_FLAG_64BITS;
 	int img_type = 0;
 	int flags = 0;
+	int mount_flags = 0;
 	int qcow2_fd = 0;
 	int fd = 0;
 	int ret = 0;
+	int ignore_rw_mount = 0;
 	struct stat st;
 
 #ifdef ENABLE_NLS
@@ -1269,7 +1271,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, "rsIQaf")) != EOF)
 		switch (c) {
 		case 'I':
 			flags |= E2IMAGE_INSTALL_FLAG;
@@ -1290,6 +1292,9 @@  int main (int argc, char ** argv)
 		case 'a':
 			all_data = 1;
 			break;
+		case 'f':
+			ignore_rw_mount = 1;
+			break;
 		default:
 			usage();
 		}
@@ -1305,6 +1310,21 @@  int main (int argc, char ** argv)
 	device_name = argv[optind];
 	image_fn = argv[optind+1];
 
+	ext2fs_check_if_mounted(device_name, &mount_flags);
+
+	if ((mount_flags & EXT2_MF_MOUNTED) &&
+	   !(mount_flags & EXT2_MF_READONLY)) {
+		fprintf(stderr, "\nWarning: Running e2image on a mounted "
+				"RW filesystem can result in an inconsistent "
+				"image which will not be useful for "
+				"debugging purposes.\n");
+		if (img_type && !ignore_rw_mount) {
+			fprintf(stderr, "Use -f option if you really want to "
+					"do that.\n");
+			exit(1);
+		}
+	}
+
 	if (flags & E2IMAGE_INSTALL_FLAG) {
 		install_image(device_name, image_fn, img_type);
 		exit (0);