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

feature #200: Added default DISK_ID, simplified states

This commit is contained in:
Ruben S. Montero 2010-07-21 17:50:04 +02:00
parent 6e3d697d9e
commit 2eafe51805
5 changed files with 99 additions and 109 deletions

View File

@ -45,10 +45,9 @@ public:
enum ImageState
{
INIT = 0, /** < Initialization state */
LOCKED = 1, /** < FS operation on the image in progress, don't use */
READY = 2, /** < Image ready to use */
USED = 3, /** < Image in use */
DISABLED = 4 /** < Image can not be instantiated by a VM */
READY = 1, /** < Image ready to use */
USED = 2, /** < Image in use */
DISABLED = 3 /** < Image can not be instantiated by a VM */
};
/**
@ -140,10 +139,9 @@ public:
/**
* Get an image to be used in a VM, and updates its state.
* @param overwrite true if the image is going to be overwritten
* @return 0 if success
*/
int acquire_image(bool overwrite);
int acquire_image();
/**
@ -209,7 +207,7 @@ public:
* automatically increased.
* @param img_type will be set to the used image's type
*/
int disk_attribute(VectorAttribute * disk, int* index, ImageType& img_type);
int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type);
// ------------------------------------------------------------------------
// Template

View File

@ -168,14 +168,16 @@ 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
* @return 0 on success, -1 error, -2 not using the pool
*/
int disk_attribute(VectorAttribute * disk, int* index,
Image::ImageType& img_type);
int disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type);
/**
* Generates an Authorization token for the DISK attribute
* @param disk the disk to be authorized

View File

@ -478,7 +478,7 @@ string& Image::to_str(string& str) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::acquire_image(bool overwrite)
int Image::acquire_image()
{
int rc = 0;
@ -487,30 +487,14 @@ int Image::acquire_image(bool overwrite)
{
case READY:
running_vms++;
if ( overwrite == true)
{
state = LOCKED;
}
else
{
state = USED;
}
state = USED;
break;
case USED:
if ( overwrite == true)
{
rc = -1;
}
else
{
running_vms++;
}
running_vms++;
break;
case DISABLED:
case LOCKED:
default:
rc = -1;
break;
@ -529,7 +513,6 @@ bool Image::release_image()
switch (state)
{
case USED:
case LOCKED:
running_vms--;
if ( running_vms == 0)
@ -553,15 +536,15 @@ bool Image::release_image()
/* ------------------------------------------------------------------------ */
int Image::disk_attribute( VectorAttribute * disk,
int * index,
ImageType& img_type)
int * index,
ImageType* img_type)
{
string bus;
ostringstream iid;
img_type = type;
bus = disk->vector_value("BUS");
*img_type = type;
bus = disk->vector_value("BUS");
iid << oid;
string template_bus;
@ -574,7 +557,7 @@ int Image::disk_attribute( VectorAttribute * disk,
// Acquire the image
//--------------------------------------------------------------------------
if ( acquire_image(true) != 0 )
if ( acquire_image() != 0 )
{
return -1;
}

View File

@ -165,12 +165,16 @@ int ImagePool::dump(ostringstream& oss, const string& where)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::disk_attribute( VectorAttribute * disk,
int * index,
Image::ImageType& img_type)
int ImagePool::disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type)
{
string source;
Image * img = 0;
int rc;
ostringstream oss;
source = disk->vector_value("IMAGE");
@ -181,37 +185,68 @@ int ImagePool::disk_attribute( VectorAttribute * disk,
source = disk->vector_value("IMAGE_ID");
if (source.empty())
if (!source.empty())
{
return -2;
}
is.str(source);
is >> image_id;
is.str(source);
is >> image_id;
if( !is.fail() )
{
img = get(image_id,true);
if( !is.fail() )
{
img = get(image_id,true);
if (img == 0)
{
return -1;
}
}
}
}
else
{
img = get(source,true);
if (img == 0)
{
return -1;
}
}
if (img == 0)
{
return -1;
string type = disk->vector_value("TYPE");
transform(type.begin(), type.end(), type.begin(), (int(*)(int))toupper);
if( type == "SWAP" )
{
string target = disk->vector_value("TARGET");
if ( target.empty() )
{
string dev_prefix = _default_dev_prefix;
dev_prefix += "d";
disk->replace("TARGET", dev_prefix);
}
}
rc = -2;
}
int rc = img->disk_attribute(disk,index, img_type);
if ( rc == 0 )
else
{
update(img);
rc = img->disk_attribute(disk, index, img_type);
if ( rc == 0 )
{
update(img);
}
img->unlock();
}
img->unlock();
oss << disk_id;
disk->replace("DISK_ID",oss.str());
return rc;
}

View File

@ -829,6 +829,9 @@ int VirtualMachine::get_disk_images()
int n_cd = 0;
string type;
ostringstream oss;
Image::ImageType img_type;
Nebula& nd = Nebula::instance();
ipool = nd.get_ipool();
@ -836,7 +839,6 @@ int VirtualMachine::get_disk_images()
for(int i=0, index=0; i<num_disks; i++)
{
disk = dynamic_cast<VectorAttribute * >(disks[i]);
if ( disk == 0 )
@ -844,70 +846,40 @@ int VirtualMachine::get_disk_images()
continue;
}
Image::ImageType img_type;
rc = ipool->disk_attribute(disk, &index, img_type);
rc = ipool->disk_attribute(disk, i, &index, &img_type);
switch(rc)
if (rc == 0 )
{
case 0: // OK
switch(img_type)
{
case Image::OS:
n_os++;
break;
case Image::CDROM:
n_cd++;
break;
default:
break;
}
switch(img_type)
{
case Image::OS:
n_os++;
break;
case Image::CDROM:
n_cd++;
break;
default:
break;
}
if( n_os > 1 ) // Max. number of OS images is 1
{
goto error_max_os;
}
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;
}
break;
case -2: // not using the Image pool
type = disk->vector_value("TYPE");
transform (type.begin(), type.end(), type.begin(),
(int(*)(int))toupper);
if( type == "SWAP" )
{
string target = disk->vector_value("TARGET");
if ( target.empty() )
{
Nebula& nd = Nebula::instance();
string dev_prefix;
nd.get_configuration_attribute("DEFAULT_DEVICE_PREFIX",
dev_prefix);
dev_prefix += "d";
disk->replace("TARGET", dev_prefix);
}
}
break;
case -1: // ERROR
goto error_image;
break;
if( n_cd > 1 ) // Max. number of CDROM images is 1
{
goto error_max_cd;
}
}
else if ( rc == -1 )
{
goto error_image;
}
}
return 0;
error_max_os:
NebulaLog::log("ONE",Log::ERROR,
"VM can not use more than one OS image.");