Skip to content

Commit

Permalink
Allow KVM VM live migration with ROOT volume on file storage type (#2997
Browse files Browse the repository at this point in the history
)

* Allow KVM VM live migration with ROOT volume on file

* Allow KVM VM live migration with ROOT volume on file
- Add JUnit tests

* Address reviewers and change some variable names to ease future
implementation (developers can easily guess the name and use
autocomplete)
  • Loading branch information
GabrielBrascher authored and rafaelweingartner committed Dec 14, 2018
1 parent ecd2b95 commit bf20940
Show file tree
Hide file tree
Showing 17 changed files with 899 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class VirtualMachineTO {
private long id;
private String name;
private BootloaderType bootloader;
private VirtualMachine.State state;
Type type;
int cpus;

Expand Down Expand Up @@ -147,6 +148,14 @@ public void setBootloader(BootloaderType bootloader) {
this.bootloader = bootloader;
}

public VirtualMachine.State getState() {
return state;
}

public void setState(VirtualMachine.State state) {
this.state = state;
}

public int getCpus() {
return cpus;
}
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/com/cloud/vm/DiskProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public DiskProfile(Volume vol, DiskOffering offering, HypervisorType hyperType)
offering.isCustomized(),
null);
this.hyperType = hyperType;
this.provisioningType = offering.getProvisioningType();
}

public DiskProfile(DiskProfile dp) {
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/com/cloud/agent/api/MigrateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package com.cloud.agent.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloud.agent.api.to.VirtualMachineTO;
Expand All @@ -33,6 +35,7 @@ public class MigrateCommand extends Command {
private boolean isWindows;
private VirtualMachineTO vmTO;
private boolean executeInSequence = false;
private List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();

protected MigrateCommand() {
}
Expand Down Expand Up @@ -90,6 +93,14 @@ public boolean executeInSequence() {
return executeInSequence;
}

public List<MigrateDiskInfo> getMigrateDiskInfoList() {
return migrateDiskInfoList;
}

public void setMigrateDiskInfoList(List<MigrateDiskInfo> migrateDiskInfoList) {
this.migrateDiskInfoList = migrateDiskInfoList;
}

public static class MigrateDiskInfo {
public enum DiskType {
FILE, BLOCK;
Expand Down Expand Up @@ -123,6 +134,7 @@ public String toString() {
private final DriverType driverType;
private final Source source;
private final String sourceText;
private boolean isSourceDiskOnStorageFileSystem;

public MigrateDiskInfo(final String serialNumber, final DiskType diskType, final DriverType driverType, final Source source, final String sourceText) {
this.serialNumber = serialNumber;
Expand Down Expand Up @@ -151,5 +163,13 @@ public Source getSource() {
public String getSourceText() {
return sourceText;
}

public boolean isSourceDiskOnStorageFileSystem() {
return isSourceDiskOnStorageFileSystem;
}

public void setSourceDiskOnStorageFileSystem(boolean isDiskOnFileSystemStorage) {
this.isSourceDiskOnStorageFileSystem = isDiskOnFileSystemStorage;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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 org.apache.cloudstack.storage.motion;

import java.io.File;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority;
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;

import com.cloud.agent.api.Answer;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.MigrateCommand.MigrateDiskInfo;
import com.cloud.agent.api.storage.CreateAnswer;
import com.cloud.agent.api.storage.CreateCommand;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.VolumeVO;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.DiskProfile;

/**
* Extends {@link StorageSystemDataMotionStrategy}, allowing KVM hosts to migrate VMs with the ROOT volume on a non managed local storage pool.
* As {@link StorageSystemDataMotionStrategy} is considering KVM, this implementation also migrates only from/to KVM hosts.
*/
public class KvmNonManagedStorageDataMotionStrategy extends StorageSystemDataMotionStrategy {

@Inject
private TemplateDataFactory templateDataFactory;

/**
* Uses the canHandle from the Super class {@link StorageSystemDataMotionStrategy}. If the storage pool is of file and the internalCanHandle from {@link StorageSystemDataMotionStrategy} CANT_HANDLE, returns the StrategyPriority.HYPERVISOR strategy priority. otherwise returns CANT_HANDLE.
* Note that the super implementation (override) is called by {@link #canHandle(Map, Host, Host)} which ensures that {@link #internalCanHandle(Map)} will be executed only if the source host is KVM.
*/
@Override
protected StrategyPriority internalCanHandle(Map<VolumeInfo, DataStore> volumeMap) {
if (super.internalCanHandle(volumeMap) == StrategyPriority.CANT_HANDLE) {
Set<VolumeInfo> volumeInfoSet = volumeMap.keySet();

for (VolumeInfo volumeInfo : volumeInfoSet) {
StoragePoolVO storagePoolVO = _storagePoolDao.findById(volumeInfo.getPoolId());
if (storagePoolVO.getPoolType() != StoragePoolType.Filesystem && storagePoolVO.getPoolType() != StoragePoolType.NetworkFilesystem) {
return StrategyPriority.CANT_HANDLE;
}
}
return StrategyPriority.HYPERVISOR;
}
return StrategyPriority.CANT_HANDLE;
}

/**
* Configures a {@link MigrateDiskInfo} object configured for migrating a File System volume and calls rootImageProvisioning.
*/
@Override
protected MigrateCommand.MigrateDiskInfo configureMigrateDiskInfo(VolumeInfo srcVolumeInfo, String destPath) {
return new MigrateCommand.MigrateDiskInfo(srcVolumeInfo.getPath(), MigrateCommand.MigrateDiskInfo.DiskType.FILE, MigrateCommand.MigrateDiskInfo.DriverType.QCOW2,
MigrateCommand.MigrateDiskInfo.Source.FILE, destPath);
}

/**
* Generates the volume path by appending the Volume UUID to the Libvirt destiny images path.</br>
* Example: /var/lib/libvirt/images/f3d49ecc-870c-475a-89fa-fd0124420a9b
*/
@Override
protected String generateDestPath(VirtualMachineTO vmTO, VolumeVO srcVolume, Host destHost, StoragePoolVO destStoragePool, VolumeInfo destVolumeInfo) {
DiskOfferingVO diskOffering = _diskOfferingDao.findById(srcVolume.getDiskOfferingId());
DiskProfile diskProfile = new DiskProfile(destVolumeInfo, diskOffering, HypervisorType.KVM);
String templateUuid = getTemplateUuid(destVolumeInfo.getTemplateId());
CreateCommand rootImageProvisioningCommand = new CreateCommand(diskProfile, templateUuid, destStoragePool, true);

Answer rootImageProvisioningAnswer = _agentMgr.easySend(destHost.getId(), rootImageProvisioningCommand);

if (rootImageProvisioningAnswer == null) {
throw new CloudRuntimeException(String.format("Migration with storage of vm [%s] failed while provisioning root image", vmTO.getName()));
}

if (!rootImageProvisioningAnswer.getResult()) {
throw new CloudRuntimeException(String.format("Unable to modify target volume on the host [host id:%s, name:%s]", destHost.getId(), destHost.getName()));
}

String libvirtDestImgsPath = null;
if (rootImageProvisioningAnswer instanceof CreateAnswer) {
libvirtDestImgsPath = ((CreateAnswer)rootImageProvisioningAnswer).getVolume().getName();
}
// File.getAbsolutePath is used to keep the file separator as it should be and eliminate a verification to check if exists a file separator in the last character of libvirtDestImgsPath.
return new File(libvirtDestImgsPath, destVolumeInfo.getUuid()).getAbsolutePath();
}

/**
* Returns the template UUID with the given id. If the template ID is null, it returns null.
*/
protected String getTemplateUuid(Long templateId) {
if (templateId == null) {
return null;
}
TemplateInfo templateImage = templateDataFactory.getTemplate(templateId, DataStoreRole.Image);
return templateImage.getUuid();
}

/**
* Sets the volume path as the volume UUID.
*/
@Override
protected void setVolumePath(VolumeVO volume) {
volume.setPath(volume.getUuid());
}

/**
* Return true if the volume should be migrated. Currently only supports migrating volumes on storage pool of the type StoragePoolType.Filesystem.
* This ensures that volumes on shared storage are not migrated and those on local storage pools are migrated.
*/
@Override
protected boolean shouldMigrateVolume(StoragePoolVO sourceStoragePool, Host destHost, StoragePoolVO destStoragePool) {
return sourceStoragePool.getPoolType() == StoragePoolType.Filesystem;
}
}
Loading

0 comments on commit bf20940

Please sign in to comment.