3 ### Create and remove snapshots of block devices
5 ### (c) 2011 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU General Public License as published by
12 ### the Free Software Foundation; either version 2 of the License, or
13 ### (at your option) any later version.
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ### GNU General Public License for more details.
20 ### You should have received a copy of the GNU General Public License
21 ### along with this program; if not, write to the Free Software Foundation,
22 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 use Getopt::Long qw(:config gnu_compat bundling no_ignore_case);
27 our $VERSION = "@VERSION@";
29 our %C = ( etc => "@sysconfdir@",
31 snap => "@snaplibexecdir@" );
33 ###--------------------------------------------------------------------------
36 (our $QUIS = $0) =~ s:^.*/::;
37 sub whine ($) { my ($msg) = @_; print STDERR "$QUIS: $msg\n"; }
38 sub fail ($) { my ($msg) = @_; whine $msg; exit $! || ($? >> 8) || 255; }
40 ###--------------------------------------------------------------------------
41 ### Parse command line.
43 our $USAGE = "usage: $QUIS [-u] [-c FILE] DEVICE [KEY=VALUE ...]";
44 sub version { print "$QUIS, version $VERSION\n"; }
50 -h, --help Show this help text.
51 -v, --version Show the program version number.
52 -c, --config=FILE Use configuration FILE, not $CONF.
53 -n, --no-act Don't actually do anything; show what would be done.
54 -u, --unsnap Remove a snapshot taken earlier.
58 our $CONF = "$C{etc}/snaptab";
61 GetOptions('help|h|?' => sub { version; help; exit; },
62 'version|v' => sub { version; exit; },
63 'config-file|c=s' => \$CONF,
64 'no-act|n' => \$NOACT,
65 'unsnap|u' => sub { $OP = "unsnap"; })
67 or do { print STDERR $USAGE, "\n"; exit 1; };
72 ###--------------------------------------------------------------------------
73 ### Parse the configuration file.
75 open CF, "<", $CONF or fail "open config ($CONF): $!";
78 while (my $line = <CF>) {
80 while ($line =~ /\\\s*$/) {
81 chomp (my $more = <CF>);
82 $line =~ s/\\\s*$/$more/;
84 next if $line =~ /^\s*(\#|$)/;
85 my ($dev, $type, @opts) = shellwords $line;
88 if ($i !~ /^\*\.(.+)$/) { push @nopts, $i; next; }
90 for my $o (@{$DEF{$ty}}) {
91 $o =~ /^([^=]+)=(.*)$/;
92 my ($k, $v) = ($1, $2);
93 ($k, $ty) = ($1, $2) if $k =~ /^(.+)\.([^.]+)/;
94 push @nopts, "$k.$ty=$v";
98 if ($dev eq "*") { push @{$DEF{$type}}, @opts; }
99 elsif ($dev eq $DEV) { push @KV, "type=$type", @{$DEF{$type}}, @opts; }
101 close CF or fail "close config ($CONF): $!";
103 ###--------------------------------------------------------------------------
104 ### Pick out the winning options.
109 for my $i (reverse @KV, "op=$OP", @ARGV) {
110 $i =~ /^([^=]+)=(.*)$/ or fail "malformed option `$i': missing `='";
111 my ($k, $v) = ($1, $2);
112 unless (exists $seen{$k}) {
114 if ($k eq "type") { $TYPE = $v; }
115 else { push @OPT, "$k=$v"; }
119 defined $TYPE or fail "no snapshot type for device `$DEV'";
122 ###--------------------------------------------------------------------------
123 ### Invoke the type-specific handler.
125 ## Fix up the path, to make sure our tools are available.
126 my $path = $ENV{PATH};
127 my %path = map { $_ => 1 } split /:/, $path;
128 for my $p (qw( /bin /sbin /usr/bin /usr/sbin ), $C{sbin}) {
129 $path = "$p:$path" unless exists $path{$p};
133 ## Prepare the arguments.
134 my @args = ("$C{snap}/snap.$TYPE", $DEV, @OPT);
138 whine "run " . join(" ",
140 grep { s/'/\\'/g; 1 }
144 fail "exec (snap.$TYPE): $!";
147 ###----- That's all, folks --------------------------------------------------