Skip to content

Commit 59e78cb

Browse files
authored
Fix KVM unmanage disks path (apache#8483)
This PR fixes the volumes path on KVM import unmanaged instances Fixes: apache#8479
1 parent 64f4480 commit 59e78cb

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetUnmanagedInstancesCommandWrapper.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ private List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<LibvirtVMD
191191
disk.setLabel(diskDef.getDiskLabel());
192192
disk.setController(diskDef.getBusType().toString());
193193

194-
195194
Pair<String, String> sourceHostPath = getSourceHostPath(libvirtComputingResource, diskDef.getSourcePath());
196195
if (sourceHostPath != null) {
197196
disk.setDatastoreHost(sourceHostPath.first());
@@ -210,11 +209,26 @@ private List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<LibvirtVMD
210209
disk.setDatastorePort(diskDef.getSourceHostPort());
211210
disk.setImagePath(diskDef.getSourcePath());
212211
disk.setDatastoreName(disk.getDatastorePath());
212+
disk.setFileBaseName(getDiskRelativePath(diskDef));
213213
disks.add(disk);
214214
}
215215
return disks;
216216
}
217217

218+
protected String getDiskRelativePath(LibvirtVMDef.DiskDef diskDef) {
219+
if (diskDef == null || diskDef.getDiskType() == null || diskDef.getDiskType() == LibvirtVMDef.DiskDef.DiskType.BLOCK) {
220+
return null;
221+
}
222+
String sourcePath = diskDef.getSourcePath();
223+
if (StringUtils.isBlank(sourcePath)) {
224+
return null;
225+
}
226+
if (!sourcePath.contains("/")) {
227+
return sourcePath;
228+
}
229+
return sourcePath.substring(sourcePath.lastIndexOf("/") + 1);
230+
}
231+
218232
private Pair<String, String> getSourceHostPath(LibvirtComputingResource libvirtComputingResource, String diskPath) {
219233
int pathEnd = diskPath.lastIndexOf("/");
220234
if (pathEnd >= 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.hypervisor.kvm.resource.wrapper;
18+
19+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mockito;
24+
import org.mockito.Spy;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
import java.util.UUID;
28+
29+
@RunWith(MockitoJUnitRunner.class)
30+
public class LibvirtGetUnmanagedInstancesCommandWrapperTest {
31+
32+
@Spy
33+
private LibvirtGetUnmanagedInstancesCommandWrapper wrapper = new LibvirtGetUnmanagedInstancesCommandWrapper();
34+
35+
@Test
36+
public void testGetDiskRelativePathNullDisk() {
37+
Assert.assertNull(wrapper.getDiskRelativePath(null));
38+
}
39+
40+
@Test
41+
public void testGetDiskRelativePathBlockType() {
42+
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
43+
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.BLOCK);
44+
Assert.assertNull(wrapper.getDiskRelativePath(diskDef));
45+
}
46+
47+
@Test
48+
public void testGetDiskRelativePathNullPath() {
49+
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
50+
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
51+
Mockito.when(diskDef.getSourcePath()).thenReturn(null);
52+
Assert.assertNull(wrapper.getDiskRelativePath(diskDef));
53+
}
54+
55+
@Test
56+
public void testGetDiskRelativePathWithoutSlashes() {
57+
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
58+
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
59+
String imagePath = UUID.randomUUID().toString();
60+
Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath);
61+
Assert.assertEquals(imagePath, wrapper.getDiskRelativePath(diskDef));
62+
}
63+
64+
@Test
65+
public void testGetDiskRelativePathFullPath() {
66+
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
67+
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
68+
String relativePath = "ea4b2296-d349-4968-ab72-c8eb523b556e";
69+
String imagePath = String.format("/mnt/97e4c9ed-e3bc-3e26-b103-7967fc9feae1/%s", relativePath);
70+
Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath);
71+
Assert.assertEquals(relativePath, wrapper.getDiskRelativePath(diskDef));
72+
}
73+
}

0 commit comments

Comments
 (0)