From 96f26468a1bdddef67c78fa472404c8858738bf0 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 7 May 2011 02:49:07 +0200 Subject: [PATCH] feature #523: Image repository is now decopled from storage backend. Source is generated by drivers using a driver-specific URL. --- include/Image.h | 17 ++++----- include/ImageManagerDriver.h | 7 ++-- include/ImagePool.h | 10 ------ include/test/NebulaTest.h | 1 - share/etc/oned.conf | 5 --- src/image/Image.cc | 51 ++------------------------ src/image/ImageManagerActions.cc | 9 +++-- src/image/ImageManagerDriver.cc | 54 ++++++++++++++++++++++++---- src/image/ImagePool.cc | 3 -- src/image/test/ImagePoolTest.cc | 6 ++-- src/image_mad/one_image.rb | 12 +++---- src/image_mad/remotes/fs/cp | 12 ++++--- src/image_mad/remotes/fs/mkfs | 10 ++++-- src/image_mad/remotes/fs/mv | 14 ++++++-- src/nebula/Nebula.cc | 3 -- src/nebula/NebulaTemplate.cc | 7 ---- src/rm/RequestManagerSaveDisk.cc | 1 - src/rm/RequestManagerTemplateInfo.cc | 3 -- src/test/Nebula.cc | 2 -- src/test/NebulaTest.cc | 4 +-- 20 files changed, 102 insertions(+), 129 deletions(-) diff --git a/include/Image.h b/include/Image.h index 6e72bc0fb2..1ce8c795c8 100644 --- a/include/Image.h +++ b/include/Image.h @@ -103,6 +103,15 @@ public: return source; } + /** + * Returns the source path of the image + * @return source of image + */ + void set_source(const string& _source) + { + source = _source; + } + /** * Returns the type of the image * @return type @@ -250,14 +259,6 @@ public: */ int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type); - /** - * Generates the source path for the repository. - * @param uid of the image owner - * @param name of the image - * @return source for the image - */ - static string generate_source(int uid, const string& name); - private: // ------------------------------------------------------------------------- diff --git a/include/ImageManagerDriver.h b/include/ImageManagerDriver.h index 4844a3b68d..7a07c73350 100644 --- a/include/ImageManagerDriver.h +++ b/include/ImageManagerDriver.h @@ -70,12 +70,13 @@ private: */ //Template driver_conf; - void cp(int oid, const string& source, const string& destination) const; + void cp(int oid, const string& source) const; /** * Sends a move request to the MAD: "MV IMAGE_ID SRC_PATH DST_PATH" * @param oid the image id. - * @param destination is the path to the image to be created + * @param destination is a driver specific location or "-" if not + * initialized * @param size_mb of the image to be created */ void mv(int oid, const string& source, const string& destination) const; @@ -83,12 +84,10 @@ private: /** * Sends a make filesystem request to the MAD: "MKFS IMAGE_ID PATH SIZE_MB" * @param oid the image id. - * @param destination is the path to the image to be created * @param fs type * @param size_mb of the image to be created */ void mkfs(int oid, - const string& destination, const string& fs, const string& size_mb) const; /** diff --git a/include/ImagePool.h b/include/ImagePool.h index d319ff1dda..9dae46d2ed 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -39,7 +39,6 @@ class ImagePool : public PoolSQL public: ImagePool(SqlDB * db, - const string& _source_prefix, const string& _default_type, const string& _default_dev_prefix); @@ -146,11 +145,6 @@ public: */ void authorize_disk(VectorAttribute * disk, int uid, AuthRequest * ar); - static const string& source_prefix() - { - return _source_prefix; - }; - static const string& default_type() { return _default_type; @@ -165,10 +159,6 @@ private: //-------------------------------------------------------------------------- // Configuration Attributes for Images // ------------------------------------------------------------------------- - /** - * Path to the image repository - **/ - static string _source_prefix; /** * Default image type diff --git a/include/test/NebulaTest.h b/include/test/NebulaTest.h index 9078500764..52c551799d 100644 --- a/include/test/NebulaTest.h +++ b/include/test/NebulaTest.h @@ -97,7 +97,6 @@ public: virtual UserPool* create_upool(SqlDB* db); virtual ImagePool* create_ipool( SqlDB* db, - string repository_path, string default_image_type, string default_device_prefix); diff --git a/share/etc/oned.conf b/share/etc/oned.conf index a0fa2ffb9b..0c8e7dba48 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -82,9 +82,6 @@ MAC_PREFIX = "02:00" #******************************************************************************* # Image Repository Configuration #******************************************************************************* -# IMAGE_REPOSITORY_PATH: Define the path to the image repository, by default -# is set to $ONE_LOCATION/var/images -# # DEFAULT_IMAGE_TYPE: This can take values # OS Image file holding an operating system # CDROM Image file holding a CDROM @@ -96,8 +93,6 @@ MAC_PREFIX = "02:00" # xvd XEN Virtual Disk # vd KVM virtual disk #******************************************************************************* - -#IMAGE_REPOSITORY_PATH = /srv/cloud/var/images DEFAULT_IMAGE_TYPE = "OS" DEFAULT_DEVICE_PREFIX = "hd" diff --git a/src/image/Image.cc b/src/image/Image.cc index e5e6167a99..618f226ba1 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -41,7 +41,7 @@ Image::Image(int _uid, user_name(_user_name), type(OS), regtime(time(0)), - source(""), + source("-"), state(INIT), running_vms(0) { @@ -78,22 +78,6 @@ const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool (" /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ -string Image::generate_source(int uid, const string& name) -{ - ostringstream tmp_hashstream; - ostringstream tmp_sourcestream; - - tmp_hashstream << uid << ":" << name; - - tmp_sourcestream << ImagePool::source_prefix() << "/"; - tmp_sourcestream << sha1_digest(tmp_hashstream.str()); - - return tmp_sourcestream.str(); -} - -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - int Image::insert(SqlDB *db, string& error_str) { int rc; @@ -163,6 +147,7 @@ int Image::insert(SqlDB *db, string& error_str) { SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX", ImagePool::default_dev_prefix()); + obj_template->set(dev_att); } @@ -203,11 +188,6 @@ int Image::insert(SqlDB *db, string& error_str) goto error_path_and_source; } - if (source.empty()) - { - source = Image::generate_source(uid,name); - } - state = LOCKED; //LOCKED till the ImageManager copies it to the Repository //-------------------------------------------------------------------------- @@ -529,32 +509,5 @@ int Image::disk_attribute( VectorAttribute * disk, return 0; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string Image::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; iget_oid() << " as " << img->get_source(); + << img->get_oid(); imd->mv(img->get_oid(),source,img->get_source()); @@ -400,7 +400,7 @@ int ImageManager::register_image(int iid) if ( img->get_type() == Image::DATABLOCK && !size.empty() && !fs.empty() ) { - imd->mkfs(img->get_oid(), img->get_source(), fs, size); + imd->mkfs(img->get_oid(), fs, size); oss << "Creating disk at " << img->get_source() << " of " << size << "Mb with format " << fs; @@ -416,9 +416,8 @@ int ImageManager::register_image(int iid) } else //PATH -> COPY TO REPOSITORY AS SOURCE { - imd->cp(img->get_oid(), path, img->get_source()); - oss << "Copying image " << path - << " to repository as " << img->get_source(); + imd->cp(img->get_oid(), path); + oss << "Copying " << path <<" to repository for image "<get_oid(); } NebulaLog::log("ImM",Log::INFO,oss); diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index 4bd5aca413..3e2b5baaae 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -27,12 +27,11 @@ /* ************************************************************************** */ void ImageManagerDriver::cp(int oid, - const string& source, - const string& destination) const + const string& source) const { ostringstream os; - os << "CP " << oid << " " << source << " " << destination << endl; + os << "CP " << oid << " " << source << endl; write(os); } @@ -53,14 +52,12 @@ void ImageManagerDriver::mv(int oid, /* -------------------------------------------------------------------------- */ void ImageManagerDriver::mkfs(int oid, - const string& destination, const string& fs, const string& size_mb) const { ostringstream os; - os << "MKFS " << oid << " " << destination << " " << - fs << " " << size_mb << endl; + os << "MKFS " << oid << " " << fs << " " << size_mb << endl; write(os); } @@ -146,7 +143,21 @@ void ImageManagerDriver::protocol( { if ( result == "SUCCESS" ) { + string source; + + if ( is.good() ) + { + is >> source >> ws; + } + + if ( is.fail() ) + { + goto error_cp; + } + + image->set_source(source); image->set_state(Image::READY); + ipool->update(image); NebulaLog::log("ImM", Log::INFO, "Image copied and ready to use."); @@ -160,6 +171,23 @@ void ImageManagerDriver::protocol( { if ( result == "SUCCESS" ) { + if (image->get_source() == "-") + { + string source; + + if ( is.good() ) + { + is >> source >> ws; + } + + if ( is.fail() ) + { + goto error_mv; + } + + image->set_source(source); + } + image->set_state(Image::READY); ipool->update(image); @@ -174,7 +202,21 @@ void ImageManagerDriver::protocol( { if ( result == "SUCCESS" ) { + string source; + + if ( is.good() ) + { + is >> source >> ws; + } + + if ( is.fail() ) + { + goto error_mkfs; + } + + image->set_source(source); image->set_state(Image::READY); + ipool->update(image); NebulaLog::log("ImM", Log::INFO, "Image created and ready to use"); diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index d5243cd95d..b59c8470a9 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -24,7 +24,6 @@ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string ImagePool::_source_prefix; string ImagePool::_default_type; string ImagePool::_default_dev_prefix; @@ -32,7 +31,6 @@ string ImagePool::_default_dev_prefix; /* -------------------------------------------------------------------------- */ ImagePool::ImagePool(SqlDB * db, - const string& __source_prefix, const string& __default_type, const string& __default_dev_prefix): PoolSQL(db,Image::table) @@ -40,7 +38,6 @@ ImagePool::ImagePool(SqlDB * db, ostringstream sql; // Init static defaults - _source_prefix = __source_prefix; _default_type = __default_type; _default_dev_prefix = __default_dev_prefix; diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index 187759853c..409462624a 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -70,12 +70,10 @@ class ImagePoolFriend : public ImagePool { public: ImagePoolFriend(SqlDB * db, - const string& _source_prefix, const string& _default_type, const string& _default_dev_prefix): ImagePool( db, - _source_prefix, _default_type, _default_dev_prefix){}; @@ -147,7 +145,7 @@ protected: PoolSQL* create_pool(SqlDB* db) { ImagePoolFriend * imp = - new ImagePoolFriend(db, "source_prefix", "OS", "hd"); + new ImagePoolFriend(db, "OS", "hd"); return imp; }; @@ -201,7 +199,7 @@ public: // Create a new pool, using the same DB. This new pool should read the // allocated images. - imp = new ImagePool(db, "source_prefix", "OS", "hd"); + imp = new ImagePool(db,"OS", "hd"); img = imp->get(names[0], uids[0], false); CPPUNIT_ASSERT( img != 0 ); diff --git a/src/image_mad/one_image.rb b/src/image_mad/one_image.rb index 70739c2434..1bed418423 100755 --- a/src/image_mad/one_image.rb +++ b/src/image_mad/one_image.rb @@ -69,19 +69,19 @@ class ImageDriver < OpenNebulaDriver # Image Manager Protocol Actions (generic implementation # ------------------------------------------------------------------------- def mv(id, src, dst) - local_action("#{@actions_path}/mv #{src} #{dst}",id,ACTION[:mv]) + local_action("#{@actions_path}/mv #{src} #{dst} #{id}",id,ACTION[:mv]) end - def cp(id, src, dst) - local_action("#{@actions_path}/cp #{src} #{dst}",id,ACTION[:cp]) + def cp(id, src) + local_action("#{@actions_path}/cp #{src} #{id}",id,ACTION[:cp]) end def rm(id, dst) - local_action("#{@actions_path}/rm #{dst}",id,ACTION[:rm]) + local_action("#{@actions_path}/rm #{dst} #{id}",id,ACTION[:rm]) end - def mkfs(id, dst, fs, size) - local_action("#{@actions_path}/mkfs #{dst} #{fs} #{size}",id,ACTION[:mkfs]) + def mkfs(id, fs, size) + local_action("#{@actions_path}/mkfs #{fs} #{size} #{id}",id,ACTION[:mkfs]) end end diff --git a/src/image_mad/remotes/fs/cp b/src/image_mad/remotes/fs/cp index 78d3236276..24da5fbaff 100755 --- a/src/image_mad/remotes/fs/cp +++ b/src/image_mad/remotes/fs/cp @@ -21,7 +21,7 @@ # Several SRC types are supported ############################################################################### -# ------------ Set up the environment to source common tools ------------ +# -------- Set up the environment to source common tools & conf ------------ if [ -z "${ONE_LOCATION}" ]; then LIB_LOCATION=/usr/lib/one @@ -30,11 +30,13 @@ else fi . $LIB_LOCATION/sh/scripts_common.sh - -# ------------ Copy the image to the repository ------------ +source $(dirname $0)/fsrc SRC=$1 -DST=$2 +ID=$2 +DST=`generate_image_path` + +# ------------ Copy the image to the repository ------------- case $SRC in http://*) @@ -51,3 +53,5 @@ http://*) esac exec_and_log "chmod 0660 $DST" + +echo "$DST" diff --git a/src/image_mad/remotes/fs/mkfs b/src/image_mad/remotes/fs/mkfs index e6b1949366..1a20060482 100755 --- a/src/image_mad/remotes/fs/mkfs +++ b/src/image_mad/remotes/fs/mkfs @@ -33,12 +33,16 @@ fi # ------------ Create the image to the repository ------------ -DST=$1 -FSTYPE=$2 -SIZE=$3 +FSTYPE=$1 +SIZE=$2 +ID=$3 + +DST=`generate_image_path` exec_and_log "$DD if=/dev/zero of=$DST bs=1 count=1 seek=${SIZE}M" \ "Could not create image $DST" exec_and_log "$MKFS -t $FSTYPE -F $DST" \ "Unable to create filesystem $FSTYPE in $DST" exec_and_log "chmod 0660 $DST" + +echo "$DST" diff --git a/src/image_mad/remotes/fs/mv b/src/image_mad/remotes/fs/mv index 74ee2f7199..c7ede18aec 100755 --- a/src/image_mad/remotes/fs/mv +++ b/src/image_mad/remotes/fs/mv @@ -31,10 +31,18 @@ fi . $LIB_LOCATION/sh/scripts_common.sh -# ------------ Move the image to the repository ------------ - SRC=$1 DST=$2 +ID=$3 + +# ------------ Generate a filename for the image ------------ + +if [ $DST -eq "-" ] ; then + DST=`generate_image_path` +fi + +# ------------ Move the image to the repository ------------ + case $SRC in http://*) @@ -51,3 +59,5 @@ http://*) esac exec_and_log "chmod 0660 $DST" + +echo "$DST" diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 067e121cc8..fa8fc57ab1 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -246,7 +246,6 @@ void Nebula::start() { string mac_prefix; int size; - string repository_path; string default_image_type; string default_device_prefix; @@ -266,13 +265,11 @@ void Nebula::start() upool = new UserPool(db); - nebula_configuration->get("IMAGE_REPOSITORY_PATH", repository_path); nebula_configuration->get("DEFAULT_IMAGE_TYPE", default_image_type); nebula_configuration->get("DEFAULT_DEVICE_PREFIX", default_device_prefix); ipool = new ImagePool(db, - repository_path, default_image_type, default_device_prefix); diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index e4ade7fab9..91c0f29812 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -126,17 +126,10 @@ NebulaTemplate::NebulaTemplate(string& etc_location, string& var_location) #******************************************************************************* # Image Repository Configuration #******************************************************************************* -# IMAGE_REPOSITORY_PATH # DEFAULT_IMAGE_TYPE # DEFAULT_DEVICE_PREFIX #******************************************************************************* */ - //IMAGE_REPOSITORY_PATH - value = var_location + "/images"; - - attribute = new SingleAttribute("IMAGE_REPOSITORY_PATH",value); - conf_default.insert(make_pair(attribute->name(),attribute)); - //DEFAULT_IMAGE_TYPE value = "OS"; diff --git a/src/rm/RequestManagerSaveDisk.cc b/src/rm/RequestManagerSaveDisk.cc index 2d49a9d6df..7b62095cf8 100644 --- a/src/rm/RequestManagerSaveDisk.cc +++ b/src/rm/RequestManagerSaveDisk.cc @@ -102,7 +102,6 @@ void RequestManager::VirtualMachineSaveDisk::execute( oss << "NAME= " << img_name << endl; oss << "PUBLIC = NO " << endl; - oss << "SOURCE = " << Image::generate_source(uid,img_name); img_template = new ImageTemplate; diff --git a/src/rm/RequestManagerTemplateInfo.cc b/src/rm/RequestManagerTemplateInfo.cc index 8567080f42..ca6d15990a 100644 --- a/src/rm/RequestManagerTemplateInfo.cc +++ b/src/rm/RequestManagerTemplateInfo.cc @@ -29,7 +29,6 @@ void RequestManager::TemplateInfo::execute( string session; int oid; - int uid; // owner user id int rc; // Requesting user id VMTemplate * vm_template; @@ -55,8 +54,6 @@ void RequestManager::TemplateInfo::execute( goto error_get; } - uid = vm_template->get_uid(); - // Check if it is a valid user rc = TemplateInfo::upool->authenticate(session); diff --git a/src/test/Nebula.cc b/src/test/Nebula.cc index ea1355ed8f..0c07de18cf 100644 --- a/src/test/Nebula.cc +++ b/src/test/Nebula.cc @@ -183,7 +183,6 @@ void Nebula::start() { string mac_prefix = "00:00"; int size = 1; - string repository_path; string default_image_type; string default_device_prefix; @@ -210,7 +209,6 @@ void Nebula::start() if (tester->need_image_pool) { ipool = tester->create_ipool(db, - repository_path, default_image_type, default_device_prefix); } diff --git a/src/test/NebulaTest.cc b/src/test/NebulaTest.cc index 1b9f8ab4c8..c0a6b04808 100644 --- a/src/test/NebulaTest.cc +++ b/src/test/NebulaTest.cc @@ -41,12 +41,10 @@ UserPool* NebulaTest::create_upool(SqlDB* db) } ImagePool* NebulaTest::create_ipool( SqlDB* db, - string repository_path, string default_image_type, string default_device_prefix) { - return new ImagePool(db,repository_path,default_image_type, - default_device_prefix); + return new ImagePool(db,default_image_type,default_device_prefix); } ClusterPool* NebulaTest::create_cpool(SqlDB* db)