I have some path I have to access, which is the result of mounting.
I would like the mounting to be automatic, via a script, and I want that script to run just before an error is thrown from not being able to access the path.
For example, assume the script is
echo scripting!
mkdir -p /non_existing_path
and I want it to run when trying to access (in any way) to the path /non_existing_path
.
So when I do for example
cd /non_existing_path
or
touch /non_existing_path/my_file.txt
It would always succeed, with the output scripting!
. In reality, the script would be more elaborated than that.
Is this possible at all?
CodePudding user response:
Yes, the important case is that 3rd parties (such as a new C program, command line, or other scripts) that would call for example cd should also be affected, and a call by them to cd as they normally would, should invoke the hooked script beforehand.
Out of kernel:
Write a fuse filesystem that would mount on top of other filesystem, that would upon open()
syscall run fork() execve()
a custom script.
In kernel:
Write kernel filesystem that would expose /proc/some/interface
and would create a filesystem "on-top" of underlying existing handle. Such kernel module would execute a special command upon open()
system call and forward all others. In open()
system call, the kernel would write some data to /proc/some/interface
and wait for an answer. After receiving the answer, open()
syscall would continue.
Write a user-space demon that would for example poll()
on events on a /proc/some/interface
waiting for events, then read()
the events, parse them and execute custom script. Then after the script completion, it would write to /proc/some/interface
on the same file descriptor to notify the kernel module that the operation has compleated.
CodePudding user response:
Why don't you use autofs?
autofs is a program for automatically mounting directories on an as-needed basis.
https://help.ubuntu.com/community/Autofs
CodePudding user response:
Not sure I understand.
- Do you want the script to run even if the path is not accessible?
- Do you want the script to run only if the path is not accessible?
- Is the script supposed to mount the "not accessible" path?
In any case, I think you should just use an if else
statement
The script would look like:
#!/bin/bash
if [ -d "/non_existing_path" ]
then
bash $1
else
bash $1
mkdir -p /non_existing_path
fi
Lets assume this script's name is "myScript.sh" and the external script's name is "extScript.sh". You would call:
bash myScript.sh /home/user/extScript.sh
This script will check if path exist.
If yes, execute bash /home/user/extScript.sh
If no, execute bash /home/user/extScript.sh
and mkdir...
Again, I'm not sure to get you goal, but you can adapt it to your needs.