From: Dmitriy Chestnykh Date: Mon, 4 Jan 2021 11:17:57 +0000 (+0100) Subject: cfdisk: Implemented cfdisk's opening in read-only mode X-Git-Tag: v2.37-rc1~212 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d182490e3e6d0f23a774b73eb3805d9e8668e07;p=thirdparty%2Futil-linux.git cfdisk: Implemented cfdisk's opening in read-only mode [kzak@redhat.com: - clean up the patch - add note "Changes will remain in memory only."] Addresses: https://github.com/karelzak/util-linux/issues/1209 Addresses: https://github.com/karelzak/util-linux/pull/1213 Signed-off-by: Karel Zak --- diff --git a/bash-completion/cfdisk b/bash-completion/cfdisk index 4da6fb8b5b..3772ba4307 100644 --- a/bash-completion/cfdisk +++ b/bash-completion/cfdisk @@ -19,6 +19,7 @@ _cfdisk_module() --zero --lock --help + --read-only --version" COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) return 0 diff --git a/disk-utils/cfdisk.8 b/disk-utils/cfdisk.8 index a61aad71f3..dcceef13a6 100644 --- a/disk-utils/cfdisk.8 +++ b/disk-utils/cfdisk.8 @@ -69,6 +69,9 @@ environment variable \fB$LOCK_BLOCK_DEVICE\fR. The default is not to use any lock at all, but it's recommended to avoid collisions with udevd or other tools. .TP +.BR \-r , " \-\-read-only" +Forced open in read-only mode. +.TP .BR \-V , " \-\-version" Display version information and exit. .TP diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index d96b6e9e03..b549fe95ba 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -2568,7 +2568,7 @@ static int ui_run(struct cfdisk *cf) ui_draw_extra(cf); if (fdisk_is_readonly(cf->cxt)) - ui_warnx(_("Device is open in read-only mode.")); + ui_warnx(_("Device is open in read-only mode. Changes will remain in memory only.")); else if (cf->wrong_order) ui_info(_("Note that partition table entries are not in disk order now.")); @@ -2667,6 +2667,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -z, --zero start with zeroed partition table\n"), out); fprintf(out, _(" --lock[=] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); + fputs(_(" -r, --read-only forced open cfdisk in read-only mode\n"), out); fputs(USAGE_SEPARATOR, out); printf(USAGE_HELP_OPTIONS(26)); @@ -2679,6 +2680,7 @@ int main(int argc, char *argv[]) { const char *diskpath = NULL, *lockmode = NULL; int rc, c, colormode = UL_COLORMODE_UNDEF; + int read_only = 0; struct cfdisk _cf = { .lines_idx = 0 }, *cf = &_cf; enum { @@ -2690,6 +2692,7 @@ int main(int argc, char *argv[]) { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, { "zero", no_argument, NULL, 'z' }, + { "read-only", no_argument, NULL, 'r' }, { NULL, 0, NULL, 0 }, }; @@ -2698,7 +2701,7 @@ int main(int argc, char *argv[]) textdomain(PACKAGE); close_stdout_atexit(); - while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) { + while((c = getopt_long(argc, argv, "L::hVzr", longopts, NULL)) != -1) { switch(c) { case 'h': usage(); @@ -2709,6 +2712,9 @@ int main(int argc, char *argv[]) colormode = colormode_or_err(optarg, _("unsupported color mode")); break; + case 'r': + read_only = 1; + break; case 'V': print_version(EXIT_SUCCESS); case 'z': @@ -2752,8 +2758,8 @@ int main(int argc, char *argv[]) } else diskpath = argv[optind]; - rc = fdisk_assign_device(cf->cxt, diskpath, 0); - if (rc == -EACCES) + rc = fdisk_assign_device(cf->cxt, diskpath, read_only); + if (rc == -EACCES && read_only == 0) rc = fdisk_assign_device(cf->cxt, diskpath, 1); if (rc != 0) err(EXIT_FAILURE, _("cannot open %s"), diskpath);