2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +04:00
/* */
/* 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_MANAGER_H_
# define VIRTUAL_MACHINE_MANAGER_H_
# include "VirtualMachineManagerDriver.h"
2020-06-29 13:14:00 +03:00
# include "DriverManager.h"
2020-07-24 17:00:59 +03:00
# include "Listener.h"
2008-06-17 20:27:32 +04:00
2020-06-29 13:14:00 +03:00
class DatastorePool ;
class HostPool ;
class VirtualMachinePool ;
2008-06-17 20:27:32 +04:00
2017-02-03 16:19:15 +03:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
2020-06-29 13:14:00 +03:00
class VirtualMachineManager :
public DriverManager < VirtualMachineManagerDriver > ,
2020-07-24 17:00:59 +03:00
public Listener
2017-02-03 16:19:15 +03:00
{
public :
VirtualMachineManager (
2020-06-29 13:14:00 +03:00
const std : : string & _mads ) ;
2017-02-03 16:19:15 +03:00
2020-06-29 13:14:00 +03:00
~ VirtualMachineManager ( ) = default ;
2017-02-03 16:19:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2012-06-19 17:26:22 +04:00
* This functions starts the associated listener thread , and creates a
2008-06-17 20:27:32 +04:00
* new thread for the Virtual Machine Manager . This thread will wait in
* an action loop till it receives ACTION_FINALIZE .
* @ return 0 on success .
*/
int start ( ) ;
/**
* Loads Virtual Machine Manager Mads defined in configuration file
2020-06-29 13:14:00 +03:00
* @ param _mads configuration of drivers
2008-06-17 20:27:32 +04:00
*/
2020-06-29 13:14:00 +03:00
int load_drivers ( const std : : vector < const VectorAttribute * > & _mads ) ;
2012-06-19 17:26:22 +04:00
2015-11-20 17:44:37 +03:00
/**
* Check if action is supported for imported VMs
* @ param mad name of the driver
* @ param action
* @ return True if it is supported
*/
2020-07-02 23:42:10 +03:00
bool is_imported_action_supported ( const std : : string & mad , VMActions : : Action action )
2015-11-20 17:44:37 +03:00
{
const VirtualMachineManagerDriver * vmd = get ( mad ) ;
2020-06-29 13:14:00 +03:00
if ( vmd = = nullptr )
2015-11-20 17:44:37 +03:00
{
return false ;
}
return vmd - > is_imported_action_supported ( action ) ;
}
2016-03-02 01:31:31 +03:00
/**
* Updates firewall rules of a VM
* @ param vm pointer to VM , needs to be locked
* @ param sgid the id of the security group
*
* @ return 0 on success
*/
int updatesg ( VirtualMachine * vm , int sgid ) ;
2016-05-18 20:48:43 +03:00
/**
* Get keep_snapshots capability from driver
*/
2020-07-02 23:42:10 +03:00
bool is_keep_snapshots ( const std : : string & name )
2016-05-18 20:48:43 +03:00
{
const VirtualMachineManagerDriver * vmd = get ( name ) ;
2020-02-26 19:51:40 +03:00
if ( vmd = = nullptr )
2016-05-18 20:48:43 +03:00
{
return false ;
}
return vmd - > is_keep_snapshots ( ) ;
}
2020-02-26 19:51:40 +03:00
/**
* Get cold_nic_attach behavior for the driver . When true the driver will be
* invoked in cold NIC attach operations
*/
2020-07-02 23:42:10 +03:00
bool is_cold_nic_attach ( const std : : string & name )
2020-02-26 19:51:40 +03:00
{
const VirtualMachineManagerDriver * vmd = get ( name ) ;
if ( vmd = = nullptr )
{
return false ;
}
return vmd - > is_cold_nic_attach ( ) ;
}
2019-01-31 19:27:55 +03:00
/**
* Returns a pointer to a Virtual Machine Manager driver . The driver is
* searched by its name .
* @ param name the name of the driver
* @ return the VM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
2020-07-05 23:01:32 +03:00
const VirtualMachineManagerDriver * get ( const std : : string & name ) const
2019-01-31 19:27:55 +03:00
{
2020-06-29 13:14:00 +03:00
return DriverManager : : get_driver ( name ) ;
2019-01-31 19:27:55 +03:00
} ;
2020-05-18 03:23:29 +03:00
/**
* Validates raw sections in the Virtual Machine Template for the
* target driver
* @ param template of the virtual machine
* @ param error_str error if any
*
* @ return 0 on success ( valid raw )
*/
2020-07-02 23:42:10 +03:00
int validate_raw ( const Template * vmt , std : : string & error_str ) ;
2020-05-18 03:23:29 +03:00
2008-06-17 20:27:32 +04:00
private :
/**
* Pointer to the Virtual Machine Pool , to access VMs
*/
VirtualMachinePool * vmpool ;
/**
* Pointer to the Host Pool , to access hosts
*/
HostPool * hpool ;
2012-06-19 17:26:22 +04:00
2015-10-29 03:58:35 +03:00
/**
* Pointer to the Datastore Pool
*/
DatastorePool * ds_pool ;
2020-06-29 13:14:00 +03:00
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
2020-07-02 23:42:10 +03:00
static void _undefined ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
2008-06-17 20:27:32 +04:00
/**
2020-06-29 13:14:00 +03:00
*
2008-06-17 20:27:32 +04:00
*/
2020-07-02 23:42:10 +03:00
void _deploy ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _shutdown ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _reset ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _reboot ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _cancel ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _cleanup ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _checkpoint ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _save ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _restore ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _migrate ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _attachdisk ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _detachdisk ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _attachnic ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _detachnic ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _snapshotcreate ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _snapshotrevert ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _snapshotdelete ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _disksnapshotcreate ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _disksnapshotrevert ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _resizedisk ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _updateconf ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _updatesg ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
void _driver_cancel ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
2020-07-02 23:42:10 +03:00
static void _log ( std : : unique_ptr < vm_msg_t > msg ) ;
2020-06-29 13:14:00 +03:00
/**
*
*/
void log_error ( VirtualMachine * vm_id ,
const std : : string & payload ,
const char * msg ) ;
/**
*
*/
void log_error ( int vm_id , const std : : string & payload , const char * msg ) ;
/**
*
*/
bool check_vm_state ( int vm_id , vm_msg_t * msg ) ;
2008-06-17 20:27:32 +04:00
2017-02-03 16:19:15 +03:00
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
2020-06-29 13:14:00 +03:00
static const int drivers_timeout = 10 ;
2020-07-24 17:00:59 +03:00
void finalize_action ( )
2017-02-03 16:19:15 +03:00
{
2020-06-29 13:14:00 +03:00
DriverManager : : stop ( drivers_timeout ) ;
2017-02-03 16:19:15 +03:00
} ;
2011-11-10 14:15:58 +04:00
/**
* Function to format a VMM Driver message in the form :
* < VMM_DRIVER_ACTION_DATA >
* < HOST > hostname < / HOST >
* < MIGR_HOST > m_hostname < / MIGR_HOST >
* < DOMAIN > domain_id < / DOMAIN >
* < DEPLOYMENT_FILE > dfile < / DEPLOYMENT_FILE >
* < CHECKPOINT_FILE > cfile < / CHECKPOINT_FILE >
2011-11-11 03:45:33 +04:00
* < VM >
2011-11-10 14:15:58 +04:00
* VM representation in XML
2011-11-11 03:45:33 +04:00
* < / VM >
2015-10-29 03:58:35 +03:00
* < DATASTORE >
* System DS information in XML
* < / DATASTORE >
2011-11-10 14:15:58 +04:00
* < / VMM_DRIVER_ACTION_DATA >
*
* @ param hostname of the host to perform the action
* @ param m_hostname name of the host to migrate the VM
* @ param domain domain id as returned by the hypervisor
* @ param dfile deployment file to boot the VM
* @ param cfile checkpoint file to save the VM
2012-06-13 20:42:42 +04:00
* @ param disk_id Disk to attach / detach , if any
* @ param tm_command Transfer Manager command to attach / detach , if any
2013-11-10 23:40:04 +04:00
* @ param tm_command_rollback TM command in case of attach failure
2012-06-13 20:42:42 +04:00
* @ param disk_target_path Path of the disk to attach , if any
2011-11-10 14:15:58 +04:00
* @ param tmpl the VM information in XML
2015-10-29 03:58:35 +03:00
* @ param ds_id of the system datastore
2016-03-02 01:31:31 +03:00
* @ param id of the security group
2011-11-10 14:15:58 +04:00
*/
2020-07-05 23:01:32 +03:00
std : : string format_message (
2020-07-02 23:42:10 +03:00
const std : : string & hostname ,
const std : : string & m_hostname ,
const std : : string & domain ,
const std : : string & ldfile ,
const std : : string & rdfile ,
const std : : string & cfile ,
const std : : string & tm_command ,
const std : : string & tm_command_rollback ,
const std : : string & disk_target_path ,
const std : : string & tmpl ,
2016-03-02 01:31:31 +03:00
int ds_id ,
int sgid ) ;
2012-06-19 17:26:22 +04:00
2020-07-24 17:00:59 +03:00
public :
2008-06-17 20:27:32 +04:00
/**
* Function executed when a DEPLOY action is received . It deploys a VM on
* a Host .
* @ param vid the id of the VM to be deployed .
*/
2020-07-24 17:00:59 +03:00
void trigger_deploy ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
2012-06-19 17:26:22 +04:00
* Function to stop a running VM and generate a checkpoint file . This
2008-06-17 20:27:32 +04:00
* function is executed when a SAVE action is triggered .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_save ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* Shutdowns a VM when a SHUTDOWN action is received .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_shutdown ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* Cancels a VM when a CANCEL action is received .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_cancel ( int vid ) ;
2008-06-17 20:27:32 +04:00
2009-07-09 18:34:34 +04:00
/**
* Cancels a VM ( in the previous host ) when a CANCEL action is received .
* Note that the domain - id is the last one returned by a boot action
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_cancel_previous ( int vid ) ;
2009-07-09 18:34:34 +04:00
2013-01-21 03:15:46 +04:00
/**
* Cleanups a host ( cancel VM + delete disk images ) .
* @ param vid the id of the VM .
2013-01-21 15:27:18 +04:00
* @ param cancel_previous if true the VM will be canceled in the previous
* host ( only relevant to delete VM ' s in MIGRATE state )
2013-01-21 03:15:46 +04:00
*/
2020-07-24 17:00:59 +03:00
void trigger_cleanup ( int vid , bool cancel_previous ) ;
2013-01-21 03:15:46 +04:00
2013-01-21 15:27:18 +04:00
/**
* Cleanups the previous host ( cancel VM + delete disk images ) .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_cleanup_previous ( int vid ) ;
2013-01-21 15:27:18 +04:00
2008-06-17 20:27:32 +04:00
/**
* Function to migrate ( live ) a VM ( MIGRATE action ) .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_migrate ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* Restores a VM from a checkpoint file .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_restore ( int vid ) ;
2008-06-22 05:51:49 +04:00
2011-12-26 02:46:19 +04:00
/**
* Reboots a running VM .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_reboot ( int vid ) ;
2012-06-19 17:26:22 +04:00
2012-05-09 00:33:59 +04:00
/**
* Resets a running VM .
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_reset ( int vid ) ;
2011-12-26 02:46:19 +04:00
2012-06-13 20:42:42 +04:00
/**
* Attaches a new disk to a VM . The VM must have a disk with the
* attribute ATTACH = YES
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_attach ( int vid ) ;
2012-06-13 20:42:42 +04:00
2012-06-14 19:45:41 +04:00
/**
* Detaches a disk from a VM . The VM must have a disk with the
* attribute ATTACH = YES
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_detach ( int vid ) ;
2012-06-14 19:45:41 +04:00
2012-12-12 21:31:27 +04:00
/**
* Attaches a new NIC to a VM . The VM must have a NIC with the
* attribute ATTACH = YES
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_attach_nic ( int vid ) ;
2012-12-12 21:31:27 +04:00
/**
* Detaches a NIC from a VM . The VM must have a NIC with the
* attribute ATTACH = YES
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_detach_nic ( int vid ) ;
2012-12-12 21:31:27 +04:00
2016-12-17 21:30:11 +03:00
/**
* This function cancels the current driver operation
*/
2020-07-24 17:00:59 +03:00
void trigger_driver_cancel ( int vid ) ;
2016-12-17 21:30:11 +03:00
2013-02-20 19:04:09 +04:00
/**
* Creates a new system snapshot . The VM must have a snapshot with the
* attribute ACTIVE = YES
*
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_snapshot_create ( int vid ) ;
2013-02-20 19:04:09 +04:00
/**
* Reverts to a snapshot . The VM must have a snapshot with the
* attribute ACTIVE = YES
*
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_snapshot_revert ( int vid ) ;
2013-02-20 19:04:09 +04:00
2013-02-21 18:01:48 +04:00
/**
* Deletes a snapshot . The VM must have a snapshot with the
* attribute ACTIVE = YES
*
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_snapshot_delete ( int vid ) ;
2013-02-21 18:01:48 +04:00
2015-07-01 14:37:58 +03:00
/**
* Creates a new disk system snapshot .
*
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_disk_snapshot_create ( int vid ) ;
2015-07-01 14:37:58 +03:00
2009-07-09 18:34:34 +04:00
/**
2016-12-17 21:30:11 +03:00
* Resize a VM disk
*
* @ param vid the id of the VM .
2009-07-09 18:34:34 +04:00
*/
2020-07-24 17:00:59 +03:00
void trigger_disk_resize ( int vid ) ;
2019-07-26 14:45:26 +03:00
/**
* Update VM context
*
* @ param vid the id of the VM .
*/
2020-07-24 17:00:59 +03:00
void trigger_update_conf ( int vid ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*VIRTUAL_MACHINE_MANAGER_H*/