Skip to content

Commit 2932522

Browse files
committed
implement listByIsoId method in VmIsoMapDao and update TemplateManagerImpl for ISO deletion checks
1 parent 7717002 commit 2932522

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

engine/schema/src/main/java/com/cloud/vm/dao/VmIsoMapDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
public interface VmIsoMapDao extends GenericDao<VmIsoMapVO, Long> {
2525
List<VmIsoMapVO> listByVmId(long vmId);
2626

27+
List<VmIsoMapVO> listByIsoId(long isoId);
28+
2729
VmIsoMapVO findByVmIdDeviceSeq(long vmId, int deviceSeq);
2830

2931
VmIsoMapVO findByVmIdIsoId(long vmId, long isoId);

engine/schema/src/main/java/com/cloud/vm/dao/VmIsoMapDaoImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class VmIsoMapDaoImpl extends GenericDaoBase<VmIsoMapVO, Long> implements VmIsoMapDao {
3030

3131
private SearchBuilder<VmIsoMapVO> ListByVmId;
32+
private SearchBuilder<VmIsoMapVO> ListByIsoId;
3233
private SearchBuilder<VmIsoMapVO> ByVmIdDeviceSeq;
3334
private SearchBuilder<VmIsoMapVO> ByVmIdIsoId;
3435

@@ -37,6 +38,10 @@ protected VmIsoMapDaoImpl() {
3738
ListByVmId.and("vmId", ListByVmId.entity().getVmId(), SearchCriteria.Op.EQ);
3839
ListByVmId.done();
3940

41+
ListByIsoId = createSearchBuilder();
42+
ListByIsoId.and("isoId", ListByIsoId.entity().getIsoId(), SearchCriteria.Op.EQ);
43+
ListByIsoId.done();
44+
4045
ByVmIdDeviceSeq = createSearchBuilder();
4146
ByVmIdDeviceSeq.and("vmId", ByVmIdDeviceSeq.entity().getVmId(), SearchCriteria.Op.EQ);
4247
ByVmIdDeviceSeq.and("deviceSeq", ByVmIdDeviceSeq.entity().getDeviceSeq(), SearchCriteria.Op.EQ);
@@ -55,6 +60,13 @@ public List<VmIsoMapVO> listByVmId(long vmId) {
5560
return listBy(sc);
5661
}
5762

63+
@Override
64+
public List<VmIsoMapVO> listByIsoId(long isoId) {
65+
SearchCriteria<VmIsoMapVO> sc = ListByIsoId.create();
66+
sc.setParameters("isoId", isoId);
67+
return listBy(sc);
68+
}
69+
5870
@Override
5971
public VmIsoMapVO findByVmIdDeviceSeq(long vmId, int deviceSeq) {
6072
SearchCriteria<VmIsoMapVO> sc = ByVmIdDeviceSeq.create();

server/src/main/java/com/cloud/template/TemplateManagerImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,17 +1244,20 @@ protected TemplateManagerImpl() {
12441244

12451245
@Override
12461246
public boolean templateIsDeleteable(long templateId) {
1247+
// ISO can only be referenced by user_vm.iso_id (primary cdrom slot) or vm_iso_map (extra slots).
1248+
// Templates always live on primary storage and aren't tracked here.
12471249
List<UserVmJoinVO> userVmUsingIso = _userVmJoinDao.listActiveByIsoId(templateId);
1248-
// check if there is any Vm using this ISO. We only need to check the
1249-
// case where templateId is an ISO since
1250-
// VM can be launched from ISO in secondary storage, while template will
1251-
// always be copied to
1252-
// primary storage before deploying VM.
12531250
if (!userVmUsingIso.isEmpty()) {
12541251
logger.debug("ISO " + templateId + " is not deleteable because it is attached to " + userVmUsingIso.size() + " Instances");
12551252
return false;
12561253
}
1257-
1254+
for (VmIsoMapVO row : _vmIsoMapDao.listByIsoId(templateId)) {
1255+
UserVmVO vm = _userVmDao.findById(row.getVmId());
1256+
if (vm != null && vm.getState() != State.Error && vm.getState() != State.Expunging) {
1257+
logger.debug("ISO " + templateId + " is not deleteable because it is attached to Instance " + vm.getUuid() + " at slot " + row.getDeviceSeq());
1258+
return false;
1259+
}
1260+
}
12581261
return true;
12591262
}
12601263

0 commit comments

Comments
 (0)