2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2018-01-02 20:27:37 +03:00
/* Copyright 2002-2018, 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 "MadManager.h"
# include "ActionManager.h"
# include "VirtualMachineManagerDriver.h"
# include "VirtualMachinePool.h"
# include "HostPool.h"
2015-10-29 03:58:35 +03:00
# include "DatastorePool.h"
2008-06-17 20:27:32 +04:00
# include "NebulaTemplate.h"
using namespace std ;
extern " C " void * vmm_action_loop ( void * arg ) ;
2017-02-03 16:19:15 +03:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class VMMAction : public ActionRequest
2008-06-17 20:27:32 +04:00
{
public :
enum Actions
{
DEPLOY ,
SAVE ,
SHUTDOWN ,
CANCEL ,
2009-07-09 18:34:34 +04:00
CANCEL_PREVIOUS ,
2013-01-21 03:15:46 +04:00
CLEANUP ,
CLEANUP_BOTH ,
2013-01-21 15:27:18 +04:00
CLEANUP_PREVIOUS ,
2008-06-17 20:27:32 +04:00
MIGRATE ,
RESTORE ,
2011-12-26 02:46:19 +04:00
REBOOT ,
2012-05-09 00:33:59 +04:00
RESET ,
2008-06-22 05:51:49 +04:00
POLL ,
2009-07-09 18:34:34 +04:00
DRIVER_CANCEL ,
2012-06-14 19:45:41 +04:00
ATTACH ,
2012-12-12 21:31:27 +04:00
DETACH ,
ATTACH_NIC ,
2013-03-06 18:59:58 +04:00
DETACH_NIC ,
2013-02-20 19:04:09 +04:00
SNAPSHOT_CREATE ,
2013-02-21 18:01:48 +04:00
SNAPSHOT_REVERT ,
2015-07-01 14:37:58 +03:00
SNAPSHOT_DELETE ,
2016-12-17 21:30:11 +03:00
DISK_SNAPSHOT_CREATE ,
DISK_RESIZE
2008-06-17 20:27:32 +04:00
} ;
2017-02-03 16:19:15 +03:00
VMMAction ( Actions a , int v ) : ActionRequest ( ActionRequest : : USER ) ,
_action ( a ) , _vm_id ( v ) { } ;
2017-02-08 14:24:42 +03:00
VMMAction ( const VMMAction & o ) : ActionRequest ( o . _type ) , _action ( o . _action ) ,
_vm_id ( o . _vm_id ) { } ;
2017-02-03 16:19:15 +03:00
Actions action ( ) const
{
return _action ;
}
int vm_id ( ) const
{
return _vm_id ;
}
2017-02-08 14:24:42 +03:00
ActionRequest * clone ( ) const
{
return new VMMAction ( * this ) ;
}
2017-02-03 16:19:15 +03:00
private :
Actions _action ;
int _vm_id ;
} ;
class VirtualMachineManager : public MadManager , public ActionListener
{
public :
VirtualMachineManager (
time_t _timer_period ,
time_t _poll_period ,
bool _do_vm_poll ,
int _vm_limit ,
vector < const VectorAttribute * > & _mads ) ;
~ VirtualMachineManager ( ) { } ;
2008-06-17 20:27:32 +04:00
/**
* Triggers specific actions to the Virtual Machine Manager . This function
* wraps the ActionManager trigger function .
* @ param action the VMM action
2012-06-19 17:26:22 +04:00
* @ param vid VM unique id . This is the argument of the passed to the
2008-06-17 20:27:32 +04:00
* invoked action .
*/
2017-02-03 16:19:15 +03:00
void trigger ( VMMAction : : Actions action , int vid )
{
VMMAction vmm_ar ( action , vid ) ;
am . trigger ( vmm_ar ) ;
}
void finalize ( )
{
am . finalize ( ) ;
}
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 ( ) ;
/**
* Gets the thread identification .
* @ return pthread_t for the manager thread ( that in the action loop ) .
*/
pthread_t get_thread_id ( ) const
{
return vmm_thread ;
} ;
2012-06-19 17:26:22 +04:00
2008-06-17 20:27:32 +04:00
/**
* Loads Virtual Machine Manager Mads defined in configuration file
2012-06-19 17:26:22 +04:00
* @ param uid of the user executing the driver . When uid is 0 the nebula
2008-06-17 20:27:32 +04:00
* identity will be used . Otherwise the Mad will be loaded through the
2012-06-19 17:26:22 +04:00
* sudo application .
2008-06-17 20:27:32 +04:00
*/
2013-10-25 17:16:44 +04:00
int load_mads ( int uid ) ;
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
*/
bool is_imported_action_supported ( const string & mad , History : : VMAction action )
{
const VirtualMachineManagerDriver * vmd = get ( mad ) ;
if ( vmd = = 0 )
{
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
*/
bool is_keep_snapshots ( const string & name )
{
const VirtualMachineManagerDriver * vmd = get ( name ) ;
if ( vmd = = 0 )
{
return false ;
}
return vmd - > is_keep_snapshots ( ) ;
}
2008-06-17 20:27:32 +04:00
private :
/**
* Thread id for the Virtual Machine Manager
*/
pthread_t vmm_thread ;
/**
* 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 ;
2008-06-17 20:27:32 +04:00
/**
* Timer period for the Virtual Machine Manager .
*/
time_t timer_period ;
/**
* Virtual Machine polling interval
*/
time_t poll_period ;
2014-05-16 14:24:40 +04:00
/**
* Perform pro - active VM monitoring
*/
bool do_vm_poll ;
2011-08-16 20:12:45 +04:00
/**
* Virtual Machine polling limit
*/
int vm_limit ;
2008-06-17 20:27:32 +04:00
/**
* Action engine for the Manager
*/
ActionManager am ;
/**
2012-06-19 17:26:22 +04:00
* Function to execute the Manager action loop method within a new pthread
2008-06-17 20:27:32 +04:00
* ( requires C linkage )
*/
friend void * vmm_action_loop ( void * arg ) ;
/**
* Returns a pointer to a Virtual Machine Manager driver .
* @ param uid of the owner of the driver
* @ param name of an attribute of the driver ( e . g . its type )
* @ param value of the attribute
* @ return the VM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const VirtualMachineManagerDriver * get (
const string & name ,
const string & value )
{
return static_cast < const VirtualMachineManagerDriver * >
2009-07-13 17:47:42 +04:00
( MadManager : : get ( 0 , name , value ) ) ;
2008-06-17 20:27:32 +04:00
} ;
/**
2012-06-19 17:26:22 +04:00
* Returns a pointer to a Virtual Machine Manager driver . The driver is
2008-06-17 20:27:32 +04:00
* 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
*/
const VirtualMachineManagerDriver * get (
const string & name )
{
string _name ( " NAME " ) ;
return static_cast < const VirtualMachineManagerDriver * >
2009-07-13 17:47:42 +04:00
( MadManager : : get ( 0 , _name , name ) ) ;
2008-06-17 20:27:32 +04:00
} ;
2012-06-19 17:26:22 +04:00
2017-02-03 16:19:15 +03:00
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
2017-02-03 16:19:15 +03:00
* This function is executed periodically to poll the running VMs
2008-06-17 20:27:32 +04:00
*/
2017-02-03 16:19:15 +03:00
void timer_action ( const ActionRequest & ar ) ;
void finalize_action ( const ActionRequest & ar )
{
NebulaLog : : log ( " VMM " , Log : : INFO , " Stopping Virtual Machine Manager... " ) ;
MadManager : : stop ( ) ;
} ;
void user_action ( const ActionRequest & ar ) ;
2008-06-17 20:27:32 +04: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
*/
string * format_message (
const string & hostname ,
const string & m_hostname ,
const string & domain ,
2011-11-16 21:11:43 +04:00
const string & ldfile ,
const string & rdfile ,
2011-11-10 14:15:58 +04:00
const string & cfile ,
2012-06-13 20:42:42 +04:00
const string & tm_command ,
2013-11-10 23:40:04 +04:00
const string & tm_command_rollback ,
2012-06-13 20:42:42 +04:00
const string & disk_target_path ,
2015-10-29 03:58:35 +03:00
const string & tmpl ,
2016-03-02 01:31:31 +03:00
int ds_id ,
int sgid ) ;
2012-06-19 17:26:22 +04:00
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 .
*/
void deploy_action (
int vid ) ;
/**
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 .
*/
void save_action (
int vid ) ;
/**
* Shutdowns a VM when a SHUTDOWN action is received .
* @ param vid the id of the VM .
*/
void shutdown_action (
int vid ) ;
/**
* Cancels a VM when a CANCEL action is received .
* @ param vid the id of the VM .
*/
void cancel_action (
int vid ) ;
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 .
*/
void cancel_previous_action (
int vid ) ;
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
*/
void cleanup_action (
int vid , bool cancel_previous ) ;
2013-01-21 15:27:18 +04:00
/**
* Cleanups the previous host ( cancel VM + delete disk images ) .
* @ param vid the id of the VM .
*/
void cleanup_previous_action (
int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* Function to migrate ( live ) a VM ( MIGRATE action ) .
* @ param vid the id of the VM .
*/
void migrate_action (
int vid ) ;
/**
* Restores a VM from a checkpoint file .
* @ param vid the id of the VM .
*/
void restore_action (
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 .
*/
void reboot_action (
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 .
*/
void reset_action (
int vid ) ;
2011-12-26 02:46:19 +04:00
2008-06-22 05:51:49 +04:00
/**
* Polls a VM .
* @ param vid the id of the VM .
*/
void poll_action (
int vid ) ;
2012-06-19 17:26:22 +04:00
2009-07-09 18:34:34 +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 .
*/
void attach_action (
int vid ) ;
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 .
*/
void detach_action (
int vid ) ;
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 .
*/
void attach_nic_action (
int vid ) ;
/**
* Detaches a NIC from a VM . The VM must have a NIC with the
* attribute ATTACH = YES
* @ param vid the id of the VM .
*/
void detach_nic_action (
int vid ) ;
2016-12-17 21:30:11 +03:00
/**
* This function cancels the current driver operation
*/
void driver_cancel_action (
int vid ) ;
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 .
*/
2016-12-17 21:30:11 +03:00
void snapshot_create_action (
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 .
*/
2016-12-17 21:30:11 +03:00
void snapshot_revert_action (
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 .
*/
2016-12-17 21:30:11 +03:00
void snapshot_delete_action (
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 .
*/
2016-12-17 21:30:11 +03:00
void disk_snapshot_create_action (
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
*/
2016-12-17 21:30:11 +03:00
void disk_resize_action (
2009-07-09 18:34:34 +04:00
int vid ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*VIRTUAL_MACHINE_MANAGER_H*/