diff --git a/src/cli/onevm b/src/cli/onevm index 263ce30525..cd1fdb6a42 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -342,7 +342,15 @@ Commands: * resume (Resumes the execution of a saved VM) onevm resume - + +* saveas (Set the specified vm's disk to be saved in a new image (image_name) + when the vm shutdowns) + onevm saveas + +* save (Set the specified vm's disk to be saved, overwriting the original image + when the vm shutdowns) + onevm saveas + * delete (Deletes a VM from the pool and DB) onevm delete @@ -670,7 +678,81 @@ when "delete" break end end + +when "saveas" + check_parameters("saveas", 3) + vm_id = get_vm_id(ARGV[0]) + disk_id = ARGV[1] + image_name = ARGV[2] + # Get the Image ID for this disk + vm = OpenNebula::VirtualMachine.new( + OpenNebula::VirtualMachine.build_xml(vm_id), + get_one_client) + + result = vm.info + if !is_successful?(result) + puts result.message + exit -1 + end + + image_id = vm["TEMPLATE/DISK[DISK_ID=\"#{disk_id}\"]/IMAGE_ID"] + + # Get the image type + image = OpenNebula::Image.new( + OpenNebula::Image.build_xml(image_id), + get_one_client) + + result = image.info + if !is_successful?(result) + puts result.message + exit -1 + end + + if ops[:type] + image_type = ops[:type] + else + image_type = image.type_str + end + + # Build the template and allocate the new Image + template = "NAME=#{image_name}\n" + template << "TYPE=#{image_type}\n" if type + + + 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 + + result = vm.save_disk(disk_id.to_i, image.id) + +when "save" + check_parameters("save", 2) + vm_id = get_vm_id(ARGV[0]) + disk_id = ARGV[1] + + # Get the Image ID for this disk + vm = OpenNebula::VirtualMachine.new( + OpenNebula::VirtualMachine.build_xml(vm_id), + get_one_client) + + result = vm.info + if !is_successful?(result) + puts result.message + exit -1 + end + + image_id = vm["TEMPLATE/DISK[DISK_ID=\"#{disk_id}\"]/IMAGE_ID"] + + result = vm.save_disk(disk_id.to_i, image_id) + when "show" check_parameters("get_info", 1) args=expand_args(ARGV) diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index 4440117f8a..1378cb50a7 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -10,7 +10,8 @@ module OpenNebula :allocate => "vm.allocate", :action => "vm.action", :migrate => "vm.migrate", - :deploy => "vm.deploy" + :deploy => "vm.deploy", + :savedisk => "vm.savedisk" } VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED} @@ -167,6 +168,15 @@ module OpenNebula return rc end + + def save_as(disk_id, image_id) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(VM_METHODS[:savedisk], @pe_id, disk_id, image_id) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end ####################################################################### # Helpers to get VirtualMachine information