From 146ca09e08040b789dc753013e61b35fdf58e890 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 1 Mar 2013 19:04:56 +0100 Subject: [PATCH] feature #1791: new saveas hot functionality in the core --- include/Image.h | 12 +++ include/ImageTemplate.h | 20 ++++ include/LifeCycleManager.h | 6 ++ include/RequestManagerVirtualMachine.h | 2 +- include/TransferManager.h | 7 ++ include/VirtualMachine.h | 15 ++- src/image/ImageManagerDriver.cc | 53 ++++++++-- src/image/ImageTemplate.cc | 3 +- src/lcm/LifeCycleManager.cc | 16 +++ src/lcm/LifeCycleStates.cc | 36 +++++++ src/rm/RequestManagerVirtualMachine.cc | 11 +++ src/tm/TransferManager.cc | 131 +++++++++++++++++++++++++ src/tm/TransferManagerDriver.cc | 8 ++ src/vm/VirtualMachine.cc | 61 ++++++++++++ 14 files changed, 370 insertions(+), 11 deletions(-) diff --git a/include/Image.h b/include/Image.h index 08949f6c72..29284b9d3b 100644 --- a/include/Image.h +++ b/include/Image.h @@ -348,6 +348,18 @@ public: return it->is_saving(); } + /** + * Check if the image is a hot snapshot + * @return true if image is a hot snapshot + */ + bool isHot() + { + ImageTemplate * it = static_cast(obj_template); + + return it->is_hot(); + } + + /** * Set permissions for the Image. Extends the PoolSQLObject method * by checking the persistent state of the image. diff --git a/include/ImageTemplate.h b/include/ImageTemplate.h index 7f7b0b2a43..6cddd2eda7 100644 --- a/include/ImageTemplate.h +++ b/include/ImageTemplate.h @@ -52,6 +52,15 @@ public: return (saving.empty() == false); } + bool is_hot() + { + string snapshot_hot; + + get(snapshot_hot_attribute, snapshot_hot); + + return (snapshot_hot.empty() == false); + } + void set_saving() { SingleAttribute * attr= new SingleAttribute(saving_attribute, "YES"); @@ -61,6 +70,16 @@ public: set(attr); } + void set_snapshot_hot() + { + SingleAttribute * attr = new SingleAttribute( + snapshot_hot_attribute, "YES"); + + erase(snapshot_hot_attribute); + + set(attr); + } + void unset_saving() { erase(saving_attribute); @@ -72,6 +91,7 @@ private: static vector restricted_attributes; static string saving_attribute; + static string snapshot_hot_attribute; /** * Stores the attributes as restricted, these attributes will be used in diff --git a/include/LifeCycleManager.h b/include/LifeCycleManager.h index 34d7eece7c..5702178dd6 100644 --- a/include/LifeCycleManager.h +++ b/include/LifeCycleManager.h @@ -64,6 +64,8 @@ public: DETACH_FAILURE, /**< Sent by the VMM when a detach action fails */ CLEANUP_SUCCESS, /**< Sent by the VMM when a cleanup action succeeds */ CLEANUP_FAILURE, /**< Sent by the VMM when a cleanup action fails */ + HOTPLUG_SAVEAS_SUCCESS, /**< Sent by the VMM when hot saveas succeeds */ + HOTPLUG_SAVEAS_FAILURE, /**< Sent by the VMM when hot saveas fails */ DEPLOY, /**< Sent by the DM to deploy a VM on a host */ SUSPEND, /**< Sent by the DM to suspend an running VM */ RESTORE, /**< Sent by the DM to restore a suspended VM */ @@ -187,6 +189,10 @@ private: void detach_failure_action(int vid); + void hotplug_saveas_success_action(int vid); + + void hotplug_saveas_failure_action(int vid); + void cleanup_callback_action(int vid); void deploy_action(int vid); diff --git a/include/RequestManagerVirtualMachine.h b/include/RequestManagerVirtualMachine.h index b4bf5a9852..63f22fe44a 100644 --- a/include/RequestManagerVirtualMachine.h +++ b/include/RequestManagerVirtualMachine.h @@ -152,7 +152,7 @@ public: VirtualMachineSaveDisk(): RequestManagerVirtualMachine("VirtualMachineSaveDisk", "Saves a disk from virtual machine as a new image", - "A:siiss"){}; + "A:siissb"){}; ~VirtualMachineSaveDisk(){}; diff --git a/include/TransferManager.h b/include/TransferManager.h index a31a8484bb..085eeeedbe 100644 --- a/include/TransferManager.h +++ b/include/TransferManager.h @@ -153,6 +153,13 @@ public: ostream& xfr, bool local, bool previous); + + + /** + * This function starts the hot saveas of an image + */ + void saveas_hot_transfer_command(int vid, int disk_id, string& save_source); + private: /** * Thread id for the Transfer Manager diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index be43d917ca..4bb9c61d44 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -89,7 +89,8 @@ public: BOOT_POWEROFF = 20, BOOT_SUSPENDED = 21, BOOT_STOPPED = 22, - CLEANUP_DELETE = 23 + CLEANUP_DELETE = 23, + HOTPLUG_SAVEAS = 24 }; // ------------------------------------------------------------------------- @@ -953,6 +954,18 @@ public: */ int detach_failure(); + /** + * Sets the hotplug_saveas attribute to the given disk + * @param disk_id of the DISK + * @return 0 if the disk_id was found -1 otherwise + */ + int set_hotplug_saveas(int disk_id); + + /** + * Cleans the HOTPLUG_SAVEAS = YES attribute from the disks + */ + void clear_hotplug_saveas(); + private: // ------------------------------------------------------------------------- diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index a252e219ae..64196c7a9e 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -319,6 +319,7 @@ static void mkfs_action(istringstream& is, string source; Image * image; bool is_saving; + bool is_hot; string disk_id; string vm_id; @@ -373,6 +374,8 @@ static void mkfs_action(istringstream& is, is_saving = image->isSaving(); + is_hot = image->isHot(); + image->set_source(source); if (is_saving) @@ -396,8 +399,6 @@ static void mkfs_action(istringstream& is, return; } - /* ---------------- Set up information for the Saved Image -------------- */ - vm = vmpool->get(vm_id, true); if ( vm == 0 ) @@ -405,14 +406,50 @@ static void mkfs_action(istringstream& is, goto error_save_get; } - rc = vm->save_disk(disk_id, source, id); - - if ( rc == -1 ) + if ( is_hot ) { - goto error_save_state; - } + istringstream iss; + int vm_oid; + int disk_oid; - vmpool->update(vm); + /* change state */ + + iss.str(vm_id); + iss >> vm_oid; + + iss.clear(); + + iss.str(disk_id); + iss >> disk_oid; + + vm->set_state(VirtualMachine::HOTPLUG_SAVEAS); + vm->set_hotplug_saveas(disk_oid); + + vm->set_resched(false); + + vmpool->update(vm); + + vm->log("LCM", Log::INFO, "New VM state is HOTPLUG_SAVEAS"); + + /* call tm */ + + TransferManager * tm = nd.get_tm(); + + vm->unlock(); + tm->saveas_hot_transfer_command(vm_oid, disk_oid, source); + } + else + { + /* -------------- Set up information for the Saved Image ------------ */ + rc = vm->save_disk(disk_id, source, id); + + if ( rc == -1 ) + { + goto error_save_state; + } + + vmpool->update(vm); + } vm->unlock(); diff --git a/src/image/ImageTemplate.cc b/src/image/ImageTemplate.cc index 802569bfc0..543491a7ec 100644 --- a/src/image/ImageTemplate.cc +++ b/src/image/ImageTemplate.cc @@ -21,7 +21,8 @@ vector ImageTemplate::restricted_attributes; -string ImageTemplate::saving_attribute = "SAVE_AS"; +string ImageTemplate::saving_attribute = "SAVE_AS"; +string ImageTemplate::snapshot_hot_attribute = "SNAPSHOT_HOT"; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/lcm/LifeCycleManager.cc b/src/lcm/LifeCycleManager.cc index 326da9c36f..7ae2a9ea28 100644 --- a/src/lcm/LifeCycleManager.cc +++ b/src/lcm/LifeCycleManager.cc @@ -145,6 +145,14 @@ void LifeCycleManager::trigger(Actions action, int _vid) aname = "DETACH_FAILURE"; break; + case HOTPLUG_SAVEAS_SUCCESS: + aname = "HOTPLUG_SAVEAS_SUCCESS"; + break; + + case HOTPLUG_SAVEAS_FAILURE: + aname = "HOTPLUG_SAVEAS_FAILURE"; + break; + case CLEANUP_SUCCESS: aname = "CLEANUP_SUCCESS"; break; @@ -306,6 +314,14 @@ void LifeCycleManager::do_action(const string &action, void * arg) { detach_failure_action(vid); } + else if (action == "HOTPLUG_SAVEAS_SUCCESS") + { + hotplug_saveas_success_action(vid); + } + else if (action == "HOTPLUG_SAVEAS_FAILURE") + { + hotplug_saveas_failure_action(vid); + } else if (action == "CLEANUP_SUCCESS") { cleanup_callback_action(vid); diff --git a/src/lcm/LifeCycleStates.cc b/src/lcm/LifeCycleStates.cc index 8c17bcd206..5e19917dcb 100644 --- a/src/lcm/LifeCycleStates.cc +++ b/src/lcm/LifeCycleStates.cc @@ -1197,3 +1197,39 @@ void LifeCycleManager::detach_failure_action(int vid) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +void LifeCycleManager::hotplug_saveas_success_action(int vid) +{ + VirtualMachine * vm; + + vm = vmpool->get(vid,true); + + if ( vm == 0 ) + { + return; + } + + if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SAVEAS) + { + vm->clear_attach_disk(); + + vm->set_state(VirtualMachine::RUNNING); + + vmpool->update(vm); + } + else + { + vm->log("LCM",Log::ERROR,"hotplug_saveas_success_action," + " VM in a wrong state"); + } + + vm->unlock(); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void LifeCycleManager::hotplug_saveas_failure_action(int vid) +{ + hotplug_saveas_success_action(vid); +} diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 51bf57b1b7..180a9e5395 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -690,6 +690,12 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis int disk_id = xmlrpc_c::value_int(paramList.getInt(2)); string img_name = xmlrpc_c::value_string(paramList.getString(3)); string img_type = xmlrpc_c::value_string(paramList.getString(4)); + bool is_hot = false; //Optional XML-RPC argument + + if ( paramList.size() > 5 ) + { + is_hot = xmlrpc_c::value_boolean(paramList.getBoolean(5)); + } VirtualMachine * vm; int iid; @@ -829,6 +835,11 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis itemplate->add("SAVED_DISK_ID",disk_id); itemplate->add("SAVED_VM_ID", id); + if ( is_hot ) + { + itemplate->set_snapshot_hot(); + } + if ( img_type.empty() ) { itemplate->add("TYPE", Image::type_to_str(type)); diff --git a/src/tm/TransferManager.cc b/src/tm/TransferManager.cc index 73393a8456..63ddd30039 100644 --- a/src/tm/TransferManager.cc +++ b/src/tm/TransferManager.cc @@ -1625,6 +1625,137 @@ void TransferManager::checkpoint_action(int vid) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void TransferManager::saveas_hot_transfer_command(int vid, + int disk_id, + string& save_source) +{ + string save; + string tm_mad; + string ds_id; + + int num; + int disk_id_iter; + + ostringstream os; + + ofstream xfr; + string xfr_name; + + string source; + + const VectorAttribute * disk; + vector attrs; + + VirtualMachine * vm; + Nebula& nd = Nebula::instance(); + + const TransferManagerDriver * tm_md; + + // ------------------------------------------------------------------------ + // Setup & Transfer script + // ------------------------------------------------------------------------ + vm = vmpool->get(vid,true); + + if (vm == 0) + { + vm->log("TM", Log::ERROR, "Could not obtain the VM");; + goto error_common; + } + + if (!vm->hasHistory()) + { + vm->log("TM", Log::ERROR, "The VM has no history");; + goto error_common; + } + + tm_md = get(); + + if (tm_md == 0) + { + goto error_driver; + } + + num = vm->get_template_attribute("DISK",attrs); + + for (int i=0 ; i < num ; i++) + { + disk = dynamic_cast(attrs[i]); + + if ( disk == 0 ) + { + continue; + } + + disk->vector_value("DISK_ID", disk_id_iter); + + if (disk_id == disk_id_iter) + { + tm_mad = disk->vector_value("TM_MAD"); + ds_id = disk->vector_value("DATASTORE_ID"); + + break; + } + } + + if ( ds_id.empty() || tm_mad.empty() ) + { + vm->log("TM", Log::ERROR, "No DS_ID or TM_MAD to save disk image"); + goto error_common; + } + + if (save_source.empty()) + { + vm->log("TM", Log::ERROR, "No SOURCE to save disk image"); + goto error_common; + } + + xfr_name = vm->get_transfer_file() + ".saveas_hot"; + xfr.open(xfr_name.c_str(),ios::out | ios::trunc); + + if (xfr.fail() == true) + { + goto error_file; + } + + //MVDS tm_mad hostname:remote_system_dir/disk.0 vmid dsid + xfr << "CPDS " + << tm_mad << " " + << vm->get_hostname() << ":" + << vm->get_remote_system_dir() << "/disk." << disk_id << " " + << save_source << " " + << vm->get_oid() << " " + << ds_id + << endl; + + xfr.close(); + + tm_md->transfer(vid, xfr_name); + + vm->unlock(); + + return; + +error_driver: + os << "saveas_hot_transfer, error getting TM driver."; + goto error_common; + +error_file: + os << "saveas_hot_transfer, could not open file: " << xfr_name; + os << ". You may need to manually clean hosts (previous & current)"; + goto error_common; + +error_common: + vm->log("TM", Log::ERROR, os); + + (nd.get_lcm())->trigger(LifeCycleManager::HOTPLUG_SAVEAS_FAILURE, vid); + + vm->unlock(); + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void TransferManager::migrate_transfer_command( VirtualMachine * vm, ostream& xfr) diff --git a/src/tm/TransferManagerDriver.cc b/src/tm/TransferManagerDriver.cc index 7f5e85cc79..416d44091f 100644 --- a/src/tm/TransferManagerDriver.cc +++ b/src/tm/TransferManagerDriver.cc @@ -131,6 +131,10 @@ void TransferManagerDriver::protocol( lcm_action = LifeCycleManager::EPILOG_SUCCESS; break; + case VirtualMachine::HOTPLUG_SAVEAS: + lcm_action = LifeCycleManager::HOTPLUG_SAVEAS_SUCCESS; + break; + default: goto error_state; } @@ -168,6 +172,10 @@ void TransferManagerDriver::protocol( lcm_action = LifeCycleManager::EPILOG_FAILURE; break; + case VirtualMachine::HOTPLUG_SAVEAS: + lcm_action = LifeCycleManager::HOTPLUG_SAVEAS_FAILURE; + break; + default: goto error_state; } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 7ea7d9576c..94bfec379d 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -1854,6 +1854,67 @@ VectorAttribute * VirtualMachine::delete_attach_disk() return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachine::set_hotplug_saveas(int disk_id) +{ + + int num_disks; + int d_id; + + vector disks; + VectorAttribute * disk; + + num_disks = obj_template->get("DISK", disks); + + for(int i=0; i(disks[i]); + + if ( disk == 0 ) + { + continue; + } + + disk->vector_value("DISK_ID", d_id); + + if ( d_id == disk_id ) + { + disk->replace("HOTPLUG_SAVEAS", "YES"); + return 0; + } + } + + return -1; +} +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void VirtualMachine::clear_hotplug_saveas() +{ + int num_disks; + vector disks; + VectorAttribute * disk; + + num_disks = obj_template->get("DISK", disks); + + for(int i=0; i(disks[i]); + + if ( disk == 0 ) + { + continue; + } + + if ( disk->vector_value("HOTPLUG_SAVEAS") == "YES" ) + { + disk->remove("HOTPLUG_SAVEAS"); + return; + } + } +} /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */