diff --git a/SConstruct b/SConstruct index 917346a2fe..7557aea8e2 100644 --- a/SConstruct +++ b/SConstruct @@ -52,6 +52,7 @@ main_env.Append(LIBPATH=[ cwd+'/src/tm', cwd+'/src/dm', cwd+'/src/im', + cwd+'/src/image', cwd+'/src/rm', cwd+'/src/vnm', cwd+'/src/hm', @@ -154,6 +155,7 @@ build_scripts=[ 'src/rm/SConstruct', 'src/tm/SConstruct', 'src/im/SConstruct', + 'src/image/SConstruct', 'src/dm/SConstruct', 'src/scheduler/SConstruct', 'src/vnm/SConstruct', diff --git a/include/Image.h b/include/Image.h index 6c380e7bfc..5fcfea3dab 100644 --- a/include/Image.h +++ b/include/Image.h @@ -108,6 +108,58 @@ public: return name; }; + /** + * Set enum type + * @return 0 on success, -1 otherwise + */ + int set_type(string _type) + { + int rc = 0; + + if ( _type == "OS" ) + { + type = OS; + } + else if ( _type == "CDROM" ) + { + type = CDROM; + } + else if ( _type == "DATABLOCK" ) + { + type = DATABLOCK; + } + else + { + rc = -1; + } + + return rc; + } + + /** + * Set enum bus + * @return 0 on success, -1 otherwise + */ + int set_bus(string _bus) + { + int rc = 0; + + if ( _bus == "IDE" ) + { + bus = IDE; + } + else if ( _bus == "SCSI" ) + { + bus = SCSI; + } + else + { + rc = -1; + } + + return rc; + } + /** * Get an image to be used in a VM * @param overwrite true if the image is going to be overwritten @@ -190,9 +242,9 @@ public: { Attribute * single_attr; - single_attr = new SongleAttribute(name,value); + single_attr = new SingleAttribute(name,value); - image_template->set(single_attr); + image_template.set(single_attr); } /** @@ -208,7 +260,7 @@ public: vector_attr = new VectorAttribute(name,value); - image_template->set(vector_attr); + image_template.set(vector_attr); } /** @@ -218,12 +270,19 @@ public: int remove_template_attribute( const string& name) { - int rc; + int rc; vector values; + Attribute * value; - rc = image_template->remove(name, values); + rc = image_template.remove(name, values); - delete[] values; + // Delete all pointers in the vector + while (!values.empty()) + { + value = values.front(); // Gets the pointer + values.erase(values.begin()); // Removes it from the vector + delete value; // Frees memory + } return rc; } @@ -263,7 +322,7 @@ private: /** * Registration time */ - time_t registration_time; + time_t regtime; /** * Path to the image @@ -396,16 +455,7 @@ protected: * @param db pointer to the db * @return 0 on success */ - int drop(SqlDB *db) - { - int rc; - - rc = image_template.drop(db); - - rc += image_drop(db); - - return rc; - } + virtual int drop(SqlDB *db); /** * Function to output an Image object in to an stream in XML format diff --git a/include/ImagePool.h b/include/ImagePool.h index 7362406407..8236e39509 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -19,12 +19,12 @@ #include "PoolSQL.h" #include "Image.h" +#include "NebulaLog.h" #include #include #include - #include using namespace std; @@ -36,7 +36,38 @@ class ImagePool : public PoolSQL { public: - ImagePool(SqlDB * db):PoolSQL(db,Image::table){}; + ImagePool(SqlDB * db, + const string& _source_prefix, + const string& _default_type, + const string& _default_bus): + 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; + } + }; ~ImagePool(){}; @@ -136,6 +167,21 @@ private: * This map stores the association between IIDs and Image names */ map image_names; + + /** + * Path to the image repository + **/ + string source_prefix; + + /** + * Default image type + **/ + string default_type; + + /** + * Default bus type + **/ + string default_bus; /** * Factory method to produce Image objects @@ -155,6 +201,13 @@ private: * @return 0 on success */ int dump_cb(void * _oss, int num, char **values, char **names); + + /** + * "Encrypts" the password with SHA1 digest + * @param password + * @return sha1 encrypted password + */ + string sha1_digest(const string& pass); }; #endif /*IMAGE_POOL_H_*/ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 3c28ab7532..f223efdcf4 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -21,6 +21,7 @@ #include "PoolSQL.h" #include "History.h" #include "Log.h" +#include "NebulaLog.h" #include #include @@ -1041,24 +1042,14 @@ protected: virtual int update(SqlDB * db); /** - * Deletes a VM from the database and all its associated information: - * - History records - * - VM template + * Deletes a VM from the database and all its associated information * @param db pointer to the db - * @return 0 on success + * @return -1 */ virtual int drop(SqlDB * db) { - int rc; - - rc = vm_template.drop(db); - - if ( history != 0 ) - { - rc += history->drop(db); - } - - return rc; + NebulaLog::log("ONE",Log::ERROR, "VM Drop not implemented!"); + return -1; } /** diff --git a/src/image/Image.cc b/src/image/Image.cc index 0b6e797300..771711a199 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -31,11 +31,11 @@ Image::Image(int _uid): uid(_uid), name(""), description(""), - type(0), - registration_time(time(0)), + type(OS), + regtime(time(0)), source(""), target(""), - bus(0), + bus(IDE), state(INIT) {}; @@ -421,8 +421,8 @@ string& Image::to_str(string& str) const /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ - -bool get_image(bool overwrite) +// TODO update? +bool Image::get_image(bool overwrite) { if ( state == READY || state == USED ) { @@ -446,8 +446,8 @@ bool get_image(bool overwrite) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ - -void release_image() + // TODO update? +void Image::release_image() { running_vms--; diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 654f2a135b..4fe2997b26 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -19,6 +19,8 @@ /* ************************************************************************** */ #include "ImagePool.h" +#include +#include int ImagePool::allocate ( int uid, @@ -26,11 +28,16 @@ int ImagePool::allocate ( int * oid) { Image * img; - string name; - string type; - string original_path; - string target; - string bus; + string name = ""; + string description = ""; + string source = ""; + string type = ""; + string original_path = ""; + string target = ""; + string bus = ""; + + ostringstream tmp_hashstream; + ostringstream tmp_sourcestream; char * error_msg; int rc; @@ -59,48 +66,54 @@ int ImagePool::allocate ( { goto error_name; } + + img->get_template_attribute("DESCRIPTION", description); img->get_template_attribute("TYPE", type); if ( type.empty() == true ) { - goto error_type; - } - + type = default_type; + } + img->get_template_attribute("ORIGINAL_PATH", original_path); - if ( type == "OS" || type == "CDROM" ) + if ( (type == "OS" || type == "CDROM") && + original_path.empty() == true ) { - if ( original_path.empty() == true ) - { - goto error_original_path; - } - } - - img->get_template_attribute("TARGET", target); - - if ( target.empty() == true ) - { - target = "hda"; // TODO add to oned.configuration + goto error_original_path; } img->get_template_attribute("BUS", bus); if ( bus.empty() == true ) { - bus = "IDE"; // TODO add to oned.configuration + bus = default_bus; } - - - // generatesource - - img->name = name; - img->type = type; - img->target = target; - img->bus = bus; - + img->running_vms = 0; + + // Generate path to store the image + tmp_hashstream << oid << ":" << uid << ":" << name; + tmp_hashstream.str(sha1_digest(tmp_hashstream.str())); + + tmp_sourcestream << source_prefix << "/" << tmp_hashstream; + + img->name = name; + img->description = description; + img->source = tmp_sourcestream.str(); + + if (img->set_type(type) != 0) + { + goto error_type; + } + + if (img->set_bus(bus) != 0) + { + goto error_bus; + } + // --------------------------------------------------------------------- // Insert the Object in the pool // --------------------------------------------------------------------- @@ -121,26 +134,26 @@ error_name: NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template"); goto error_common; error_type: - NebulaLog::log("IMG", Log::ERROR, "TYPE not present in image template"); + NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template"); goto error_common; +error_bus: + NebulaLog::log("IMG", Log::ERROR, "Incorrect BUS in image template"); + goto error_common; error_original_path: NebulaLog::log("IMG", Log::ERROR, "ORIGINAL_PATH compulsory and not present in image template of this type."); goto error_common; error_common: delete img; - *oid = -1; return -1; + error_parse: ostringstream oss; - oss << "ImagePool template parse error: " << error_msg; NebulaLog::log("IMG", Log::ERROR, oss); free(error_msg); - delete img; - *oid = -2; return -2; } @@ -183,3 +196,30 @@ int ImagePool::dump(ostringstream& oss, const string& where) return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string ImagePool::sha1_digest(const string& pass) +{ + EVP_MD_CTX mdctx; + unsigned char md_value[EVP_MAX_MD_SIZE]; + unsigned int md_len; + ostringstream oss; + + EVP_MD_CTX_init(&mdctx); + EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL); + + EVP_DigestUpdate(&mdctx, pass.c_str(), pass.length()); + + EVP_DigestFinal_ex(&mdctx,md_value,&md_len); + EVP_MD_CTX_cleanup(&mdctx); + + for(unsigned int i = 0; i