From c16631873f35389ad58f3091fd21ef515c27dfc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 28 May 2010 18:56:35 +0200 Subject: [PATCH] feature #200: Images can now modify a DISK attribute vector to include correct BUS and TARGET. --- include/Image.h | 44 ++++++++++++++++---------- include/ImagePool.h | 51 ++++++++++++------------------ src/image/Image.cc | 72 +++++++++++++++++++++++++++++++++++++++++- src/image/ImagePool.cc | 14 ++++++++ 4 files changed, 133 insertions(+), 48 deletions(-) diff --git a/include/Image.h b/include/Image.h index b178a9088e..d46696abf1 100644 --- a/include/Image.h +++ b/include/Image.h @@ -55,22 +55,22 @@ public: friend ostream& operator<<(ostream& os, Image& i); // ************************************************************************* - // Virtual Network Public Methods + // Image Public Methods // ************************************************************************* - /** - * Function to print the Image object into a string in plain text - * @param str the resulting string - * @return a reference to the generated string - */ - string& to_str(string& str) const; + /** + * Function to print the Image object into a string in plain text + * @param str the resulting string + * @return a reference to the generated string + */ + string& to_str(string& str) const; - /** - * Function to print the Image object into a string in XML format - * @param xml the resulting XML string - * @return a reference to the generated string - */ - string& to_xml(string& xml) const; + /** + * Function to print the Image object into a string in XML format + * @param xml the resulting XML string + * @return a reference to the generated string + */ + string& to_xml(string& xml) const; /** * Get the Image unique identifier IID, that matches the OID of the object @@ -98,7 +98,7 @@ public: { return name; }; - + /** * Set enum type * @return 0 on success, -1 otherwise @@ -133,13 +133,25 @@ public: * @return boolean true if the image can be used */ bool get_image(bool overwrite); - + /** * Releases an image being used by a VM */ void release_image(); - + + /** + * Modifies the given disk attribute to set the target value. + * * 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... + * returns: 0 if the disk was not modified + * 1 if the disk was modified correctly + * -1 in case of error + */ + int get_disk_attribute(VectorAttribute * disk, int index); // ------------------------------------------------------------------------ // Template diff --git a/include/ImagePool.h b/include/ImagePool.h index 8236e39509..67a81b3b1d 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -36,38 +36,27 @@ class ImagePool : public PoolSQL { public: - ImagePool(SqlDB * db, + ImagePool(SqlDB * db, const string& _source_prefix, const string& _default_type, - const string& _default_bus): + const string& _default_dev_prefix): PoolSQL(db,Image::table), - source_prefix(_source_prefix) - { - if (_default_type != "OS" || - _default_type != "CDROM" || - _default_type != "DATABLOCK") - { - NebulaLog::log("IMG", Log::ERROR, - "Bad default for image type, setting OS"); - default_type = "OS"; - } - else - { - default_type = _default_type; - } - - if (_default_bus != "IDE" || - _default_bus != "SCSI") - { - NebulaLog::log("IMG", Log::ERROR, - "Bad default for bus type, setting IDE"); - default_bus = "IDE"; - } - else - { - default_bus = _default_bus; - } - }; + source_prefix(_source_prefix), + default_dev_prefix(_default_dev_prefix) + { + if (_default_type != "OS" || + _default_type != "CDROM" || + _default_type != "DATABLOCK") + { + NebulaLog::log("IMG", Log::ERROR, + "Bad default for image type, setting OS"); + default_type = "OS"; + } + else + { + default_type = _default_type; + } + }; ~ImagePool(){}; @@ -179,9 +168,9 @@ private: string default_type; /** - * Default bus type + * Default device prefix **/ - string default_bus; + string default_dev_prefix; /** * Factory method to produce Image objects diff --git a/src/image/Image.cc b/src/image/Image.cc index 7730368e5e..ad8074808e 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -363,7 +363,7 @@ string& Image::to_str(string& str) const "SOURCE = " << source << endl << "STATE = " << state << endl << "RUNNING_VMS = " << running_vms << endl << - "TEMPLATE" << endl + "TEMPLATE" << endl << image_template.to_str(template_str) << endl; @@ -400,6 +400,7 @@ bool Image::get_image(bool overwrite) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ + // TODO update? void Image::release_image() { @@ -410,3 +411,72 @@ void Image::release_image() state = READY; } } + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Image::get_disk_attribute(VectorAttribute * disk, int index) +{ + string target = ""; + string bus = ""; + + // The BUS attribute isn't mandatory. + + get_template_attribute("BUS", bus); + + if( !bus.empty() ) // If the image has a BUS defined ... + { + string disk_bus = disk->vector_value("BUS"); + + if( disk_bus.empty() ) // ... and the disk doesn't have already one + { + disk->replace("BUS", 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() ) + { + // Generate the target from the image's prefix and type + + get_template_attribute("DEV_PREFIX", target); + + 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... + + if( index < 0 ) + { + return -1; + } + + char letter = ('d' + index); + target += letter; + break; + }; + + // "Replace" inserts the name-value pair even if it doesn't exist. + disk->replace("TARGET", target); + + return 1; + } + + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 3fb3118a4d..2dad241336 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -32,6 +32,7 @@ int ImagePool::allocate ( string source = ""; string type = ""; string original_path = ""; + string dev_prefix = ""; ostringstream tmp_hashstream; ostringstream tmp_sourcestream; @@ -113,6 +114,19 @@ int ImagePool::allocate ( goto error_original_path; } + // DEV_PREFIX template attribute must exist for every image, if it + // isn't present it will be set to the default value. + + img->get_template_attribute("DEV_PREFIX", dev_prefix); + + if( dev_prefix.empty() ) + { + SingleAttribute * dev_att = + new SingleAttribute("DEV_PREFIX", default_dev_prefix); + + img->image_template.set(dev_att); + } + img->running_vms = 0;