@@ -61,19 +61,21 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
6161 List <String > backedVolumeUUIDs = command .getBackupVolumesUUIDs ();
6262 List <String > restoreVolumePaths = command .getRestoreVolumePaths ();
6363 String restoreVolumeUuid = command .getRestoreVolumeUUID ();
64+ Integer mountTimeout = command .getMountTimeout () * 1000 ;
6465
6566 String newVolumeId = null ;
6667 try {
68+ String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions , mountTimeout );
6769 if (Objects .isNull (vmExists )) {
6870 String volumePath = restoreVolumePaths .get (0 );
6971 int lastIndex = volumePath .lastIndexOf ("/" );
7072 newVolumeId = volumePath .substring (lastIndex + 1 );
71- restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
72- new Pair <>(vmName , command .getVmState ()), mountOptions );
73+ restoreVolume (backupPath , volumePath , diskType , restoreVolumeUuid ,
74+ new Pair <>(vmName , command .getVmState ()), mountDirectory );
7375 } else if (Boolean .TRUE .equals (vmExists )) {
74- restoreVolumesOfExistingVM (restoreVolumePaths , backedVolumeUUIDs , backupPath , backupRepoType , backupRepoAddress , mountOptions );
76+ restoreVolumesOfExistingVM (restoreVolumePaths , backedVolumeUUIDs , backupPath , mountDirectory );
7577 } else {
76- restoreVolumesOfDestroyedVMs (restoreVolumePaths , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
78+ restoreVolumesOfDestroyedVMs (restoreVolumePaths , vmName , backupPath , mountDirectory );
7779 }
7880 } catch (CloudRuntimeException e ) {
7981 String errorMessage = e .getMessage () != null ? e .getMessage () : "" ;
@@ -92,10 +94,9 @@ private void verifyBackupFile(String backupPath, String volUuid) {
9294 }
9395 }
9496
95- private void restoreVolumesOfExistingVM (List <String > restoreVolumePaths , List <String > backedVolumesUUIDs , String backupPath ,
96- String backupRepoType , String backupRepoAddress , String mountOptions ) {
97+ private void restoreVolumesOfExistingVM (List <String > restoreVolumePaths , List <String > backedVolumesUUIDs ,
98+ String backupPath , String mountDirectory ) {
9799 String diskType = "root" ;
98- String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
99100 try {
100101 for (int idx = 0 ; idx < restoreVolumePaths .size (); idx ++) {
101102 String restoreVolumePath = restoreVolumePaths .get (idx );
@@ -113,9 +114,7 @@ private void restoreVolumesOfExistingVM(List<String> restoreVolumePaths, List<St
113114 }
114115 }
115116
116- private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath ,
117- String backupRepoType , String backupRepoAddress , String mountOptions ) {
118- String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
117+ private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath , String mountDirectory ) {
119118 String diskType = "root" ;
120119 try {
121120 for (int i = 0 ; i < volumePaths .size (); i ++) {
@@ -133,9 +132,8 @@ private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmNam
133132 }
134133 }
135134
136- private void restoreVolume (String backupPath , String backupRepoType , String backupRepoAddress , String volumePath ,
137- String diskType , String volumeUUID , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
138- String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
135+ private void restoreVolume (String backupPath , String volumePath , String diskType , String volumeUUID ,
136+ Pair <String , VirtualMachine .State > vmNameAndState , String mountDirectory ) {
139137 Pair <String , String > bkpPathAndVolUuid ;
140138 try {
141139 bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , volumeUUID );
@@ -155,36 +153,42 @@ private void restoreVolume(String backupPath, String backupRepoType, String back
155153 }
156154
157155
158- private String mountBackupDirectory (String backupRepoAddress , String backupRepoType , String mountOptions ) {
156+ private String mountBackupDirectory (String backupRepoAddress , String backupRepoType , String mountOptions , Integer mountTimeout ) {
159157 String randomChars = RandomStringUtils .random (5 , true , false );
160158 String mountDirectory = String .format ("%s.%s" ,BACKUP_TEMP_FILE_PREFIX , randomChars );
159+
161160 try {
162161 mountDirectory = Files .createTempDirectory (mountDirectory ).toString ();
163- String mount = String .format (MOUNT_COMMAND , backupRepoType , backupRepoAddress , mountDirectory );
164- if ("cifs" .equals (backupRepoType )) {
165- if (Objects .isNull (mountOptions ) || mountOptions .trim ().isEmpty ()) {
166- mountOptions = "nobrl" ;
167- } else {
168- mountOptions += ",nobrl" ;
169- }
170- }
171- if (Objects .nonNull (mountOptions ) && !mountOptions .trim ().isEmpty ()) {
172- mount += " -o " + mountOptions ;
162+ } catch (IOException e ) {
163+ logger .error (String .format ("Failed to create the tmp mount directory {} for restore" , mountDirectory ), e );
164+ throw new CloudRuntimeException ("Failed to create the tmp mount directory for restore on the KVM host" );
165+ }
166+
167+ String mount = String .format (MOUNT_COMMAND , backupRepoType , backupRepoAddress , mountDirectory );
168+ if ("cifs" .equals (backupRepoType )) {
169+ if (Objects .isNull (mountOptions ) || mountOptions .trim ().isEmpty ()) {
170+ mountOptions = "nobrl" ;
171+ } else {
172+ mountOptions += ",nobrl" ;
173173 }
174- Script .runSimpleBashScript (mount );
175- } catch (Exception e ) {
176- logger .error (String .format ("Failed to mount repository {} of type {} to the directory {}" , backupRepoAddress , backupRepoType , mountDirectory ), e );
174+ }
175+ if (Objects .nonNull (mountOptions ) && !mountOptions .trim ().isEmpty ()) {
176+ mount += " -o " + mountOptions ;
177+ }
178+
179+ int exitValue = Script .runSimpleBashScriptForExitValue (mount , mountTimeout , false );
180+ if (exitValue != 0 ) {
181+ logger .error (String .format ("Failed to mount repository {} of type {} to the directory {}" , backupRepoAddress , backupRepoType , mountDirectory ));
177182 throw new CloudRuntimeException ("Failed to mount the backup repository on the KVM host" );
178183 }
179184 return mountDirectory ;
180185 }
181186
182187 private void unmountBackupDirectory (String backupDirectory ) {
183- try {
184- String umountCmd = String .format (UMOUNT_COMMAND , backupDirectory );
185- Script .runSimpleBashScript (umountCmd );
186- } catch (Exception e ) {
187- logger .error (String .format ("Failed to unmount backup directory {}" , backupDirectory ), e );
188+ String umountCmd = String .format (UMOUNT_COMMAND , backupDirectory );
189+ int exitValue = Script .runSimpleBashScriptForExitValue (umountCmd );
190+ if (exitValue != 0 ) {
191+ logger .error (String .format ("Failed to unmount backup directory {}" , backupDirectory ));
188192 throw new CloudRuntimeException ("Failed to unmount the backup directory" );
189193 }
190194 }
0 commit comments