Manage LVM locks

It's not possible de take 2 snapshot of the same LVM volume at the same time.
This is a problem when using a single file system (backed by a single LVM volume) to store
several VM as trying to run 2 backups at the same time will fail (at least, taking the snapshot for one will fail).

Add locking before taking a snapshot and wait up to 10 sec for the lock to be released. This should fix
concurrent backups
tags/virt-backup-0.2.12-1
Daniel Berteaud 10 years ago
parent d715a0103e
commit 6d8dd735fa
  1. 30
      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;
}

Loading…
Cancel
Save