1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-25 02:50:08 +03:00

Merge branch 'bug-295' of dsa-research.org:one into bug-295

This commit is contained in:
Tino Vázquez 2010-08-02 13:01:53 +02:00
commit 61e49edc1e
18 changed files with 378 additions and 279 deletions

View File

@ -236,6 +236,7 @@ private:
etc_location = "/etc/one/";
log_location = "/var/log/one/";
var_location = "/var/lib/one/";
hook_location= "/usr/share/one/hooks/";
}
else
{
@ -250,6 +251,7 @@ private:
etc_location = nebula_location + "etc/";
log_location = nebula_location + "var/";
var_location = nebula_location + "var/";
hook_location= nebula_location + "share/hooks/";
}
};
@ -345,6 +347,7 @@ private:
string etc_location;
string log_location;
string var_location;
string hook_location;
string hostname;
// ---------------------------------------------------------------

View File

@ -46,7 +46,7 @@ public:
* @param table the name of the table supporting the pool (to set the oid
* counter). If null the OID counter is not updated.
*/
PoolSQL(SqlDB * _db, const char * table=0);
PoolSQL(SqlDB * _db, const char * table);
virtual ~PoolSQL();

View File

@ -32,7 +32,9 @@ class VirtualMachinePool : public PoolSQL
{
public:
VirtualMachinePool(SqlDB * db, vector<const Attribute *> hook_mads);
VirtualMachinePool(SqlDB * db,
vector<const Attribute *> hook_mads,
const string& hook_location);
~VirtualMachinePool(){};

View File

@ -99,7 +99,7 @@ if [ -z "$ROOT" ] ; then
RUN_LOCATION="/var/run/one"
LOCK_LOCATION="/var/lock/one"
INCLUDE_LOCATION="/usr/include"
SHARE_LOCATION="/usr/share/doc/opennebula"
SHARE_LOCATION="/usr/share/one"
if [ "$CLIENT" = "no" ]; then
MAKE_DIRS="$BIN_LOCATION $LIB_LOCATION $ETC_LOCATION $VAR_LOCATION \
@ -306,6 +306,7 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
src/oca/ruby/OpenNebula/VirtualNetworkPool.rb \
src/oca/ruby/OpenNebula/Image.rb \
src/oca/ruby/OpenNebula/ImagePool.rb \
src/oca/ruby/OpenNebula/ImageRepository.rb \
src/oca/ruby/OpenNebula/Cluster.rb \
src/oca/ruby/OpenNebula/ClusterPool.rb \
src/oca/ruby/OpenNebula/XMLUtils.rb"

View File

@ -82,7 +82,7 @@ MAC_PREFIX = "02:00"
# vd KVM virtual disk
#*******************************************************************************
IMAGE_REPOSITORY_PATH = [IMAGES_LOCATION]
#IMAGE_REPOSITORY_PATH = /srv/cloud/var/images
DEFAULT_IMAGE_TYPE = "OS"
DEFAULT_DEVICE_PREFIX = "hd"

View File

@ -37,6 +37,7 @@ end
client = Client.new()
img_repo = ImageRepository.new
vm = VirtualMachine.new(
VirtualMachine.build_xml(vm_id),
@ -52,17 +53,8 @@ vm.each('TEMPLATE/DISK') do |disk|
Image.build_xml(image_id),
client)
result = image.info
exit -1 if OpenNebula.is_error?(result)
# Disable the Image for a safe overwriting
# image.disable
# Save the image file
result = image.move(source_path, image['SOURCE'])
result = img_repo.update_source(image, source_path)
exit -1 if OpenNebula.is_error?(result)
# image.enable
end
end

View File

@ -251,6 +251,11 @@ EOT
table.print_help
end
def special_options(opts, options)
opts.on_tail("-n", "--no-cp", "Do not copy the source") do |o|
options[:no_cp]=true
end
end
end
@ -300,58 +305,18 @@ result=[false, "Unknown error"]
command=ARGV.shift
img_repo = OpenNebula::ImageRepository.new
case command
when "register", "create", "add"
# ---------- Get the Template ------------
when "register", "create", "add"
check_parameters("register", 1)
template = get_template(ARGV[0])
# ---------- Allocate the Image file ------------
image = OpenNebula::Image.new(
OpenNebula::Image.build_xml,
get_one_client)
result = image.allocate(template)
if !is_successful?(result)
puts result.message
exit -1
end
image = OpenNebula::Image.new(OpenNebula::Image.build_xml, get_one_client)
# ---------- Copy the Image file ------------
image.info
if image['TEMPLATE/PATH']
file_path = image['TEMPLATE/PATH']
if !File.exists?(file_path)
error_msg = "Image file could not be found, aborting."
result = OpenNebula::Error.new(error_msg)
end
result = image.copy(file_path, image['SOURCE'])
elsif image['TEMPLATE/SIZE'] and
image['TEMPLATE/FSTYPE'] and
image['TEMPLATE/TYPE'] == 'DATABLOCK'
result = image.mk_datablock(
image['TEMPLATE/SIZE'],
image['TEMPLATE/FSTYPE'],
image['SOURCE'])
else
error_msg = "Image not present, aborting."
result = OpenNebula::Error.new(error_msg)
end
if is_successful?(result)
image.enable
result = img_repo.create(image, template, !ops[:no_cp])
if is_successful?(result)
puts "ID: " + image.id.to_s if ops[:verbose]
exit 0
else
image.delete
end
@ -494,22 +459,14 @@ when "delete"
args=expand_args(ARGV)
args.each do |param|
image_id=get_image_id(param)
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result = image.info
image_id = get_image_id(param)
image = OpenNebula::Image.new(
OpenNebula::Image.build_xml(image_id),
get_one_client)
result = img_repo.delete(image)
if is_successful?(result)
file_path = image['SOURCE']
result=image.delete
if is_successful?(result)
FileUtils.rm(file_path) if File.exists?(file_path)
puts "Image correctly deleted" if ops[:verbose]
end
puts "Image correctly deleted" if ops[:verbose]
end
end

View File

@ -58,6 +58,7 @@ class CloudServer
@one_client = Client.new()
@user_pool = UserPool.new(@one_client)
@img_repo = OpenNebula::ImageRepository.new
end
#
@ -110,47 +111,23 @@ class CloudServer
if file
if file[:tempfile]
file_path = file[:tempfile].path
template = image.to_one_template
template << "\nPATH = #{file_path}"
else
error_msg = "Image not present, aborting."
error = OpenNebula::Error.new(error_msg)
return error
end
if !File.exists?(file_path)
error_msg = "Image file could not be found, aborting."
error = OpenNebula::Error.new(error_msg)
return error
end
end
# ---------- Allocate the Image file ------------
rc = @img_repo.create(image, template)
rc = image.allocate(image.to_one_template)
file[:tempfile].unlink
if OpenNebula.is_error?(rc)
return rc
end
# ---------- Copy the Image file ------------
image.info
if file_path
rc = image.copy(file_path, image['SOURCE'])
file[:tempfile].unlink
elsif image['TEMPLATE/SIZE'] and
image['TEMPLATE/FSTYPE'] and
image['TEMPLATE/TYPE'] == 'DATABLOCK'
rc = image.mk_datablock(
image['TEMPLATE/SIZE'],
image['TEMPLATE/FSTYPE'],
image['SOURCE'])
end
if OpenNebula.is_error?(rc)
image.delete
return rc
end
return nil
end

View File

@ -355,7 +355,7 @@ class OCCIServer < CloudServer
get_client(request.env))
# --- Delete the Image ---
rc = image.delete
rc = @img_repo.delete(image)
return rc, 500 if OpenNebula::is_error?(rc)
return "", 204

View File

@ -540,17 +540,21 @@ int Image::disk_attribute( VectorAttribute * disk,
ImageType* img_type)
{
string bus;
string target;
ostringstream iid;
*img_type = type;
bus = disk->vector_value("BUS");
target = disk->vector_value("TARGET");
iid << oid;
string template_bus;
string template_target;
string prefix;
get_template_attribute("BUS", template_bus);
get_template_attribute("TARGET", template_target);
get_template_attribute("DEV_PREFIX", prefix);
//--------------------------------------------------------------------------
@ -572,17 +576,14 @@ int Image::disk_attribute( VectorAttribute * disk,
new_disk.insert(make_pair("IMAGE_ID", iid.str()));
new_disk.insert(make_pair("SOURCE", source));
if (bus.empty())
{
if (!template_bus.empty())
{
new_disk.insert(make_pair("BUS",template_bus));
}
}
else
if (!bus.empty())
{
new_disk.insert(make_pair("BUS",bus));
}
else if (!template_bus.empty())
{
new_disk.insert(make_pair("BUS",template_bus));
}
//---------------------------------------------------------------------------
// TYPE, READONLY, CLONE, and SAVE attributes
@ -609,23 +610,35 @@ int Image::disk_attribute( VectorAttribute * disk,
// TARGET attribute
//---------------------------------------------------------------------------
switch(type)
if (!target.empty())
{
case OS:
prefix += "a";
break;
case CDROM:
prefix += "c"; // b is for context
break;
case DATABLOCK:
prefix += static_cast<char>(('e'+ *index));
*index = *index + 1;
break;
new_disk.insert(make_pair("TARGET", target));
}
else if (!template_target.empty())
{
new_disk.insert(make_pair("TARGET", template_target));
}
else
{
switch(type)
{
case OS:
prefix += "a";
break;
case CDROM:
prefix += "c"; // b is for context
break;
case DATABLOCK:
prefix += static_cast<char>(('e'+ *index));
*index = *index + 1;
break;
}
new_disk.insert(make_pair("TARGET", prefix));
}
new_disk.insert(make_pair("TARGET", prefix));
disk->replace(new_disk);

View File

@ -224,7 +224,7 @@ void Nebula::start()
nebula_configuration->get("VM_HOOK", vm_hooks);
vmpool = new VirtualMachinePool(db, vm_hooks);
vmpool = new VirtualMachinePool(db, vm_hooks,hook_location);
hpool = new HostPool(db);
nebula_configuration->get("MAC_PREFIX", mac_prefix);
@ -235,12 +235,6 @@ void Nebula::start()
upool = new UserPool(db);
nebula_configuration->get("IMAGE_REPOSITORY_PATH", repository_path);
if (repository_path.empty()) // Defaults to ONE_LOCATION/var
{
repository_path = var_location;
}
nebula_configuration->get("DEFAULT_IMAGE_TYPE", default_image_type);
nebula_configuration->get("DEFAULT_DEVICE_PREFIX",
default_device_prefix);

View File

@ -31,60 +31,114 @@ NebulaTemplate::NebulaTemplate(string& etc_location, string& var_location)
{
ostringstream os;
SingleAttribute * attribute;
VectorAttribute * vattribute;
string value;
conf_file = etc_location + conf_name;
// MANAGER_TIMER
value = "15";
attribute = new SingleAttribute("MANAGER_TIMER",value);
conf_default.insert(make_pair(attribute->name(),attribute));
/*
#*******************************************************************************
# Daemon configuration attributes
#-------------------------------------------------------------------------------
# HOST_MONITORING_INTERVAL
# VM_POLLING_INTERVAL
# VM_DIR
# PORT
# DB
# VNC_BASE_PORT
#*******************************************************************************
*/
// MONITOR_INTERVAL
value = "600";
attribute = new SingleAttribute("HOST_MONITORING_INTERVAL",value);
conf_default.insert(make_pair(attribute->name(),attribute));
// POLL_INTERVAL
value = "300";
value = "600";
attribute = new SingleAttribute("VM_POLLING_INTERVAL",value);
conf_default.insert(make_pair(attribute->name(),attribute));
// MANAGER_TIMER
value = "30";
attribute = new SingleAttribute("MANAGER_TIMER",value);
conf_default.insert(make_pair(attribute->name(),attribute));
// MONITOR_INTERVAL
value = "300";
attribute = new SingleAttribute("HOST_MONITORING_INTERVAL",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//XML-RPC Server PORT
value = "2633";
attribute = new SingleAttribute("PORT",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//VM_DIR
//VM_DIR
attribute = new SingleAttribute("VM_DIR",var_location);
conf_default.insert(make_pair(attribute->name(),attribute));
//MAC_PREFIX
value = "00:01";
attribute = new SingleAttribute("MAC_PREFIX",value);
//XML-RPC Server PORT
value = "2633";
attribute = new SingleAttribute("PORT",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//NETWORK_SIZE
value = "254";
attribute = new SingleAttribute("NETWORK_SIZE",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//DB CONFIGURATION
map<string,string> vvalue;
vvalue.insert(make_pair("BACKEND","sqlite"));
//NETWORK_SIZE
vattribute = new VectorAttribute("DB",vvalue);
conf_default.insert(make_pair(attribute->name(),vattribute));
//VNC_BASE_PORT
value = "5900";
attribute = new SingleAttribute("VNC_BASE_PORT",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//DEBUG_LEVEL
value = Log::WARNING;
attribute = new SingleAttribute("DEBUG_LEVEL",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//XML-RPC Server PORT
value = "2633";
attribute = new SingleAttribute("PORT",value);
conf_default.insert(make_pair(attribute->name(),attribute));
/*
#*******************************************************************************
# Physical Networks configuration
#*******************************************************************************
# NETWORK_SIZE
# MAC_PREFIX
#*******************************************************************************
*/
//MAC_PREFIX
value = "02:00";
attribute = new SingleAttribute("MAC_PREFIX",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//NETWORK_SIZE
value = "254";
attribute = new SingleAttribute("NETWORK_SIZE",value);
conf_default.insert(make_pair(attribute->name(),attribute));
/*
#*******************************************************************************
# 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";
attribute = new SingleAttribute("DEFAULT_IMAGE_TYPE",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//DEFAULT_DEVICE_PREFIX
value = "hd";
attribute = new SingleAttribute("DEFAULT_DEVICE_PREFIX",value);
conf_default.insert(make_pair(attribute->name(),attribute));
}
@ -95,31 +149,31 @@ NebulaTemplate::NebulaTemplate(string& etc_location, string& var_location)
int NebulaTemplate::load_configuration()
{
char * error = 0;
map<string, Attribute *>::iterator iter, j;
map<string, Attribute *>::iterator iter, j;
int rc;
string aname;
Attribute * attr;
rc = parse(conf_file.c_str(), &error);
if ( rc != 0 && error != 0)
{
cout << "\nError while parsing configuration file:\n" << error << endl;
free(error);
return -1;
}
for(iter=conf_default.begin();iter!=conf_default.end();)
{
aname = iter->first;
attr = iter->second;
j = attributes.find(aname);
if ( j == attributes.end() )
{
attributes.insert(make_pair(aname,attr));
@ -131,7 +185,7 @@ int NebulaTemplate::load_configuration()
conf_default.erase(iter++);
}
}
return 0;
}

View File

@ -15,6 +15,7 @@ require 'OpenNebula/VirtualNetwork'
require 'OpenNebula/VirtualNetworkPool'
require 'OpenNebula/Image'
require 'OpenNebula/ImagePool'
require 'OpenNebula/ImageRepository'
require 'OpenNebula/User'
require 'OpenNebula/UserPool'
require 'OpenNebula/Host'

View File

@ -55,7 +55,6 @@ module OpenNebula
super(xml,client)
@client = client
@immanager = ImageManager.new
end
#######################################################################
@ -114,22 +113,7 @@ module OpenNebula
def delete()
super(IMAGE_METHODS[:delete])
end
def copy(path, source)
@immanager.copy(path, source)
end
def move(path, source)
@immanager.move(path, source)
end
def mk_datablock(size, fstype, source)
rc = @immanager.dd(size, source)
return rc if OpenNebula.is_error?(rc)
@immanager.mkfs(fstype, source)
end
#######################################################################
# Helpers to get Image information
@ -195,81 +179,4 @@ module OpenNebula
end
end
class ImageManager
# ---------------------------------------------------------------------
# Constants and Class Methods
# ---------------------------------------------------------------------
FS_UTILS = {
:dd => "env dd",
:mkfs => "env mkfs"
}
def copy(path, source)
if source.nil? or path.nil?
return OpenNebula::Error.new("copy Image: missing parameters.")
end
begin
FileUtils.copy(path, source)
FileUtils.chmod(0660, source)
rescue Exception => e
return OpenNebula::Error.new(e.message)
end
return nil
end
def move(path, source)
if source.nil? or path.nil?
return OpenNebula::Error.new("copy Image: missing parameters.")
end
begin
FileUtils.move(path, source)
FileUtils.chmod(0660, source)
rescue Exception => e
return OpenNebula::Error.new(e.message)
end
return nil
end
def dd(size, source)
if source.nil? or size.nil?
return OpenNebula::Error.new("dd Image: missing parameters.")
end
command = ""
command << FS_UTILS[:dd]
command << " if=/dev/zero of=#{source} ibs=1 count=1"
command << " obs=1048576 seek=#{size}"
local_command=LocalCommand.run(command)
if local_command.code!=0
return OpenNebula::Error.new("dd Image: in dd command.")
end
return nil
end
def mkfs(fstype, source)
if source.nil? or fstype.nil?
return OpenNebula::Error.new("mkfs Image: missing parameters.")
end
command = ""
command << FS_UTILS[:mkfs]
command << " -t #{fstype} -F #{source}"
local_command=LocalCommand.run(command)
if local_command.code!=0
return OpenNebula::Error.new("mkfs Image: in mkfs command.")
end
return nil
end
end
end

View File

@ -0,0 +1,185 @@
require 'OpenNebula/Image'
require 'fileutils'
module OpenNebula
class ImageRepository
def create(image, template, copy=true)
if image.nil?
error_msg = "Image could not be found, aborting."
result = OpenNebula::Error.new(error_msg)
end
# ------ Allocate the Image ------
result = image.allocate(template)
if OpenNebula.is_error?(result)
puts result.message
exit -1
end
# ------ Copy the Image file ------
image.info
if image['TEMPLATE/PATH']
if copy
# --- CDROM, DATABLOCK or OS based on a PATH ---
file_path = image['TEMPLATE/PATH']
if !File.exists?(file_path)
error_msg = "Image file could not be found, aborting."
result = OpenNebula::Error.new(error_msg)
end
result = copy(file_path, image['SOURCE'])
end
elsif image['TEMPLATE/SIZE'] and image['TEMPLATE/FSTYPE'] and \
image['TEMPLATE/TYPE'] == 'DATABLOCK'
# --- Empty DATABLOCK ---
result = dd(image['TEMPLATE/SIZE'], image['SOURCE'])
if !OpenNebula.is_error?(result)
result = mkfs(image['TEMPLATE/FSTYPE'], image['SOURCE'])
end
else
error_msg = "Image not present, aborting."
result = OpenNebula::Error.new(error_msg)
end
# ------ Enable the Image ------
if !OpenNebula.is_error?(result)
image.enable
else
image.delete
end
return result
end
def delete(image)
if image.nil?
error_msg = "Image could not be found, aborting."
result = OpenNebula::Error.new(error_msg)
end
result = image.info
if !OpenNebula.is_error?(result)
file_path = image['SOURCE']
result = image.delete
if !OpenNebula.is_error?(result)
result = remove(file_path)
end
end
return result
end
def update_source(image, source)
if image.nil?
error_msg = "Image could not be found, aborting."
result = OpenNebula::Error.new(error_msg)
end
result = image.info
if !OpenNebula.is_error?(result)
result = move(source, image['SOURCE'])
image.enable
end
return result
end
private
FS_UTILS = {
:dd => "env dd",
:mkfs => "env mkfs"
}
def copy(path, source)
if source.nil? or path.nil?
return OpenNebula::Error.new("copy Image: missing parameters.")
end
begin
FileUtils.copy(path, source)
FileUtils.chmod(0660, source)
rescue Exception => e
return OpenNebula::Error.new(e.message)
end
return nil
end
def move(path, source)
if source.nil? or path.nil?
return OpenNebula::Error.new("copy Image: missing parameters.")
end
begin
FileUtils.move(path, source)
FileUtils.chmod(0660, source)
rescue Exception => e
return OpenNebula::Error.new(e.message)
end
return nil
end
def dd(size, source)
if source.nil? or size.nil?
return OpenNebula::Error.new("dd Image: missing parameters.")
end
command = ""
command << FS_UTILS[:dd]
command << " if=/dev/zero of=#{source} ibs=1 count=1"
command << " obs=1048576 seek=#{size}"
local_command=LocalCommand.run(command)
if local_command.code!=0
return OpenNebula::Error.new("dd Image: in dd command.")
end
return nil
end
def mkfs(fstype, source)
if source.nil? or fstype.nil?
return OpenNebula::Error.new("mkfs Image: missing parameters.")
end
command = ""
command << FS_UTILS[:mkfs]
command << " -t #{fstype} -F #{source}"
local_command=LocalCommand.run(command)
if local_command.code!=0
return OpenNebula::Error.new("mkfs Image: in mkfs command.")
end
return nil
end
def remove(source)
if File.exists?(source)
begin
FileUtils.rm(source)
rescue Exception => e
return OpenNebula::Error.new(e.message)
end
end
return nil
end
end
end

View File

@ -904,6 +904,7 @@ error_common:
void VirtualMachine::release_disk_images()
{
string iid;
string saveas;
int num_disks;
vector<Attribute const * > disks;
@ -939,11 +940,17 @@ void VirtualMachine::release_disk_images()
continue;
}
if (img->release_image() == true)
img->release_image();
saveas = disk->vector_value("SAVE_AS");
if ( !saveas.empty() && saveas == iid )
{
ipool->update(img);
img->enable(false);
}
ipool->update(img);
img->unlock();
}
}

View File

@ -25,7 +25,8 @@
/* -------------------------------------------------------------------------- */
VirtualMachinePool::VirtualMachinePool(SqlDB * db,
vector<const Attribute *> hook_mads)
vector<const Attribute *> hook_mads,
const string& hook_location)
: PoolSQL(db,VirtualMachine::table)
{
const VectorAttribute * vattr;
@ -79,6 +80,11 @@ VirtualMachinePool::VirtualMachinePool(SqlDB * db,
}
}
if (cmd[0] != '/')
{
cmd = hook_location + cmd;
}
if ( on == "CREATE" )
{
VirtualMachineAllocateHook * hook;

View File

@ -87,7 +87,7 @@ class VirtualMachinePoolFriend : public VirtualMachinePool
{
public:
VirtualMachinePoolFriend(SqlDB * db, vector<const Attribute *> hook_mads):
VirtualMachinePool(db, hook_mads)
VirtualMachinePool(db, hook_mads, "./")
{};