a17b5eb28337f488dcc0639d40cb2194dd076f8e
[captive.git] / src / client / lufs / mount.captive.in
1 #! /usr/bin/perl -w
2
3 # $Id$
4 # External mount command for mount(8) to interface lufsmount(1)
5 # Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use strict;
22 use Carp qw(cluck confess);
23 use Getopt::Long;
24 # Do not use any non-core Perl modules here.
25 # Be perl-5.0 compatible (is it needed if we require Linux kernel >= 2.4 ?)
26
27
28 my $vardir='@localstatedir@/lib/captive';
29 $vardir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
30 $vardir="/var/lib/captive" if $vardir=~/^@/;
31 my $lufsmount_bin='@bindir@/lufsmount';
32 $lufsmount_bin=~s#\$\Q{exec_prefix}\E#'@exec_prefix@';#ge;
33 $lufsmount_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
34 $lufsmount_bin="/usr/bin/lufsmount" if $lufsmount_bin=~/^@/;
35 my $captive_sandbox_server_bin='@sbindir@/captive-sandbox-server';
36 $captive_sandbox_server_bin=~s#\$\Q{exec_prefix}\E#'@exec_prefix@';#ge;
37 $captive_sandbox_server_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
38 $captive_sandbox_server_bin="/usr/sbin/captive-sandbox-server" if $captive_sandbox_server_bin=~/^@/;
39
40
41 my $fsname=($0=~m#([^/]*)$#)[0] or die "Cannot detetect my basename from: $0";
42 $fsname=~s/^mount[.]captive-// or die "Cannot detect captive filesystem module from my name: $fsname";
43 my $image=shift @ARGV or die "Missing argv[1]: device or image pathname";
44 -r $image or die "Image pathname not readable: $image";
45 warn "warning: '-o loop' is not neccessary for Captive filesystem mounts"
46                 if $image=~m#^/dev/loop#;
47 my $dir=shift @ARGV or die "Missing argv[2]: mount point directory";
48 -d $dir or die "Mount point directory not a valid directory: $dir";
49 shift @ARGV if my $nomtab =$ARGV[0] && $ARGV[0] eq "-n";
50 shift @ARGV if my $verbose=$ARGV[0] && $ARGV[0] eq "-v";
51 my $oo="";
52 do { shift @ARGV; $oo=shift @ARGV; } if $ARGV[0] && $ARGV[0] eq "-o";
53 die "Excessive arguments: @ARGV" if @ARGV;
54
55 sub die_install
56 {
57         die
58 "You should run captive-install-acquire(1) of 'captive-install' package,
59 otherwise you can also acquire this file from URL:
60 http://www.microsoft.com/WindowsXP/pro/downloads/servicepacks/sp1/checkedbuild.asp
61 ";
62 }
63
64 my $filesystem=$vardir."/".$fsname.".sys";
65 if (!-r $filesystem) {
66         warn "W32 filesystem .sys module not found: $filesystem";
67         die_install();
68         }
69 my $ntoskrnl=$vardir."/ntoskrnl.exe";
70 if (!-r $ntoskrnl) {
71         warn "W32 ntoskrnl.exe not found: $ntoskrnl";
72         die_install();
73         }
74
75 # Shameless advertisement:
76 if ($fsname eq "ntfs") {
77         for my $fh (*STDERR,*STDOUT) {
78                 if (-t $fh) {
79                         print $fh 'Captive NTFS v@VERSION@.  Check new version: http://www.jankratochvil.net/'."\n";
80                         last;
81                         }
82                 }
83         }
84
85
86 # Double-dashes are generally optional here.
87 # Convert ',' to ';' to prevent its parsing by LUFS to keep its ordering
88 # to let the options be overridable by user (such as 'ro').
89 $oo=";$oo;";
90 $oo=~tr/,/;/;
91 $oo=~s/;(ro|rw);/;--$1;/g;
92 $oo=~s/;(?:no)?auto;/;/g;       # ignore
93 $oo=""
94                 ."mntent.mnt_fsname=$image,mntent.mnt_type=captive-$fsname,"    # Double-dashes forbidden.
95                 ."dir_cache_entries=0,"
96                 ."image=$image,"
97                 ."captive_options="
98                                 .($fsname eq "cdfs" ? "--cdrom;--ro;" : "--disk;--rw;")
99                                 ."--load-module=$ntoskrnl;--filesystem=$filesystem;"
100                                 ."--sandbox-server=$captive_sandbox_server_bin;"
101                                 ."--bug-pathname=$vardir/bug-%FT%T.captivebug.xml.gz;"
102                                 ."--syslog;"
103                                 .$oo;
104
105 warn "$0: '-n' not supported - ignored" if $nomtab;
106
107 # Use "'$oo'" to permit another expansion by bash(1) during lufsmnt(8) execution.
108 my @argv=($lufsmount_bin,"captivefs://",$dir,"-o","'$oo'");
109
110 print STDERR "$0: @argv\n" if $verbose;
111 exec $lufsmount_bin @argv;
112 die "Failed to execute: @argv";