diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java index 13dab6942f8b..1953f9a84311 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java @@ -434,7 +434,6 @@ boolean waitForDiskToBecomeAvailable(AddressInfo address, KVMStoragePool pool, l if (waitTimeInSec < 60) { waitTimeInSec = 60; } - KVMPhysicalDisk physicalDisk = null; // Rescan before checking for the physical disk int tries = 0; @@ -463,13 +462,12 @@ boolean waitForDiskToBecomeAvailable(AddressInfo address, KVMStoragePool pool, l output.append(" "); } - physicalDisk = getPhysicalDisk(address, pool); - if (physicalDisk != null && physicalDisk.getSize() > 0) { + // check the device directly, going through getPhysicalDisk would start another connect + if (isConnected(address.getPath()) && getPhysicalDiskSize(address.getPath()) > 0) { LOGGER.debug("Found the volume using id: " + address.getPath() + " of the storage pool: " + pool.getUuid()); return true; } - - break; + LOGGER.debug("Volume " + address.getPath() + " is not usable yet after discovery - try " + tries); } else { LOGGER.warn("Failure discovering LUN via " + connectScript); BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); @@ -504,15 +502,30 @@ boolean waitForDiskToBecomeAvailable(AddressInfo address, KVMStoragePool pool, l } boolean isConnected(String path) { - // run a command to test if this is a binary device at this path + if (!isBlockDevice(path)) { + return false; + } + // A multipath map whose paths have all failed still exists as a block device but cannot be read + if (!hasActivePath(path)) { + LOGGER.warn("Device {} exists but has no active paths, treating it as not connected", path); + return false; + } + return true; + } + + boolean isBlockDevice(String path) { Script blockTest = new Script("/bin/test", LOGGER); blockTest.add("-b", path); blockTest.execute(); - int rc = blockTest.getExitValue(); - if (rc == 0) { - return true; - } - return false; + return blockTest.getExitValue() == 0; + } + + boolean hasActivePath(String path) { + Script pathTest = new Script("/bin/bash", LOGGER); + pathTest.add("-c"); + pathTest.add(String.format("multipath -ll '%s' 2>/dev/null | grep -q 'active ready'", path)); + pathTest.execute(); + return pathTest.getExitValue() == 0; } long getPhysicalDiskSize(String diskPath) { diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBaseTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBaseTest.java new file mode 100644 index 000000000000..91c38d8e5be7 --- /dev/null +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBaseTest.java @@ -0,0 +1,70 @@ +// 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 static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import com.cloud.utils.script.Script; + +public class MultipathSCSIAdapterBaseTest { + + private static final String PATH = "/dev/mapper/3624a937049646854b0ae410b00011266"; + + @BeforeClass + public static void loadAdapterClass() { + // the adapter looks up its helper scripts when the class loads + try (MockedStatic