| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Sys::Virt; |
| 7 |
|
| 8 |
|
| 9 |
my $c = Sys::Virt->new(uri => "qemu:///session"); |
| 10 |
|
| 11 |
my $st = $c->new_stream(); |
| 12 |
|
| 13 |
die "syntax: $0 VOL-PATH OUTPUT-FILE" unless int(@ARGV) == 2; |
| 14 |
|
| 15 |
my $volpath = shift @ARGV; |
| 16 |
my $f = shift @ARGV; |
| 17 |
|
| 18 |
my $vol = $c->get_storage_volume_by_path($volpath); |
| 19 |
|
| 20 |
open FILE, ">$f" or die "cannot create $f: $!"; |
| 21 |
|
| 22 |
sub foo { |
| 23 |
my $st = shift; |
| 24 |
my $data = shift; |
| 25 |
my $nbytes = shift; |
| 26 |
return syswrite FILE, $data, $nbytes; |
| 27 |
}; |
| 28 |
|
| 29 |
eval { |
| 30 |
$vol->download($st, 0, 0); |
| 31 |
$st->recv_all(\&foo); |
| 32 |
|
| 33 |
$st->finish(); |
| 34 |
}; |
| 35 |
if ($@) { |
| 36 |
unlink $f if $@; |
| 37 |
close FILE; |
| 38 |
die $@; |
| 39 |
} |
| 40 |
close FILE or die "cannot save $f: $!"; |
| 41 |
|