1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #523: Image repository is now decopled from storage backend. Source is generated by drivers using a driver-specific URL.

This commit is contained in:
Ruben S. Montero 2011-05-07 02:49:07 +02:00
parent e050429a7d
commit 96f26468a1
20 changed files with 102 additions and 129 deletions

View File

@ -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:
// -------------------------------------------------------------------------

View File

@ -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;
/**

View File

@ -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

View File

@ -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);

View File

@ -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"

View File

@ -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; i<md_len; i++)
{
oss << setfill('0') << setw(2) << hex << nouppercase
<< (unsigned short) md_value[i];
}
return oss.str();
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

View File

@ -122,7 +122,7 @@ void ImageManager::move_image(Image *img, const string& source)
}
oss << "Moving disk " << source << " to repository image "
<< img->get_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 "<<img->get_oid();
}
NebulaLog::log("ImM",Log::INFO,oss);

View File

@ -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");

View File

@ -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;

View File

@ -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 );

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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);

View File

@ -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";

View File

@ -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;

View File

@ -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);

View File

@ -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);
}

View File

@ -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)