diff mbox series

[LEDE-DEV,mountd] mount: don't mount device that is already mounted

Message ID 20180208173416.4585-1-zajec5@gmail.com
State Accepted
Delegated to: Rafał Miłecki
Headers show
Series [LEDE-DEV,mountd] mount: don't mount device that is already mounted | expand

Commit Message

Rafał Miłecki Feb. 8, 2018, 5:34 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

It may happen that on mountd start some devices are already mounted.
This could due to killing previous mountd instance or just a crash. In
such case device shouldn't get remounted but added to the list with a
mounted flag set. That will make mountd monitor it and take needed steps
when it disappears.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 mount.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Comments

Rafał Miłecki Feb. 9, 2018, 8:57 a.m. UTC | #1
On 8 February 2018 at 18:34, Rafał Miłecki <zajec5@gmail.com> wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
>
> It may happen that on mountd start some devices are already mounted.
> This could due to killing previous mountd instance or just a crash. In
> such case device shouldn't get remounted but added to the list with a
> mounted flag set. That will make mountd monitor it and take needed steps
> when it disappears.

Uh, this may not exactly work as expected.

Even if device is mounted, e.g. to /var/run/mountd/sda1 mountd on start does:
/bin/mount -t autofs -o (...) /tmp/run/mountd/
which causes /var/run/mountd/sda1 to /disappear/.

I've to think about it for a bit more.
diff mbox series

Patch

diff --git a/mount.c b/mount.c
index f995745..d7aa27c 100644
--- a/mount.c
+++ b/mount.c
@@ -138,7 +138,7 @@  static void mount_add_list(char *name, char *dev, char *serial,
 	char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
 {
 	struct mount *mount;
-	char tmp[64], tmp2[64];
+	char tmp[64], run_mount_path[64];
 
 	mount  = malloc(sizeof(struct mount));
 	INIT_LIST_HEAD(&mount->list);
@@ -154,12 +154,18 @@  static void mount_add_list(char *name, char *dev, char *serial,
 	mount->mounted = 0;
 	mount->fs = fs;
 	list_add(&mount->list, &mounts);
+
+	snprintf(run_mount_path, 64, "/tmp/run/mountd/%s", dev);
+	if (is_mounted(NULL, run_mount_path)) {
+		log_printf("mount point %s already exists!\n", run_mount_path);
+		mount->mounted = 1;
+		return;
+	}
 	if (!mount->ignore)
 	{
 		log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
 		snprintf(tmp, 64, "%s%s", uci_path, name);
-		snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
-		symlink(tmp2, tmp);
+		symlink(run_mount_path, tmp);
 		if (!mount_new("/tmp/run/mountd/", dev))
 			system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
 	}