pmilne has asked for the wisdom of the Perl Monks concerning the following question:

To test whether a ramdisk has been mounted I have created a file in the mount point directory. I then test whether that file exists and if not I presume a ramdisk has been mounted there. Is there a more elegant method without needing a test file?

Replies are listed 'Best First'.
Re: Check that a ramdisk is mounted
by jcb (Parson) on May 15, 2021 at 02:18 UTC

    The first element of the list returned by stat will be different on a directory and its parent directory if and only if the directory is a mountpoint.

    #!/usr/bin/perl use strict; use warnings; use Cwd qw/realpath/; use File::Spec; my $Dir = realpath(shift || File::Spec->curdir); my $Parent = realpath(File::Spec->catdir($Dir, File::Spec->updir)); my $Dir_fsid = (stat($Dir))[0]; my $Parent_fsid = (stat($Parent))[0]; printf 'dir: [on %4x] %s%s', $Dir_fsid,$Dir,"\n"; printf ' up: [on %4x] %s%s', $Parent_fsid,$Parent,"\n"; if ($Dir_fsid == $Parent_fsid) { print "$Dir is not a mountpoint\n" } else { print "$Dir is a mountpoint\n" } __END__
Re: Check that a ramdisk is mounted
by afoken (Canon) on May 15, 2021 at 15:55 UTC
    To test whether a ramdisk has been mounted I have created a file in the mount point directory. I then test whether that file exists and if not I presume a ramdisk has been mounted there. Is there a more elegant method without needing a test file?

    That test just check if anything was mounted on the moint point directory. Of course, it will be false negative once the mounted filesystem contains a file with the same name as the test file. jcb's stat test Re: Check that a ramdisk is mounted does that better, without false negative results.

    None of the tests checks for a ramdisk, both just check for any filesystem mounted. Detecting a ramdisk is highly specific for the operating system. The device number of the file system (the first value in stat's return value) may be a clue. With knowledge about how device numbers are allocated, you may at least be able to identify disk- or flash-backed block devices. Linux has a file named "Documentation/devices.txt", "Documentation/admin-guide/devices.txt" in newer versions, that contains major and minor device numbers. Some major device numbers are used for RAM disks, floppies, various hard disk interfaces, cdrom drives, and so on, they are quite obvious. Other block devices are in-between: Loop devices create a block device from a file that may or may not exist on a ramdisk. Network block devices import other computer's block devices that may or may not be ramdisks. Then there are experimental, local and dynamically allocated block devices, the latter for virtual filesystems like tmpfs that need just a dummy block device number, but no real block device. And ramdisks implemented as real, physical peripherals plugged into some system bus, or simply appearing as a SCSI disk. And that's just Linux. Feel free to explore OSX and the *BSDs ...

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Rather than trying to guess what device numbers mean what (which will vary between systems and even versions of the "same" system, especially with modern Linux kernels where /dev is assumed to be a RAM filesystem populated at boot), the better way to determine what is on a mountpoint is to examine the output returned by qx/mount/. This will allow you to easily determine if a mountpoint is a RAM filesystem, but there is no one reliable way to determine if a physical block device is actually a RAM disk, especially a hardware device, which may even be relatively non-volatile with battery-backed storage or an SSD that can retain data with no power at all.

      I suspect that our questioner is concerned with ensuring that a RAM filesystem has been mounted before some daemon begins its work. In this case, there is a bit of an XY problem here: the real concern is that a filesystem has been mounted on the designated directory, since other configuration ensures that that filesystem will be the intended RAM filesystem.

Re: Check that a ramdisk is mounted
by jdporter (Canon) on May 17, 2021 at 16:34 UTC

    What operating system?

A reply falls below the community's threshold of quality. You may see it by logging in.