@@ -161,46 +161,61 @@ public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd)
161161 boolean success = false ;
162162 String snapshotBackupUuid = null ;
163163
164- VmwareContext context = hostService .getServiceContext (cmd );
164+ VmwareContext context = hostService .getServiceContext (cmd );
165+ VirtualMachineMO vmMo ;
166+
165167 try {
166168 VmwareHypervisorHost hyperHost = hostService .getHyperHost (context , cmd );
167169 morDs = hyperHost .findDatastore (cmd .getPool ().getUuid ());
168-
169- VirtualMachineMO vmMo = hyperHost .findVmOnHyperHost (cmd .getVmName ());
170- if (vmMo == null ) {
171- if (s_logger .isDebugEnabled ())
172- s_logger .debug ("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost .getHyperHostName () + ", will try within datacenter" );
173-
174- vmMo = hyperHost .findVmOnPeerHyperHost (cmd .getVmName ());
175- if (vmMo == null ) {
176- dsMo = new DatastoreMO (hyperHost .getContext (), morDs );
177- // restrict VM name to 32 chars, (else snapshot descriptor file name will be truncated to 32 chars of vm name)
178- workerVMName = UUID .randomUUID ().toString ().replaceAll ("-" , "" );
179-
180- // attach a volume to dummay wrapper VM for taking snapshot and exporting the VM for backup
181- if (!hyperHost .createBlankVm (workerVMName , 1 , 512 , 0 , false , 4 , 0 , VirtualMachineGuestOsIdentifier ._otherGuest .toString (), morDs , false )) {
182- String msg = "Unable to create worker VM to execute BackupSnapshotCommand" ;
183- s_logger .error (msg );
184- throw new Exception (msg );
185- }
186- vmMo = hyperHost .findVmOnHyperHost (workerVMName );
187- if (vmMo == null ) {
188- throw new Exception ("Failed to find the newly create or relocated VM. vmName: " + workerVMName );
189- }
190- workerVm = vmMo ;
191170
192- // attach volume to worker VM
193- String datastoreVolumePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), volumePath );
194- vmMo .attachDisk (new String [] { datastoreVolumePath }, morDs );
195- snapshotUUID = UUID .randomUUID ().toString ();
196- if (!vmMo .createSnapshot (snapshotUUID , "Snapshot taken for " + cmd .getSnapshotName (), false , false )) {
197- throw new Exception ("Failed to take snapshot " + cmd .getSnapshotName () + " on vm: " + cmd .getVmName ());
171+ try {
172+ vmMo = hyperHost .findVmOnHyperHost (cmd .getVmName ());
173+ if (vmMo == null ) {
174+ if (s_logger .isDebugEnabled ())
175+ s_logger .debug ("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost .getHyperHostName () + ", will try within datacenter" );
176+
177+ vmMo = hyperHost .findVmOnPeerHyperHost (cmd .getVmName ());
178+ if (vmMo == null ) {
179+ dsMo = new DatastoreMO (hyperHost .getContext (), morDs );
180+
181+ workerVMName = hostService .getWorkerName (context , cmd );
182+
183+ // attach a volume to dummay wrapper VM for taking snapshot and exporting the VM for backup
184+ if (!hyperHost .createBlankVm (workerVMName , 1 , 512 , 0 , false , 4 , 0 , VirtualMachineGuestOsIdentifier ._otherGuest .toString (), morDs , false )) {
185+ String msg = "Unable to create worker VM to execute BackupSnapshotCommand" ;
186+ s_logger .error (msg );
187+ throw new Exception (msg );
188+ }
189+ vmMo = hyperHost .findVmOnHyperHost (workerVMName );
190+ if (vmMo == null ) {
191+ throw new Exception ("Failed to find the newly create or relocated VM. vmName: " + workerVMName );
192+ }
193+ workerVm = vmMo ;
194+
195+ // attach volume to worker VM
196+ String datastoreVolumePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), volumePath );
197+ vmMo .attachDisk (new String [] { datastoreVolumePath }, morDs );
198+ snapshotUUID = UUID .randomUUID ().toString ();
199+ if (!vmMo .createSnapshot (snapshotUUID , "Snapshot taken for " + cmd .getSnapshotName (), false , false )) {
200+ throw new Exception ("Failed to take snapshot " + cmd .getSnapshotName () + " on vm: " + cmd .getVmName ());
201+ }
198202 }
199- }
200- } else {
201- if (!vmMo .createSnapshot (snapshotUuid , "Snapshot taken for " + cmd .getSnapshotName (), false , false )) {
202- throw new Exception ("Failed to take snapshot " + cmd .getSnapshotName () + " on vm: " + cmd .getVmName ());
203- }
203+ } else {
204+ if (!vmMo .createSnapshot (snapshotUuid , "Snapshot taken for " + cmd .getSnapshotName (), false , false )) {
205+ throw new Exception ("Failed to take snapshot " + cmd .getSnapshotName () + " on vm: " + cmd .getVmName ());
206+ }
207+ }
208+ } finally {
209+ try {
210+ if (workerVm != null ) {
211+ // detach volume and destroy worker vm
212+ workerVm .moveAllVmDiskFiles (dsMo , "" , false );
213+ workerVm .detachAllDisks ();
214+ workerVm .destroy ();
215+ }
216+ } catch (Throwable e ) {
217+ s_logger .warn ("Failed to destroy worker VM: " + workerVMName );
218+ }
204219 }
205220
206221 snapshotBackupUuid = backupSnapshotToSecondaryStorage (vmMo , accountId ,
@@ -224,17 +239,6 @@ public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd)
224239
225240 details = "BackupSnapshotCommand exception: " + StringUtils .getExceptionStackInfo (e );
226241 return new BackupSnapshotAnswer (cmd , false , details , snapshotBackupUuid , true );
227- } finally {
228- try {
229- if (workerVm != null ) {
230- // detach volume and destroy worker vm
231- workerVm .moveAllVmDiskFiles (dsMo , "" , false );
232- workerVm .detachAllDisks ();
233- workerVm .destroy ();
234- }
235- } catch (Throwable e ) {
236- s_logger .warn ("Failed to destroy worker VM: " + workerVMName );
237- }
238242 }
239243
240244 return new BackupSnapshotAnswer (cmd , success , details , snapshotBackupUuid , true );
0 commit comments