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

Feature #1727: Fix system_disk quotas

Add DS disk attributes; add disk-snapshots to total
This commit is contained in:
Carlos Martín 2015-07-22 17:20:37 +02:00
parent 2f00893227
commit fc3de05fb6
11 changed files with 129 additions and 35 deletions

View File

@ -181,9 +181,8 @@ public:
* @param disk
* @param inherit_attrs Attributes to be inherited from the DS template
* into the disk
* @return 0 on success
*/
int disk_attribute(
void disk_attribute(
VectorAttribute * disk,
const vector<string>& inherit_attrs);

View File

@ -416,9 +416,8 @@ public:
* @param inherit_attrs Attributes to be inherited from the image template
* into the disk
*
* @return 0 on success, -1 otherwise
*/
int disk_attribute( VectorAttribute * disk,
void disk_attribute(VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix,
const vector<string>& inherit_attrs);

View File

@ -150,7 +150,8 @@ public:
}
/**
* Generates a DISK attribute for VM templates using the Image metadata
* Generates a DISK attribute for VM templates using the Image metadata.
* If the disk uses an Image, it tries to acquire it.
*
* @param disk the disk to be generated
* @param disk_id the id for this disk
@ -164,7 +165,7 @@ public:
*
* @return 0 on success, -1 otherwise
*/
int disk_attribute(int vm_id,
int acquire_disk( int vm_id,
VectorAttribute * disk,
int disk_id,
Image::ImageType& img_type,
@ -173,6 +174,19 @@ public:
int& image_id,
Snapshots ** snaps,
string& error_str);
/**
* 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 uid of VM owner (to look for the image id within its images)
*
*/
void disk_attribute(VectorAttribute * disk,
int disk_id,
int uid);
/**
* Generates an Authorization token for the DISK attribute
* @param disk the disk to be authorized

View File

@ -1367,6 +1367,15 @@ public:
AuthRequest& ar,
VirtualMachineTemplate *tmpl);
/**
* Adds extra info to the given template:
* DISK/IMAGE_ID and SIZE
* @param uid for template owner
* @param tmpl the virtual machine template
*/
static void disk_extended_info(int uid,
VirtualMachineTemplate *tmpl);
// -------------------------------------------------------------------------
// Hotplug related functions
// -------------------------------------------------------------------------

View File

@ -101,7 +101,7 @@ int Datastore::enable(bool enable, string& error_str)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::disk_attribute(
void Datastore::disk_attribute(
VectorAttribute * disk,
const vector<string>& inherit_attrs)
{
@ -148,8 +148,6 @@ int Datastore::disk_attribute(
disk->replace(*it, inherit_val);
}
}
return 0;
}
/* ************************************************************************ */

View File

@ -488,7 +488,7 @@ int Image::from_xml(const string& xml)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::disk_attribute( VectorAttribute * disk,
void Image::disk_attribute( VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix,
const vector<string>& inherit_attrs)
@ -508,6 +508,7 @@ int Image::disk_attribute( VectorAttribute * disk,
dev_prefix = disk->vector_value("DEV_PREFIX");
long long size = -1;
unsigned int snap_size;
string template_target;
string template_driver;
@ -558,6 +559,9 @@ int Image::disk_attribute( VectorAttribute * disk,
disk->replace("SIZE", size_mb);
}
snap_size = snapshots.get_total_size();
disk->replace("DISK_SNAPSHOT_TOTAL_SIZE", snap_size);
if (driver.empty() && !template_driver.empty())//DRIVER in Image,not in DISK
{
disk->replace("DRIVER",template_driver);
@ -671,8 +675,6 @@ int Image::disk_attribute( VectorAttribute * disk,
disk->replace(*it, inherit_val);
}
}
return 0;
}
/* ------------------------------------------------------------------------ */

View File

@ -307,7 +307,7 @@ int ImagePool::get_disk_id(const string& id_s)
/* -------------------------------------------------------------------------- */
int ImagePool::disk_attribute(int vm_id,
int ImagePool::acquire_disk( int vm_id,
VectorAttribute * disk,
int disk_id,
Image::ImageType& img_type,
@ -440,7 +440,7 @@ int ImagePool::disk_attribute(int vm_id,
}
iid = img->get_oid();
rc = img->disk_attribute(disk, img_type, dev_prefix, inherit_image_attrs);
img->disk_attribute(disk, img_type, dev_prefix, inherit_image_attrs);
image_id = img->get_oid();
datastore_id = img->get_ds_id();
@ -453,17 +453,6 @@ int ImagePool::disk_attribute(int vm_id,
img->unlock();
if (rc == -1)
{
imagem->release_image(vm_id, iid, false);
error_str = "Unknown internal error";
delete *snap;
*snap = 0;
return -1;
}
ds = ds_pool->get(datastore_id, true);
if ( ds == 0 )
@ -490,13 +479,71 @@ int ImagePool::disk_attribute(int vm_id,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ImagePool::disk_attribute(
VectorAttribute * disk,
int disk_id,
int uid)
{
string source;
Image * img = 0;
int datastore_id;
string dev_prefix;
Image::ImageType img_type;
ostringstream oss;
Nebula& nd = Nebula::instance();
DatastorePool * ds_pool = nd.get_dspool();
Datastore * ds;
if (!(source = disk->vector_value("IMAGE")).empty())
{
int uiid = get_disk_uid(disk,uid);
if ( uiid != -1)
{
img = get(source, uiid, true);
}
}
else if (!(source = disk->vector_value("IMAGE_ID")).empty())
{
int iid = get_disk_id(source);
if ( iid != -1)
{
img = get(iid, true);
}
}
if ( img != 0 )
{
img->disk_attribute(disk, img_type, dev_prefix, inherit_image_attrs);
datastore_id = img->get_ds_id();
img->unlock();
ds = ds_pool->get(datastore_id, true);
if ( ds != 0 )
{
ds->disk_attribute(disk, inherit_datastore_attrs);
ds->unlock();
}
}
disk->replace("DISK_ID", disk_id);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ImagePool::authorize_disk(VectorAttribute * disk,int uid, AuthRequest * ar)
{
string source;
string dev_prefix;
Image::ImageType img_type;
Image * img = 0;
PoolObjectAuth perm;
if (!(source = disk->vector_value("IMAGE")).empty())
@ -534,9 +581,6 @@ void ImagePool::authorize_disk(VectorAttribute * disk,int uid, AuthRequest * ar)
img->get_permissions(perm);
// TODO: we could have img->authorize_disk, to add only SIZE and CLONE attrs.
img->disk_attribute(disk, img_type, dev_prefix, inherit_image_attrs);
img->unlock();
ar->add_auth(AuthRequest::USE, perm);

View File

@ -114,6 +114,8 @@ bool VirtualMachineAllocate::allocate_authorization(
// -------------------------- Check Quotas ----------------------------
VirtualMachine::disk_extended_info(att.uid, ttmpl);
if ( quota_authorization(tmpl, Quotas::VIRTUALMACHINE, att) == false )
{
return false;

View File

@ -112,7 +112,7 @@ void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList,
if (extended)
{
VirtualMachine::set_auth_request(att.uid, ar, extended_tmpl);
VirtualMachine::disk_extended_info(att.uid, extended_tmpl);
}
if ( att.uid != UserPool::ONEADMIN_ID && att.gid != GroupPool::ONEADMIN_ID )

View File

@ -163,6 +163,8 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
return;
}
VirtualMachine::disk_extended_info(att.uid, tmpl);
if ( quota_authorization(tmpl, Quotas::VIRTUALMACHINE, att) == false )
{
delete tmpl;

View File

@ -1841,7 +1841,7 @@ int VirtualMachine::get_disk_images(string& error_str)
disk = static_cast<VectorAttribute * >(disks[i]);
rc = ipool->disk_attribute(oid,
rc = ipool->acquire_disk( oid,
disk,
i,
img_type,
@ -2092,7 +2092,7 @@ VectorAttribute * VirtualMachine::set_up_attach_disk(
// Acquire the new disk image
// -------------------------------------------------------------------------
int rc = ipool->disk_attribute(vm_id,
int rc = ipool->acquire_disk( vm_id,
new_disk,
max_disk_id + 1,
img_type,
@ -3492,7 +3492,6 @@ int VirtualMachine::get_saveas_disk(int& disk_id, string& source,
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -3565,6 +3564,32 @@ void VirtualMachine::set_auth_request(int uid,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachine::disk_extended_info(int uid,
VirtualMachineTemplate *tmpl)
{
int num;
vector<Attribute * > disks;
VectorAttribute * disk;
ImagePool * ipool = Nebula::instance().get_ipool();
num = tmpl->get("DISK",disks);
for(int i=0; i<num; i++)
{
disk = dynamic_cast<VectorAttribute * >(disks[i]);
if ( disk == 0 )
{
continue;
}
ipool->disk_attribute(disk, i, uid);
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
pthread_mutex_t VirtualMachine::lex_mutex = PTHREAD_MUTEX_INITIALIZER;
extern "C"