diff mbox series

[OpenWrt-Devel] fstools: add a hook before mounting the overlay

Message ID 1570624897-16993-1-git-send-email-alin.nastac@gmail.com
State Changes Requested
Delegated to: John Crispin
Headers show
Series [OpenWrt-Devel] fstools: add a hook before mounting the overlay | expand

Commit Message

Alin Năstac Oct. 9, 2019, 12:41 p.m. UTC
Scripts located in the directory /lib/mount_root will be executed
before mounting the overlay.

Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
---
 libfstools/hook.h    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libfstools/overlay.c |  3 ++-
 2 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 libfstools/hook.h

Comments

John Crispin Oct. 9, 2019, 12:59 p.m. UTC | #1
On 09/10/2019 14:41, Alin Nastac wrote:
> Scripts located in the directory /lib/mount_root will be executed
> before mounting the overlay.
>
> Signed-off-by: Alin Nastac <alin.nastac@gmail.com>

Hi,

should it not be /etc/mount_root.d/ ? what do you need this for if I may 
ask ?

further comments inline ...

     John


> ---
>   libfstools/hook.h    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>   libfstools/overlay.c |  3 ++-
>   2 files changed, 53 insertions(+), 1 deletion(-)
>   create mode 100644 libfstools/hook.h
>
> diff --git a/libfstools/hook.h b/libfstools/hook.h
> new file mode 100644
> index 0000000..76ee9d0
> --- /dev/null
> +++ b/libfstools/hook.h
> @@ -0,0 +1,51 @@
> +#ifndef _HOOK_H
> +#define _HOOK_H
> +
> +#include <sys/types.h>
> +#include <sys/wait.h>
> +
> +static inline int hook_execute(const char *path)
this should not be in a header file nor should it be inline
> +{
> +	DIR *dir;
> +	struct dirent *dent;
> +	char script[256];
> +	pid_t pid;
> +
> +	ULOG_INFO("executing script in %s\n", path);
> +
> +	if ((dir = opendir(path)) == NULL) {
> +		ULOG_INFO("cannot open %s (%s)\n", path, strerror(errno));
> +		return 0;
> +	}
> +
> +	while ((dent = readdir(dir)) != NULL) {
> +		struct stat st;
> +		int wstatus;
> +
> +		snprintf(script, sizeof(script), "%s/%s", path, dent->d_name);
> +		if (stat(script, &st))
> +			continue;
> +		if (!S_ISREG(st.st_mode))
> +			continue;
> +		ULOG_INFO("%s\n", script);
     you should consider using runqueue API here
> +		pid = fork();
> +		if (!pid) {
> +			char *cmd[] = {script, NULL};
> +
> +			execvp(cmd[0], cmd);
> +			ULOG_ERR("Failed to execute %s\n", script);
> +			exit(-1);
> +		}
> +		if (pid <= 0) {
> +			ULOG_INFO("Failed to fork() for %s\n", script);
> +			continue;
> +		}
> +		waitpid(pid, &wstatus, 0);
> +	}
> +
> +	closedir(dir);
> +
> +	return 0;
> +}
> +
> +#endif
> diff --git a/libfstools/overlay.c b/libfstools/overlay.c
> index 14214a3..10a16b5 100644
> --- a/libfstools/overlay.c
> +++ b/libfstools/overlay.c
> @@ -29,6 +29,7 @@
>   
>   #include "libfstools.h"
>   #include "volume.h"
> +#include "hook.h"
>   
>   #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
>   
> @@ -439,7 +440,7 @@ int mount_overlay(struct volume *v)
>   
>   	fs_name = overlay_fs_name(volume_identify(v));
>   	ULOG_INFO("switching to %s overlay\n", fs_name);
> -	if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
> +	if (mount_move("/tmp", "", "/overlay") || hook_execute("/lib/mount_root") || fopivot("/overlay", "/rom")) {
>   		ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
>   		return ramoverlay();
>   	}
John Crispin Oct. 9, 2019, 2:41 p.m. UTC | #2
On 09/10/2019 16:34, Alin Năstac wrote:
> On Wed, Oct 9, 2019 at 2:59 PM John Crispin <john@phrozen.org> wrote:
>>
>> On 09/10/2019 14:41, Alin Nastac wrote:
>>> Scripts located in the directory /lib/mount_root will be executed
>>> before mounting the overlay.
>>>
>>> Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
>> Hi,
>>
>> should it not be /etc/mount_root.d/ ? what do you need this for if I may
>> ask ?
>>
>> further comments inline ...
>>
>>       John
>>
> Hi John,
>
> My target is dual bank and I need to copy parts of the customization
> from the old bank after upgrade.

please dont remove the CC tot he mailing list

I dont understamd this part, should sysupgrade not be able to handle 
this for you ?

> To resume your observations:
>   - scripts must be relocated to /etc/mount_root.d/
>   - use runqueue API
>   - implement the necessary functions in overlay.c
> Would that be OK? If so, I will be back with a 2nd version of this patch.

I would first like to understand the use-case

     John

>
> Alin
Alin Năstac Oct. 9, 2019, 2:52 p.m. UTC | #3
On Wed, Oct 9, 2019 at 4:41 PM John Crispin <john@phrozen.org> wrote:
>
>
> On 09/10/2019 16:34, Alin Năstac wrote:
> > On Wed, Oct 9, 2019 at 2:59 PM John Crispin <john@phrozen.org> wrote:
> >>
> >> On 09/10/2019 14:41, Alin Nastac wrote:
> >>> Scripts located in the directory /lib/mount_root will be executed
> >>> before mounting the overlay.
> >>>
> >>> Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
> >> Hi,
> >>
> >> should it not be /etc/mount_root.d/ ? what do you need this for if I may
> >> ask ?
> >>
> >> further comments inline ...
> >>
> >>       John
> >>
> > Hi John,
> >
> > My target is dual bank and I need to copy parts of the customization
> > from the old bank after upgrade.
>
> please dont remove the CC tot he mailing list
Sorry, I pushed the wrong reply button.

> I dont understamd this part, should sysupgrade not be able to handle
> this for you ?

Well, it is not that straightforward as saving & restoring the old
configuration files. Only parts of the UCI configuration must be
migrated. For instance, UCI option a.b.c must be copied from the old
image, but a.b.d must be reset to the value found in the new image.

> > To resume your observations:
> >   - scripts must be relocated to /etc/mount_root.d/
> >   - use runqueue API
> >   - implement the necessary functions in overlay.c
> > Would that be OK? If so, I will be back with a 2nd version of this patch.
>
> I would first like to understand the use-case
>
>      John
>
> >
> > Alin
Alin Năstac Oct. 10, 2019, 11:02 a.m. UTC | #4
On Wed, Oct 9, 2019 at 4:52 PM Alin Năstac <alin.nastac@gmail.com> wrote:
>
> On Wed, Oct 9, 2019 at 4:41 PM John Crispin <john@phrozen.org> wrote:
> >
> >
> > On 09/10/2019 16:34, Alin Năstac wrote:
> > > On Wed, Oct 9, 2019 at 2:59 PM John Crispin <john@phrozen.org> wrote:
> > >>
> > >> On 09/10/2019 14:41, Alin Nastac wrote:
> > >>> Scripts located in the directory /lib/mount_root will be executed
> > >>> before mounting the overlay.
> > >>>
> > >>> Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
> > >> Hi,
> > >>
> > >> should it not be /etc/mount_root.d/ ? what do you need this for if I may
> > >> ask ?
> > >>
> > >> further comments inline ...
> > >>
> > >>       John
> > >>
> > > Hi John,
> > >
> > > My target is dual bank and I need to copy parts of the customization
> > > from the old bank after upgrade.
> >
> > please dont remove the CC tot he mailing list
> Sorry, I pushed the wrong reply button.
>
> > I dont understamd this part, should sysupgrade not be able to handle
> > this for you ?
>
> Well, it is not that straightforward as saving & restoring the old
> configuration files. Only parts of the UCI configuration must be
> migrated. For instance, UCI option a.b.c must be copied from the old
> image, but a.b.d must be reset to the value found in the new image.
>

Is this use case a good enough reason to implement this feature?

> > > To resume your observations:
> > >   - scripts must be relocated to /etc/mount_root.d/
> > >   - use runqueue API
> > >   - implement the necessary functions in overlay.c
> > > Would that be OK? If so, I will be back with a 2nd version of this patch.
> >
> > I would first like to understand the use-case
> >
> >      John

I've tried to use runqueue API... This API is designed to be used in
conjunction with uloop, which is designed to be used within daemons.
mount_root is not a daemon and it does not have a main loop, so IMHO
it does not fit well in this place.
John Crispin Oct. 11, 2019, 6:51 a.m. UTC | #5
On 10/10/2019 13:02, Alin Năstac wrote:
> On Wed, Oct 9, 2019 at 4:52 PM Alin Năstac <alin.nastac@gmail.com> wrote:
>> On Wed, Oct 9, 2019 at 4:41 PM John Crispin <john@phrozen.org> wrote:
>>>
>>> On 09/10/2019 16:34, Alin Năstac wrote:
>>>> On Wed, Oct 9, 2019 at 2:59 PM John Crispin <john@phrozen.org> wrote:
>>>>> On 09/10/2019 14:41, Alin Nastac wrote:
>>>>>> Scripts located in the directory /lib/mount_root will be executed
>>>>>> before mounting the overlay.
>>>>>>
>>>>>> Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
>>>>> Hi,
>>>>>
>>>>> should it not be /etc/mount_root.d/ ? what do you need this for if I may
>>>>> ask ?
>>>>>
>>>>> further comments inline ...
>>>>>
>>>>>        John
>>>>>
>>>> Hi John,
>>>>
>>>> My target is dual bank and I need to copy parts of the customization
>>>> from the old bank after upgrade.
>>> please dont remove the CC tot he mailing list
>> Sorry, I pushed the wrong reply button.
>>
>>> I dont understamd this part, should sysupgrade not be able to handle
>>> this for you ?
>> Well, it is not that straightforward as saving & restoring the old
>> configuration files. Only parts of the UCI configuration must be
>> migrated. For instance, UCI option a.b.c must be copied from the old
>> image, but a.b.d must be reset to the value found in the new image.
>>
> Is this use case a good enough reason to implement this feature?
>
>>>> To resume your observations:
>>>>    - scripts must be relocated to /etc/mount_root.d/
>>>>    - use runqueue API
>>>>    - implement the necessary functions in overlay.c
>>>> Would that be OK? If so, I will be back with a 2nd version of this patch.
>>> I would first like to understand the use-case
>>>
>>>       John
> I've tried to use runqueue API... This API is designed to be used in
> conjunction with uloop, which is designed to be used within daemons.
> mount_root is not a daemon and it does not have a main loop, so IMHO
> it does not fit well in this place.

Of course you are right about runqueue, I dont quite follow the actual 
use case but the feature wont harm i guess

     John
diff mbox series

Patch

diff --git a/libfstools/hook.h b/libfstools/hook.h
new file mode 100644
index 0000000..76ee9d0
--- /dev/null
+++ b/libfstools/hook.h
@@ -0,0 +1,51 @@ 
+#ifndef _HOOK_H
+#define _HOOK_H
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+static inline int hook_execute(const char *path)
+{
+	DIR *dir;
+	struct dirent *dent;
+	char script[256];
+	pid_t pid;
+
+	ULOG_INFO("executing script in %s\n", path);
+
+	if ((dir = opendir(path)) == NULL) {
+		ULOG_INFO("cannot open %s (%s)\n", path, strerror(errno));
+		return 0;
+	}
+
+	while ((dent = readdir(dir)) != NULL) {
+		struct stat st;
+		int wstatus;
+
+		snprintf(script, sizeof(script), "%s/%s", path, dent->d_name);
+		if (stat(script, &st))
+			continue;
+		if (!S_ISREG(st.st_mode))
+			continue;
+		ULOG_INFO("%s\n", script);
+		pid = fork();
+		if (!pid) {
+			char *cmd[] = {script, NULL};
+
+			execvp(cmd[0], cmd);
+			ULOG_ERR("Failed to execute %s\n", script);
+			exit(-1);
+		}
+		if (pid <= 0) {
+			ULOG_INFO("Failed to fork() for %s\n", script);
+			continue;
+		}
+		waitpid(pid, &wstatus, 0);
+	}
+
+	closedir(dir);
+
+	return 0;
+}
+
+#endif
diff --git a/libfstools/overlay.c b/libfstools/overlay.c
index 14214a3..10a16b5 100644
--- a/libfstools/overlay.c
+++ b/libfstools/overlay.c
@@ -29,6 +29,7 @@ 
 
 #include "libfstools.h"
 #include "volume.h"
+#include "hook.h"
 
 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
 
@@ -439,7 +440,7 @@  int mount_overlay(struct volume *v)
 
 	fs_name = overlay_fs_name(volume_identify(v));
 	ULOG_INFO("switching to %s overlay\n", fs_name);
-	if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
+	if (mount_move("/tmp", "", "/overlay") || hook_execute("/lib/mount_root") || fopivot("/overlay", "/rom")) {
 		ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
 		return ramoverlay();
 	}