| 1 | // Package mountinfo provides a set of functions to retrieve information about OS mounts. |
| 2 | // |
| 3 | // Currently it supports Linux. For historical reasons, there is also some support for FreeBSD and OpenBSD, |
| 4 | // and a shallow implementation for Windows, but in general this is Linux-only package, so |
| 5 | // the rest of the document only applies to Linux, unless explicitly specified otherwise. |
| 6 | // |
| 7 | // In Linux, information about mounts seen by the current process is available from |
| 8 | // /proc/self/mountinfo. Note that due to mount namespaces, different processes can |
| 9 | // see different mounts. A per-process mountinfo table is available from /proc/<PID>/mountinfo, |
| 10 | // where <PID> is a numerical process identifier. |
| 11 | // |
| 12 | // In general, /proc is not a very efficient interface, and mountinfo is not an exception. |
| 13 | // For example, there is no way to get information about a specific mount point (i.e. it |
| 14 | // is all-or-nothing). This package tries to hide the /proc ineffectiveness by using |
| 15 | // parse filters while reading mountinfo. A filter can skip some entries, or stop |
| 16 | // processing the rest of the file once the needed information is found. |
| 17 | // |
| 18 | // For mountinfo filters that accept path as an argument, the path must be absolute, |
| 19 | // having all symlinks resolved, and being cleaned (i.e. no extra slashes or dots). |
| 20 | // One way to achieve all of the above is to employ filepath.Abs followed by |
| 21 | // filepath.EvalSymlinks (the latter calls filepath.Clean on the result so |
| 22 | // there is no need to explicitly call filepath.Clean). |
| 23 | // |
| 24 | // NOTE that in many cases there is no need to consult mountinfo at all. Here are some |
| 25 | // of the cases where mountinfo should not be parsed: |
| 26 | // |
| 27 | // 1. Before performing a mount. Usually, this is not needed, but if required (say to |
| 28 | // prevent over-mounts), to check whether a directory is mounted, call os.Lstat |
| 29 | // on it and its parent directory, and compare their st.Sys().(*syscall.Stat_t).Dev |
| 30 | // fields -- if they differ, then the directory is the mount point. NOTE this does |
| 31 | // not work for bind mounts. Optionally, the filesystem type can also be checked |
| 32 | // by calling unix.Statfs and checking the Type field (i.e. filesystem type). |
| 33 | // |
| 34 | // 2. After performing a mount. If there is no error returned, the mount succeeded; |
| 35 | // checking the mount table for a new mount is redundant and expensive. |
| 36 | // |
| 37 | // 3. Before performing an unmount. It is more efficient to do an unmount and ignore |
| 38 | // a specific error (EINVAL) which tells the directory is not mounted. |
| 39 | // |
| 40 | // 4. After performing an unmount. If there is no error returned, the unmount succeeded. |
| 41 | // |
| 42 | // 5. To find the mount point root of a specific directory. You can perform os.Stat() |
| 43 | // on the directory and traverse up until the Dev field of a parent directory differs. |
| 44 | package mountinfo |