1

My question is a combination of sorts of this question and this question. I want to mount a file with spaces in its path with sudo permissions, as such:

sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name

If there's no spaces in the paths, I can just do this, since this user has sudo permissions for the mount command:

Runtime.exec("sudo mount /path/to/mount/point /dir/without/spaces");

But there are spaces, so I tried:

Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

This gave me "Unrecognized arg '\'" or something to that effect from mount. Surrounding the pathnames with single or double quotes also didn't work.

Then I tried:

Runtime.exec("sudo", "mount", "/path/to/mount/point", "/dir with/spaces in name");

which of course fails because sudo wants some method of password entry, even though I don't normally need to enter a password for sudo mount. Faaantastic.

This seems like a catch-22 to me. I can get sudo working if I go with the single String method, or I can get the pathnames working if I go with the String array method. How do I get both working?

Community
  • 1
  • 1
Graph Theory
  • 699
  • 1
  • 7
  • 18

1 Answers1

1

Single backslash cannot be written verbatim in string literal. Backslashes should be escaped, so use \\:

Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

As for the password, sudo may not ask for password if recently you entered it on the same shell. If you want to allow to mount given path without superuser permissions (for regular user) edit its /etc/fstab entry as shown here.

Add user to its options, e.g.:

/dev/sda8    /media/foo    ext4    rw,user,exec 0 0

and then add privileges to read and write:

# chmod a+rw /media/foo

Then your line can be:

Runtime.exec("mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

or

Runtime.exec("/bin/mount", "/path/to/mount/point", "/dir with/spaces in name");

or alike.

Community
  • 1
  • 1
user2622016
  • 6,060
  • 3
  • 32
  • 53
  • Thanks for the answer. And sorry, I was in a hurry yesterday and forgot to escape my backslashes: that wasn't the problem I was running into. I've tried every combination of single, double, and even triple backslashes ("\\", "\\\\", and "\\\\\\" in Java terms), but unfortunately none of those work. Sudo also has to stay in there, because the account I'm running this from has sudo permission for mount but not regular permission, and I don't have control over that. It's a very particular set of constraints, I know. – Graph Theory Oct 16 '15 at 15:46
  • You want to `sudo` without supplying user password? – user2622016 Oct 16 '15 at 15:55
  • Basically I need a solution which both allows paths with spaces and doesn't require me to supply the password. I already have the mount command set up with NOPASSWD in /etc/sudoers for this account. – Graph Theory Oct 16 '15 at 16:00
  • I'm just guessing: `Runtime.exec("/usr/bin/sudo","/bin/mount",...);`. You have to say something more on the result (process output? error message?) than just "does not work". – user2622016 Oct 16 '15 at 16:08