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

feature #200: Generate Disk attributes with the ImagePool metadata

This commit is contained in:
Ruben S. Montero 2010-06-24 18:35:18 +02:00
parent 1c9e70dd80
commit 11fbfb5636
4 changed files with 146 additions and 71 deletions

View File

@ -202,9 +202,9 @@ public:
* - CDROM images will be at prefix + c: hdc, sdc.
* - Several DATABLOCK images can be mounted, they will be set to
* prefix + (d + index) : hdd, hde, hdf...
* returns: 0 on success, -1 in case of error.
* @param disk attribute for the VM template
*/
int get_disk_attribute(VectorAttribute * disk, int index);
void disk_attribute(VectorAttribute ** disk, int index);
// ------------------------------------------------------------------------
// Template

View File

@ -29,6 +29,9 @@
using namespace std;
#define IMAGE_TO_UPPER(S) transform (S.begin(),S.end(),S.begin(), \
(int(*)(int))toupper)
/**
* The Image Pool class.
*/
@ -78,7 +81,7 @@ public:
* @return a pointer to the Image, 0 if the User could not be loaded
*/
Image * get(
string name,
const string& name,
bool lock)
{
map<string, int>::iterator index;
@ -163,6 +166,37 @@ public:
*/
int dump(ostringstream& oss, const string& where);
/**
* Generates a DISK attribute for VM templates using the Image metadata
* @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)
{
string source;
Image * img;
source = disk->vector_value("NAME");
if (source.empty())
{
return -2;
}
img = get(source,true);
if (img == 0)
{
return -1;
}
img->disk_attribute(&disk,index);
img->unlock();
return 0;
}
private:
/**
* This map stores the association between IIDs and Image names

View File

@ -21,6 +21,8 @@
#include <sstream>
#include "Image.h"
#include "ImagePool.h"
/* ************************************************************************ */
/* Image :: Constructor/Destructor */
@ -456,69 +458,111 @@ void Image::release_image()
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::get_disk_attribute(VectorAttribute * disk, int index)
void Image::disk_attribute(VectorAttribute ** disk, int index)
{
string target = "";
string bus = "";
string overwrite;
string saveas;
string name;
string bus;
name = (*disk)->vector_value("NAME");
overwrite = (*disk)->vector_value("OVERWRITE");
saveas = (*disk)->vector_value("SAVE_AS");
bus = (*disk)->vector_value("BUS");
// SOURCE attribute
disk->replace("SOURCE", source);
string template_bus;
string prefix;
get_template_attribute("BUS", template_bus);
get_template_attribute("DEV_PREFIX", prefix);
// The BUS attribute isn't mandatory.
//---------------------------------------------------------------------------
// NEW DISK ATTRIBUTES
//---------------------------------------------------------------------------
VectorAttribute * new_disk = new VectorAttribute("DISK");
get_template_attribute("BUS", bus);
new_disk->replace("NAME",name);
new_disk->replace("OVERWRITE",overwrite);
new_disk->replace("SAVE_AS",saveas);
if( !bus.empty() ) // If the image has a BUS defined ...
new_disk->replace("SOURCE", source);
if (bus.empty())
{
string disk_bus = disk->vector_value("BUS");
if( disk_bus.empty() ) // ... and the disk doesn't have already one
if (!template_bus.empty())
{
disk->replace("BUS", bus);
new_disk->replace("BUS",template_bus);
}
}
// If the disk has already a user-defined target, then it will be used.
// First, check if it exists.
target = disk->vector_value("TARGET");
if ( target.empty() )
else
{
// Generate the target from the image's prefix and type
new_disk->replace("BUS",bus);
}
get_template_attribute("DEV_PREFIX", target);
//---------------------------------------------------------------------------
// TYPE, READONLY, CLONE, and SAVE attributes
//---------------------------------------------------------------------------
switch( type )
IMAGE_TO_UPPER(overwrite);
IMAGE_TO_UPPER(saveas);
switch(type)
{
case OS:
target += "a";
break;
case CDROM:
target += "c";
break;
case DATABLOCK:
// Multiple datablocks can be defined, and they are mounted as
// sdd, sde, sdf...
new_disk->replace("TYPE","DISK");
new_disk->replace("READONLY","NO");
if( index < 0 )
if (overwrite == "YES")
{
return -1;
new_disk->replace("CLONE","NO");
new_disk->replace("SAVE","YES");
}
else if (saveas == "YES")
{
new_disk->replace("CLONE","YES");
new_disk->replace("SAVE","YES");
}
else
{
new_disk->replace("CLONE","YES");
new_disk->replace("SAVE","NO");
}
char letter = ('d' + index);
target += letter;
break;
};
// "Replace" inserts the name-value pair even if it doesn't exist.
disk->replace("TARGET", target);
case CDROM:
new_disk->replace("TYPE","CDROM");
new_disk->replace("READONLY","YES");
new_disk->replace("CLONE","YES");
new_disk->replace("SAVE","NO");
break;
}
return 0;
//---------------------------------------------------------------------------
// TARGET attribute
//---------------------------------------------------------------------------
switch(type)
{
case OS:
prefix += "a";
break;
case CDROM:
prefix += "c"; // b is for context
break;
case DATABLOCK:
prefix += static_cast<char>(('d'+ index));
break;
}
new_disk->replace("TARGET", prefix);
delete (*disk);
(*disk) = new_disk;
}
/* ------------------------------------------------------------------------ */

View File

@ -22,9 +22,6 @@
#include <openssl/evp.h>
#include <iomanip>
#define TO_UPPER(S) transform (S.begin(),S.end(),S.begin(), \
(int(*)(int))toupper)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -149,7 +146,7 @@ int ImagePool::allocate (
}
img->get_template_attribute("PUBLIC", public_attr);
TO_UPPER( public_attr );
IMAGE_TO_UPPER( public_attr );
img->image_template.erase("PUBLIC");
img->get_template_attribute("ORIGINAL_PATH", original_path);