Remote Removable Drive

This is a simple Perl CGI script that lets a remote user mount and unmount removable drives via a web page. This eliminates the need to interact with a file server via command line or point-and-click console (either locally or remote) in order to mount or unmount a removable drive.

The CGI script uses a REST API which is called by Javascript client code on the web page. This makes it very easy to create other clients (e.g. scripts or GUI widgets) that use the same protocol.

The script is split into two parts, a frontend called request.pl and a backend called response.pl. The backend is called from the frontend via a setuid-root program named setuid-wrapper. This allows the backend to perform the mount and umount commands with superuser privileges.

The REST API looks like this:

/cgi-bin/removable/request.pl?op=mount&volid=Foo - Mounts the volume Foo.
/cgi-bin/removable/request.pl?op=unmount&volid=Foo - Unmounts the volume Foo.
/cgi-bin/removable/request.pl?op=list&volid=all - Lists (in XML) all mount points managed by the script.
/cgi-bin/removable/request.pl?op=list&volid=mounted - Like above, but only lists currently mounted volumes.
/cgi-bin/removable/request.pl?op=list&volid=unmounted - Like above, but only lists currently unmounted volumes.

Note: the list of unmounted volumes contains the names of volumes that are attached but not mounted, so while the intersection of mounted and unmounted volume names is empty by definition, their union is a subset of all mount-points.

The code can be downloaded here. A brief explanation about the contents:

Makefile - Compiles and installs.
mount.html - Browser client with Javascript code, can be installed anywhere.
request.pl - CGI script frontend part.
response.pl - CGI script backend part (privileged).
sample.htaccess - For the CGI scripts.
setuid-wrapper.c - Source code for setuid-wrapper.

Installation:

  1. If desired, edit Makefile, setuid-wrapper.c, request.pl, response.pl and mount.html in order to change the installation paths and mount-point root from the defaults.
  2. Then do "make" to compile setuid-wrapper for your CPU architecture. This also copies the file sample.htaccess to .htaccess which will be the source for installation later.
  3. Edit .htaccess to fit your requirements. This file handles access to the REST API scripts.
  4. Do "sudo make install" to install the scripts.
  5. Do "sudo make install-web" to install the client web page mount.html in the default location under the mount-points root. Alternatively you can install it manually wherever you like under the document root of the web server.

Read below for how to set up the rest of your system.

Prepare your file server

First create the root for the mount points:

mkdir /removable

Then create a mount point for each individual volume:

mkdir /removable/My-1st-Volume
mkdir /removable/My-2nd-Volume
mkdir /removable/My-3rd-Volume
(Repeat for as many volumes as desired)

Then connect the volume labels with the mount points by adding a line to /etc/fstab for each volume:

/dev/disk/by-label/My-1st-Volume /removable/My-1st-Volume auto noauto 0 0
/dev/disk/by-label/My-2nd-Volume /removable/My-2nd-Volume auto noauto 0 0
/dev/disk/by-label/My-3rd-Volume /removable/My-3rd-Volume auto noauto 0 0
(Note: the volume labels and mount points must match, because I'm too lazy to make my scripts parse fstab to figure out the connections)

Configure WebDAV sharing

This step is optional. You can use any file sharing protocol you want, but WebDAV is very versatile. Be careful with NFS since you may need to unexport the share before it is possible to unmount it. This requires some extra hacking. For WebDAV, enable the relevant Apache modules and add the following configuration:

DavLockDB "/srv/www/DavLocks/TestDavLockDB"
Alias /removable "/removable"
<Directory "/removable">
    Dav On
    Options +Indexes
    IndexOptions FancyIndexing Charset=UTF-8
    Order Deny,Allow
    Deny from all
    Allow from 192.168.0.0/16
</Directory>

(You can combine or replace this with password-based authorization if you like)