3

So I've been handed a python project in which there are a lot Linux system calls to mount/unmout/format/etc backup drives for a customized NAS.

Right now I'd like to handle the output of the umount command and handle the case of an umounted path:

print subprocess.check_output(['umount', '/storage/backup'])

Which may return:

umount: /storage/backup: not mounted
Command '['umount', '/storage/backup']' returned non-zero exit status 32

Now, I could just parse the output string and search for not mounted, but I'd prefer to process the exit status values (32 in this case). I've tried to find a list of exit codes for the umount command but have been unlucky so far.
Also, I've tried finding the source code for umount but haven't been able to find it (google keeps pointing me to manual pages for the umount command or the source code for mount.c)

Edit

The man pages for umount have a list of Errors (non numeric) like:

  • EBUSY - target could not be unmounted because it is busy.
  • EFAULT - target points outside the user address space.

And then: The error values given below result from file-system type independent errors. Each file system type may have its own special errors and its own special behavior. See the Linux kernel source code for details.

Any pointers?

Emanuel Ey
  • 2,724
  • 5
  • 30
  • 38
  • 2
    man pages are a good resource. Look at the return code section for `man mount` – 301_Moved_Permanently Oct 16 '15 at 10:02
  • 1
    The return codes of `umount` are probably the same as for `mount`. See @LaPriWa 's answer. – jotrocken Oct 16 '15 at 10:12
  • Hmm, ok. I wasn't sure if i could just assume that the return codes for mount and unmount were the same – Emanuel Ey Oct 16 '15 at 10:14
  • This may be helpful: https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html though mount and umount may be abusing them some (e.g., 1 which is EPERM should be returned for permissions errors but it appears mount also returns that error for incorrect invocations). – MrMas Jun 21 '23 at 20:43

4 Answers4

10

From man mount:

RETURN CODES
   mount has the following return codes (the bits can be ORed):

   0      success

   1      incorrect invocation or permissions

   2      system error (out of memory, cannot fork, no more loop devices)

   4      internal mount bug

   8      user interrupt

   16     problems writing or locking /etc/mtab

   32     mount failure

   64     some mount succeeded

   The command mount -a returns 0 (all succeeded), 32 (all failed), or  64
   (some failed, some succeeded).
parsley72
  • 8,449
  • 8
  • 65
  • 98
LaPriWa
  • 1,787
  • 1
  • 12
  • 19
  • Thanks, that's just what i need. Can you provide a source for this info? – Emanuel Ey Oct 16 '15 at 10:10
  • 2
    @EmanuelEy: I've added the link to the mount(8) Linux man page. – jfs Oct 16 '15 at 10:28
  • 4
    This does *NOT* answer the question since the return values of `umount` do *not* match those of `mount`! Actually the return code is the number of mount points that failed to unmount, at least for util-linux's `umount`. Cf. https://github.com/karelzak/util-linux/blob/master/sys-utils/umount.c – stefanct Feb 15 '19 at 12:44
2

While this isn't a direct answer to your question, a more robust(?) and efficient way would be to call the mount()/umount() syscall wrappers in libc rather than executing an external process and try to figure out its output. Unfortunately there is no builtin wrapper for those in the python standard library, but it's easy enough to do yourself using ctypes, see https://stackoverflow.com/a/29156997/75652 for how to call mount() via ctypes.

http://man7.org/linux/man-pages/man2/mount.2.html

http://man7.org/linux/man-pages/man2/umount.2.html

janneb
  • 36,249
  • 2
  • 81
  • 97
1

Unlike presumed by about every other answer here the return values of umount do not necessarily match those of mount! Actually the return code is the number of mount points that failed to unmount, at least for util-linux's umount, cf. its source.

Since the question of the OP is about a NAS I assume that it might be some BSD variant that uses another umount implementation. AFAIK the commands mount and umount are also OS-specific and not standardized by POSIX (unlike the respective C functions) so there is little hope for any implementation-agnostic solution.

stefanct
  • 2,503
  • 1
  • 28
  • 32
-1

Return code for any successful command is 0 when failure it return other code, it depends

you can check return code in terminal after running command by:

echo $?
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • Yes, I am aware. But I'd like to have a table with all available numeric return codes. – Emanuel Ey Oct 16 '15 at 10:06
  • @EmanuelEy so you are aware 0 return code means success, but you have no idea what a `manpage` is??? – guido Oct 16 '15 at 10:12
  • @Hackaholic You are right, there is nothing wrong per-se but it didn't answer the question. – Emanuel Ey Oct 16 '15 at 10:18
  • 1
    @ᴳᵁᴵᴰᴼ I am well aware of what a man page is, I have spent hours reading them, but a) if you go check `man umount` there is no info on error codes and b) I did not assume that i could just use the error codes for `mount`. – Emanuel Ey Oct 16 '15 at 10:22
  • "Return code for any successful command is 0" -- it seems to be wrong unfortunately, see my answer here https://stackoverflow.com/a/69537938/2007631 – tsul Oct 12 '21 at 09:17