1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-02 09:47:00 +03:00

feature #200: Integrates ImagePool and VirtualMachinePool. Now you can requests Images from VM templates!

This commit is contained in:
Ruben S. Montero 2010-06-25 13:24:54 +02:00
parent 694685a959
commit cacbe7bf5e
7 changed files with 155 additions and 36 deletions

View File

@ -204,7 +204,7 @@ public:
* prefix + (d + index) : hdd, hde, hdf...
* @param disk attribute for the VM template
*/
int disk_attribute(VectorAttribute * disk, int index);
int disk_attribute(VectorAttribute * disk, int * index);
// ------------------------------------------------------------------------
// Template

View File

@ -29,9 +29,6 @@
using namespace std;
#define IMAGE_TO_UPPER(S) transform (S.begin(),S.end(),S.begin(), \
(int(*)(int))toupper)
/**
* The Image Pool class.
*/
@ -171,7 +168,7 @@ public:
* @param disk the disk to be generated
* @return 0 on success, -1 error, -2 not using the pool
*/
int disk_attribute(VectorAttribute * disk, int index)
int disk_attribute(VectorAttribute * disk, int * index)
{
string source;
Image * img;

View File

@ -69,6 +69,11 @@ public:
return upool;
};
ImagePool * get_ipool()
{
return ipool;
};
// --------------------------------------------------------------
// Manager Accessors
// --------------------------------------------------------------
@ -212,32 +217,32 @@ private:
// -----------------------------------------------------------------------
Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0),upool(0),
lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0)
ipool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0)
{
const char * nl = getenv("ONE_LOCATION");
const char * nl = getenv("ONE_LOCATION");
if (nl == 0) //OpenNebula installed under root directory
{
nebula_location = "/";
nebula_location = "/";
mad_location = "/usr/lib/one/mads/";
etc_location = "/etc/one/";
log_location = "/var/log/one/";
var_location = "/var/lib/one/";
mad_location = "/usr/lib/one/mads/";
etc_location = "/etc/one/";
log_location = "/var/log/one/";
var_location = "/var/lib/one/";
}
else
{
nebula_location = nl;
nebula_location = nl;
if ( nebula_location.at(nebula_location.size()-1) != '/' )
{
nebula_location += "/";
}
if ( nebula_location.at(nebula_location.size()-1) != '/' )
{
nebula_location += "/";
}
mad_location = nebula_location + "lib/mads/";
etc_location = nebula_location + "etc/";
log_location = nebula_location + "var/";
var_location = nebula_location + "var/";
mad_location = nebula_location + "lib/mads/";
etc_location = nebula_location + "etc/";
log_location = nebula_location + "var/";
var_location = nebula_location + "var/";
}
};
@ -263,6 +268,11 @@ private:
delete upool;
}
if ( ipool != 0)
{
delete ipool;
}
if ( vmm != 0)
{
delete vmm;

View File

@ -714,7 +714,7 @@ public:
void get_requirements (int& cpu, int& memory, int& disk);
// ------------------------------------------------------------------------
// Network Leases
// Network Leases & Disk Images
// ------------------------------------------------------------------------
/**
* Get all network leases for this Virtual Machine
@ -727,6 +727,17 @@ public:
*/
void release_network_leases();
/**
* Get all disk images for this Virtual Machine
* @return 0 if success
*/
int get_disk_images();
/**
* Releases all disk images taken by this Virtual Machine
*/
void release_disk_images();
// ------------------------------------------------------------------------
// Context related functions
// ------------------------------------------------------------------------
@ -881,11 +892,11 @@ private:
* @return 0 on success
*/
int select_cb(void *nil, int num, char **names, char ** values);
/**
* Execute an INSERT or REPLACE Sql query.
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);

View File

@ -459,7 +459,7 @@ void Image::release_image()
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::disk_attribute(VectorAttribute * disk, int index)
int Image::disk_attribute(VectorAttribute * disk, int * index)
{
string overwrite;
string saveas;
@ -474,8 +474,11 @@ int Image::disk_attribute(VectorAttribute * disk, int index)
bus = disk->vector_value("BUS");
iid << oid;
IMAGE_TO_UPPER(overwrite);
IMAGE_TO_UPPER(saveas);
transform(overwrite.begin(), overwrite.end(), overwrite.begin(),
(int(*)(int))toupper);
transform(saveas.begin(), saveas.end(), saveas.begin(),
(int(*)(int))toupper);
string template_bus;
string prefix;
@ -569,7 +572,8 @@ int Image::disk_attribute(VectorAttribute * disk, int index)
break;
case DATABLOCK:
prefix += static_cast<char>(('d'+ index));
prefix += static_cast<char>(('d'+ *index));
*index = *index + 1;
break;
}

View File

@ -148,7 +148,8 @@ int ImagePool::allocate (
img->get_template_attribute("PUBLIC", public_attr);
IMAGE_TO_UPPER(public_attr);
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
img->public_img = (public_attr == "YES");

View File

@ -303,6 +303,17 @@ int VirtualMachine::insert(SqlDB * db)
goto error_leases;
}
// ------------------------------------------------------------------------
// Get disk images
// ------------------------------------------------------------------------
rc = get_disk_images();
if ( rc != 0 )
{
goto error_images;
}
// ------------------------------------------------------------------------
// Insert the template first, so we get a valid template ID. Then the VM
// ------------------------------------------------------------------------
@ -337,6 +348,11 @@ error_leases:
NebulaLog::log("ONE",Log::ERROR, "Could not get network lease for VM");
release_network_leases();
return -1;
error_images:
NebulaLog::log("ONE",Log::ERROR, "Could not get disk image for VM");
release_disk_images();
return -1;
}
/* ------------------------------------------------------------------------ */
@ -585,18 +601,98 @@ void VirtualMachine::get_requirements (int& cpu, int& memory, int& disk)
return;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::get_disk_images()
{
int num_disks, rc;
vector<Attribute * > disks;
ImagePool * ipool;
VectorAttribute * disk;
Nebula& nd = Nebula::instance();
ipool = nd.get_ipool();
num_disks = vm_template.get("DISK",disks);
for(int i=0, index=0; i<num_disks; i++)
{
disk = dynamic_cast<VectorAttribute * >(disks[i]);
if ( disk == 0 )
{
continue;
}
rc = ipool->disk_attribute(disk, &index);
if (rc == -1) // 0 OK, -2 not using the Image pool
{
return -1;
}
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachine::release_disk_images()
{
string iid;
int num_disks;
vector<Attribute const * > disks;
Image * img;
ImagePool * ipool;
Nebula& nd = Nebula::instance();
ipool = nd.get_ipool();
num_disks = get_template_attribute("DISK",disks);
for(int i=0; i<num_disks; i++)
{
VectorAttribute const * disk =
dynamic_cast<VectorAttribute const * >(disks[i]);
if ( disk == 0 )
{
continue;
}
iid = disk->vector_value("IID");
if ( iid.empty() )
{
continue;
}
img = ipool->get(atoi(iid.c_str()),true);
if ( img == 0 )
{
continue;
}
img->release_image();
img->unlock();
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::get_network_leases()
{
int num_nics, rc;
vector<Attribute * > nics;
VirtualNetworkPool * vnpool;
VectorAttribute * nic;
// Set the networking attributes.
int num_nics, rc;
vector<Attribute * > nics;
VirtualNetworkPool * vnpool;
VectorAttribute * nic;
Nebula& nd = Nebula::instance();
vnpool = nd.get_vnpool();