Mount Remote Drives on KDE Startup With a Zenity Dialog

I use Kubuntu on my laptop. I noticed that wherever I go, I usually end up mounting some windows shares or remote drives. I have 5 or 6 entries in fstab but I don’t have them auto mounting at startup. Why? Because it’s a laptop. Sometimes I use it at home, sometimes I use it at school, and sometimes I use it at work. And depending on where I am at the moment different drives need to be mounted.

Most of the time these shares will get mounted as I access them with KDE apps (thank you kio slaves!) but some of my perl scripts require this or that share to be mounted to run. And I habitually forget to mount stuff every morning, and don’t realize it until some cron job starts spewing errors. So I wrote myself a simple script that will prompt me to mount my drives at startup. I used zenity to create a nice looking GUI dialog:

#!/bin/bash

l=$(cat ~/.mountme | xargs)
input=$(zenity --list --checklist --column "Mount" --column "FS" $l --separator=" ")

retval=$?

case $retval in
	0)
		array=($input)
		for i in ${array[@]}; do mount $i; done
		;;
	1)
		exit;;
esac

The scrupt makes some assumptions. First one is that you have file called .mountme in your home directory. This file should contain annotated list of all the filesy stems you want to show up in your dialog box. Mine looks like this:

TRUE //dc01/Public
TRUE //dc01/Private/maciak
FALSE //dc02/Share
FALSE //elrond/Share
FALSE //eoran/Shared
FALSE //grendel/Share

Each line should consist of two words. First word should be a TRUE/FALSE statement. Each line annotated with TRUE will show up as pre-checked in the zenity dialog. Lines annotated with FALSE will be un-checked.

The second word on the line should be the actual path to the remote file system you want to mount, as it appears in your fstab. Each line has to have a corresponding fstab entry. Finally, watch out for spaces in the paths – they will break stuff.

The script will read in this file, flatten it out using xargs and pass it to zenity as one of the arguments. It will produce a dialog like this:

Zenity Mount Script

The output of zenity will be a space separated lists of paths to be mounted. I plugged it into array and then use a loop to mount each of them in succession.

The only thing left was to make this dialog appear every time I log into KDE. To do this you have to create a small .desktop file in your ~/.kde/Autostart/ directory. I called mine remotemount.desktop:

[Desktop Entry]
Exec=~/bin/remotemount
Name=RemoteMount
Type=Application
X-KDE-StartupNotify=false
X-KDE-autostart-phase=2

The Exec line is the important one. It specifies the path to your script. I have it in ~/bin. The two X-KDE-autostart-phase variable tells KDE to wait until everything else loads before launching this app. It doesn’t really make sense to have this dialog pop-up before for example my Kmail and assorted apps like Kgpg and etc…

Is this a perfect solution? Of course not, but it works. Why don’t I parse the fstab directly? I could but it would mean more scripting. I like to keep things simple.

[tags]linux, kde, mount, zenity, mount network files[/tags]

This entry was posted in sysadmin notes and tagged , . Bookmark the permalink.



Leave a Reply

Your email address will not be published. Required fields are marked *