-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-rbd-diff
executable file
·336 lines (260 loc) · 7.18 KB
/
apply-rbd-diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env perl
use 5.010;
use utf8;
use strict;
use warnings;
use Fcntl qw(SEEK_SET);
# Make Time::HiRes an optional dependency. It's only used for the
# progress meter, so we can do without it.
BEGIN {
if (eval { require Time::HiRes; }) {
Time::HiRes->import("time");
}
}
# The maximum number of bytes we will read at a time.
use constant MAX_READ => 1048576;
sub main;
sub _read;
sub size;
sub progress;
sub readleint;
sub read_snap;
sub read_size;
sub read_data;
sub read_end;
main(@ARGV);
die "shouldn't happen: fell out of main";
sub main {
if (@_) {
# Keep Pod::Perldoc as an optional dependency.
if (eval { require Pod::Perldoc; }) {
@ARGV = ("-T", __FILE__);
*STDOUT = *STDERR;
Pod::Perldoc->run;
}
die "no arguments expected";
}
my %readers = (
f => \&read_snap,
t => \&read_snap,
s => \&read_size,
w => \&read_data,
z => \&read_data,
e => \&read_end,
);
open my $in, "<&=", 0 or die "open stdin: $!";
open my $out, ">&=", 1 or die "open stdout: $!";
open my $zero, "<", "/dev/zero" or die "open /dev/zero: $!";
my $ident = _read $in, 12;
die "read from stdin: $@" unless defined $ident;
die "stdin is not an rbd diff v1 stream"
unless $ident eq "rbd diff v1\n";
while (1) {
my $type = _read $in, 1;
die "read from stdin: $@" unless defined $type;
my $r = $readers{$type} or
die "unknown record type in rbd diff: $type";
&$r($type, $in, $out, $zero);
}
}
sub _read {
my ($in, $want) = @_;
my ($count, $out);
my $off = 0;
while ($want) {
my $count = read $in, $out, $want, $off;
unless (defined $count) {
$@ = "$!";
return undef;
}
unless ($count) {
$@ = "unexpected end of stream";
return undef;
}
$want -= $count;
$off += $count;
}
$out;
}
sub size {
state $size = undef;
my $newsize = shift;
$size = $newsize if defined $newsize;
$size;
}
sub progress {
my ($pos, $bytes) = @_;
state $start = time;
state $time = $start;
state $speed = 0;
state $cbytes = 0;
state $tbytes = 0;
$cbytes += $bytes;
$tbytes += $bytes;
if ($pos eq "done") {
# Produce the final progress state by calculating the entire
# duration of the copy. Prevent division-by-zero by assuming a
# duration of 1 second in that case.
my $elapsed = time - $start;
$elapsed = 1 if $elapsed == 0;
$speed = $tbytes / $elapsed;
} elsif ($cbytes) {
my $newtime = time;
my $elapsed = $newtime - $time;
# Avoid division-by-zero by only updating $speed when 'time'
# changes.
if ($elapsed) {
$speed = $cbytes / $elapsed;
$cbytes = 0;
$time = $newtime;
}
}
my $hspeed = $speed;
for (
[1024 * 1024 * 1024 * 1024, "TiB/s"],
[1024 * 1024 * 1024, "GiB/s"],
[1024 * 1024, "MiB/s"],
[1024, "KiB/s"],
[0, "B/s"],
) {
if ($hspeed >= $_->[0]) {
$hspeed /= $_->[0] if $_->[0] != 0;
$hspeed = sprintf "%.3g %s", $hspeed, $_->[1];
last;
}
}
print STDERR "\r[";
my ($len, $p) = (50, undef);
if ($pos eq "done") {
$p = 1;
printf STDERR ("=" x $len);
} elsif (my $size = size) {
$p = $pos / $size;
my $prog = int($p * $len);
$prog = $len if $prog > $len;
my $left = $len - $prog - 1;
$left = 0 if $left < 0;
print STDERR ("=" x $prog);
print STDERR ">" if $prog < $len;
print STDERR (" " x $left);
} else {
printf STDERR "%-${len}s", "(unknown size)";
}
print STDERR "] (";
if (defined $p) {
printf STDERR "%d%%", ($p * 100);
} else {
print STDERR "??%";
}
printf STDERR ", %-11s", "$hspeed)";
}
sub readleint {
my ($in, $bytes) = @_;
local $_ = _read $in, $bytes;
return undef unless defined $_;
unpack ($bytes == 8 ? "q<" : "l<");
}
sub read_snap {
my ($type, $in, $out, $zero) = @_;
my $len = readleint $in, 4;
die "read from stdin: $@" unless defined $len;
die "bad snapshot name length: $len" if $len < 0;
die "excessive snapshot name length: $len" if $len > MAX_READ;
# Skip over the snapshot name; we won't use it.
die "read from stdin: $@" unless defined (_read $in, $len);
}
sub read_size {
my ($type, $in, $out, $zero) = @_;
my $size = readleint $in, 8;
die "read from stdin: $@" unless defined $size;
die "bad image size: $size" if $size < 0;
size $size;
progress 0, 0;
}
sub read_data {
my ($type, $in, $out, $zero) = @_;
my $off = readleint $in, 8;
die "read from stdin: $@" unless defined $off;
die "bad chunk offset: $off" if $off < 0;
my $len = readleint $in, 8;
die "read from stdin: $@" unless defined $len;
die "bad chunk length: $len" if $len < 0;
if (defined (my $size = size)) {
die "chunk of $len bytes at offset $off extends past size $size"
if ($off + $len) > $size;
}
progress $off, 0;
my $in_is = "stdin";
if ($type eq "z") {
$in = $zero;
$in_is = "/dev/zero";
}
while ($len) {
my $want = MAX_READ;
$want = $len if $len < $want;
my $count = read $in, (my $data), $want;
die "read from $in_is: $!" unless defined $count;
die "read from $in_is: unexpected end of stream" unless $count;
sysseek $out, $off, SEEK_SET or die "seek on stdout: $!";
my ($todo, $wcount, $woff) = ($count, 0, 0);
while ($todo) {
my $wcount = syswrite $out, $data, $todo, $woff;
if (!defined $wcount) {
next if $!{EINTR};
die "write to stdout: $!";
}
$todo -= $wcount;
$woff += $wcount;
}
$len -= $count;
$off += $count;
progress $off, $count;
}
}
sub read_end {
progress "done", 0;
print STDERR "\n";
exit 0;
}
=head1 NAME
apply-rbd-diff - apply an RBD diff to an ordinary volume
=head1 SYNOPSIS
rbd --no-progress export-diff pool/vol - --from-snap snap | \
apply-rbd-diff >/dev/sd0a
=head1 DESCRIPTION
This script reads an RBD diff stream on standard input and writes the
changed portions of the image to standard output. It presents a
progress meter on standard error.
No arguments are accepted at present. If any are provided, this
documentation will be displayed on standard error and the script will
exit unsuccessfully.
=head1 COMPATIBILITY
This script has no dependencies other than Perl 5.10 or newer and the
Fcntl module included with Perl. It can therefore simply be copied onto
any relatively recent system and run.
Note that some additional functionality may be available if you have
other Perl modules installed:
=over 4
=item Time::HiRes
Improves the precision of the progress meter.
=item Pod::Perldoc
Displays this documentation if invalid arguments are supplied.
=back
=head1 SEE ALSO
rbd(8),
L<RBD Incremental Backup|http://docs.ceph.com/docs/master/dev/rbd-diff/>
=head1 AUTHOR
Steven McDonald, E<lt>[email protected]<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2016 Anchor Systems Pty Ltd
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
=cut