How to Interpret /Proc/Mounts

How to interpret /proc/mounts?


Format of /proc/mounts

The 1st column specifies the device that is mounted.

The 2nd column reveals the mount point.

The 3rd column tells the file-system type.

The 4th column tells you if it is mounted read-only (ro) or read-write (rw).

The 5th and 6th columns are dummy values designed to match the format used in /etc/mtab.

More details on filesystem mount-points are available here.



tmpfs /export/ftp/import tmpfs rw,relatime,size=102400k 0 0
tmpfs /export/ftp/export tmpfs rw,relatime,size=10240k,mode=755 0 0

Meaning : Two independent tmpfs-es are mounted at both /export/ftp/import and /export/ftp/export. Any data stored into these directories is lost upon rebooting the kernel. tmpfs is essentially a ramdisk-like construct that stores data in the RAM. Technically speaking tmpfs is mapped into virtual memory which uses RAM and swap (if present).



ubi18_0 /nvdata1/temporary-download ubifs rw,sync 0 0
ubi18_0 /export/ftp/import ubifs rw,sync 0 0
ubi18_0 /export/http/import ubifs rw,sync 0 0
tmpfs /export/ftp/export tmpfs rw,size=10240k,mode=755 0 0

Meaning : The same "partition" on the NAND device (ubi18_0) is mounted at 3 different mount-points. ubi is a intermediate file-system layer that simplifies and optimises I/O with the underlying flash media devices. Also a temporary filesystem is mounted at /export/ftp/export.

Discover true size of /proc/mounts

It is conceivable that the mounts have changed but /proc/mounts file size stays the same. The file size is not a robust summary of the file contents.

I don't want to have to buffer the entire output, because I would have to allocate quite a large buffer which wouldn't be portable.

Read the file character-by-character or line-by-line and make a hash of all characters (hash of the file contents). Compare the current hash with the previous one to determine whether the file contents have changed.


Your application can parse /proc/mounts or /proc/self/mountinfo, build a sorted list of mounts (e.g. mount-point and major-minor) and keep it. When /proc/mounts is updated, parse again and build a new sorted list, compare it with the kept one to determine mount additions/removals, act on the additions/removals, replace the kept list with the new one.

Monitoring mount point changes via /proc/mounts

There was a bugfix in linux kernel describing that behavior:

SUSv3 says "Regular files shall always poll TRUE for reading and
writing". see
http://www.opengroup.org/onlinepubs/009695399/functions/poll.html

So, you have to use poll with POLLPRI | POLLERR flags. Something like this:



int mfd = open("/proc/mounts", O_RDONLY, 0);
struct pollfd pfd;
int rv;

int changes = 0;
pfd.fd = mfd;
pfd.events = POLLERR | POLLPRI;
pfd.revents = 0;
while ((rv = poll(&pfd, 1, 5)) >= 0) {
if (pfd.revents & POLLERR) {
fprintf(stdout, "Mount points changed. %d.\n", changes++);
}

pfd.revents = 0;
if (changes > 10) {
exit(EXIT_FAILURE);
}
}

monitoring proc mounts using poll or select

This does work for /proc/mounts monitoring. Any changes to lines are treated as exception and hence adding the fd of /proc/mounts to exceptfds(which is the third parameter in select of select.select([],[],[f]))

import select

f = open("e.txt")
while True:
r,w,x = select.select([],[],[f])
f.seek(0)
print f.read()

Ordering of /proc/mounts and /etc/mtab

The mount points in /proc/mounts are in mounted order. If you mount a new file system it gets appended to the list of filesystems, essentially obscuring the original mount point.

There are absolutely no guarantees about the content of /etc/mtab, it is purely managed by the userland mount command, so can be bypassed or rewritten arbitrarily, plus you can use the '-n' option to mount will actually omit placing entries in the /etc/mtab, thus making it incorrect relative to the kernel.

As a silly question, why do you need this information?

Linux: Getting Mount Point Utilization from /proc?

You probably want the statfs(2) syscall (it does not use /proc/). You probably would use it thru statvfs(3) Posix function.

Notice that the /proc/ filesystem (and also /sys/ ...) contains pseudo-files which are generated by the kernel on demand without any IO. See proc(5). You could read sequentially /proc/self/mounts and/or /proc/self/mountstats and/or appropriate files under /proc/fs/ and/or /sys/ (such as some files in /proc/fs/ext4/sda1/ or even in /sys/block/sda/sda1/ for my desktop computer ; it would be different on yours ....)

Perhaps systemd is also able to give such information, but I don't know it enough.

In Linux terminal, what would grep -q '/dev/sda1' /proc/mounts || ... do?

The instructions state

assuming that the device is detected as /dev/sda

If the assumption is different from the actual mount point, you must modify the commands to match your configuration.

The grep -q is used to test for existence without cluttering the screen with the text which is found. The two parameters are

  1. the text sought "/dev/sda1", and
  2. the file in which the text is sought "/proc/mounts".

In other scripts, you may see something like

grep /dev/sda1 /proc/mounts >/dev/null

to achieve the same effect as the -q option.

Parsing /proc/mounts in Java

I wound up using Apache Commons Lang.

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html#unescapeJava(java.lang.String)

That seems to unescape everything properly.

is there any limitations in using /proc/mounts in android

/proc is a mountpoint for a Linux virtual filesystem, procfs. It is specific to Linux, therefore works with all Android installations.

The best possible documentation for procfs is its source code, but it requires some familiarity with Linux kernel and C. Also see Documentation/filesystems/proc.txt in the kernel sources, it may contain relevant information for you.



Related Topics



Leave a reply



Submit