Search code examples
javalinuxmount

How can you mount a device using just java?


Okay, I realize this might not be possible ...

I have a device that runs linux, and it will likely not have a user interface that is visible. It will have a USB port that is open ... I want to be able to write java to allow the overall system to detect when a device is plugged into the USB port and then mount the device.

I know how to do this from a command line, but it requires "sudo" to fork a new process with elevated privilege to perform the task. I don't know how to write a java program that can get around (yes, I want to get around) the security protection of mount.

runtime.exec() doesn't really do what I am hoping to accomplish. Is this a "can't be done in Java" problem?

I'm hoping to do this without resorting to external executable code.


Solution

  • You can't get around the security protection of mount. The protection is not in mount. The protection is in the kernel.

    If by chance you find a way and publish it, it will get removed.

    If you have control of the OS you are running on, you can add the option user to the mountpoint in /etc/fstab. After doing so, "mount /path" and "umount /path" work from runtime.exec() without elevation. mount and umount are setuid-root to allow this to work.