/* -------------------------------------------------------------------------- */ /* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */ /* not use this file except in compliance with the License. You may obtain */ /* a copy of the License at */ /* */ /* http://www.apache.org/licenses/LICENSE-2.0 */ /* */ /* Unless required by applicable law or agreed to in writing, software */ /* distributed under the License is distributed on an "AS IS" BASIS, */ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ /* -------------------------------------------------------------------------- */ #ifndef VIRTUAL_MACHINE_H_ #define VIRTUAL_MACHINE_H_ #include "VirtualMachineTemplate.h" #include "VirtualMachineDisk.h" #include "VirtualMachineNic.h" #include "VirtualMachineMonitorInfo.h" #include "PoolSQL.h" #include "History.h" #include "Image.h" #include "Log.h" #include "NebulaLog.h" #include "NebulaUtil.h" #include "Quotas.h" #include #include #include using namespace std; class AuthRequest; class Snapshots; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /** * The Virtual Machine class. It represents a VM... */ class VirtualMachine : public PoolObjectSQL { public: // ------------------------------------------------------------------------- // VM States // ------------------------------------------------------------------------- /** * Global Virtual Machine state */ enum VmState { INIT = 0, PENDING = 1, HOLD = 2, ACTIVE = 3, STOPPED = 4, SUSPENDED = 5, DONE = 6, //FAILED = 7, POWEROFF = 8, UNDEPLOYED = 9, CLONING = 10, CLONING_FAILURE = 11 }; /** * Virtual Machine state associated to the Life-cycle Manager */ enum LcmState { LCM_INIT = 0, PROLOG = 1, BOOT = 2, RUNNING = 3, MIGRATE = 4, SAVE_STOP = 5, SAVE_SUSPEND = 6, SAVE_MIGRATE = 7, PROLOG_MIGRATE = 8, PROLOG_RESUME = 9, EPILOG_STOP = 10, EPILOG = 11, SHUTDOWN = 12, //CANCEL = 13, //FAILURE = 14, CLEANUP_RESUBMIT = 15, UNKNOWN = 16, HOTPLUG = 17, SHUTDOWN_POWEROFF = 18, BOOT_UNKNOWN = 19, BOOT_POWEROFF = 20, BOOT_SUSPENDED = 21, BOOT_STOPPED = 22, CLEANUP_DELETE = 23, HOTPLUG_SNAPSHOT = 24, HOTPLUG_NIC = 25, HOTPLUG_SAVEAS = 26, HOTPLUG_SAVEAS_POWEROFF = 27, HOTPLUG_SAVEAS_SUSPENDED = 28, SHUTDOWN_UNDEPLOY = 29, EPILOG_UNDEPLOY = 30, PROLOG_UNDEPLOY = 31, BOOT_UNDEPLOY = 32, HOTPLUG_PROLOG_POWEROFF = 33, HOTPLUG_EPILOG_POWEROFF = 34, BOOT_MIGRATE = 35, BOOT_FAILURE = 36, BOOT_MIGRATE_FAILURE = 37, PROLOG_MIGRATE_FAILURE = 38, PROLOG_FAILURE = 39, EPILOG_FAILURE = 40, EPILOG_STOP_FAILURE = 41, EPILOG_UNDEPLOY_FAILURE = 42, PROLOG_MIGRATE_POWEROFF = 43, PROLOG_MIGRATE_POWEROFF_FAILURE = 44, PROLOG_MIGRATE_SUSPEND = 45, PROLOG_MIGRATE_SUSPEND_FAILURE = 46, BOOT_UNDEPLOY_FAILURE = 47, BOOT_STOPPED_FAILURE = 48, PROLOG_RESUME_FAILURE = 49, PROLOG_UNDEPLOY_FAILURE = 50, DISK_SNAPSHOT_POWEROFF = 51, DISK_SNAPSHOT_REVERT_POWEROFF = 52, DISK_SNAPSHOT_DELETE_POWEROFF = 53, DISK_SNAPSHOT_SUSPENDED = 54, DISK_SNAPSHOT_REVERT_SUSPENDED = 55, DISK_SNAPSHOT_DELETE_SUSPENDED = 56, DISK_SNAPSHOT = 57, //DISK_SNAPSHOT_REVERT = 58, DISK_SNAPSHOT_DELETE = 59, PROLOG_MIGRATE_UNKNOWN = 60, PROLOG_MIGRATE_UNKNOWN_FAILURE = 61, DISK_RESIZE = 62, DISK_RESIZE_POWEROFF = 63, DISK_RESIZE_UNDEPLOYED = 64 }; /** * Functions to convert to/from string the VM states */ static int vm_state_from_str(string& st, VmState& state); static string& vm_state_to_str(string& st, VmState state); static int lcm_state_from_str(string& st, LcmState& state); static string& lcm_state_to_str(string& st, LcmState state); /** * Returns the VM state to string, using the lcm state if the current state * is ACTIVE. * @return the state sting */ string state_str(); /** * Returns the VM state (Dispatch Manager) * @return the VM state */ VmState get_state() const { return state; }; VmState get_prev_state() const { return prev_state; }; /** * Returns the VM state (life-cycle Manager) * @return the VM state */ LcmState get_lcm_state() const { return lcm_state; }; LcmState get_prev_lcm_state() const { return prev_lcm_state; }; /** * Sets VM state * @param s state */ void set_state(VmState s) { string st; state = s; log("VM", Log::INFO, "New state is " + vm_state_to_str(st, s)); }; /** * Sets VM LCM state * @param s state */ void set_state(LcmState s) { string st; lcm_state = s; log("VM", Log::INFO, "New LCM state is " + lcm_state_to_str(st, s)); }; /** * Sets the previous state to the current one */ void set_prev_state() { prev_state = state; prev_lcm_state = lcm_state; }; /** * Test if the VM has changed state since last time prev state was set * @return true if VM changed state */ bool has_changed_state() { return (prev_lcm_state != lcm_state || prev_state != state); } /** * Sets the re-scheduling flag * @param set or unset the re-schedule flag */ void set_resched(bool do_sched) { resched = do_sched ? 1 : 0; }; // ------------------------------------------------------------------------- // Log & Print // ------------------------------------------------------------------------- /** * writes a log message in vm.log. The class lock should be locked and * the VM MUST BE obtained through the VirtualMachinePool get() method. */ void log( const char * module, const Log::MessageType type, const ostringstream& message) const { if (_log != 0) { _log->log(module,type,message.str().c_str()); } }; /** * writes a log message in vm.log. The class lock should be locked and * the VM MUST BE obtained through the VirtualMachinePool get() method. */ void log( const char * module, const Log::MessageType type, const char * message) const { if (_log != 0) { _log->log(module,type,message); } }; /** * writes a log message in vm.log. The class lock should be locked and * the VM MUST BE obtained through the VirtualMachinePool get() method. */ void log( const char * module, const Log::MessageType type, const string& message) const { log(module, type, message.c_str()); }; // ------------------------------------------------------------------------ // Dynamic Info // ------------------------------------------------------------------------ /** * Updates VM dynamic information (id). * @param _deploy_id the VMM driver specific id */ void set_deploy_id (const string& _deploy_id) { deploy_id = _deploy_id; }; /** * Updates VM dynamic information (usage counters), and updates last_poll, * and copies it to history record for acct. */ int update_info(const string& monitor_data); /** * Clears the VM monitor information usage counters (MEMORY, CPU), * last_poll, custom attributes, and copies it to the history record * for acct. */ void reset_info() { last_poll = time(0); monitoring.replace("CPU","0.0"); monitoring.replace("MEMORY","0"); set_vm_info(); clear_template_monitor_error(); } VirtualMachineMonitorInfo& get_info() { return monitoring; } /** * Returns the deployment ID * @return the VMM driver specific ID */ const string& get_deploy_id() const { return deploy_id; }; /** * Sets the VM exit time * @param _et VM exit time (when it arrived DONE/FAILED states) */ void set_exit_time(time_t et) { etime = et; }; /** * Sets the KERNEL OS attribute (path to the kernel file). Used when * the template is using a FILE Datastore for it * @param path to the kernel (in the remote host) */ void set_kernel(const string& kernel) { VectorAttribute * os = obj_template->get("OS"); if ( os == 0 ) { return; } os->replace("KERNEL", kernel); }; /** * Sets the INITRD OS attribute (path to the initrd file). Used when * the template is using a FILE Datastore for it * @param path to the initrd (in the remote host) */ void set_initrd(const string& initrd) { VectorAttribute * os = obj_template->get("OS"); if ( os == 0 ) { return; } os->replace("INITRD", initrd); }; // ------------------------------------------------------------------------ // Access to VM locations // ------------------------------------------------------------------------ /** * Returns the remote VM directory. The VM remote dir is in the form: * $DATASTORE_LOCATION/$SYSTEM_DS_ID/$VM_ID. The system_dir stores * disks for a running VM in the target host. * @return the remote system directory for the VM */ const string& get_system_dir() const { return history->system_dir; } /** * Returns the remote VM directory for the previous host. It maybe different * if a system ds migration * The hasPreviousHistory() function MUST be called before this one. * @return the remote system directory for the VM */ const string & get_previous_system_dir() const { return previous_history->system_dir; }; // ------------------------------------------------------------------------ // History // ------------------------------------------------------------------------ /** * Adds a new history record an writes it in the database. */ void add_history( int hid, int cid, const string& hostname, const string& vmm_mad, const string& tm_mad, int ds_id); /** * Duplicates the last history record. Only the host related fields are * affected (i.e. no counter is copied nor initialized). * @param reason explaining the new addition. */ void cp_history(); /** * Duplicates the previous history record. Only the host related fields are * affected (i.e. no counter is copied nor initialized). * @param reason explaining the new addition. */ void cp_previous_history(); /** * Checks if the VM has a valid history record. This function * MUST be called before using any history related function. * @return true if the VM has a record */ bool hasHistory() const { return (history!=0); }; /** * Checks if the VM has a valid previous history record. This function * MUST be called before using any previous_history related function. * @return true if the VM has a previous record */ bool hasPreviousHistory() const { return (previous_history!=0); }; bool is_history_open() const { return (history != 0) && (history->etime == 0); } bool is_previous_history_open() const { return (previous_history != 0) && (previous_history->etime == 0); } /** * Returns the VMM driver name for the current host. The hasHistory() * function MUST be called before this one. * @return the VMM mad name */ const string & get_vmm_mad() const { return history->vmm_mad_name; }; /** * Returns the VMM driver name for the previous host. The hasPreviousHistory() * function MUST be called before this one. * @return the VMM mad name */ const string & get_previous_vmm_mad() const { return previous_history->vmm_mad_name; }; /** * Returns the datastore ID of the system DS for the host. The hasHistory() * function MUST be called before this one. * @return the ds id */ int get_ds_id() const { return history->ds_id; }; /** * Returns the datastore ID of the system DS for the previous host. * The hasPreviousHistory() function MUST be called before this one. * @return the TM mad name */ int get_previous_ds_id() const { return previous_history->ds_id; }; /** * Returns the TM driver name for the current host. The hasHistory() * function MUST be called before this one. * @return the TM mad name */ const string & get_tm_mad() const { return history->tm_mad_name; }; /** * Returns the TM driver name for the previous host. The * hasPreviousHistory() function MUST be called before this one. * @return the TM mad name */ const string & get_previous_tm_mad() const { return previous_history->tm_mad_name; }; /** * Returns the transfer filename. The transfer file is in the form: * $ONE_LOCATION/var/vms/$VM_ID/transfer.$SEQ * or, in case that OpenNebula is installed in root * /var/lib/one/vms/$VM_ID/transfer.$SEQ * The hasHistory() function MUST be called before this one. * @return the transfer filename */ const string & get_transfer_file() const { return history->transfer_file; }; /** * Returns the deployment filename. The deployment file is in the form: * $ONE_LOCATION/var/vms/$VM_ID/deployment.$SEQ * or, in case that OpenNebula is installed in root * /var/lib/one/vms/$VM_ID/deployment.$SEQ * The hasHistory() function MUST be called before this one. * @return the deployment file path */ const string & get_deployment_file() const { return history->deployment_file; }; /** * Returns the context filename. The context file is in the form: * $ONE_LOCATION/var/vms/$VM_ID/context.sh * or, in case that OpenNebula is installed in root * /var/lib/one/vms/$VM_ID/context.sh * The hasHistory() function MUST be called before this one. * @return the context file path */ const string & get_context_file() const { return history->context_file; } /** * Returns the token filename. The token file is in the form: * $ONE_LOCATION/var/vms/$VM_ID/token.txt * or, in case that OpenNebula is installed in root * /var/lib/one/vms/$VM_ID/token.txt * The hasHistory() function MUST be called before this one. * @return the token file path */ const string & get_token_file() const { return history->token_file; } /** * Returns the remote deployment filename. The file is in the form: * $DS_LOCATION/$SYSTEM_DS/$VM_ID/deployment.$SEQ * The hasHistory() function MUST be called before this one. * @return the deployment filename */ const string & get_remote_deployment_file() const { return history->rdeployment_file; }; /** * Returns the checkpoint filename for the current host. The checkpoint file * is in the form: * $DS_LOCATION/$SYSTEM_DS/$VM_ID/checkpoint * The hasHistory() function MUST be called before this one. * @return the checkpoint filename */ const string & get_checkpoint_file() const { return history->checkpoint_file; }; /** * Returns the checkpoint filename for the previous host. * The hasPreviousHistory() function MUST be called before this one. * @return the checkpoint filename */ const string & get_previous_checkpoint_file() const { return previous_history->checkpoint_file; }; /** * Returns the hostname for the current host. The hasHistory() * function MUST be called before this one. * @return the hostname */ const string & get_hostname() const { return history->hostname; }; /** * Returns if the host is a public cloud based on the system ds and tm_mad. * The hasHistory() function MUST be called before this one. * @return the hostname */ bool get_host_is_cloud() const { return ((history->ds_id == -1) && history->tm_mad_name.empty()); }; /** * Updates the current hostname. The hasHistory() * function MUST be called before this one. * @param hostname New hostname */ void set_hostname(const string& hostname) { history->hostname = hostname; }; /** * Returns the hostname for the previous host. The hasPreviousHistory() * function MUST be called before this one. * @return the hostname */ const string & get_previous_hostname() const { return previous_history->hostname; }; /** * Returns the action that closed the current history record. The hasHistory() * function MUST be called before this one. * @return the action that closed the current history record */ const History::VMAction get_action() const { return history->action; }; /** * Returns the action that closed the history record in the previous host * @return the action that closed the history record in the previous host */ const History::VMAction get_previous_action() const { return previous_history->action; }; /** * Get host id where the VM is or is going to execute. The hasHistory() * function MUST be called before this one. */ int get_hid() { return history->hid; } /** * Get host id where the VM was executing. The hasPreviousHistory() * function MUST be called before this one. */ int get_previous_hid() { return previous_history->hid; } /** * Get cluster id where the VM is or is going to execute. The hasHistory() * function MUST be called before this one. */ int get_cid() { return history->cid; } /** * Get cluster id where the VM was executing. The hasPreviousHistory() * function MUST be called before this one. */ int get_previous_cid() { return previous_history->cid; } /** * Sets start time of a VM. * @param _stime time when the VM started */ void set_stime(time_t _stime) { history->stime=_stime; }; /** * Sets VM info (with monitoring info) in the history record */ void set_vm_info() { to_xml_extended(history->vm_info, 0); }; /** * Sets VM info (with monitoring info) in the previous history record */ void set_previous_vm_info() { to_xml_extended(previous_history->vm_info, 0); }; /** * Sets end time of a VM. * @param _etime time when the VM finished */ void set_etime(time_t _etime) { history->etime=_etime; }; /** * Sets end time of a VM in the previous Host. * @param _etime time when the VM finished */ void set_previous_etime(time_t _etime) { previous_history->etime=_etime; }; /** * Sets start time of VM prolog. * @param _stime time when the prolog started */ void set_prolog_stime(time_t _stime) { history->prolog_stime=_stime; }; /** * Sets end time of VM prolog. * @param _etime time when the prolog finished */ void set_prolog_etime(time_t _etime) { history->prolog_etime=_etime; }; /** * Sets start time of VM running state. * @param _stime time when the running state started */ void set_running_stime(time_t _stime) { history->running_stime=_stime; }; /** * Gets the running start time for the VM */ time_t get_running_stime() { return history->running_stime; } /** * Sets end time of VM running state. * @param _etime time when the running state finished */ void set_running_etime(time_t _etime) { history->running_etime=_etime; }; /** * Sets end time of VM running state in the previous host. * @param _etime time when the running state finished */ void set_previous_running_etime(time_t _etime) { previous_history->running_etime=_etime; }; /** * Sets start time of VM epilog. * @param _stime time when the epilog started */ void set_epilog_stime(time_t _stime) { history->epilog_stime=_stime; }; /** * Sets end time of VM epilog. * @param _etime time when the epilog finished */ void set_epilog_etime(time_t _etime) { history->epilog_etime=_etime; }; /** * Sets the action that closed the history record * @param action that closed the history record */ void set_action(History::VMAction action, int uid, int gid, int req_id) { history->action = action; history->uid = uid; history->gid = gid; history->req_id = req_id; }; void set_internal_action(History::VMAction action) { history->action = action; history->uid = -1; history->gid = -1; history->req_id = -1; }; void clear_action() { history->action = History::NONE_ACTION; history->uid = -1; history->gid = -1; history->req_id = -1; } void set_previous_action(History::VMAction action, int uid, int gid,int rid) { previous_history->action = action; previous_history->uid = uid; previous_history->gid = gid; previous_history->req_id = rid; }; // ------------------------------------------------------------------------ // Template & Object Representation // ------------------------------------------------------------------------ /** * Function to print the VirtualMachine object into a string in * XML format * @param xml the resulting XML string * @return a reference to the generated string */ string& to_xml(string& xml) const { return to_xml_extended(xml, 1); } /** * Function to print the VirtualMachine object into a string in * XML format, with reduced information * @param xml the resulting XML string * @return a reference to the generated string */ string& to_xml_short(string& xml); /** * Function to print the VirtualMachine object into a string in * XML format, with extended information (full history records) * @param xml the resulting XML string * @return a reference to the generated string */ string& to_xml_extended(string& xml) const { return to_xml_extended(xml, 2); } /** * Rebuilds the object from an xml formatted string * @param xml_str The xml-formatted string * * @return 0 on success, -1 otherwise */ int from_xml(const string &xml_str); /** * Factory method for virtual machine templates */ Template * get_new_template() const { return new VirtualMachineTemplate; } /** * Returns a copy of the VirtualMachineTemplate * @return A copy of the VirtualMachineTemplate */ VirtualMachineTemplate * clone_template() const { return new VirtualMachineTemplate( *(static_cast(obj_template))); }; /** * This function replaces the *user template*. * @param tmpl_str new contents * @param keep_restricted If true, the restricted attributes of the * current template will override the new template * @param error string describing the error if any * @return 0 on success */ int replace_template(const string& tmpl_str, bool keep_restricted, string& error); /** * Append new attributes to the *user template*. * @param tmpl_str new contents * @param keep_restricted If true, the restricted attributes of the * current template will override the new template * @param error string describing the error if any * @return 0 on success */ int append_template(const string& tmpl_str, bool keep_restricted, string& error); /** * This function gets an attribute from the user template * @param name of the attribute * @param value of the attribute */ void get_user_template_attribute(const char * name, string& value) const { user_obj_template->get(name,value); } /** * Sets an error message with timestamp in the template * @param message Message string */ void set_template_error_message(const string& message); /** * Sets an error message with timestamp in the template * @param name of the error attribute * @param message Message string */ void set_template_error_message(const string& name, const string& message); /** * Deletes the error message from the template */ void clear_template_error_message(); /** * Sets an error message with timestamp in the template (ERROR_MONITOR) * @param message Message string */ void set_template_monitor_error(const string& message); /** * Deletes the error message from the template (ERROR_MONITOR) * @param message Message string */ void clear_template_monitor_error(); // ------------------------------------------------------------------------ // Timers & Requirements // ------------------------------------------------------------------------ /** * Gets time from last information polling. * @return time of last poll (epoch) or 0 if never polled */ time_t get_last_poll() const { return last_poll; }; /** * Sets time of last information polling. * @param poll time in epoch, normally time(0) */ void set_last_poll(time_t poll) { last_poll = poll; }; /** * Get the VM physical requirements for the host. * @param cpu * @param memory * @param disk * @param pci_dev */ void get_requirements(int& cpu, int& memory, int& disk, vector& pci_dev); /** * Adds automatic placement requirements: Datastore and Cluster * @param cluster_ids set of viable clusters for this VM * @param error_str Returns the error reason, if any * @return 0 on success */ int automatic_requirements(set& cluster_ids, string& error_str) { std::set datastore_ids; return automatic_requirements(cluster_ids, datastore_ids, error_str); } /** * Checks if the resize parameters are valid * @param cpu New CPU. 0 means unchanged. * @param memory New MEMORY. 0 means unchanged. * @param vcpu New VCPU. 0 means unchanged. * @param error_str Error reason, if any * * @return 0 on success */ int check_resize(float cpu, long int memory, int vcpu, string& error_str); /** * Resize the VM capacity * @param cpu * @param memory * @param vcpu * @param error_str Error reason, if any * * @return 0 on success */ int resize(float cpu, long int memory, int vcpu, string& error_str); // ------------------------------------------------------------------------ // Virtual Machine Disks // ------------------------------------------------------------------------ /** * Releases all disk images taken by this Virtual Machine * @param quotas disk space to free from image datastores * @param check_state to update image state based on VM state */ void release_disk_images(vector