Skip to content

Commit 08106e3

Browse files
Refactoring the LibvirtComputingResource
- Adding LibvirtResizeVolumeCommandWrapper - 5 unit tests added - KVM hypervisor plugin with 22.1% coverage
1 parent ae505e7 commit 08106e3

4 files changed

Lines changed: 321 additions & 93 deletions

File tree

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java‎

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.libvirt.DomainInterfaceStats;
6161
import org.libvirt.LibvirtException;
6262
import org.libvirt.NodeInfo;
63-
import org.libvirt.StorageVol;
6463

6564
import com.cloud.agent.api.Answer;
6665
import com.cloud.agent.api.Command;
@@ -80,15 +79,12 @@
8079
import com.cloud.agent.api.routing.IpAssocVpcCommand;
8180
import com.cloud.agent.api.routing.NetworkElementCommand;
8281
import com.cloud.agent.api.routing.SetSourceNatCommand;
83-
import com.cloud.agent.api.storage.ResizeVolumeAnswer;
84-
import com.cloud.agent.api.storage.ResizeVolumeCommand;
8582
import com.cloud.agent.api.to.DataStoreTO;
8683
import com.cloud.agent.api.to.DataTO;
8784
import com.cloud.agent.api.to.DiskTO;
8885
import com.cloud.agent.api.to.IpAddressTO;
8986
import com.cloud.agent.api.to.NfsTO;
9087
import com.cloud.agent.api.to.NicTO;
91-
import com.cloud.agent.api.to.StorageFilerTO;
9288
import com.cloud.agent.api.to.VirtualMachineTO;
9389
import com.cloud.agent.resource.virtualnetwork.VirtualRouterDeployer;
9490
import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
@@ -408,6 +404,10 @@ public String getOvsPvlanVmPath() {
408404
return _ovsPvlanVmPath;
409405
}
410406

407+
public String getResizeVolumePath() {
408+
return _resizeVolumePath;
409+
}
410+
411411
private static final class KeyValueInterpreter extends OutputInterpreter {
412412
private final Map<String, String> map = new HashMap<String, String>();
413413

@@ -1259,8 +1259,6 @@ public Answer executeRequest(final Command cmd) {
12591259
return execute((StartCommand)cmd);
12601260
} else if (cmd instanceof NetworkElementCommand) {
12611261
return _virtRouterResource.executeRequest((NetworkElementCommand)cmd);
1262-
} else if (cmd instanceof ResizeVolumeCommand) {
1263-
return execute((ResizeVolumeCommand)cmd);
12641262
} else if (cmd instanceof StorageSubSystemCommand) {
12651263
return storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
12661264
} else {
@@ -1401,7 +1399,7 @@ public KVMPhysicalDisk templateToPrimaryDownload(final String templateUrl, final
14011399
}
14021400
}
14031401

1404-
private String getResizeScriptType(final KVMStoragePool pool, final KVMPhysicalDisk vol) {
1402+
public String getResizeScriptType(final KVMStoragePool pool, final KVMPhysicalDisk vol) {
14051403
final StoragePoolType poolType = pool.getType();
14061404
final PhysicalDiskFormat volFormat = vol.getFormat();
14071405

@@ -1417,92 +1415,6 @@ private String getResizeScriptType(final KVMStoragePool pool, final KVMPhysicalD
14171415
throw new CloudRuntimeException("Cannot determine resize type from pool type " + pool.getType());
14181416
}
14191417

1420-
/* uses a local script now, eventually support for virStorageVolResize() will maybe work on
1421-
qcow2 and lvm and we can do this in libvirt calls */
1422-
public Answer execute(final ResizeVolumeCommand cmd) {
1423-
final String volid = cmd.getPath();
1424-
final long newSize = cmd.getNewSize();
1425-
final long currentSize = cmd.getCurrentSize();
1426-
final String vmInstanceName = cmd.getInstanceName();
1427-
final boolean shrinkOk = cmd.getShrinkOk();
1428-
final StorageFilerTO spool = cmd.getPool();
1429-
final String notifyOnlyType = "NOTIFYONLY";
1430-
1431-
if ( currentSize == newSize) {
1432-
// nothing to do
1433-
s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
1434-
return new ResizeVolumeAnswer(cmd, true, "success", currentSize);
1435-
}
1436-
1437-
try {
1438-
KVMStoragePool pool = _storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
1439-
final KVMPhysicalDisk vol = pool.getPhysicalDisk(volid);
1440-
final String path = vol.getPath();
1441-
String type = getResizeScriptType(pool, vol);
1442-
1443-
if (pool.getType() != StoragePoolType.RBD) {
1444-
if (type.equals("QCOW2") && shrinkOk) {
1445-
return new ResizeVolumeAnswer(cmd, false, "Unable to shrink volumes of type " + type);
1446-
}
1447-
} else {
1448-
s_logger.debug("Volume " + path + " is on a RBD storage pool. No need to query for additional information.");
1449-
}
1450-
1451-
s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + type + "," + vmInstanceName + "," + shrinkOk);
1452-
1453-
/* libvirt doesn't support resizing (C)LVM devices, and corrupts QCOW2 in some scenarios, so we have to do these via Bash script */
1454-
if (pool.getType() != StoragePoolType.CLVM && vol.getFormat() != PhysicalDiskFormat.QCOW2) {
1455-
s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
1456-
try {
1457-
final Connect conn = LibvirtConnection.getConnection();
1458-
final StorageVol v = conn.storageVolLookupByPath(path);
1459-
int flags = 0;
1460-
1461-
if (conn.getLibVirVersion() > 1001000 && vol.getFormat() == PhysicalDiskFormat.RAW && pool.getType() != StoragePoolType.RBD) {
1462-
flags = 1;
1463-
}
1464-
if (shrinkOk) {
1465-
flags = 4;
1466-
}
1467-
1468-
v.resize(newSize, flags);
1469-
type = notifyOnlyType;
1470-
} catch (final LibvirtException e) {
1471-
return new ResizeVolumeAnswer(cmd, false, e.toString());
1472-
}
1473-
}
1474-
s_logger.debug("Invoking resize script to handle type " + type);
1475-
final Script resizecmd = new Script(_resizeVolumePath, _cmdsTimeout, s_logger);
1476-
resizecmd.add("-s", String.valueOf(newSize));
1477-
resizecmd.add("-c", String.valueOf(currentSize));
1478-
resizecmd.add("-p", path);
1479-
resizecmd.add("-t", type);
1480-
resizecmd.add("-r", String.valueOf(shrinkOk));
1481-
resizecmd.add("-v", vmInstanceName);
1482-
final String result = resizecmd.execute();
1483-
1484-
if (result != null) {
1485-
if(type.equals(notifyOnlyType)) {
1486-
return new ResizeVolumeAnswer(cmd, true, "Resize succeeded, but need reboot to notify guest");
1487-
} else {
1488-
return new ResizeVolumeAnswer(cmd, false, result);
1489-
}
1490-
}
1491-
1492-
/* fetch new size as seen from libvirt, don't want to assume anything */
1493-
pool = _storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
1494-
pool.refresh();
1495-
final long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
1496-
s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
1497-
return new ResizeVolumeAnswer(cmd, true, "success", finalSize);
1498-
} catch (final CloudRuntimeException e) {
1499-
final String error = "Failed to resize volume: " + e.getMessage();
1500-
s_logger.debug(error);
1501-
return new ResizeVolumeAnswer(cmd, false, error);
1502-
}
1503-
1504-
}
1505-
15061418
private String getBroadcastUriFromBridge(final String brName) {
15071419
final String pif = matchPifFileInDirectory(brName);
15081420
final Pattern pattern = Pattern.compile("(\\D+)(\\d+)(\\D*)(\\d*)");

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.cloud.agent.api.storage.CreateCommand;
7575
import com.cloud.agent.api.storage.DestroyCommand;
7676
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
77+
import com.cloud.agent.api.storage.ResizeVolumeCommand;
7778
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
7879
import com.cloud.resource.CommandWrapper;
7980
import com.cloud.resource.RequestWrapper;
@@ -148,6 +149,7 @@ private void init() {
148149
linbvirtCommands.put(CreatePrivateTemplateFromSnapshotCommand.class, new LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper());
149150
linbvirtCommands.put(CopyVolumeCommand.class, new LibvirtCopyVolumeCommandWrapper());
150151
linbvirtCommands.put(PvlanSetupCommand.class, new LibvirtPvlanSetupCommandWrapper());
152+
linbvirtCommands.put(ResizeVolumeCommand.class, new LibvirtResizeVolumeCommandWrapper());
151153

152154
resources.put(LibvirtComputingResource.class, linbvirtCommands);
153155
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.kvm.resource.wrapper;
21+
22+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
23+
import org.apache.log4j.Logger;
24+
import org.libvirt.Connect;
25+
import org.libvirt.LibvirtException;
26+
import org.libvirt.StorageVol;
27+
28+
import com.cloud.agent.api.Answer;
29+
import com.cloud.agent.api.storage.ResizeVolumeAnswer;
30+
import com.cloud.agent.api.storage.ResizeVolumeCommand;
31+
import com.cloud.agent.api.to.StorageFilerTO;
32+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
33+
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
34+
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
35+
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
36+
import com.cloud.resource.CommandWrapper;
37+
import com.cloud.storage.Storage.StoragePoolType;
38+
import com.cloud.utils.exception.CloudRuntimeException;
39+
import com.cloud.utils.script.Script;
40+
41+
/*
42+
* Uses a local script now, eventually support for virStorageVolResize() will maybe work on qcow2 and lvm and we can do this in libvirt calls
43+
*/
44+
public final class LibvirtResizeVolumeCommandWrapper extends CommandWrapper<ResizeVolumeCommand, Answer, LibvirtComputingResource> {
45+
46+
private static final Logger s_logger = Logger.getLogger(LibvirtResizeVolumeCommandWrapper.class);
47+
48+
@Override
49+
public Answer execute(final ResizeVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
50+
final String volid = command.getPath();
51+
final long newSize = command.getNewSize();
52+
final long currentSize = command.getCurrentSize();
53+
final String vmInstanceName = command.getInstanceName();
54+
final boolean shrinkOk = command.getShrinkOk();
55+
final StorageFilerTO spool = command.getPool();
56+
final String notifyOnlyType = "NOTIFYONLY";
57+
58+
if ( currentSize == newSize) {
59+
// nothing to do
60+
s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
61+
return new ResizeVolumeAnswer(command, true, "success", currentSize);
62+
}
63+
64+
try {
65+
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
66+
KVMStoragePool pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
67+
68+
final KVMPhysicalDisk vol = pool.getPhysicalDisk(volid);
69+
final String path = vol.getPath();
70+
String type = libvirtComputingResource.getResizeScriptType(pool, vol);
71+
72+
if (pool.getType() != StoragePoolType.RBD) {
73+
if (type.equals("QCOW2") && shrinkOk) {
74+
return new ResizeVolumeAnswer(command, false, "Unable to shrink volumes of type " + type);
75+
}
76+
} else {
77+
s_logger.debug("Volume " + path + " is on a RBD storage pool. No need to query for additional information.");
78+
}
79+
80+
s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + type + "," + vmInstanceName + "," + shrinkOk);
81+
82+
/* libvirt doesn't support resizing (C)LVM devices, and corrupts QCOW2 in some scenarios, so we have to do these via Bash script */
83+
if (pool.getType() != StoragePoolType.CLVM && vol.getFormat() != PhysicalDiskFormat.QCOW2) {
84+
s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
85+
try {
86+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
87+
88+
final Connect conn = libvirtUtilitiesHelper.getConnection();
89+
final StorageVol v = conn.storageVolLookupByPath(path);
90+
int flags = 0;
91+
92+
if (conn.getLibVirVersion() > 1001000 && vol.getFormat() == PhysicalDiskFormat.RAW && pool.getType() != StoragePoolType.RBD) {
93+
flags = 1;
94+
}
95+
if (shrinkOk) {
96+
flags = 4;
97+
}
98+
99+
v.resize(newSize, flags);
100+
type = notifyOnlyType;
101+
} catch (final LibvirtException e) {
102+
return new ResizeVolumeAnswer(command, false, e.toString());
103+
}
104+
}
105+
s_logger.debug("Invoking resize script to handle type " + type);
106+
107+
final Script resizecmd = new Script(libvirtComputingResource.getResizeVolumePath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
108+
resizecmd.add("-s", String.valueOf(newSize));
109+
resizecmd.add("-c", String.valueOf(currentSize));
110+
resizecmd.add("-p", path);
111+
resizecmd.add("-t", type);
112+
resizecmd.add("-r", String.valueOf(shrinkOk));
113+
resizecmd.add("-v", vmInstanceName);
114+
final String result = resizecmd.execute();
115+
116+
if (result != null) {
117+
if(type.equals(notifyOnlyType)) {
118+
return new ResizeVolumeAnswer(command, true, "Resize succeeded, but need reboot to notify guest");
119+
} else {
120+
return new ResizeVolumeAnswer(command, false, result);
121+
}
122+
}
123+
124+
/* fetch new size as seen from libvirt, don't want to assume anything */
125+
pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
126+
pool.refresh();
127+
final long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
128+
s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
129+
return new ResizeVolumeAnswer(command, true, "success", finalSize);
130+
} catch (final CloudRuntimeException e) {
131+
final String error = "Failed to resize volume: " + e.getMessage();
132+
s_logger.debug(error);
133+
return new ResizeVolumeAnswer(command, false, error);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)