From c8f44ede9aab94b28abd733654fb3592cc112409 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 31 Aug 2011 16:21:18 +0200 Subject: [PATCH 1/3] feature #788: Compute image size when moving an image to the repository. Drivers now return SUCESS IMAGE_ID SOURCE SIZE. The fs based image repository drivers has been updates, oneimage utility has been also updated to show the size (list and show operations). --- include/Image.h | 26 ++++++++--- src/cli/etc/oneimage.yaml | 7 ++- src/cli/one_helper/oneimage_helper.rb | 16 ++++--- src/image/Image.cc | 3 ++ src/image/ImageManagerDriver.cc | 65 ++++++++++++--------------- src/image_mad/remotes/fs/cp | 5 ++- src/image_mad/remotes/fs/fsrc | 13 ++++++ src/image_mad/remotes/fs/mkfs | 5 ++- src/image_mad/remotes/fs/mv | 5 ++- src/mad/sh/scripts_common.sh | 1 + 10 files changed, 94 insertions(+), 52 deletions(-) diff --git a/include/Image.h b/include/Image.h index 9804de2a57..5f5defe5ea 100644 --- a/include/Image.h +++ b/include/Image.h @@ -90,14 +90,21 @@ public: } /** - * Returns the source path of the image - * @return source of image + * Sets the source path of the image */ void set_source(const string& _source) { source = _source; } + /** + * Sets the size for the image + */ + void set_size(unsigned int _size_mb) + { + size_mb = _size_mb; + } + /** * Returns the type of the image * @return type @@ -285,27 +292,32 @@ private: /** * Type of the Image */ - ImageType type; + ImageType type; /** * Persistency of the Image */ - int persistent_img; + int persistent_img; /** * Registration time */ - time_t regtime; + time_t regtime; /** * Path to the image */ - string source; + string source; + + /** + * Size of the image in MB + */ + unsigned int size_mb; /** * Image state */ - ImageState state; + ImageState state; /** * Number of VMs using the image diff --git a/src/cli/etc/oneimage.yaml b/src/cli/etc/oneimage.yaml index c632dd5669..baf79e95ea 100644 --- a/src/cli/etc/oneimage.yaml +++ b/src/cli/etc/oneimage.yaml @@ -5,7 +5,7 @@ :NAME: :desc: Name of the Image - :size: 16 + :size: 12 :left: true :USER: @@ -18,6 +18,10 @@ :size: 8 :left: true +:SIZE: + :desc: Size of the Image + :size: 7 + :TYPE: :desc: Type of the Image :size: 4 @@ -47,6 +51,7 @@ - :USER - :GROUP - :NAME +- :SIZE - :TYPE - :REGTIME - :PUBLIC diff --git a/src/cli/one_helper/oneimage_helper.rb b/src/cli/one_helper/oneimage_helper.rb index c79b67f72b..2af923b7f0 100644 --- a/src/cli/one_helper/oneimage_helper.rb +++ b/src/cli/one_helper/oneimage_helper.rb @@ -57,10 +57,10 @@ class OneImageHelper < OpenNebulaHelper::OneHelper str_h1="%-80s" CLIHelper.print_header(str_h1 % "IMAGE #{image['ID']} INFORMATION") - puts str % ["ID", image.id.to_s] + puts str % ["ID", image.id.to_s] puts str % ["NAME", image.name] puts str % ["USER", image['UNAME']] - puts str % ["GROUP", image['GNAME']] + puts str % ["GROUP",image['GNAME']] puts str % ["TYPE", image.type_str] puts str % ["REGISTER TIME", OpenNebulaHelper.time_to_str(image['REGTIME'])] @@ -68,7 +68,8 @@ class OneImageHelper < OpenNebulaHelper::OneHelper OpenNebulaHelper.boolean_to_str(image['PUBLIC'])] puts str % ["PERSISTENT", OpenNebulaHelper.boolean_to_str(image["PERSISTENT"])] - puts str % ["SOURCE", image['SOURCE']] + puts str % ["SOURCE",image['SOURCE']] + puts str % ["SIZE", image['SIZE']] puts str % ["STATE", image.short_state_str] puts str % ["RUNNING_VMS", image['RUNNING_VMS']] puts @@ -85,7 +86,7 @@ class OneImageHelper < OpenNebulaHelper::OneHelper d["ID"] end - column :NAME, "Name of the Image", :left, :size=>15 do |d| + column :NAME, "Name of the Image", :left, :size=>12 do |d| d["NAME"] end @@ -127,7 +128,12 @@ class OneImageHelper < OpenNebulaHelper::OneHelper d['RUNNING_VMS'] end - default :ID, :USER, :GROUP, :NAME, :TYPE, :REGTIME, :PUBLIC, + column :SIZE, "Size of the image", + :size=>7 do |d| + OpenNebulaHelper.unit_to_str(d['SIZE'].to_i,options) + end + + default :ID, :USER, :GROUP, :NAME, :SIZE, :TYPE, :REGTIME, :PUBLIC, :PERSISTENT , :STAT, :RVMS end diff --git a/src/image/Image.cc b/src/image/Image.cc index b02a3f7d8a..a66af4935c 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -43,6 +43,7 @@ Image::Image(int _uid, type(OS), regtime(time(0)), source("-"), + size_mb(0), state(INIT), running_vms(0) { @@ -334,6 +335,7 @@ string& Image::to_xml(string& xml) const "" << persistent_img << "" << "" << regtime << "" << "" << source << "" << + "" << size_mb << "" << "" << state << "" << "" << running_vms << "" << obj_template->to_xml(template_xml) << @@ -374,6 +376,7 @@ int Image::from_xml(const string& xml) rc += xpath(regtime, "/IMAGE/REGTIME", 0); rc += xpath(source, "/IMAGE/SOURCE", "not_found"); + rc += xpath(size_mb, "/IMAGE/SIZE", 0); rc += xpath(int_state, "/IMAGE/STATE", 0); rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1); diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index b1723b2e57..a7663799e1 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -91,6 +91,7 @@ void ImageManagerDriver::protocol( int id; Image * image; string source; + unsigned int size_mb; string info; @@ -130,6 +131,27 @@ void ImageManagerDriver::protocol( else return; + // Parse driver message for CP, MV and MKFS + // SUCESS IMAGE_ID SOURCE SIZE + if ( (result == "SUCCESS") && (action != "RM") ) + { + if ( is.good() ) + { + is >> source >> ws; + } + + if ( is.good() ) + { + is >> size_mb >> ws; + } + + if ( is.fail() ) + { + result = "FAILURE"; + } + } + + // Get the image from the pool image = ipool->get(id,true); @@ -143,19 +165,9 @@ 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_size(size_mb); + image->set_state(Image::READY); ipool->update(image); @@ -173,22 +185,13 @@ void ImageManagerDriver::protocol( { if (image->get_source() == "-") { - string source; - - if ( is.good() ) - { - is >> source >> ws; - } - - if ( is.fail() ) - { - goto error_mv; - } - image->set_source(source); } + image->set_size(size_mb); + image->set_state(Image::READY); + ipool->update(image); NebulaLog::log("ImM", Log::INFO, "Image saved and ready to use."); @@ -202,19 +205,9 @@ 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_size(size_mb); + image->set_state(Image::READY); ipool->update(image); diff --git a/src/image_mad/remotes/fs/cp b/src/image_mad/remotes/fs/cp index 24da5fbaff..86276ec30a 100755 --- a/src/image_mad/remotes/fs/cp +++ b/src/image_mad/remotes/fs/cp @@ -52,6 +52,9 @@ http://*) ;; esac +# ---------------- Get the size of the image & fix perms ------------ exec_and_log "chmod 0660 $DST" -echo "$DST" +SIZE=`fs_du $DST` + +echo "$DST $SIZE" diff --git a/src/image_mad/remotes/fs/fsrc b/src/image_mad/remotes/fs/fsrc index 57e87cdfe0..423f27e26b 100644 --- a/src/image_mad/remotes/fs/fsrc +++ b/src/image_mad/remotes/fs/fsrc @@ -39,3 +39,16 @@ EOF echo "$IMAGE_REPOSITORY_PATH/`echo $CANONICAL_MD5 | cut -d ' ' -f1`" } + +function fs_du { + +SIZE=`$DU -m $1 2>/dev/null` + +if [ $? -ne 0 ]; then + SIZE=0 +else + SIZE=`echo $SIZE | cut -f1 -d' '` +fi + +echo "$SIZE" +} diff --git a/src/image_mad/remotes/fs/mkfs b/src/image_mad/remotes/fs/mkfs index 4747d12084..d4e8c61157 100755 --- a/src/image_mad/remotes/fs/mkfs +++ b/src/image_mad/remotes/fs/mkfs @@ -65,4 +65,7 @@ exec_and_log "$MKFS -t $FSTYPE $OPTS $DST" \ exec_and_log "chmod 0660 $DST" -echo "$DST" +# ---------------- Get the size of the image ------------ +SIZE=`fs_du $DST` + +echo "$DST $SIZE" diff --git a/src/image_mad/remotes/fs/mv b/src/image_mad/remotes/fs/mv index 4b9a4fed4a..121bdf9e11 100755 --- a/src/image_mad/remotes/fs/mv +++ b/src/image_mad/remotes/fs/mv @@ -64,4 +64,7 @@ esac exec_and_log "chmod 0660 $DST" -echo "$DST" +# ---------------- Get the size of the image ------------ +SIZE=`fs_du $DST` + +echo "$DST $SIZE" diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 6cfaec0477..221c997959 100755 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -20,6 +20,7 @@ BASH=/bin/bash CUT=cut DATE=/bin/date DD=/bin/dd +DU=/bin/du LVCREATE=/sbin/lvcreate LVREMOVE=/sbin/lvremove LVS=/sbin/lvs From d488cffddcfdfac5c05d7668047c2d75756ebe37 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 31 Aug 2011 16:29:43 +0200 Subject: [PATCH 2/3] feature #788: Optional base unit for unit_to_str method --- src/cli/one_helper.rb | 4 ++-- src/cli/one_helper/oneimage_helper.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 0faa621144..d908eb100e 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -363,11 +363,11 @@ EOT BinarySufix = ["K", "M", "G", "T" ] - def OpenNebulaHelper.unit_to_str(value, options) + def OpenNebulaHelper.unit_to_str(value, options, unit="K") if options[:kilobytes] value else - i=0 + i=BinarySufix.index(unit).to_i while value > 1024 && i < 3 do value /= 1024.0 diff --git a/src/cli/one_helper/oneimage_helper.rb b/src/cli/one_helper/oneimage_helper.rb index 2af923b7f0..cf3a00059f 100644 --- a/src/cli/one_helper/oneimage_helper.rb +++ b/src/cli/one_helper/oneimage_helper.rb @@ -130,7 +130,7 @@ class OneImageHelper < OpenNebulaHelper::OneHelper column :SIZE, "Size of the image", :size=>7 do |d| - OpenNebulaHelper.unit_to_str(d['SIZE'].to_i,options) + OpenNebulaHelper.unit_to_str(d['SIZE'].to_i,options,"M") end default :ID, :USER, :GROUP, :NAME, :SIZE, :TYPE, :REGTIME, :PUBLIC, From d9792bb1f58eefe44d01b6ca3ade5249a5e234f3 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 31 Aug 2011 17:00:19 +0200 Subject: [PATCH 3/3] feature #720: Fix ruby 1.9 problem in watch_client --- src/acct/watch_client.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index 6f65d07b48..1b1feb6a5c 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -98,14 +98,14 @@ module OneWatchClient max_per_vm = rsql. group(:id, :last_poll). - select(:last_poll, :MAX[mr.to_sym].as(:max_mr)) + select{[:last_poll, max(mr.to_sym).as(:max_mr)]} # SUM the monitoring resource for each last_poll value last_poll_and_sum = max_per_vm. from_self. group(:last_poll). - select(:last_poll, :SUM[:max_mr].as(:sum_mr)) + select{[:last_poll, sum(:max_mr).as(:sum_mr)]} # Retrieve the information in an Array a = Array.new @@ -113,7 +113,7 @@ module OneWatchClient if row[:last_poll] && row[:last_poll] != 0 a << [row[:last_poll], row[:sum_mr].to_i] end - end + end a end @@ -229,4 +229,4 @@ module OneWatchClient end end end -end \ No newline at end of file +end