Skip to content

Commit 7cfd1a1

Browse files
abh1sarjschoiRR
authored andcommitted
Fix restore from NAS backup when datadisk is older than the root disk. (apache#11258)
1 parent 2901187 commit 7cfd1a1

File tree

5 files changed

+48
-43
lines changed

5 files changed

+48
-43
lines changed

plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.text.SimpleDateFormat;
5252
import java.util.ArrayList;
5353
import java.util.Collections;
54+
import java.util.Comparator;
5455
import java.util.Date;
5556
import java.util.List;
5657
import java.util.Locale;
@@ -162,6 +163,7 @@ public boolean takeBackup(final VirtualMachine vm) {
162163

163164
if (VirtualMachine.State.Stopped.equals(vm.getState())) {
164165
List<VolumeVO> vmVolumes = volumeDao.findByInstance(vm.getId());
166+
vmVolumes.sort(Comparator.comparing(Volume::getDeviceId));
165167
List<String> volumePaths = getVolumePaths(vmVolumes);
166168
command.setVolumePaths(volumePaths);
167169
}
@@ -212,7 +214,10 @@ private BackupVO createBackupObject(VirtualMachine vm, String backupPath) {
212214
@Override
213215
public boolean restoreVMFromBackup(VirtualMachine vm, Backup backup) {
214216
List<Backup.VolumeInfo> backedVolumes = backup.getBackedUpVolumes();
215-
List<VolumeVO> volumes = backedVolumes.stream().map(volume -> volumeDao.findByUuid(volume.getUuid())).collect(Collectors.toList());
217+
List<VolumeVO> volumes = backedVolumes.stream()
218+
.map(volume -> volumeDao.findByUuid(volume.getUuid()))
219+
.sorted((v1, v2) -> Long.compare(v1.getDeviceId(), v2.getDeviceId()))
220+
.collect(Collectors.toList());
216221

217222
LOG.debug("Restoring vm {} from backup {} on the NAS Backup Provider", vm, backup);
218223
BackupRepository backupRepository = getBackupRepository(vm, backup);

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,25 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
6262
String restoreVolumeUuid = command.getRestoreVolumeUUID();
6363

6464
String newVolumeId = null;
65-
if (Objects.isNull(vmExists)) {
66-
String volumePath = volumePaths.get(0);
67-
int lastIndex = volumePath.lastIndexOf("/");
68-
newVolumeId = volumePath.substring(lastIndex + 1);
69-
restoreVolume(backupPath, backupRepoType, backupRepoAddress, volumePath, diskType, restoreVolumeUuid,
70-
new Pair<>(vmName, command.getVmState()), mountOptions);
71-
} else if (Boolean.TRUE.equals(vmExists)) {
72-
restoreVolumesOfExistingVM(volumePaths, backupPath, backupRepoType, backupRepoAddress, mountOptions);
73-
} else {
74-
restoreVolumesOfDestroyedVMs(volumePaths, vmName, backupPath, backupRepoType, backupRepoAddress, mountOptions);
65+
try {
66+
if (Objects.isNull(vmExists)) {
67+
String volumePath = volumePaths.get(0);
68+
int lastIndex = volumePath.lastIndexOf("/");
69+
newVolumeId = volumePath.substring(lastIndex + 1);
70+
restoreVolume(backupPath, backupRepoType, backupRepoAddress, volumePath, diskType, restoreVolumeUuid,
71+
new Pair<>(vmName, command.getVmState()), mountOptions);
72+
} else if (Boolean.TRUE.equals(vmExists)) {
73+
restoreVolumesOfExistingVM(volumePaths, backupPath, backupRepoType, backupRepoAddress, mountOptions);
74+
} else {
75+
restoreVolumesOfDestroyedVMs(volumePaths, vmName, backupPath, backupRepoType, backupRepoAddress, mountOptions);
76+
}
77+
} catch (CloudRuntimeException e) {
78+
String errorMessage = "Failed to restore backup for VM: " + vmName + ".";
79+
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
80+
errorMessage += " Details: " + e.getMessage();
81+
}
82+
logger.error(errorMessage);
83+
return new BackupAnswer(command, false, errorMessage);
7584
}
7685

7786
return new BackupAnswer(command, true, newVolumeId);
@@ -86,10 +95,8 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
8695
String volumePath = volumePaths.get(idx);
8796
Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, null);
8897
diskType = "datadisk";
89-
try {
90-
replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first());
91-
} catch (IOException e) {
92-
throw new CloudRuntimeException(String.format("Unable to revert backup for volume [%s] due to [%s].", bkpPathAndVolUuid.second(), e.getMessage()), e);
98+
if (!replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first())) {
99+
throw new CloudRuntimeException(String.format("Unable to restore backup for volume [%s].", bkpPathAndVolUuid.second()));
93100
}
94101
}
95102
} finally {
@@ -108,10 +115,8 @@ private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmNam
108115
String volumePath = volumePaths.get(i);
109116
Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, null);
110117
diskType = "datadisk";
111-
try {
112-
replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first());
113-
} catch (IOException e) {
114-
throw new CloudRuntimeException(String.format("Unable to revert backup for volume [%s] due to [%s].", bkpPathAndVolUuid.second(), e.getMessage()), e);
118+
if (!replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first())) {
119+
throw new CloudRuntimeException(String.format("Unable to restore backup for volume [%s].", bkpPathAndVolUuid.second()));
115120
}
116121
}
117122
} finally {
@@ -126,15 +131,13 @@ private void restoreVolume(String backupPath, String backupRepoType, String back
126131
Pair<String, String> bkpPathAndVolUuid;
127132
try {
128133
bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, volumeUUID);
129-
try {
130-
replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first());
131-
if (VirtualMachine.State.Running.equals(vmNameAndState.second())) {
132-
if (!attachVolumeToVm(vmNameAndState.first(), volumePath)) {
133-
throw new CloudRuntimeException(String.format("Failed to attach volume to VM: %s", vmNameAndState.first()));
134-
}
134+
if (!replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first())) {
135+
throw new CloudRuntimeException(String.format("Unable to restore backup for volume [%s].", bkpPathAndVolUuid.second()));
136+
}
137+
if (VirtualMachine.State.Running.equals(vmNameAndState.second())) {
138+
if (!attachVolumeToVm(vmNameAndState.first(), volumePath)) {
139+
throw new CloudRuntimeException(String.format("Failed to attach volume to VM: %s", vmNameAndState.first()));
135140
}
136-
} catch (IOException e) {
137-
throw new CloudRuntimeException(String.format("Unable to revert backup for volume [%s] due to [%s].", bkpPathAndVolUuid.second(), e.getMessage()), e);
138141
}
139142
} catch (Exception e) {
140143
throw new CloudRuntimeException("Failed to restore volume", e);
@@ -194,8 +197,9 @@ private Pair<String, String> getBackupPath(String mountDirectory, String volumeP
194197
return new Pair<>(bkpPath, volUuid);
195198
}
196199

197-
private void replaceVolumeWithBackup(String volumePath, String backupPath) throws IOException {
198-
Script.runSimpleBashScript(String.format(RSYNC_COMMAND, backupPath, volumePath));
200+
private boolean replaceVolumeWithBackup(String volumePath, String backupPath) {
201+
int exitValue = Script.runSimpleBashScriptForExitValue(String.format(RSYNC_COMMAND, backupPath, volumePath));
202+
return exitValue == 0;
199203
}
200204

201205
private boolean attachVolumeToVm(String vmName, String volumePath) {

server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,9 @@ public Volume attachVolumeToVM(Long vmId, Long volumeId, Long deviceId, Boolean
25952595

25962596
excludeLocalStorageIfNeeded(volumeToAttach);
25972597

2598-
checkForDevicesInCopies(vmId, vm);
2598+
checkForVMSnapshots(vmId, vm);
2599+
2600+
checkForBackups(vm, true);
25992601

26002602
checkRightsToAttach(caller, volumeToAttach, vm);
26012603

@@ -2697,18 +2699,12 @@ private void checkRightsToAttach(Account caller, VolumeInfo volumeToAttach, User
26972699
}
26982700
}
26992701

2700-
private void checkForDevicesInCopies(Long vmId, UserVmVO vm) {
2702+
private void checkForVMSnapshots(Long vmId, UserVmVO vm) {
27012703
// if target VM has associated VM snapshots
27022704
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vmId);
27032705
if (vmSnapshots.size() > 0) {
27042706
throw new InvalidParameterValueException(String.format("Unable to attach volume to VM %s/%s, please specify a VM that does not have VM snapshots", vm.getName(), vm.getUuid()));
27052707
}
2706-
2707-
// if target VM has backups
2708-
List<Backup> backups = backupDao.listByVmId(vm.getDataCenterId(), vm.getId());
2709-
if (vm.getBackupOfferingId() != null && !backups.isEmpty()) {
2710-
throw new InvalidParameterValueException(String.format("Unable to attach volume to VM %s/%s, please specify a VM that does not have any backups", vm.getName(), vm.getUuid()));
2711-
}
27122708
}
27132709

27142710
/**
@@ -2811,7 +2807,7 @@ private void checkDeviceId(Long deviceId, VolumeInfo volumeToAttach, UserVmVO vm
28112807
return volumeToAttach;
28122808
}
28132809

2814-
protected void validateIfVmHasBackups(UserVmVO vm, boolean attach) {
2810+
protected void checkForBackups(UserVmVO vm, boolean attach) {
28152811
if ((vm.getBackupOfferingId() == null || CollectionUtils.isEmpty(vm.getBackupVolumeList())) || BooleanUtils.isTrue(BackupManager.BackupEnableAttachDetachVolumes.value())) {
28162812
return;
28172813
}
@@ -3050,7 +3046,7 @@ public Volume detachVolumeFromVM(DetachVolumeCmd cmmd) {
30503046
throw new InvalidParameterValueException("Unable to detach volume, please specify a VM that does not have VM snapshots");
30513047
}
30523048

3053-
validateIfVmHasBackups(vm, false);
3049+
checkForBackups(vm, false);
30543050

30553051
AsyncJobExecutionContext asyncExecutionContext = AsyncJobExecutionContext.getCurrentExecutionContext();
30563052
if (asyncExecutionContext != null) {

server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ public boolean deleteBackupOffering(final Long offeringId) {
320320

321321
public static String createVolumeInfoFromVolumes(List<VolumeVO> vmVolumes) {
322322
List<Backup.VolumeInfo> list = new ArrayList<>();
323+
vmVolumes.sort(Comparator.comparing(VolumeVO::getDeviceId));
323324
for (VolumeVO vol : vmVolumes) {
324325
list.add(new Backup.VolumeInfo(vol.getUuid(), vol.getPath(), vol.getVolumeType(), vol.getSize()));
325326
}

server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ public void testResourceLimitCheckForUploadedVolume() throws NoSuchFieldExceptio
654654
when(vm.getState()).thenReturn(State.Running);
655655
when(vm.getDataCenterId()).thenReturn(34L);
656656
when(vm.getBackupOfferingId()).thenReturn(null);
657-
when(backupDaoMock.listByVmId(anyLong(), anyLong())).thenReturn(Collections.emptyList());
658657
when(volumeDaoMock.findByInstanceAndType(anyLong(), any(Volume.Type.class))).thenReturn(new ArrayList<>(10));
659658
when(volumeDataFactoryMock.getVolume(9L)).thenReturn(volumeToAttach);
660659
when(volumeToAttach.getState()).thenReturn(Volume.State.Uploaded);
@@ -1293,7 +1292,7 @@ public void validateIfVmHaveBackupsTestExceptionWhenTryToDetachVolumeFromVMWhich
12931292
try {
12941293
UserVmVO vm = Mockito.mock(UserVmVO.class);
12951294
when(vm.getBackupOfferingId()).thenReturn(1l);
1296-
volumeApiServiceImpl.validateIfVmHasBackups(vm, false);
1295+
volumeApiServiceImpl.checkForBackups(vm, false);
12971296
} catch (Exception e) {
12981297
Assert.assertEquals("Unable to detach volume, cannot detach volume from a VM that has backups. First remove the VM from the backup offering or set the global configuration 'backup.enable.attach.detach.of.volumes' to true.", e.getMessage());
12991298
}
@@ -1304,7 +1303,7 @@ public void validateIfVmHaveBackupsTestExceptionWhenTryToAttachVolumeFromVMWhich
13041303
try {
13051304
UserVmVO vm = Mockito.mock(UserVmVO.class);
13061305
when(vm.getBackupOfferingId()).thenReturn(1l);
1307-
volumeApiServiceImpl.validateIfVmHasBackups(vm, true);
1306+
volumeApiServiceImpl.checkForBackups(vm, true);
13081307
} catch (Exception e) {
13091308
Assert.assertEquals("Unable to attach volume, please specify a VM that does not have any backups or set the global configuration 'backup.enable.attach.detach.of.volumes' to true.", e.getMessage());
13101309
}
@@ -1314,7 +1313,7 @@ public void validateIfVmHaveBackupsTestExceptionWhenTryToAttachVolumeFromVMWhich
13141313
public void validateIfVmHaveBackupsTestSuccessWhenVMDontHaveBackupOffering() {
13151314
UserVmVO vm = Mockito.mock(UserVmVO.class);
13161315
when(vm.getBackupOfferingId()).thenReturn(null);
1317-
volumeApiServiceImpl.validateIfVmHasBackups(vm, true);
1316+
volumeApiServiceImpl.checkForBackups(vm, true);
13181317
}
13191318

13201319
@Test

0 commit comments

Comments
 (0)