diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptor.java index ba689d5107f7..f8aa8b8adcd7 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptor.java @@ -353,20 +353,27 @@ public boolean disconnectPhysicalDiskByPath(String localPath) { String search3 = "-iscsi-"; String search4 = "-lun-"; - if (!localPath.contains(search3)) { + if (localPath == null || !localPath.startsWith(search1) || !localPath.contains(search3)) { return false; } - int index = localPath.indexOf(search2); + int index = localPath.indexOf(search2, search1.length()); + int index2 = localPath.indexOf(search3); - String host = localPath.substring(search1.length(), index); + if (index < 0 || index2 < index) { + return false; + } - int index2 = localPath.indexOf(search3); + String host = localPath.substring(search1.length(), index); String port = localPath.substring(index + search2.length(), index2); index = localPath.indexOf(search4); + if (index < index2) { + return false; + } + String iqn = localPath.substring(index2 + search3.length(), index); String lun = localPath.substring(index + search4.length()); diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptorTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptorTest.java new file mode 100644 index 000000000000..78d479459554 --- /dev/null +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/IscsiAdmStorageAdaptorTest.java @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.hypervisor.kvm.storage; + +import org.junit.Assert; +import org.junit.Test; + +public class IscsiAdmStorageAdaptorTest { + + private final IscsiAdmStorageAdaptor adaptor = new IscsiAdmStorageAdaptor(); + + @Test + public void disconnectPhysicalDiskByPathTestSharedMountPointPathContainingIscsiReturnsFalse() { + Assert.assertFalse(adaptor.disconnectPhysicalDiskByPath("/mnt/fa-iscsi-primary/4650a2f7-fce5-48e2-beaa-bcdf063194e6")); + } + + @Test + public void disconnectPhysicalDiskByPathTestPathWithoutIscsiReturnsFalse() { + Assert.assertFalse(adaptor.disconnectPhysicalDiskByPath("/mnt/primary/4650a2f7-fce5-48e2-beaa-bcdf063194e6")); + } + + @Test + public void disconnectPhysicalDiskByPathTestByPathWithoutPortReturnsFalse() { + Assert.assertFalse(adaptor.disconnectPhysicalDiskByPath("/dev/disk/by-path/ip-10.0.0.1-iscsi-iqn.2012-03.com.test:vol1-lun-0")); + } +}