diff --git a/virt-backup b/virt-backup index 3f96d9b..e78bf95 100644 --- a/virt-backup +++ b/virt-backup @@ -776,15 +776,33 @@ sub save_xml{ sub create_snapshot{ my ($blk,$suffix) = @_; my $ret = 0; + my $lock = $blk; + $lock =~ s/\//\-/g; + $lock = $opts{backupdir} . '/' . $lock . '.lock'; my $cmd = "$opts{lvcreate} -s -n " . $blk . $suffix . " -L $opts{snapsize} $blk > /dev/null 2>&1 < /dev/null\n"; - print "Running: $cmd" if $opts{debug}; - if ( system("$cmd") == 0 ) { - $ret = 1; - open SNAPLIST, ">>$backupdir.meta/snapshots" or die "Error, couldn't open snapshot list file\n"; - print SNAPLIST $blk.$suffix ."\n"; - close SNAPLIST; + for ($cnt = 0; $cnt < 10; $cnt++ ){ + print "Running: $cmd" if $opts{debug}; + if (-e "$lock" . '.lock'){ + print "Volume $blk is locked...\n" if $opts{debug}; + sleep(1); + } + else{ + open ( LOCK, ">$lock" ); + print LOCK ""; + close LOCK; + if ( system("$cmd") == 0 ) { + $ret = 1; + open SNAPLIST, ">>$backupdir.meta/snapshots" or die "Error, couldn't open snapshot list file\n"; + print SNAPLIST $blk.$suffix ."\n"; + close SNAPLIST; + # break the loop now + $cnt = 10; + } + } } + # In any case, failed or not, remove our lock + unlink <$lock>; return $ret; }