Warm tip: This article is reproduced from stackoverflow.com, please click
linux

How to determine if a path is located on a physical device or in memory?

发布于 2020-03-27 10:23:36

I am looking for a way to know, programmatically, if a path point to a physical device or in memory.

I have noticed that the du command print a size of zero for paths in /sys or /proc for example, but I am not sure if this is a reliable because I think a file on a disk can have a size of zero.

Questioner
madjack
Viewed
94
Martin 2019-07-03 23:39

This is not easily possible because of the philosophy (one filesystem with mountpoints, network transparency, everything is a file (or folder)) used in linux. Imagine: a network file system mounted into your system. It is not in any physical device of your computer - is this physical or not?

The only heuristic that comes to mind is comparing the filesystem against a predefined list: check the output of mount, e.g.:

tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204096k,mode=755)
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)

the first column is the source device - on the first entry there is no device geiven, just the virtual filesystem tmpfs whereas in the second entry we see a proper device /dev/sda2.

a device is no guarantee for physical storage, there is /dev/mem for example, which gives direct access to the computers' memory.

So, my suggestion is to find out which mountpoint applies to a path (try lsblk), find that mountpoint in the output of mount. If this mountpoint does not match a device int the form of /dev/WHATEVER assume it is in-memory

Using the suggestion from @Chris, try e.g. this findmnt -T /proc/net/stat/arp_cache:

TARGET SOURCE FSTYPE OPTIONS
/proc  proc   proc   rw,nosuid,nodev,noexec,relatime

The relavant columns is SOURCE