4 ### Remove an LVM snapshot, without falling foul of LVM bugs
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.
29 use Getopt::Long qw(:config gnu_compat bundling no_ignore_case);
31 use Time::HiRes qw(time);
33 our $VERSION = "@VERSION@";
35 ###--------------------------------------------------------------------------
38 ## Error handling and reporting.
39 (our $QUIS = $0) =~ s:^.*/::;
41 sub whine ($) { my ($msg) = @_; print STDERR "$QUIS: $msg\n"; }
42 sub burble ($) { my ($msg) = @_; whine $msg if $DEBUG; }
43 sub fail ($) { my ($msg) = @_; whine $msg; exit $! || ($? >> 8) || 255; }
45 ## Cleanups. Call `cleanup BLOCK' to arrange to have BLOCK executed at the
46 ## end of the program.
48 sub runcleanups { for my $f (@CLEANUP) { &$f } }
50 $SIG{INT} = $SIG{TERM} = sub {
53 $SIG{$sig} = 'DEFAULT';
56 sub cleanup (&) { unshift @CLEANUP, $_[0]; }
58 sub fixint ($) { my ($x) = @_; return $x =~ /^0/ ? oct $x : $x + 0; }
60 ###--------------------------------------------------------------------------
66 ## Return a sysfs path for a device DEV.
69 my $st = stat $dev or fail "stat ($dev): $!";
71 if (S_ISBLK($st->mode)) { $kind = "block"; }
72 elsif (S_ISCHR($st->mode)) { $kind = "char"; }
73 else { fail "$dev is not a device"; }
74 my ($maj, $min) = (($st->rdev >> 8) & 0xff, $st->rdev & 0xff);
75 (my $whole = realpath "/sys/dev/$kind/$maj:$min") =~ s:^/sys/:/:;
81 sub dmtable_update () {
84 ## Update the device-mapper table in %DMTAB.
86 burble "re-read device-mapper table";
88 open my $dt, "-|", "dmsetup", "table" or fail "open (dm table): $!";
89 while (my $line = $dt->getline) {
90 my ($dev, $rest) = split /[:\s]+/, $line, 2;
91 push @{$DMTAB{$dev}}, [split ' ', $rest];
93 close $dt or fail "dmsetup table failed (rc = $?)";
99 ## Return the device-mapper node name for the sysfs path SYSPATH.
102 open my $f, "<", "/sys$sys/dm/name" or fail "open ($sys/dm/name): $!";
103 chomp (my $name = $f->getline);
108 ###--------------------------------------------------------------------------
112 ## sel TIMEOUT, [READS, WRITES, EXCEPTIONS]
114 ## Wait for at most TIMEOUT seconds (indefinitely if TIMEOUT is `undef').
115 ## Each of READS, WRITES and EXCEPTIONS is a listref containing FILE => SUB
116 ## pairs: if the FILE is readable (writable, has an exceptional condition)
117 ## then the SUB is invoked.
119 my ($t, $r, $w, $x) = @_;
123 ## Read the arguments and build a data structure.
124 for my $i ([$r, \$vr, \%r], [$w, \$vw, \%w], [$x, \$vx, \%x]) {
125 my ($a, $v, $h) = @$i;
129 my ($f, $g) = splice @a, 0, 2;
132 vec($$v, $fd, 1) = 1;
136 ## Do the wait and sift through the results.
137 defined select $vr, $vw, $vx, $t or fail "select: $!";
138 for my $i ([$vr, \%r], [$vw, \%w], [$vx, \%x]) {
140 while (my ($f, $g) = each %$h) {
141 if (vec $v, $f, 1) { &$g; }
147 ## doread FILE, [LEN]
149 ## Read LEN bytes (or a default amount) from FILE. If the file ends,
150 ## return undef. If reading would block then return an empty string.
151 ## Otherwise return he stuff.
154 $n = sysread $f, my $buf, $n // 4096;
155 if (!defined $n) { return "" if $! == EAGAIN; fail "read: $!"; }
156 elsif (!$n) { return undef; }
157 else { return $buf; }
161 ## run WHAT, PROG, ARGS...
163 ## Run PROG, passing it ARGS. Fails if PROG exits nonzero.
165 my ($what, $prog, @args) = @_;
166 system($prog, @args) == 0 or fail "$prog ($what) failed (rc = $?)";
170 ## capture PROG, ARGS...
172 ## Run PROG, passing it ARGS. Returns exit status, stdout, and stderr, as
175 my ($prog, @args) = @_;
176 my ($out, $err) = ("", "");
177 my ($outpipe_in, $outpipe_out, $errpipe_in, $errpipe_out);
178 pipe $outpipe_in, $outpipe_out or fail "pipe ($prog out): $!";
179 pipe $errpipe_in, $errpipe_out or fail "pipe ($prog err): $!";
180 defined (my $kid = fork) or fail "fork ($prog): $!";
183 and close $errpipe_in
184 and open STDOUT, ">&", $outpipe_out
185 and open STDERR, ">&", $errpipe_out
186 and exec $prog, @args
187 or fail "exec $prog: $!";
193 for my $i ([\$outpipe_in, \$out, "out"],
194 [\$errpipe_in, \$err, "err"]) {
195 my ($p, $b, $w) = @$i;
196 push @r, $$p => sub {
197 my $buf = doread $$p;
198 if (defined $buf) { $$b .= $buf; }
199 else { close $$p; $$p = undef; }
205 waitpid $kid, 0 or fail "waitpid ($prog): $!";
206 return $?, $out, $err;
209 ###--------------------------------------------------------------------------
210 ### Monitoring udev events.
212 sub umon_create (@) {
213 ## umon_create ARGS...
215 ## Create a udev monitor, with the given `udevadm monitor' arguments, and
216 ## return an object. We always select only kernel events. We try to wait
217 ## for the monitor to start up before returning. Don't trust this: use
218 ## `umon_sync' anyway.
223 ## Start the monitor process.
224 $u->{KID} = open($u->{PIPE}, "-|",
226 "udevadm", "monitor", "--kernel", "--property", @args)
227 or fail "open (umon): $!";
228 cleanup { kill 9, $u->{KID} };
229 $u->{PIPE}->blocking(0) or fail "set non-blocking (umon): $!";
231 ## Wait for the end of the preamble, indicated by the first blank line.
232 ## From observation with strace(1), this means that the monitor has
233 ## successfully attached itself to its netlink socket and is ready to fetch
242 [ $u->{PIPE} => sub {
243 defined (my $b = doread $u->{PIPE}) or fail "read (umon): eof";
245 if ($buf =~ /\n\n(.*)$/) { $ok = 1; $buf = $1; }
249 if ($now >= $end) { fail "umon timeout"; }
260 ## Read events from UMON, as a list of hash references mapping properties
266 defined (my $buf = doread $u->{PIPE}) or fail "read (umon): end of file";
268 $buf = $u->{BUF} . $buf;
269 my @r = split /\n\n/, $buf, -1;
272 push @s, { map { /^(\w+)=(.*)$/ } split /\n/, $r };
279 ## umon_sync UMON, DEV
281 ## Wait for UMON to report an event about the device DEV (without its
282 ## `/dev/' prefix), triggering periodically just in case it missed one.
283 ## This is useful for synchronizing. Returns the list of events which
284 ## weren't interesting.
291 burble "sync with udev";
295 ## Too late. Trigger a change event and try again.
296 if ($now >= $retry) {
298 run "trigger $dev", "udevadm", "trigger", "--sysname-match=$dev";
301 ## Now read events and see what happens.
304 [ $u->{PIPE} => sub {
305 my @e = umon_read $u;
308 if ($e->{DEVNAME} eq $dev) { $done = 1; push @ev, @e; last; }
309 else { push @ev, $e; }
319 ###--------------------------------------------------------------------------
322 ## Parse the command line.
323 our $USAGE = "usage: $QUIS VGNAME/LVNAME";
324 sub version { print "$QUIS, version $VERSION\n"; }
330 -h, --help Show this help text.
331 -v, --version Show the program version number.
332 -d, --debug Show debugging information.
333 -n, --no-act Don't take corrective actions.
338 GetOptions('help|h|?' => sub { version; help; exit; },
339 'version|v' => sub { version; exit; },
340 'debug|d' => \$DEBUG,
341 'noact|n' => \$NOACT)
343 and @ARGV[0] =~ m:(.+)/(.+):
344 or do { print STDERR $USAGE, "\n"; exit 1; };
345 our ($VG, $LV) = ($1, $2);
347 ## Check that the volume in question actually exists, and is a device-mapper
348 ## device, before we wheel out the big guns.
350 our $SYS = devsys "/dev/$VG/$LV";
351 burble "sysfs name is $SYS";
352 my $t = $DMTAB{dmname $SYS}
353 or fail "/dev/$VG/$LV isn't a device-mapper device";
355 burble "found table...";
356 burble "\t" . join " ", @$_ foreach @$t;
358 $t->[0][2] eq "snapshot" or fail "/dev/$VG/$LV isn't a snapshot";
360 ## Create a udev monitor. We're only interested in disk-shaped block
361 ## devices. (If we use some other device kind for synchronization then this
362 ## filter will have to be broadened.)
363 my $u = umon_create "--subsystem-match=block/disk";
365 ## Prepare for the awful synchronization hack. We need to make sure, below,
366 ## that we've read all of the interesting events resulting from an `lvremove'
367 ## call. To do this, we wait for an event on a different device -- but we
368 ## must avoid being fooled by spurious events on this device. As an attempt
369 ## to minimize the probability of this going wrong, acquire a pet device
370 ## which nobody else is using. The best idea seems to be a loopback device.
371 open my $lopipe, "-|", "losetup", "--show", "--find", "/etc/motd"
372 or fail "open (losetup attach)";
373 chomp (my $lo = $lopipe->getline);
374 { local $/ = undef; <$lopipe>; }
376 $lopipe->close or fail "wait (losetup attach): $!";
377 cleanup { system "losetup", "--detach", "/dev/$lo" };
379 ## Initial synchronization, to make sure stuff works.
382 ## Try to remove the snapshot. Capture stdout and stderr, and relay them if
383 ## nothing serious went wrong.
384 burble "initial attempt to remove snapshot";
385 my ($rc, $out, $err) = capture "lvremove", "--force", "$VG/$LV";
389 burble "lvremove didn't explode (rc = $rc): we're done here";
390 if ($rc >> 8) { $rc >>= 8 }
391 elsif ($rc & 255) { $rc += 128 }
394 burble "initial lvremove failed";
396 ## OK, stuff went wrong. First see if there was a udev cookie left over, and
397 ## if so try to release it. It's important to know that we've read all of
398 ## the relevant uevents, so synchronize again.
399 my @e = umon_sync $u, $lo;
402 $c{($e->{DM_COOKIE} & 0xffff) | 0xd4d0000} = 1
403 if $e->{DEVPATH} eq $SYS && exists $e->{DM_COOKIE};
405 burble "cookies used: " . join ", ", map { sprintf "0x%x", $_ } keys %c;
407 ## Find the used cookies which are still extant, and release them.
408 open $uc, "-|", "dmsetup", "udevcookies" or fail "open (cookies): $!";
411 while (my $l = $uc->getline) {
412 my @f = split ' ', $l;
413 push @leak, $f[0] if $c{fixint $f[0]};
415 close $uc or fail "udevcookies failed (rc = $?)";
417 burble "release leaked cookie $c";
418 run "release cookie", "dmsetup", "udevreleasecookie", $c unless $NOACT;
421 ## If we're very unlucky, the origin volume may still be suspended. Resume
422 ## it now, or the next attempt will get stuck. (Resuming is idempotent, so
423 ## we don't need to check whether it's already running.) Finding the origin
424 ## is annoying: search the device-mapper table for a device with a
425 ## `snapshot-origin' table referencing the same backing store as the
427 my $back = $DMTAB{dmname $SYS}[0][3];
429 burble "backend device $back";
430 for my $dm (keys %DMTAB) {
432 next unless @$t == 1 &&
433 $t->[0][2] eq "snapshot-origin" &&
435 defined $orig and fail "snapshot appears to have multiple origins";
438 defined $orig or fail "couldn't find snapshot origin device";
439 burble "found origin volume $orig; resuming...";
440 run "resume origin $orig", "dmsetup", "resume", $orig unless $NOACT;
442 ## See whether removing the snapshot again helps any.
443 burble "retry snapshot removal";
444 run "retry", "lvremove", "--force", "$VG/$LV" unless $NOACT;
446 ## OK, we're on the way to recovery. The origin device may now be not a
447 ## snapshot-origin any more. Refresh the device-mapper table and inspect it.
449 if (-d "/sys/dev/block/$back") {
450 my $backdm = dmname "/dev/block/$back";
451 if ($DMTAB{$orig}[0][2] ne "snapshot-origin") {
452 burble "origin released but backend $backdm still exists: remove";
453 run "remove backend $backdm", "dmsetup", "remove", $backdm
458 ## All done. There, that wasn't so bad, was it?
459 burble "completed successfully";
462 ###----- That's all, folks --------------------------------------------------