# portable way to check existence of removable medias

## Dominique_71

I am writing an icon-launcher for one of the fvwm-crystal recipes, and I want to be sure if it is portable. It check for the existence of all the mounted partitions and launch associated icons onto the desktop. When clicking one of those icons, thunar is opened on the corresponding path.

The setup for those removables medias into my system is a little bit bastard. I am using udev to assign permanent links into /dev, as well than hal and automount. 

In order to recognize the mounted partitions, the icon-launcher use the following:

```
PipeRead "cat /etc/mtab|grep ^/dev|sed -e 's,/dev/.... \\(/[^ ]*\\).*,\\1,'|sort > /tmp/Icon-Drives
```

The created file is used with another PipRead command to launch the icons.

Is it portable to check mtab?

An alternative would be to use "mount -l", but it is very slow when the drives are into sleeping mode...

----------

## krinn

i'm not sure why you do that, but removeable or not, mta will show mounted partitions.

better check /sys/block/*/removable state, where 1 = removable media and 0 a fixed one.

----------

## Dominique_71

I'am checking mtab because it is simple: I will find here only the mounted drives (I don't want icons for the non mounted ones), and I will also find their mount points. So, in one check I know 2 different things.

My question can be made the following: will I find /etc/mtab into any system that support fvwm?

Into /sys/block/*, I cannot find anything that let me know if the drive is mounted or not. But I can be wrong on this.

But I can find the media type of the drive, and this is very interesting if I want to use different icons for hard drives and cd/dvd drives. 

And the question remain the same: will I find /sys/block/* into any system that support fvwm?

----------

## dmpogo

I thought 'mount' without any arguments just reads /etc/mtab anyway.   It can't be that slow !

You use 'mount -l' which has to get labels, and for that has to read the drive

----------

## Dominique_71

 *dmpogo wrote:*   

> I thought 'mount' without any arguments just reads /etc/mtab anyway.   It can't be that slow !

 

You are right, it is fast ! And it probably answer to my question, fvwm will find it. I can also replace "cat /etc/mtab" by "mount" into the PipeRead.

----------

## Hu

 *Dominique_71 wrote:*   

> 
> 
> ```
> PipeRead "cat /etc/mtab|grep ^/dev|sed -e 's,/dev/.... \\(/[^ ]*\\).*,\\1,'|sort > /tmp/Icon-Drives
> ```
> ...

 This could be simplified to omit use of both cat and grep and replace sed with gawk for a cleaner invocation:

```
gawk '/^\/dev\// { print $2; }' </proc/mounts |sort
```

You could also filter by filesystem type, by applying an appropriate regex to $3.

----------

## Dominique_71

Hu you are right, but it will add a dependency. I must think about it.

----------

## dmpogo

 *Dominique_71 wrote:*   

> Hu you are right, but it will add a dependency. I must think about it.

 

One issue to consider is that on some systems /etc/mtab can be a symlink to /proc/mounts, which contains the same information + extra info on mount options.  Actually mount man pages advises to use such a symlink in some circumstances.

I'm not sure what 'mount'  will produce then,  but direct 'cat' will have a notch different format.Last edited by dmpogo on Sun Oct 10, 2010 8:49 pm; edited 1 time in total

----------

## Hu

 *Dominique_71 wrote:*   

> Hu you are right, but it will add a dependency. I must think about it.

 It removes three dependencies and adds one.  That seems like a net benefit to me.  What are you concerned about here?

----------

## Dominique_71

 *dmpogo wrote:*   

> 
> 
> One issue to consider is that on some systems /etc/mtab can be a symlink to /proc/mounts, which contains the same information + extra info on mount options.  Actually mount man pages advises to use such a symlink in some circumstances.

 

It also say "If /proc is mounted...". So, to check /proc/mounts will not work in any case.

 *Quote:*   

> I'm not sure what 'mount'  will produces then,  but direct 'cat' will have a notch different format.

 

I will modify it and use "mount" instead of  "cat /etc/mtab".

 *Hu wrote:*   

> 
> 
> It removes three dependencies and adds one. That seems like a net benefit to me. What are you concerned about here?

 

I want to publish it with fvwm-crystal new release. cat is part of coreutils, so it is not really a dependency, mount is present on every system (part of util-linux) so it is not really a dependency either, grep and sed are already used by fvwm crystal, and to use them one more time will not add a dependency.

----------

## Etal

What Hu means is that cat and grep are completely useless and add additional overhead of running 4 different processes instead of 2.

cat can be eliminated 99% of the time, since you can use the shell's '<' operator, and almost everything you want to pipe text into can read the text by itself annyway:

```
grep ^/dev /etc/mtab | sed -e 's,/dev/.... \(/[^ ]*\).*,\1,' | sort
```

grep can also be eliminated since sed can do the same thing:

```
sed -n -e 's,^/dev/.... \(/[^ ]*\).*,\1,gp' /etc/mtab | sort
```

Hu's suggestion of using awk (available on any POSIX platform) is better though, because the regular expression you're using is complex and error-prone: For instance, if I have /dev/mmcblk0p1 (an SD-card), it prints the whole mtab line instead of just the mount point.

----------

## krinn

the dev/ test will also make you miss bind and network mount (but this might what you were wishing, but you will miss valuables ressources)

----------

## Dominique_71

You are right Etal, gawk is better. I will use it.

But, the big problem is elsewhere. According to man mount, "The mount command internally uses udev symlinks", that imply than mount depend on udev for its correct functionalism. According to man udev, "udev - Linux dynamic device management". That imply than udev is linux specific and than any function that depend on it is not portable to other systems.

Well, I thing than I will make 2 things, first investigate how it will be possible to use some udev rule(s) in order to interact with fvwm and launch the icons. It can be an interesting alternative solution that will separate the system specific things from the fvwm specific ones.

I must also put a test into fvwm-crystal in order to run the icon launch function only on a linux system. I guess than uname can be used for that. It will be a lot of changes into this release. I still have a lot of things to cleanup and want this release to be made in the near future. Other guys will have to contribute if they want this icon-launch functionality on other systems than linux.

And thanks for your answers anyway, I appreciate it.

----------

## dmpogo

 *Dominique_71 wrote:*   

> You are right Etal, gawk is better. I will use it.
> 
> But, the big problem is elsewhere. According to man mount, "The mount command internally uses udev symlinks", that imply than mount depend on udev for its correct functionalism.
> 
> 

 

Perhaps Linux version.   Mount existed on all Unixes before udev was born.

----------

## Dominique_71

I finally get something out of this discussion: Desktop icons for Fvwm-Crystal

It is already in the svn and will be part of the next release. You can choose in the preferences if you want them. If yes, HOME is mandatory and have another preference where you can choose if you want the XDG user dirs icons or the partitions icons. A little script check the checksum of /proc/mount, and when necessary, update the partitions icons. At the moment, only thunar and mc can be launched from those icons, but I will add preferences for those actions too.

----------

