1616// under the License.
1717package com .cloud .hypervisor ;
1818
19+ import java .util .ArrayList ;
20+ import java .util .List ;
21+
1922import javax .ejb .Local ;
2023import javax .inject .Inject ;
2124
2629import com .cloud .host .dao .HostDao ;
2730import com .cloud .hypervisor .Hypervisor .HypervisorType ;
2831import com .cloud .storage .GuestOSVO ;
32+ import com .cloud .storage .Volume ;
33+ import com .cloud .storage .VolumeVO ;
2934import com .cloud .storage .dao .GuestOSDao ;
35+ import com .cloud .storage .dao .VolumeDao ;
3036import com .cloud .template .VirtualMachineTemplate .BootloaderType ;
3137import com .cloud .utils .Pair ;
38+ import com .cloud .vm .VirtualMachine ;
3239import com .cloud .vm .VirtualMachineProfile ;
3340import org .apache .cloudstack .engine .subsystem .api .storage .EndPoint ;
3441import org .apache .cloudstack .engine .subsystem .api .storage .EndPointSelector ;
42+ import org .apache .cloudstack .engine .subsystem .api .storage .VolumeDataFactory ;
3543import org .apache .cloudstack .engine .subsystem .api .storage .ZoneScope ;
3644import org .apache .cloudstack .storage .command .CopyCommand ;
45+ import org .apache .cloudstack .storage .command .DettachCommand ;
46+ import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
47+ import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
3748
3849@ Local (value =HypervisorGuru .class )
3950public class XenServerGuru extends HypervisorGuruBase implements HypervisorGuru {
@@ -42,6 +53,12 @@ public class XenServerGuru extends HypervisorGuruBase implements HypervisorGuru
4253 EndPointSelector endPointSelector ;
4354 @ Inject
4455 HostDao hostDao ;
56+ @ Inject
57+ VolumeDao _volumeDao ;
58+ @ Inject
59+ PrimaryDataStoreDao _storagePoolDao ;
60+ @ Inject
61+ VolumeDataFactory _volFactory ;
4562
4663 protected XenServerGuru () {
4764 super ();
@@ -73,6 +90,39 @@ public boolean trackVmHostChange() {
7390 return true ;
7491 }
7592
93+ @ Override
94+ public List <Command > finalizeExpungeVolumes (VirtualMachine vm ) {
95+ List <Command > commands = new ArrayList <Command >();
96+
97+ List <VolumeVO > volumes = _volumeDao .findByInstance (vm .getId ());
98+
99+ if (volumes != null ) {
100+ for (VolumeVO volume : volumes ) {
101+ if (volume .getVolumeType () == Volume .Type .DATADISK ) {
102+ StoragePoolVO storagePool = _storagePoolDao .findById (volume .getPoolId ());
103+
104+ if (storagePool .isManaged ()) {
105+ DataTO volTO = _volFactory .getVolume (volume .getId ()).getTO ();
106+ DiskTO disk = new DiskTO (volTO , volume .getDeviceId (), volume .getPath (), volume .getVolumeType ());
107+
108+ DettachCommand cmd = new DettachCommand (disk , vm .getInstanceName ());
109+
110+ cmd .setManaged (true );
111+
112+ cmd .setStorageHost (storagePool .getHostAddress ());
113+ cmd .setStoragePort (storagePool .getPort ());
114+
115+ cmd .set_iScsiName (volume .get_iScsiName ());
116+
117+ commands .add (cmd );
118+ }
119+ }
120+ }
121+ }
122+
123+ return commands ;
124+ }
125+
76126 @ Override
77127 public Pair <Boolean , Long > getCommandHostDelegation (long hostId , Command cmd ) {
78128 if (cmd instanceof CopyCommand ) {
0 commit comments