1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

Revert "bug #1232: Only allow Datastore & volatile disks. Remove automatic target generation. Remove unneeded oned.conf attributes"

This reverts commit e401b4900d.
This commit is contained in:
Carlos Martín 2012-04-25 16:47:12 +02:00
parent e9e5d95417
commit f0c1641fdc
13 changed files with 329 additions and 80 deletions

View File

@ -329,13 +329,18 @@ public:
* Modifies the given disk attribute adding the following attributes:
* * SOURCE: the file-path.
* * BUS: will only be set if the Image's definition includes it.
* * TARGET: if not set uses that in the image template
* * TARGET: the value set depends on:
* - OS images will be mounted at prefix + a: hda, sda.
* - Prefix + b is reserved for the contex cdrom.
* - 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...
* @param disk attribute for the VM template
*
* @return -1 if there is no TARGET in the disk nor in the template, 0
* otherwise
* @param index number of datablock images used by the same VM. Will be
* automatically increased.
* @param img_type will be set to the used image's type
*/
int disk_attribute(VectorAttribute * disk);
int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type);
/**
* Factory method for image templates

View File

@ -40,6 +40,7 @@ public:
ImagePool(SqlDB * db,
const string& _default_type,
const string& _default_dev_prefix,
vector<const Attribute *>& restricted_attrs);
~ImagePool(){};
@ -135,6 +136,9 @@ public:
* Generates a DISK attribute for VM templates using the Image metadata
* @param disk the disk to be generated
* @param disk_id the id for this disk
* @param index number of datablock images used by the same VM. Will be
* automatically increased.
* @param img_type will be set to the used image's type
* @param uid of VM owner (to look for the image id within its images)
* @param image_id on success returns the acquired image id
* @param error_str string describing the error
@ -144,6 +148,8 @@ public:
*/
int disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type,
int uid,
int& image_id,
string& error_str);
@ -160,6 +166,11 @@ public:
return _default_type;
};
static const string& default_dev_prefix()
{
return _default_dev_prefix;
};
private:
//--------------------------------------------------------------------------
// Configuration Attributes for Images
@ -170,6 +181,11 @@ private:
**/
static string _default_type;
/**
* Default device prefix
**/
static string _default_dev_prefix;
//--------------------------------------------------------------------------
// Pool Attributes
// -------------------------------------------------------------------------

View File

@ -104,7 +104,8 @@ public:
virtual UserPool* create_upool(SqlDB* db);
virtual ImagePool* create_ipool(SqlDB* db,
virtual ImagePool* create_ipool( SqlDB* db,
string default_image_type,
string default_device_prefix);
virtual VMTemplatePool* create_tpool(SqlDB* db);

View File

@ -88,11 +88,19 @@ MAC_PREFIX = "02:00"
# DEFAULT_IMAGE_TYPE: This can take values
# OS Image file holding an operating system
# CDROM Image file holding a CDROM
# DATABLOCK Image file holding a datablock, created as an empty block
# DATABLOCK Image file holding a datablock,
# always created as an empty block
# DEFAULT_DEVICE_PREFIX: This can be set to
# hd IDE prefix
# sd SCSI
# xvd XEN Virtual Disk
# vd KVM virtual disk
#*******************************************************************************
#DATASTORE_LOCATION = /var/lib/one/datastores
DEFAULT_IMAGE_TYPE = "OS"
DEFAULT_IMAGE_TYPE = "OS"
DEFAULT_DEVICE_PREFIX = "hd"
#*******************************************************************************
# Information Driver Configuration

View File

@ -130,6 +130,17 @@ int Image::insert(SqlDB *db, string& error_str)
persistent_img = (persistent_attr == "YES");
// ------------ PREFIX --------------------
get_template_attribute("DEV_PREFIX", dev_prefix);
if( dev_prefix.empty() )
{
SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
ImagePool::default_dev_prefix());
obj_template->set(dev_att);
}
// ------------ PATH & SOURCE --------------------
erase_template_attribute("PATH", path);
@ -426,15 +437,18 @@ int Image::from_xml(const string& xml)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::disk_attribute(VectorAttribute * disk)
int Image::disk_attribute( VectorAttribute * disk,
int * index,
ImageType* img_type)
{
string bus;
string target;
string driver;
string disk_attr_type;
string bus;
string target;
string driver;
string disk_attr_type;
ostringstream iid;
ostringstream iid;
*img_type = type;
bus = disk->vector_value("BUS");
target = disk->vector_value("TARGET");
driver = disk->vector_value("DRIVER");
@ -442,30 +456,24 @@ int Image::disk_attribute(VectorAttribute * disk)
string template_bus;
string template_target;
string prefix;
string template_driver;
get_template_attribute("BUS", template_bus);
get_template_attribute("BUS", template_bus);
get_template_attribute("TARGET", template_target);
get_template_attribute("DRIVER", template_driver);
//---------------------------------------------------------------------------
// TARGET attribute
//---------------------------------------------------------------------------
if (target.empty())
get_template_attribute("DEV_PREFIX", prefix);
if (prefix.empty())//Removed from image template, get it again from defaults
{
if (!template_target.empty())
{
disk->replace("TARGET", template_target);
}
else //No TARGET in DISK nor in Image (ERROR!)
{
return -1;
}
prefix = ImagePool::default_dev_prefix();
}
//---------------------------------------------------------------------------
// BASE DISK ATTRIBUTES
//---------------------------------------------------------------------------
disk->replace("IMAGE", name);
disk->replace("IMAGE_ID", iid.str());
disk->replace("SOURCE", source);
@ -483,6 +491,7 @@ int Image::disk_attribute(VectorAttribute * disk)
//---------------------------------------------------------------------------
// TYPE, READONLY, CLONE, and SAVE attributes
//---------------------------------------------------------------------------
if ( persistent_img )
{
disk->replace("CLONE","NO");
@ -509,8 +518,42 @@ int Image::disk_attribute(VectorAttribute * disk)
break;
}
disk->replace("TYPE",disk_attr_type);
//---------------------------------------------------------------------------
// TARGET attribute
//---------------------------------------------------------------------------
if (target.empty()) //No TARGET in DISK attribute
{
if (!template_target.empty())
{
disk->replace("TARGET", template_target);
}
else
{
switch(type)
{
case OS:
prefix += "a";
break;
case CDROM:
prefix += "c"; // b is for context
break;
case DATABLOCK:
prefix += static_cast<char>(('e'+ *index));
*index = *index + 1;
break;
}
disk->replace("TARGET", prefix);
}
}
return 0;
}

View File

@ -191,7 +191,6 @@ void ImageManager::release_image(int iid, bool failed)
img->unlock();
break;
case Image::DISABLED:
case Image::READY:
case Image::ERROR:

View File

@ -26,12 +26,14 @@
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string ImagePool::_default_type;
string ImagePool::_default_dev_prefix;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ImagePool::ImagePool(SqlDB * db,
const string& __default_type,
ImagePool::ImagePool(SqlDB * db,
const string& __default_type,
const string& __default_dev_prefix,
vector<const Attribute *>& restricted_attrs):
PoolSQL(db, Image::table, true)
{
@ -39,6 +41,7 @@ ImagePool::ImagePool(SqlDB * db,
// Init static defaults
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
// Set default type
if (_default_type != "OS" &&
@ -213,6 +216,8 @@ static int get_disk_id(const string& id_s)
int ImagePool::disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type,
int uid,
int& image_id,
string& error_str)
@ -221,7 +226,6 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
Image * img = 0;
int rc = 0;
int datastore_id;
int iid;
ostringstream oss;
@ -244,12 +248,10 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
{
return -1;
}
iid = img->get_oid();
}
else if (!(source = disk->vector_value("IMAGE_ID")).empty())
{
iid = get_disk_id(source);
int iid = get_disk_id(source);
if ( iid == -1)
{
@ -264,27 +266,28 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
return -1;
}
}
else //Not using the image repository (volatile DISK)
else //Not using the image repository
{
string type = disk->vector_value("TYPE");
string type;
rc = -2;
type = disk->vector_value("TYPE");
transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper);
if ( type == "SWAP" || type == "FS" )
if( type == "SWAP" )
{
string target = disk->vector_value("TARGET");
if ( target.empty() )
{
error_str = "Missing target for disk of type " + type;
return -1;
string dev_prefix = _default_dev_prefix;
dev_prefix += "d";
disk->replace("TARGET", dev_prefix);
}
}
else
{
error_str = "Unknown disk type " + type;
return -1;
}
}
if ( img != 0 )
@ -292,29 +295,20 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
DatastorePool * ds_pool = nd.get_dspool();
Datastore * ds;
iid = img->get_oid();
rc = img->disk_attribute(disk);
img->disk_attribute(disk, index, img_type);
image_id = img->get_oid();
datastore_id = img->get_ds_id();
update(img);
img->unlock();
if (rc == -1)
{
imagem->release_image(iid, false);
error_str = "Missing TARGET in disk";
return -1;
}
ds = ds_pool->get(datastore_id, true);
if ( ds == 0 )
{
imagem->release_image(iid, false);
error_str = "Associated datastore for the image does not exist";
return -1;
}

View File

@ -90,10 +90,12 @@ class ImagePoolFriend : public ImagePool
public:
ImagePoolFriend(SqlDB * db,
const string& _default_type,
const string& _default_dev_prefix,
vector<const Attribute *> _restricted_attrs):
ImagePool( db,
_default_type,
_default_dev_prefix,
_restricted_attrs){};
@ -113,7 +115,7 @@ public:
string gname = gnames[uid];
return ImagePool::allocate(uid, 1, uname, gname,
img_template, 0, "none", "", oid, err);
img_template, 0,"none", "", oid, err);
}
else
{
@ -143,6 +145,7 @@ class ImagePoolTest : public PoolTest
CPPUNIT_TEST ( duplicates );
CPPUNIT_TEST ( extra_attributes );
CPPUNIT_TEST ( wrong_templates );
CPPUNIT_TEST ( target_generation );
CPPUNIT_TEST ( bus_source_assignment );
CPPUNIT_TEST ( persistence );
CPPUNIT_TEST ( imagepool_disk_attribute );
@ -253,7 +256,7 @@ public:
// Create a new pool, using the same DB. This new pool should read the
// allocated images.
vector<const Attribute *> restricted_attrs;
imp = new ImagePool(db,"OS", restricted_attrs);
imp = new ImagePool(db,"OS", "hd", restricted_attrs);
img = imp->get(0, false);
CPPUNIT_ASSERT( img != 0 );
@ -424,6 +427,107 @@ public:
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void target_generation()
{
ImagePoolFriend * imp = static_cast<ImagePoolFriend *>(pool);
Image * img;
VectorAttribute * disk;
int oid;
string value;
int index=0;
Image::ImageType img_type;
disk = new VectorAttribute("DISK");
// Allocate an OS type image
oid = allocate(0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
CPPUNIT_ASSERT( oid == 0 );
img->set_state(Image::READY);
img->disk_attribute(disk, &index, &img_type);
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT( value == "hda" );
CPPUNIT_ASSERT( img_type == Image::OS );
// clean up
delete disk;
value = "";
disk = new VectorAttribute("DISK");
// Allocate a CDROM type image
string templ = "NAME = \"name A\" TYPE = CDROM PATH = /tmp";
imp->allocate(0, templ, &oid);
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->set_state(Image::READY);
img->disk_attribute(disk, &index, &img_type);
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT(value == "hdc");
CPPUNIT_ASSERT( img_type == Image::CDROM );
// clean up
delete disk;
value = "";
disk = new VectorAttribute("DISK");
// Allocate a DATABLOCK type image
templ = "NAME = \"name B\" TYPE = DATABLOCK PATH=\"/dev/null\"";
imp->allocate(0, templ, &oid);
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->set_state(Image::READY);
img->disk_attribute(disk, &index, &img_type);
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT(value == "hde");
CPPUNIT_ASSERT( img_type == Image::DATABLOCK );
// clean up
delete disk;
value = "";
disk = new VectorAttribute("DISK");
// Allocate a DATABLOCK type image
templ = "NAME = \"name C\" TYPE = DATABLOCK DEV_PREFIX = \"sd\""
" SIZE=4 FSTYPE=ext3";
imp->allocate(0, templ, &oid);
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->set_state(Image::READY);
img->disk_attribute(disk, &index, &img_type);
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT(value == "sdf");
// clean up
delete disk;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -500,10 +604,11 @@ public:
// Allocate 2 images, with different dev_prefix
string template_0 = "NAME = \"Image 0\"\n"
"TARGET = \"hdx\"\n"
"DEV_PREFIX = \"hd\"\n"
"PATH = /dev/null\n";
string template_1 = "NAME = \"Image 1\"\n"
"DEV_PREFIX = \"sd\"\n"
"PATH = /dev/null\n";
@ -527,25 +632,29 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("IMAGE_ID", "0");
((ImagePool*)imp)->disk_attribute(disk, 0, 0, img_id,error);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0, img_id,error);
value = "";
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT( value == "hdx" );
CPPUNIT_ASSERT( value == "hda" );
value = "";
value = disk->vector_value("IMAGE");
CPPUNIT_ASSERT( value == "Image 0" );
delete disk;
// Disk using image 1 index
disk = new VectorAttribute("DISK");
disk->replace("IMAGE_ID", "1");
int rc = ((ImagePool*)imp)->disk_attribute(disk, 0, 0, img_id,error);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0, img_id,error);
CPPUNIT_ASSERT( rc == -1 );
value = "";
value = disk->vector_value("TARGET");
CPPUNIT_ASSERT( value == "sda" );
value = "";
value = disk->vector_value("IMAGE");
CPPUNIT_ASSERT( value == "Image 1" );
delete disk;
}

View File

@ -299,9 +299,16 @@ void Nebula::start()
upool = new UserPool(db, expiration_time);
nebula_configuration->get("DEFAULT_IMAGE_TYPE", default_image_type);
ipool = new ImagePool(db, default_image_type, img_restricted_attrs);
nebula_configuration->get("DEFAULT_DEVICE_PREFIX",
default_device_prefix);
ipool = new ImagePool(db,
default_image_type,
default_device_prefix,
img_restricted_attrs);
tpool = new VMTemplatePool(db);
dspool = new DatastorePool(db);
}
catch (exception&)

View File

@ -179,6 +179,7 @@ void OpenNebulaTemplate::set_conf_default()
#*******************************************************************************
# DATASTORE_LOCATION
# DEFAULT_IMAGE_TYPE
# DEFAULT_DEVICE_PREFIX
#*******************************************************************************
*/
//DATASTORE_LOCATION
@ -192,6 +193,11 @@ void OpenNebulaTemplate::set_conf_default()
attribute = new SingleAttribute("DEFAULT_IMAGE_TYPE",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//DEFAULT_DEVICE_PREFIX
value = "hd";
attribute = new SingleAttribute("DEFAULT_DEVICE_PREFIX",value);
conf_default.insert(make_pair(attribute->name(),attribute));
/*
#*******************************************************************************
# Auth Manager Configuration

View File

@ -243,7 +243,9 @@ void Nebula::start()
if (tester->need_image_pool)
{
ipool = tester->create_ipool(db, default_image_type);
ipool = tester->create_ipool(db,
default_image_type,
default_device_prefix);
}
if (tester->need_template_pool)

View File

@ -44,11 +44,12 @@ UserPool* NebulaTest::create_upool(SqlDB* db)
}
ImagePool* NebulaTest::create_ipool( SqlDB* db,
string default_image_type)
string default_image_type,
string default_device_prefix)
{
vector<const Attribute *> restricted_attrs;
return new ImagePool(db, default_image_type, restricted_attrs);
return new ImagePool(db, default_image_type, default_device_prefix, restricted_attrs);
}
VMTemplatePool* NebulaTest::create_tpool(SqlDB* db)

View File

@ -376,8 +376,13 @@ int VirtualMachine::parse_context(string& error_str)
if ( target.empty() )
{
error_str = "Missing TARGET attribute in CONTEXT.";
return -1;
Nebula& nd = Nebula::instance();
string dev_prefix;
nd.get_configuration_attribute("DEFAULT_DEVICE_PREFIX",dev_prefix);
dev_prefix += "b";
context_parsed->replace("TARGET", dev_prefix);
}
obj_template->set(context_parsed);
@ -855,15 +860,21 @@ int VirtualMachine::get_disk_images(string& error_str)
VectorAttribute * disk;
vector<int> acquired_images;
int image_id;
ostringstream oss;
int n_os = 0; // Number of OS images
int n_cd = 0; // Number of CDROMS
int n_db = 0; // Number of DATABLOCKS
string type;
int image_id;
ostringstream oss;
Image::ImageType img_type;
Nebula& nd = Nebula::instance();
ipool = nd.get_ipool();
num_disks = obj_template->get("DISK",disks);
for(int i=0; i<num_disks; i++)
for(int i=0, index=0; i<num_disks; i++)
{
disk = dynamic_cast<VectorAttribute * >(disks[i]);
@ -872,13 +883,48 @@ int VirtualMachine::get_disk_images(string& error_str)
continue;
}
rc = ipool->disk_attribute(disk, i, uid, image_id, error_str);
rc = ipool->disk_attribute(disk,
i,
&index,
&img_type,
uid,
image_id,
error_str);
if (rc == 0 )
{
acquired_images.push_back(image_id);
switch(img_type)
{
case Image::OS:
n_os++;
break;
case Image::CDROM:
n_cd++;
break;
case Image::DATABLOCK:
n_db++;
break;
default:
break;
}
if( n_os > 1 ) // Max. number of OS images is 1
{
goto error_max_os;
}
if( n_cd > 1 ) // Max. number of CDROM images is 1
{
goto error_max_cd;
}
if( n_db > 10 ) // Max. number of DATABLOCK images is 10
{
goto error_max_db;
}
}
else
else if ( rc == -1 )
{
goto error_common;
}
@ -886,6 +932,18 @@ int VirtualMachine::get_disk_images(string& error_str)
return 0;
error_max_os:
error_str = "VM cannot use more than one OS image.";
goto error_common;
error_max_cd:
error_str = "VM cannot use more than one CDROM image.";
goto error_common;
error_max_db:
error_str = "VM cannot use more than 10 DATABLOCK images.";
goto error_common;
error_common:
ImageManager * imagem = nd.get_imagem();