/* -------------------------------------------------------------------------- */ /* Copyright 2002-2023, 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 "PoolObjectSQL.h" #include "History.h" #include "Image.h" #include "NebulaLog.h" #include "Backups.h" #include #include #include class AuthRequest; class Snapshots; class HostShareCapacity; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /** * 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, HOTPLUG_NIC_POWEROFF = 65, HOTPLUG_RESIZE = 66, HOTPLUG_SAVEAS_UNDEPLOYED = 67, HOTPLUG_SAVEAS_STOPPED = 68, BACKUP = 69, BACKUP_POWEROFF = 70 }; static const int MAX_VNC_PASSWD_LENGTH = 8; static const int MAX_SPICE_PASSWD_LENGTH = 59; /** * Functions to convert to/from string the VM states */ static int vm_state_from_str(std::string& st, VmState& state); static std::string& vm_state_to_str(std::string& st, VmState state); static int lcm_state_from_str(std::string& st, LcmState& state); static std::string& lcm_state_to_str(std::string& st, LcmState state); virtual ~VirtualMachine(); /** * Returns the VM state to string, using the lcm state if the current state * is ACTIVE. * @return the state sting */ std::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) { std::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) { std::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() const { 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 std::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 std::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 std::string& _deploy_id) { deploy_id = _deploy_id; }; /** * @return monitor info */ VirtualMachineMonitorInfo& get_info() { return monitoring; } /** * Read monitoring from DB */ void load_monitoring(); /** * Returns the deployment ID * @return the VMM driver specific ID */ const std::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 std::string& kernel) { VectorAttribute * os = obj_template->get("OS"); if ( os == nullptr ) { 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 std::string& initrd) { VectorAttribute * os = obj_template->get("OS"); if ( os == nullptr ) { return; } os->replace("INITRD", initrd); }; bool test_machine_type(const std::string& machine_type) const { VectorAttribute * os = obj_template->get("OS"); if ( os == nullptr ) { return false; } const std::string machine = os->vector_value("MACHINE"); return machine.find(machine_type) != std::string::npos; } // ------------------------------------------------------------------------ // 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 std::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 std::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 std::string& hostname, const std::string& vmm_mad, const std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 std::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 */ VMActions::Action 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 */ VMActions::Action 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() const { return history->hid; } /** * Get host id where the VM was executing. The hasPreviousHistory() * function MUST be called before this one. */ int get_previous_hid() const { 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() const { return history->cid; } /** * Get cluster id where the VM was executing. The hasPreviousHistory() * function MUST be called before this one. */ int get_previous_cid() const { 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() { load_monitoring(); to_xml_extended(history->vm_info, 0, false); }; /** * Sets VM info (with monitoring info) in the previous history record */ void set_previous_vm_info() { to_xml_extended(previous_history->vm_info, 0, false); }; /** * Sets end time of a VM * @param _etime time when the VM finished */ void set_etime(time_t _etime) { history->etime = _etime; }; /** * Gets end time of a VM */ time_t get_etime() { return history->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() const { 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; }; /** * Gets the running end time for the VM */ time_t get_running_etime() const { return history->running_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(VMActions::Action 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(VMActions::Action action) { history->action = action; history->uid = -1; history->gid = -1; history->req_id = -1; }; void clear_action() { history->action = VMActions::NONE_ACTION; history->uid = -1; history->gid = -1; history->req_id = -1; } void set_previous_action(VMActions::Action action, int uid, int gid,int rid) { previous_history->action = action; previous_history->uid = uid; previous_history->gid = gid; previous_history->req_id = rid; }; /** * Release the previous VNC port when a VM is migrated to another cluster * (GRAPHICS/PREVIOUS_PORT present) */ void release_previous_vnc_port(); /** * Frees current PORT from **current** cluster and sets it to PREVIOUS_PORT * (which is allocated in previous cluster). This function is called when * the migration fails. */ void rollback_previous_vnc_port(); // ------------------------------------------------------------------------ // 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 */ std::string& to_xml(std::string& xml) const override { return to_xml_extended(xml, 1, false); } /** * 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 */ std::string& to_xml_short(std::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 */ std::string& to_xml_extended(std::string& xml) const { return to_xml_extended(xml, 2, true); } /** * 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 std::string &xml_str) override; /** * Factory method for virtual machine templates */ std::unique_ptr