简体   繁体   English

如何仅使用Java挂载设备?

[英]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. 我有一台运行linux的设备,它可能没有可见的用户界面。 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. 它将有一个打开的USB端口...我希望能够编写Java,以允许整个系统检测何时将设备插入USB端口,然后安装该设备。

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. 我知道如何从命令行执行此操作,但是需要“ sudo”派生具有提升权限的新进程来执行任务。 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. 我不知道如何编写一个Java程序,它可以绕开(是的,我想绕开)mount的安全保护。

runtime.exec() doesn't really do what I am hoping to accomplish. runtime.exec()并没有真正完成我希望完成的工作。 Is this a "can't be done in Java" problem? 这是“用Java无法完成”的问题吗?

I'm hoping to do this without resorting to external executable code. 我希望做到这一点而无需诉诸外部可执行代码。

You can't get around the security protection of mount. 您无法绕过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. 如果可以控制正在运行的操作系统,则可以将选项用户添加到/ etc / fstab中的安装点。 After doing so, "mount /path" and "umount /path" work from runtime.exec() without elevation. 这样做之后,“ mount / path”和“ umount / path”从runtime.exec()而没有提升。 mount and umount are setuid-root to allow this to work. mountumount是setuid-root,以使其工作。

File mounts are controlled by the host operating system. 文件挂载由主机操作系统控制。 So controlling mounts is done through system calls. 因此,控制装载是通过系统调用完成的。 You seem to be using a Unix operating system, so that means the system call interface is C. So you could use JNI to call a small C function that makes the system call needed. 您似乎正在使用Unix操作系统,这意味着系统调用接口是C。因此,您可以使用JNI调用一个小的C函数,该函数需要进行系统调用。

But all such matters are system dependent, and privileged operations. 但是所有这些都是系统相关的特权操作。 So that approach creates portability and security problems. 因此,这种方法会带来可移植性和安全性问题。 So you might do better to reconsider why your program itself must do this, rather than letting a control component (such as systemd ) do it for you. 因此,您最好重新考虑为什么程序本身必须执行此操作,而不是让控制组件(例如systemd )为您执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM