I have raspberry pi and have microsd with raspbian from which the system is loading and the HDD on which I write logs. I encountered such a problem that sometimes the disk falls off and the logs begin to be written to the microsd in the same directory where the HDD was mounted. How can I prohibit writing logs to a USB flash drive, and write only when the drive is mounted? I write logs very often and there is no possibility to check whether a disk is mounted at each iteration.
If the logging isn't performed by
root, it would be sufficient to make the mount point read only, for that use
chmod -R 0444 /mountpoint. However,
root would still be able to write into that directory, so in that case you might need to use
chattr -i /mountpoint, which would make it completely unwritable.
These attributes won't carry over after mounting, since the permissions are inherited from the parameters set in the
Keep in mind that by restricting writing into the mount point directory when nothing's mounted there would present a different problem: how would your logging tools handle denied write permissions?