From 6d8dd735fa588a22afbcb46dd0f0856046615e7c Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Tue, 22 Apr 2014 10:22:20 +0200 Subject: [PATCH] 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 --- virt-backup | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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; }