Message ID | 20181205213549.13138-1-zajec5@gmail.com |
---|---|
State | Changes Requested |
Delegated to: | John Crispin |
Headers | show |
Series | [OpenWrt-Devel] block: generate hotplug.d mount events | expand |
On 2018-12-05 22:35, Rafał Miłecki wrote: > From: Rafał Miłecki <rafal@milecki.pl> > > This is what was implemented in mountd and what some scripts used to > use. It's a pretty generic solution for managing software that may use > e.g. USB storage. > > Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Forgot to say, it requires [PATCH fstools] blockd: don't unmount device when removing it from the list to work correctly. Without above patch blockd unmounts device, block can't find a mount point and gives up.
On 05/12/18 22:35, Rafał Miłecki wrote: > From: Rafał Miłecki <rafal@milecki.pl> > > This is what was implemented in mountd and what some scripts used to > use. It's a pretty generic solution for managing software that may use > e.g. USB storage. Could you explain a bit more about what is this doing instead of just mentioning mountd? There is pretty scarce info about mountd. -Alberto > > Signed-off-by: Rafał Miłecki <rafal@milecki.pl> > --- > block.c | 29 +++++++++++++++++++++++++++++ > 1 file changed, 29 insertions(+) > > diff --git a/block.c b/block.c > index 46050b4..fe63fb0 100644 > --- a/block.c > +++ b/block.c > @@ -880,6 +880,31 @@ static int exec_mount(const char *source, const char *target, > return err; > } > > +static void hotplug_call_mount(const char *action, const char *device) > +{ > + pid_t pid; > + > + pid = fork(); > + if (!pid) { > + char * const argv[] = { "hotplug-call", "mount", NULL }; > + char actionenv[] = "ACTION=xxxxxx"; > + char deviceenv[32]; > + char *envp[] = { actionenv, deviceenv, NULL }; > + > + snprintf(actionenv, sizeof(actionenv), "ACTION=%s", action); > + snprintf(deviceenv, sizeof(deviceenv), "DEVICE=%s", device); > + > + execve("/sbin/hotplug-call", argv, envp); > + exit(-1); > + } else if (pid > 0) { > + int status; > + > + waitpid(pid, &status, 0); > + if (WEXITSTATUS(status)) > + ULOG_ERR("hotplug-call call failed: %d\n", WEXITSTATUS(status)); > + } > +} > + > static int handle_mount(const char *source, const char *target, > const char *fstype, struct mount *m) > { > @@ -1079,6 +1104,8 @@ static int mount_device(struct probe_info *pr, int type) > > handle_swapfiles(true); > > + hotplug_call_mount("add", device); > + > return 0; > } > > @@ -1091,6 +1118,8 @@ static int umount_device(char *path) > if (!mp) > return -1; > > + hotplug_call_mount("remove", basename(path)); > + > err = umount2(mp, MNT_DETACH); > if (err) > ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,
On Wed, 5 Dec 2018 at 23:30, Alberto Bursi <bobafetthotmail@gmail.com> wrote: > On 05/12/18 22:35, Rafał Miłecki wrote: > > From: Rafał Miłecki <rafal@milecki.pl> > > > > This is what was implemented in mountd and what some scripts used to > > use. It's a pretty generic solution for managing software that may use > > e.g. USB storage. > > > Could you explain a bit more about what is this doing instead of just > mentioning mountd? > > There is pretty scarce info about mountd. This generates following hotplug.d "mount" events: 1) "add" when block device gets mounted 2) "remove" when block device gets unmounted It allows one to put custom scripts/executables in the /etc/hotplug.d/mount/ and react to appearing/disappearing mount points.
diff --git a/block.c b/block.c index 46050b4..fe63fb0 100644 --- a/block.c +++ b/block.c @@ -880,6 +880,31 @@ static int exec_mount(const char *source, const char *target, return err; } +static void hotplug_call_mount(const char *action, const char *device) +{ + pid_t pid; + + pid = fork(); + if (!pid) { + char * const argv[] = { "hotplug-call", "mount", NULL }; + char actionenv[] = "ACTION=xxxxxx"; + char deviceenv[32]; + char *envp[] = { actionenv, deviceenv, NULL }; + + snprintf(actionenv, sizeof(actionenv), "ACTION=%s", action); + snprintf(deviceenv, sizeof(deviceenv), "DEVICE=%s", device); + + execve("/sbin/hotplug-call", argv, envp); + exit(-1); + } else if (pid > 0) { + int status; + + waitpid(pid, &status, 0); + if (WEXITSTATUS(status)) + ULOG_ERR("hotplug-call call failed: %d\n", WEXITSTATUS(status)); + } +} + static int handle_mount(const char *source, const char *target, const char *fstype, struct mount *m) { @@ -1079,6 +1104,8 @@ static int mount_device(struct probe_info *pr, int type) handle_swapfiles(true); + hotplug_call_mount("add", device); + return 0; } @@ -1091,6 +1118,8 @@ static int umount_device(char *path) if (!mp) return -1; + hotplug_call_mount("remove", basename(path)); + err = umount2(mp, MNT_DETACH); if (err) ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,