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