3 ### Remove an LVM snapshot, without falling foul of LVM bugs
5 ### (c) 2011 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the distorted.org.uk backup suite.
12 ### distorted-backup is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### distorted-backup is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License along
23 ### with distorted-backup; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 use Getopt::Long qw(:config gnu_compat bundling no_ignore_case);
32 use Time::HiRes qw(time);
34 our $VERSION = "@VERSION@";
36 ###--------------------------------------------------------------------------
39 ## Error handling and reporting.
40 (our $QUIS = $0) =~ s:^.*/::;
42 sub whine ($) { my ($msg) = @_; print STDERR "$QUIS: $msg\n"; }
43 sub burble ($) { my ($msg) = @_; whine $msg if $DEBUG; }
44 sub fail ($) { my ($msg) = @_; whine $msg; exit $! || ($? >> 8) || 255; }
46 ## Cleanups. Call `cleanup BLOCK' to arrange to have BLOCK executed at the
47 ## end of the program.
49 sub runcleanups { for my $f (@CLEANUP) { &$f } }
51 $SIG{INT} = $SIG{TERM} = sub {
54 $SIG{$sig} = 'DEFAULT';
57 sub cleanup (&) { unshift @CLEANUP, $_[0]; }
59 sub fixint ($) { my ($x) = @_; return $x =~ /^0/ ? oct $x : $x + 0; }
61 ###--------------------------------------------------------------------------
67 ## Return a sysfs path for a device DEV.
70 my $st = stat $dev or fail "stat ($dev): $!";
72 if (S_ISBLK($st->mode)) { $kind = "block"; }
73 elsif (S_ISCHR($st->mode)) { $kind = "char"; }
74 else { fail "$dev is not a device"; }
75 my ($maj, $min) = (($st->rdev >> 8) & 0xff, $st->rdev & 0xff);
76 (my $whole = realpath "/sys/dev/$kind/$maj:$min") =~ s:^/sys/:/:;
82 sub dmtable_update () {
85 ## Update the device-mapper table in %DMTAB.
87 burble "re-read device-mapper table";
89 open my $dt, "-|", "dmsetup", "table" or fail "open (dm table): $!";
90 while (my $line = $dt->getline) {
91 my ($dev, $rest) = split /[:\s]+/, $line, 2;
92 push @{$DMTAB{$dev}}, [split ' ', $rest];
94 close $dt or fail "dmsetup table failed (rc = $?)";
100 ## Return the device-mapper node name for the sysfs path SYSPATH.
103 open my $f, "<", "/sys$sys/dm/name" or fail "open ($sys/dm/name): $!";
104 chomp (my $name = $f->getline);
109 ###--------------------------------------------------------------------------
113 ## sel TIMEOUT, [READS, WRITES, EXCEPTIONS]
115 ## Wait for at most TIMEOUT seconds (indefinitely if TIMEOUT is `undef').
116 ## Each of READS, WRITES and EXCEPTIONS is a listref containing FILE => SUB
117 ## pairs: if the FILE is readable (writable, has an exceptional condition)
118 ## then the SUB is invoked.
120 my ($t, $r, $w, $x) = @_;
124 ## Read the arguments and build a data structure.
125 for my $i ([$r, \$vr, \%r], [$w, \$vw, \%w], [$x, \$vx, \%x]) {
126 my ($a, $v, $h) = @$i;
130 my ($f, $g) = splice @a, 0, 2;
133 vec($$v, $fd, 1) = 1;
137 ## Do the wait and sift through the results.
138 defined select $vr, $vw, $vx, $t or fail "select: $!";
139 for my $i ([$vr, \%r], [$vw, \%w], [$vx, \%x]) {
141 while (my ($f, $g) = each %$h) {
142 if (vec $v, $f, 1) { &$g; }
148 ## doread FILE, [LEN]
150 ## Read LEN bytes (or a default amount) from FILE. If the file ends,
151 ## return undef. If reading would block then return an empty string.
152 ## Otherwise return he stuff.
155 $n = sysread $f, my $buf, $n // 4096;
156 if (!defined $n) { return "" if $! == EAGAIN; fail "read: $!"; }
157 elsif (!$n) { return undef; }
158 else { return $buf; }
162 ## run WHAT, PROG, ARGS...
164 ## Run PROG, passing it ARGS. Fails if PROG exits nonzero.
166 my ($what, $prog, @args) = @_;
167 system($prog, @args) == 0 or fail "$prog ($what) failed (rc = $?)";
171 ## capture PROG, ARGS...
173 ## Run PROG, passing it ARGS. Returns exit status, stdout, and stderr, as
176 my ($prog, @args) = @_;
177 my ($out, $err) = ("", "");
178 my ($outpipe_in, $outpipe_out, $errpipe_in, $errpipe_out);
179 pipe $outpipe_in, $outpipe_out or fail "pipe ($prog out): $!";
180 pipe $errpipe_in, $errpipe_out or fail "pipe ($prog err): $!";
181 defined (my $kid = fork) or fail "fork ($prog): $!";
184 and close $errpipe_in
185 and open STDOUT, ">&", $outpipe_out
186 and open STDERR, ">&", $errpipe_out
187 and exec $prog, @args
188 or fail "exec $prog: $!";
194 for my $i ([\$outpipe_in, \$out, "out"],
195 [\$errpipe_in, \$err, "err"]) {
196 my ($p, $b, $w) = @$i;
197 push @r, $$p => sub {
198 my $buf = doread $$p;
199 if (defined $buf) { $$b .= $buf; }
200 else { close $$p; $$p = undef; }
206 waitpid $kid, 0 or fail "waitpid ($prog): $!";
207 return $?, $out, $err;
210 ###--------------------------------------------------------------------------
211 ### Monitoring udev events.
213 sub umon_create (@) {
214 ## umon_create ARGS...
216 ## Create a udev monitor, with the given `udevadm monitor' arguments, and
217 ## return an object. We always select only kernel events. We try to wait
218 ## for the monitor to start up before returning. Don't trust this: use
219 ## `umon_sync' anyway.
224 ## Start the monitor process.
225 $u->{KID} = open($u->{PIPE}, "-|",
227 "udevadm", "monitor", "--kernel", "--property", @args)
228 or fail "open (umon): $!";
229 cleanup { kill 9, $u->{KID} };
230 $u->{PIPE}->blocking(0) or fail "set non-blocking (umon): $!";
232 ## Wait for the end of the preamble, indicated by the first blank line.
233 ## From observation with strace(1), this means that the monitor has
234 ## successfully attached itself to its netlink socket and is ready to fetch
243 [ $u->{PIPE} => sub {
244 defined (my $b = doread $u->{PIPE}) or fail "read (umon): eof";
246 if ($buf =~ /\n\n(.*)$/) { $ok = 1; $buf = $1; }
250 if ($now >= $end) { fail "umon timeout"; }
261 ## Read events from UMON, as a list of hash references mapping properties
267 defined (my $buf = doread $u->{PIPE}) or fail "read (umon): end of file";
269 $buf = $u->{BUF} . $buf;
270 my @r = split /\n\n/, $buf, -1;
273 push @s, { map { /^(\w+)=(.*)$/ } split /\n/, $r };
280 ## umon_sync UMON, DEV
282 ## Wait for UMON to report an event about the device DEV (without its
283 ## `/dev/' prefix), triggering periodically just in case it missed one.
284 ## This is useful for synchronizing. Returns the list of events which
285 ## weren't interesting.
292 burble "sync with udev";
296 ## Too late. Trigger a change event and try again.
297 if ($now >= $retry) {
299 run "trigger $dev", "udevadm", "trigger", "--sysname-match=$dev";
302 ## Now read events and see what happens.
305 [ $u->{PIPE} => sub {
306 my @e = umon_read $u;
309 if ($e->{DEVNAME} eq $dev) { $done = 1; push @ev, @e; last; }
310 else { push @ev, $e; }
320 ###--------------------------------------------------------------------------
323 ## Parse the command line.
324 our $USAGE = "usage: $QUIS VGNAME/LVNAME";
325 sub version { print "$QUIS, version $VERSION\n"; }
331 -h, --help Show this help text.
332 -v, --version Show the program version number.
333 -d, --debug Show debugging information.
334 -n, --no-act Don't take corrective actions.
339 GetOptions('help|h|?' => sub { version; help; exit; },
340 'version|v' => sub { version; exit; },
341 'debug|d' => \$DEBUG,
342 'noact|n' => \$NOACT)
344 and @ARGV[0] =~ m:(.+)/(.+):
345 or do { print STDERR $USAGE, "\n"; exit 1; };
346 our ($VG, $LV) = ($1, $2);
348 ## Check that the volume in question actually exists, and is a device-mapper
349 ## device, before we wheel out the big guns.
351 our $SYS = devsys "/dev/$VG/$LV";
352 burble "sysfs name is $SYS";
353 my $t = $DMTAB{dmname $SYS}
354 or fail "/dev/$VG/$LV isn't a device-mapper device";
356 burble "found table...";
357 burble "\t" . join " ", @$_ foreach @$t;
359 $t->[0][2] eq "snapshot" or fail "/dev/$VG/$LV isn't a snapshot";
361 ## Create a udev monitor. We're only interested in disk-shaped block
362 ## devices. (If we use some other device kind for synchronization then this
363 ## filter will have to be broadened.)
364 my $u = umon_create "--subsystem-match=block/disk";
366 ## Prepare for the awful synchronization hack. We need to make sure, below,
367 ## that we've read all of the interesting events resulting from an `lvremove'
368 ## call. To do this, we wait for an event on a different device -- but we
369 ## must avoid being fooled by spurious events on this device. As an attempt
370 ## to minimize the probability of this going wrong, acquire a pet device
371 ## which nobody else is using. The best idea seems to be a loopback device.
372 open my $lopipe, "-|", "losetup", "--show", "--find", "/etc/motd"
373 or fail "open (losetup attach)";
374 chomp (my $lo = $lopipe->getline);
375 { local $/ = undef; <$lopipe>; }
377 $lopipe->close or fail "wait (losetup attach): $!";
378 cleanup { system "losetup", "--detach", "/dev/$lo" };
380 ## Initial synchronization, to make sure stuff works.
383 ## Try to remove the snapshot. Capture stdout and stderr, and relay them if
384 ## nothing serious went wrong.
385 burble "initial attempt to remove snapshot";
386 my ($rc, $out, $err) = capture "lvremove", "--force", "$VG/$LV";
390 burble "lvremove didn't explode (rc = $rc): we're done here";
391 if ($rc >> 8) { $rc >>= 8 }
392 elsif ($rc & 255) { $rc += 128 }
395 burble "initial lvremove failed";
397 ## OK, stuff went wrong. First see if there was a udev cookie left over, and
398 ## if so try to release it. It's important to know that we've read all of
399 ## the relevant uevents, so synchronize again.
400 my @e = umon_sync $u, $lo;
403 $c{($e->{DM_COOKIE} & 0xffff) | 0xd4d0000} = 1
404 if $e->{DEVPATH} eq $SYS && exists $e->{DM_COOKIE};
406 burble "cookies used: " . join ", ", map { sprintf "0x%x", $_ } keys %c;
408 ## Find the used cookies which are still extant, and release them.
409 open $uc, "-|", "dmsetup", "udevcookies" or fail "open (cookies): $!";
412 while (my $l = $uc->getline) {
413 my @f = split ' ', $l;
414 push @leak, $f[0] if $c{fixint $f[0]};
416 close $uc or fail "udevcookies failed (rc = $?)";
418 burble "release leaked cookie $c";
419 run "release cookie", "dmsetup", "udevreleasecookie", $c unless $NOACT;
422 ## If we're very unlucky, the origin volume may still be suspended. Resume
423 ## it now, or the next attempt will get stuck. (Resuming is idempotent, so
424 ## we don't need to check whether it's already running.) Finding the origin
425 ## is annoying: search the device-mapper table for a device with a
426 ## `snapshot-origin' table referencing the same backing store as the
428 my $back = $DMTAB{dmname $SYS}[0][3];
430 burble "backend device $back";
431 for my $dm (keys %DMTAB) {
433 next unless @$t == 1 &&
434 $t->[0][2] eq "snapshot-origin" &&
436 defined $orig and fail "snapshot appears to have multiple origins";
439 defined $orig or fail "couldn't find snapshot origin device";
440 burble "found origin volume $orig; resuming...";
441 run "resume origin $orig", "dmsetup", "resume", $orig unless $NOACT;
443 ## See whether removing the snapshot again helps any.
444 burble "retry snapshot removal";
445 run "retry", "lvremove", "--force", "$VG/$LV" unless $NOACT;
447 ## OK, we're on the way to recovery. The origin device may now be not a
448 ## snapshot-origin any more. Refresh the device-mapper table and inspect it.
450 if (-d "/sys/dev/block/$back") {
451 my $backdm = dmname "/dev/block/$back";
452 if ($DMTAB{$orig}[0][2] ne "snapshot-origin") {
453 burble "origin released but backend $backdm still exists: remove";
454 run "remove backend $backdm", "dmsetup", "remove", $backdm
459 ## All done. There, that wasn't so bad, was it?
460 burble "completed successfully";
463 ###----- That's all, folks --------------------------------------------------