On Fri, Mar 17, 2000 at 06:11:54PM +1100, Andrew Clausen wrote:

> I'm writing partition program, that needs to find out
> if a partition is mounted.  Sometimes, /etc/mtab is
> not available (eg / is mounted read-only).  Therefore,
> I should check /proc/mounts first, if it's available.
> But /proc/mounts has this /dev/root brain damage.  So
> I need to find out what /dev/root really is.

I think you should avoid both /etc/mtab and /proc/mounts.
Both are unreliable - /etc/mtab because it is just a file
and in principle can have arbitrary contents, and /proc/mounts
because /proc may not exist, and /dev/root is unresolved,
and spaces in names may make it impossible to parse /proc/mounts
uniquely, changes in names since things were mounted may make
it impossible to interpret a /proc/mounts line, etc.

Indeed, it is a bad idea in general to take some imprecise
output meant for human consumption only, and use that in a
program, except possibly as a vague heuristic to generate
a warning.

Why do you want to check whether a partition is mounted?
I can see two reasons:
(i) If a partition is in use then a subsequent BLKRRPART
will fail. This is worth a warning but is not catastrophic.
The new table will be used after a reboot or after
a command like "blockdev --rereadpt device".
(ii) The sectors describing logical partitions are scattered
all over the disk. If one of these overwrites a sector on
an active partition (mounted, or in use as swap) then
terrible things may happen.

My point of view is that (ii) is so serious that it does not
suffice to rely on the heuristic provided by looking at
/proc/mounts and /proc/swaps. Fortunately there is a very
easy way to get much more reliable information:
use the BLKPG_DEL_PARTITION ioctl.

Andries

Reply via email to