From 189a1bf3b314a8fc48c29ea1e0287d5cc71021a0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 16 Jan 2019 15:00:07 +0100 Subject: libmount: add support for MS_REMOUNT on --all This patch add to support for remount-all operation to libmount and mount(8). For example: mount --all -o remount,ro -t vfat to remount read-only all VFAT filesystems. Addresses: https://github.com/karelzak/util-linux/issues/589 Signed-off-by: Karel Zak --- sys-utils/mount.8 | 4 ++++ sys-utils/mount.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'sys-utils') diff --git a/sys-utils/mount.8 b/sys-utils/mount.8 index da0ac5b8b..6ef589050 100644 --- a/sys-utils/mount.8 +++ b/sys-utils/mount.8 @@ -580,6 +580,10 @@ mount or btrfs) to detect already mounted filesystems. The kernel table with already mounted filesystems is cached during \fBmount \-\-all\fR. It means that all duplicated fstab entries will be mounted. .sp +The option \fB\-\-all\fR is possible to use for remount operation too. In this +case all filters (\fB\-t\fR and \fB\-O\fR) are applied to the table of already +mounted filesystems. +.sp Note that it is a bad practice to use \fBmount \-a\fR for .I fstab checking. The recommended solution is \fBfindmnt \-\-verify\fR. diff --git a/sys-utils/mount.c b/sys-utils/mount.c index 5e139e896..f4a387b8a 100644 --- a/sys-utils/mount.c +++ b/sys-utils/mount.c @@ -231,6 +231,57 @@ static int mount_all(struct libmnt_context *cxt) return rc; } + +/* + * mount -a -o remount + */ +static int remount_all(struct libmnt_context *cxt) +{ + struct libmnt_iter *itr; + struct libmnt_fs *fs; + int mntrc, ignored, rc = MNT_EX_SUCCESS; + + int nsucc = 0, nerrs = 0; + + itr = mnt_new_iter(MNT_ITER_FORWARD); + if (!itr) { + warn(_("failed to initialize libmount iterator")); + return MNT_EX_SYSERR; + } + + while (mnt_context_next_remount(cxt, itr, &fs, &mntrc, &ignored) == 0) { + + const char *tgt = mnt_fs_get_target(fs); + + if (ignored) { + if (mnt_context_is_verbose(cxt)) + printf(_("%-25s: ignored\n"), tgt); + } else { + if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) { + nsucc++; + + /* Note that MNT_EX_SUCCESS return code does + * not mean that FS has been really mounted + * (e.g. nofail option) */ + if (mnt_context_get_status(cxt) + && mnt_context_is_verbose(cxt)) + printf("%-25s: successfully remounted\n", tgt); + } else + nerrs++; + } + } + + if (nerrs == 0) + rc = MNT_EX_SUCCESS; /* all success */ + else if (nsucc == 0) + rc = MNT_EX_FAIL; /* all failed */ + else + rc = MNT_EX_SOMEOK; /* some success, some failed */ + + mnt_free_iter(itr); + return rc; +} + static void success_message(struct libmnt_context *cxt) { unsigned long mflags = 0; @@ -836,7 +887,10 @@ int main(int argc, char **argv) /* * A) Mount all */ - rc = mount_all(cxt); + if (has_remount_flag(cxt)) + rc = remount_all(cxt); + else + rc = mount_all(cxt); goto done; } else if (argc == 0 && (mnt_context_get_source(cxt) || -- cgit v1.2.3-55-g7522