How to mount/umount disk from a non-root account

Is it possible to mount a disk from a non-root account?

I'm developing a Java application which executes commands in the shell using the java.lang.Runtime.exec api, which runs fine for commands ls, df, etc., but for commands mount and umount, i have problems as I need to be root to eecute these. But my application must run as a standard out of the box.

Any ideas on getting mount/umount running from my non-root user account?

No way to workaround that - such commands are explicitely obligation of root account with a reason. You can use "sudo" for example to call :

import java.io.*;

public class RunCommand {

    public static void main(String args[]) {

        String s = null;

        try {
            
	    // run the Unix "ps -ef" command
            
            Process p = Runtime.getRuntime().exec("ps -ef");
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);

replace "ps -ef" with "sudo mount $parameters"

If you can define earlier which resources you want to mount/unmount using your application it can be added to /etc/fstab with user option (allow a user to mount).

You can configure sudo for it as well.

Dirty method - add suid bits to mount/umount command and try to manage access using groups.

Regards,
-Artur.

I've edited the /etc/fstab and added user option

/dev/loop1              /storage/part1          auto    noauto,user,noexec 0 0

My version of mount/umount is:

[brendan@rubidium /]$ mount -V
mount: mount-2.12a

I can mount ok as user root, but ordinary user gets:

[brendan@rubidium ~]$ mount /dev/loop1 /storage/part1 -o user
mount: only root can do that

Is this a bug perhaps?

Sudo prompts for password, using Java runtime exec, this is not an option.

Setting suid bits may be the way to go if all else fails :frowning:

[mount problem]

It's strange - I'm using:
mount: mount-2.12p

I can't test it now, but can you check permissions to file which you want to mount?

[sudo]

I'm sure you can configure sudo without asking for a password. As far as I remember I did it long time ago.

[suid]

I don't recommend it.

Regards,
-Artur.

If I edit /etc/sudoers and add:

brendan ALL=NOPASSWD: ALL

Then as user brendan execute:

sudo mount /dev/loop1 /storage/part1

This works fine, but is there any long term problems, etc. in doing this?

Have you check permissions to file which you want to mount on loopback? I havn't seen any comment about it.

Regarding sudo configuration - it's not the best idea to provide access for ALL commands, you can choose a list which is required by you.

Regards,
-Artur.

I tryied samely. I succeed below description.

mount /dev/loop1

or

mount /strage/part1

It needs either device or directory parameter only.
'man mount' says these two examples near 'fstab' word.

but it doesn't say both parameters. It may be specification.

For user mounts the user or users option needs to be put in fstab thus indicating that root approved the configuration. And the user must use only one parameter in the mount command... this is not a license to specify a different mount point. With "user" only the user who mounted (or root) can umount it later. With "users", anyone can umount.