1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-03 01:17:41 +03:00

feature #199: Native suuport of vmware in the core

This commit is contained in:
Tino Vázquez 2010-09-30 17:08:32 +02:00
parent 1da03e8310
commit 59bc8cd7fc
3 changed files with 363 additions and 1 deletions

View File

@ -44,6 +44,14 @@ private:
const VirtualMachine * vm,
const string& file_name) const;
int deployment_description_kvm(
const VirtualMachine * vm,
const string& file_name) const;
int deployment_description_vmware(
const VirtualMachine * vm,
const string& file_name) const;
const string emulator;
};

View File

@ -19,13 +19,29 @@
#include "Nebula.h"
#include <sstream>
#include <fstream>
#include <libgen.h>
int LibVirtDriver::deployment_description(
const VirtualMachine * vm,
const string& file_name) const
{
int rc = -1;
if (emulator == "kvm")
{
rc = deployment_description_kvm(vm,file_name);
}
else if (emulator == "vmware")
{
rc = deployment_description_vmware(vm,file_name);
}
return rc;
}
int LibVirtDriver::deployment_description_kvm(
const VirtualMachine * vm,
const string& file_name) const
{
ofstream file;
int num;
@ -588,3 +604,336 @@ error_disk:
file.close();
return -1;
}
int LibVirtDriver::deployment_description_vmware(
const VirtualMachine * vm,
const string& file_name) const
{
ofstream file;
int num;
vector<const Attribute *> attrs;
string vcpu;
string memory;
int memory_in_kb = 0;
string arch = "";
const VectorAttribute * disk;
string type = "";
string target = "";
string bus = "";
string ro = "";
string source = "";
string datastore = "";
bool readonly;
const VectorAttribute * nic;
string mac = "";
string bridge = "";
string script = "";
string model = "";
const VectorAttribute * raw;
string data;
// ------------------------------------------------------------------------
file.open(file_name.c_str(), ios::out);
if (file.fail() == true)
{
goto error_vmware_file;
}
// ------------------------------------------------------------------------
// Starting XML document
// ------------------------------------------------------------------------
file << "<domain type='" << emulator << "'>" << endl;
// ------------------------------------------------------------------------
// Domain name
// ------------------------------------------------------------------------
file << "\t<name>one-" << vm->get_oid() << "</name>" << endl;
// ------------------------------------------------------------------------
// CPU
// ------------------------------------------------------------------------
vm->get_template_attribute("VCPU", vcpu);
if(vcpu.empty())
{
get_default("VCPU", vcpu);
}
if (!vcpu.empty())
{
file << "\t<vcpu>" << vcpu << "</vcpu>" << endl;
}
// ------------------------------------------------------------------------
// Memory
// ------------------------------------------------------------------------
vm->get_template_attribute("MEMORY",memory);
if (memory.empty())
{
get_default("MEMORY",memory);
}
if (!memory.empty())
{
memory_in_kb = atoi(memory.c_str()) * 1024;
file << "\t<memory>" << memory_in_kb << "</memory>" << endl;
}
else
{
goto error_vmware_memory;
}
// ------------------------------------------------------------------------
// OS and boot options
// ------------------------------------------------------------------------
num = vm->get_template_attribute("OS",attrs);
// Get values & defaults
if ( num > 0 )
{
const VectorAttribute * os;
os = dynamic_cast<const VectorAttribute *>(attrs[0]);
if( os != 0 )
{
arch = os->vector_value("ARCHITECTURE");
}
}
// Start writing to the file with the info we got
file << "\t<os>" << endl;
if ( arch.empty() )
{
get_default("OS","ARCH",arch);
}
if (arch.empty())
{
goto error_vmware_arch;
}
file << "\t\t<type arch='" << arch << "'>hvm</type>" << endl;
file << "\t</os>" << endl;
attrs.clear();
// ------------------------------------------------------------------------
// Disks
// ------------------------------------------------------------------------
file << "\t<devices>" << endl;
num = vm->get_template_attribute("DISK",attrs);
if (num!=0)
{
get_default("DATASTORE", datastore);
}
for (int i=0; i < num ;i++)
{
disk = dynamic_cast<const VectorAttribute *>(attrs[i]);
if ( disk == 0 )
{
continue;
}
type = disk->vector_value("TYPE");
target = disk->vector_value("TARGET");
ro = disk->vector_value("READONLY");
bus = disk->vector_value("BUS");
source = disk->vector_value("SOURCE");
if (target.empty())
{
goto error_vmware_disk;
}
readonly = false;
if ( !ro.empty() )
{
transform(ro.begin(),ro.end(),ro.begin(),(int(*)(int))toupper);
if ( ro == "YES" )
{
readonly = true;
}
}
if (type.empty() == false)
{
transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper);
}
if ( type == "BLOCK" )
{
file << "\t\t<disk type='block' device='disk'>" << endl;
file << "\t\t\t<source dev='" << source << "'/>" << endl;
}
else if ( type == "CDROM" )
{
file << "\t\t<disk type='file' device='cdrom'>" << endl;
file << "\t\t\t<source file='" << source << "'/>" << endl;
}
else
{
file << "\t\t<disk type='file' device='disk'>" << endl
<< "\t\t\t<source file='[" << datastore <<"] " << vm->get_oid()
<< "/images/disk." << i << ".vmdk'/>" << endl;
}
file << "\t\t\t<target dev='" << target << "'";
if (!bus.empty())
{
file << " bus='" << bus << "'/>" << endl;
}
else
{
file << "/>" << endl;
}
if (readonly)
{
file << "\t\t\t<readonly/>" << endl;
}
file << "\t\t</disk>" << endl;
}
attrs.clear();
// ------------------------------------------------------------------------
// Network interfaces
// ------------------------------------------------------------------------
num = vm->get_template_attribute("NIC",attrs);
for(int i=0; i<num; i++)
{
nic = dynamic_cast<const VectorAttribute *>(attrs[i]);
if ( nic == 0 )
{
continue;
}
bridge = nic->vector_value("BRIDGE");
mac = nic->vector_value("MAC");
target = nic->vector_value("TARGET");
script = nic->vector_value("SCRIPT");
model = nic->vector_value("MODEL");
if ( bridge.empty() )
{
file << "\t\t<interface type='ethernet'>" << endl;
}
else
{
file << "\t\t<interface type='bridge'>" << endl;
file << "\t\t\t<source bridge='" << bridge << "'/>" << endl;
}
if( !mac.empty() )
{
file << "\t\t\t<mac address='" << mac << "'/>" << endl;
}
if( !target.empty() )
{
file << "\t\t\t<target dev='" << target << "'/>" << endl;
}
if( !script.empty() )
{
file << "\t\t\t<script path='" << script << "'/>" << endl;
}
if( !model.empty() )
{
file << "\t\t\t<model type='" << model << "'/>" << endl;
}
file << "\t\t</interface>" << endl;
}
attrs.clear();
// ------------------------------------------------------------------------
// Raw KVM attributes
// ------------------------------------------------------------------------
num = vm->get_template_attribute("RAW",attrs);
for(int i=0; i<num;i++)
{
raw = dynamic_cast<const VectorAttribute *>(attrs[i]);
if ( raw == 0 )
{
continue;
}
type = raw->vector_value("TYPE");
transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper);
if ( type == "VMWARE" )
{
data = raw->vector_value("DATA");
file << "\t" << data << endl;
}
}
file << "</domain>" << endl;
file.close();
return 0;
error_vmware_file:
vm->log("VMM", Log::ERROR, "Could not open VMWARE deployment file.");
return -1;
error_vmware_arch:
vm->log("VMM", Log::ERROR, "No ARCHITECTURE defined and no default provided.");
file.close();
return -1;
error_vmware_memory:
vm->log("VMM", Log::ERROR, "No MEMORY defined and no default provided.");
file.close();
return -1;
error_vmware_disk:
vm->log("VMM", Log::ERROR, "Wrong target value in DISK.");
file.close();
return -1;
}

View File

@ -920,6 +920,11 @@ void VirtualMachineManager::load_mads(int uid)
vmm_driver = new LibVirtDriver(uid, vattr->value(),
(uid != 0),vmpool,"kvm");
}
else if ( type == "VMWARE" )
{
vmm_driver = new LibVirtDriver(uid, vattr->value(),
(uid != 0),vmpool,"vmware");
}
else if ( type == "XML" )
{
vmm_driver = new XMLDriver(uid, vattr->value(),(uid != 0),vmpool);