2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2012-01-12 15:29:18 +04:00
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
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_H_
# define VIRTUAL_MACHINE_H_
# include "VirtualMachineTemplate.h"
# include "PoolSQL.h"
# include "History.h"
# include "Log.h"
2010-05-25 20:19:22 +04:00
# include "NebulaLog.h"
2008-06-17 20:27:32 +04:00
# include <time.h>
2012-06-15 14:28:20 +04:00
# include <set>
2008-06-17 20:27:32 +04:00
# include <sstream>
using namespace std ;
2011-06-01 14:41:46 +04:00
class AuthRequest ;
2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* The Virtual Machine class . It represents a VM . . .
*/
class VirtualMachine : public PoolObjectSQL
{
public :
2008-06-22 05:51:49 +04:00
// -------------------------------------------------------------------------
2010-12-31 19:00:26 +03:00
// VM States
2009-03-06 15:10:15 +03:00
// -------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* Global Virtual Machine state
*/
enum VmState
{
INIT = 0 ,
PENDING = 1 ,
HOLD = 2 ,
ACTIVE = 3 ,
STOPPED = 4 ,
SUSPENDED = 5 ,
DONE = 6 ,
FAILED = 7
} ;
/**
2009-03-06 15:10:15 +03:00
* Virtual Machine state associated to the Life - cycle Manager
2008-06-17 20:27:32 +04:00
*/
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 ,
2008-11-13 19:21:17 +03:00
PROLOG_RESUME = 9 ,
EPILOG_STOP = 10 ,
EPILOG = 11 ,
SHUTDOWN = 12 ,
2009-07-09 18:34:34 +04:00
CANCEL = 13 ,
FAILURE = 14 ,
2010-12-31 19:00:26 +03:00
CLEANUP = 15 ,
2012-06-14 15:02:18 +04:00
UNKNOWN = 16 ,
HOTPLUG = 17
2008-06-17 20:27:32 +04:00
} ;
2008-06-22 05:51:49 +04:00
// -------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
// Log & Print
2009-03-06 15:10:15 +03:00
// -------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* writes a log message in vm . log . The class lock should be locked and
2009-03-06 15:10:15 +03:00
* the VM MUST BE obtained through the VirtualMachinePool get ( ) method .
2008-06-17 20:27:32 +04:00
*/
void log (
const char * module ,
const Log : : MessageType type ,
const ostringstream & message ) const
{
if ( _log ! = 0 )
{
2010-04-21 02:40:16 +04:00
_log - > log ( module , type , message . str ( ) . c_str ( ) ) ;
2008-06-17 20:27:32 +04:00
}
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* writes a log message in vm . log . The class lock should be locked and
2009-03-06 15:10:15 +03:00
* the VM MUST BE obtained through the VirtualMachinePool get ( ) method .
2008-06-17 20:27:32 +04:00
*/
void log (
const char * module ,
const Log : : MessageType type ,
2009-03-06 15:10:15 +03:00
const char * message ) const
2008-06-17 20:27:32 +04:00
{
if ( _log ! = 0 )
{
_log - > log ( module , type , message ) ;
}
} ;
2009-03-06 15:10:15 +03:00
2010-04-21 02:40:16 +04:00
/**
* 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
*/
2012-05-11 21:15:25 +04:00
string & to_xml ( string & xml ) const
{
return to_xml_extended ( xml , 1 ) ;
}
2010-04-11 00:16:47 +04:00
2011-06-25 03:29:44 +04:00
/**
* 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
*/
2012-05-11 21:15:25 +04:00
string & to_xml_extended ( string & xml ) const
{
return to_xml_extended ( xml , 2 ) ;
}
2011-06-25 03:29:44 +04:00
2011-03-01 19:47:20 +03:00
/**
* 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 ) ;
2008-06-17 20:27:32 +04:00
// ------------------------------------------------------------------------
// Dynamic Info
2009-03-06 15:10:15 +03:00
// ------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* Updates VM dynamic information ( id ) .
* @ param _deploy_id the VMM driver specific id
*/
void update_info (
const string & _deploy_id )
{
deploy_id = _deploy_id ;
} ;
/**
* Updates VM dynamic information ( usage counters ) .
2012-05-08 18:18:24 +04:00
* @ param _memory Kilobytes used by the VM ( total )
2008-06-17 20:27:32 +04:00
* @ param _cpu used by the VM ( rate )
* @ param _net_tx transmitted bytes ( total )
* @ param _net_tx received bytes ( total )
*/
void update_info (
const int _memory ,
const int _cpu ,
const int _net_tx ,
const int _net_rx )
{
if ( _memory ! = - 1 )
{
memory = _memory ;
}
if ( _cpu ! = - 1 )
{
cpu = _cpu ;
}
if ( _net_tx ! = - 1 )
{
net_tx = _net_tx ;
}
if ( _net_rx ! = - 1 )
{
net_rx = _net_rx ;
}
} ;
/**
* Returns the deployment ID
* @ return the VMM driver specific ID
*/
const string & get_deploy_id ( ) const
{
return deploy_id ;
} ;
/**
* Sets the VM exit time
2012-05-04 19:29:36 +04:00
* @ param _et VM exit time ( when it arrived DONE / FAILED states )
2008-06-17 20:27:32 +04:00
*/
void set_exit_time ( time_t et )
{
etime = et ;
} ;
2012-02-26 02:31:44 +04:00
// ------------------------------------------------------------------------
// Access to VM locations
// ------------------------------------------------------------------------
/**
* Returns the remote VM directory . The VM remote dir is in the form :
2012-03-02 03:06:57 +04:00
* $ DATASTORE_LOCATION / $ SYSTEM_DS_ID / $ VM_ID . The remote system_dir stores
2012-02-26 02:31:44 +04:00
* disks for a running VM in the target host .
* @ return the remote system directory for the VM
*/
2012-03-02 03:06:57 +04:00
string get_remote_system_dir ( ) const ;
2012-02-26 02:31:44 +04:00
/**
* Returns the local VM directory . The VM local dir is in the form :
* $ SYSTEM_DS_BASE_PATH / $ VM_ID . Temporary stores VM disks .
* @ return the system directory for the VM
*/
2012-03-02 03:06:57 +04:00
string get_system_dir ( ) const ;
2012-02-26 02:31:44 +04:00
2008-06-17 20:27:32 +04:00
// ------------------------------------------------------------------------
// History
2009-03-06 15:10:15 +03:00
// ------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
2008-06-23 02:38:55 +04:00
* Adds a new history record an writes it in the database .
2008-06-17 20:27:32 +04:00
*/
void add_history (
2011-03-08 19:55:14 +03:00
int hid ,
const string & hostname ,
const string & vmm_mad ,
2012-06-28 17:32:52 +04:00
const string & vnm_mad ,
2012-06-29 18:48:43 +04:00
const string & tm_mad ,
int ds_id ) ;
2008-06-22 05:51:49 +04:00
/**
* Duplicates the last history record . Only the host related fields are
2008-06-23 02:38:55 +04:00
* affected ( i . e . no counter is copied nor initialized ) .
2008-06-22 05:51:49 +04:00
* @ param reason explaining the new addition .
*/
2008-06-23 02:38:55 +04:00
void cp_history ( ) ;
2008-06-22 05:51:49 +04:00
/**
* Duplicates the previous history record . Only the host related fields are
2008-06-23 02:38:55 +04:00
* affected ( i . e . no counter is copied nor initialized ) .
2008-06-22 05:51:49 +04:00
* @ param reason explaining the new addition .
*/
2008-06-23 02:38:55 +04:00
void cp_previous_history ( ) ;
2008-06-17 20:27:32 +04:00
/**
2008-06-22 05:51:49 +04:00
* Checks if the VM has a valid history record . This function
2008-06-17 20:27:32 +04:00
* MUST be called before using any history related function .
* @ return true if the VM has a record
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
bool hasHistory ( ) const
{
return ( history ! = 0 ) ;
} ;
2008-06-22 05:51:49 +04:00
/**
* 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
2009-03-06 15:10:15 +03:00
*/
2008-06-22 05:51:49 +04:00
bool hasPreviousHistory ( ) const
{
return ( previous_history ! = 0 ) ;
} ;
2009-07-09 18:34:34 +04:00
2008-06-17 20:27:32 +04:00
/**
* Returns the VMM driver name for the current host . The hasHistory ( )
* function MUST be called before this one .
* @ return the VMM mad name
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
const string & get_vmm_mad ( ) const
{
return history - > vmm_mad_name ;
} ;
2009-07-09 18:34:34 +04:00
/**
* 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 ;
} ;
2011-11-10 14:15:58 +04:00
/**
* Returns the VNM driver name for the current host . The hasHistory ( )
* function MUST be called before this one .
2011-11-11 17:55:18 +04:00
* @ return the VNM mad name
2011-11-10 14:15:58 +04:00
*/
2011-11-11 01:53:39 +04:00
const string & get_vnm_mad ( ) const
2011-11-10 14:15:58 +04:00
{
2011-11-10 20:28:32 +04:00
return history - > vnm_mad_name ;
2011-11-10 14:15:58 +04:00
} ;
/**
* Returns the VNM driver name for the previous host . The hasPreviousHistory ( )
* function MUST be called before this one .
2011-11-11 17:55:18 +04:00
* @ return the VNM mad name
2011-11-10 14:15:58 +04:00
*/
2011-11-11 01:53:39 +04:00
const string & get_previous_vnm_mad ( ) const
2011-11-10 14:15:58 +04:00
{
2011-11-10 20:28:32 +04:00
return previous_history - > vnm_mad_name ;
2011-11-10 14:15:58 +04:00
} ;
2012-07-02 20:06:07 +04:00
/**
* Returns the datastore ID of the system DS for the host . The hasHistory ( )
* function MUST be called before this one .
* @ return the ds id
*/
string get_ds_id ( ) const
{
ostringstream oss ;
oss < < history - > ds_id ;
return oss . str ( ) ;
} ;
/**
* 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
*/
string get_previous_ds_id ( ) const
{
ostringstream oss ;
oss < < previous_history - > ds_id ;
return oss . str ( ) ;
} ;
2012-06-28 17:32:52 +04:00
/**
* 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 ;
} ;
2008-06-17 20:27:32 +04:00
/**
2009-03-06 15:10:15 +03:00
* Returns the transfer filename . The transfer file is in the form :
2010-12-31 19:00:26 +03:00
* $ ONE_LOCATION / var / $ VM_ID / transfer . $ SEQ
2009-01-02 17:58:51 +03:00
* or , in case that OpenNebula is installed in root
2010-12-31 19:00:26 +03:00
* / var / lib / one / $ VM_ID / transfer . $ SEQ
2008-11-13 19:21:17 +03:00
* The hasHistory ( ) function MUST be called before this one .
* @ return the transfer filename
2009-03-06 15:10:15 +03:00
*/
2008-11-13 19:21:17 +03:00
const string & get_transfer_file ( ) const
2008-06-17 20:27:32 +04:00
{
2008-11-13 19:21:17 +03:00
return history - > transfer_file ;
2008-06-17 20:27:32 +04:00
} ;
/**
2008-11-13 19:21:17 +03:00
* Returns the deployment filename . The deployment file is in the form :
2010-12-31 19:00:26 +03:00
* $ ONE_LOCATION / var / $ VM_ID / deployment . $ SEQ
2009-01-02 17:58:51 +03:00
* or , in case that OpenNebula is installed in root
2010-12-31 19:00:26 +03:00
* / var / lib / one / $ VM_ID / deployment . $ SEQ
2008-11-13 19:21:17 +03:00
* The hasHistory ( ) function MUST be called before this one .
2008-06-17 20:27:32 +04:00
* @ return the deployment filename
2009-03-06 15:10:15 +03:00
*/
2008-11-13 19:21:17 +03:00
const string & get_deployment_file ( ) const
2008-06-17 20:27:32 +04:00
{
2008-11-13 19:21:17 +03:00
return history - > deployment_file ;
2008-06-17 20:27:32 +04:00
} ;
2009-03-06 15:10:15 +03:00
/**
* Returns the context filename . The context file is in the form :
* $ ONE_LOCATION / var / $ VM_ID / context . sh
* or , in case that OpenNebula is installed in root
* / var / lib / one / $ VM_ID / context . sh
* The hasHistory ( ) function MUST be called before this one .
* @ return the deployment filename
*/
const string & get_context_file ( ) const
{
return history - > context_file ;
}
2008-11-13 19:21:17 +03:00
/**
* Returns the remote deployment filename . The file is in the form :
2010-12-31 19:00:26 +03:00
* $ VM_DIR / $ VM_ID / images / deployment . $ SEQ
2008-11-13 19:21:17 +03:00
* The hasHistory ( ) function MUST be called before this one .
* @ return the deployment filename
2009-03-06 15:10:15 +03:00
*/
2008-11-13 19:21:17 +03:00
const string & get_remote_deployment_file ( ) const
{
return history - > rdeployment_file ;
2009-03-06 15:10:15 +03:00
} ;
2008-06-17 20:27:32 +04:00
/**
2009-03-06 15:10:15 +03:00
* Returns the checkpoint filename for the current host . The checkpoint file
* is in the form :
2010-12-31 19:00:26 +03:00
* $ VM_DIR / $ VM_ID / images / checkpoint
2008-11-13 19:21:17 +03:00
* The hasHistory ( ) function MUST be called before this one .
2008-06-17 20:27:32 +04:00
* @ return the checkpoint filename
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
const string & get_checkpoint_file ( ) const
{
return history - > checkpoint_file ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the hostname for the current host . The hasHistory ( )
* function MUST be called before this one .
* @ return the hostname
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
const string & get_hostname ( ) const
{
return history - > hostname ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-22 05:51:49 +04:00
/**
* Returns the hostname for the previous host . The hasPreviousHistory ( )
* function MUST be called before this one .
* @ return the hostname
2009-03-06 15:10:15 +03:00
*/
2008-06-22 05:51:49 +04:00
const string & get_previous_hostname ( ) const
{
return previous_history - > hostname ;
} ;
2009-03-06 15:10:15 +03:00
2008-11-13 19:21:17 +03:00
/**
* Returns the reason that originated the VM migration in the previous host
* @ return the migration reason to leave this host
*/
const History : : MigrationReason get_previous_reason ( ) const
{
return previous_history - > reason ;
2009-03-06 15:10:15 +03:00
} ;
2008-06-17 20:27:32 +04:00
/**
* 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 ;
}
2008-06-22 05:51:49 +04:00
/**
* 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 ;
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Sets start time of a VM .
* @ param _stime time when the VM started
*/
void set_stime ( time_t _stime )
{
history - > stime = _stime ;
} ;
2012-05-05 22:57:17 +04:00
/**
* Sets VM info ( with monitoring info ) in the history record
*/
2012-05-08 17:42:24 +04:00
void set_vm_info ( )
{
to_xml_extended ( history - > vm_info , 0 ) ;
} ;
2012-05-05 22:57:17 +04:00
/**
* Sets VM info ( with monitoring info ) in the previous history record
*/
2012-05-08 17:42:24 +04:00
void set_previous_vm_info ( )
{
to_xml_extended ( previous_history - > vm_info , 0 ) ;
} ;
2012-05-05 22:57:17 +04:00
2008-06-17 20:27:32 +04:00
/**
* Sets end time of a VM .
* @ param _etime time when the VM finished
*/
void set_etime ( time_t _etime )
{
history - > etime = _etime ;
} ;
2008-06-22 05:51:49 +04:00
/**
* 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 ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* 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 ;
} ;
/**
* 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 ;
} ;
2008-06-22 05:51:49 +04:00
/**
* 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 ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Sets start time of VM epilog .
* @ param _stime time when the epilog started
*/
void set_epilog_stime ( time_t _stime )
{
2008-06-22 05:51:49 +04:00
history - > epilog_stime = _stime ;
2008-06-17 20:27:32 +04:00
} ;
/**
* Sets end time of VM epilog .
* @ param _etime time when the epilog finished
*/
void set_epilog_etime ( time_t _etime )
{
2008-06-22 05:51:49 +04:00
history - > epilog_etime = _etime ;
2008-06-17 20:27:32 +04:00
} ;
/**
* Sets the reason that originated the VM migration
* @ param _reason migration reason to leave this host
*/
void set_reason ( History : : MigrationReason _reason )
{
history - > reason = _reason ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-23 02:38:55 +04:00
/**
* Sets the reason that originated the VM migration in the previous host
* @ param _reason migration reason to leave this host
*/
void set_previous_reason ( History : : MigrationReason _reason )
{
previous_history - > reason = _reason ;
2009-03-06 15:10:15 +03:00
} ;
2008-06-17 20:27:32 +04:00
// ------------------------------------------------------------------------
// Template
2009-03-06 15:10:15 +03:00
// ------------------------------------------------------------------------
2009-04-02 14:14:54 +04:00
/**
2010-04-11 00:16:47 +04:00
* Parse a string and substitute variables ( e . g . $ NAME ) using the VM
2009-04-02 14:14:54 +04:00
* template values :
* @ param attribute , the string to be parsed
* @ param parsed , the resulting parsed string
* @ return 0 on success .
2010-04-11 00:16:47 +04:00
*/
2011-04-08 03:02:55 +04:00
int parse_template_attribute ( const string & attribute , string & parsed ) ;
2011-06-02 01:53:09 +04:00
/**
* Factory method for virtual machine templates
*/
2012-03-14 18:48:06 +04:00
Template * get_new_template ( ) const
2011-06-02 01:53:09 +04:00
{
return new VirtualMachineTemplate ;
}
2011-04-08 03:02:55 +04:00
2012-06-06 19:05:11 +04:00
/**
* Returns a copy of the VirtualMachineTemplate
* @ return A copy of the VirtualMachineTemplate
*/
VirtualMachineTemplate * clone_template ( ) const
{
return new VirtualMachineTemplate (
* ( static_cast < VirtualMachineTemplate * > ( obj_template ) ) ) ;
} ;
2008-06-17 20:27:32 +04:00
// ------------------------------------------------------------------------
// States
2009-03-06 15:10:15 +03:00
// ------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* Returns the VM state ( Dispatch Manager )
* @ return the VM state
*/
VmState get_state ( ) const
{
return state ;
} ;
/**
* Returns the VM state ( life - cycle Manager )
* @ return the VM state
*/
LcmState get_lcm_state ( ) const
{
return lcm_state ;
} ;
/**
* Sets VM state
* @ param s state
*/
void set_state ( VmState s )
{
state = s ;
} ;
/**
* Sets VM LCM state
* @ param s state
*/
void set_state ( LcmState s )
{
lcm_state = s ;
} ;
2009-03-06 15:10:15 +03:00
2012-04-26 21:06:49 +04:00
/**
* Sets the re - scheduling flag
* @ param set or unset the re - schedule flag
*/
void set_resched ( bool do_sched )
{
if ( do_sched = = true )
{
resched = 1 ;
}
else
{
resched = 0 ;
}
} ;
2008-06-17 20:27:32 +04:00
// ------------------------------------------------------------------------
// Timers
2009-03-06 15:10:15 +03:00
// ------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* 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 ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Sets time of last information polling .
* @ param poll time in epoch , normally time ( 0 )
*/
void set_last_poll ( time_t poll )
{
last_poll = poll ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Get the VM physical requirements for the host .
* @ param cpu
* @ param memory
2009-03-06 15:10:15 +03:00
* @ param disk
2008-06-17 20:27:32 +04:00
*/
void get_requirements ( int & cpu , int & memory , int & disk ) ;
2009-03-06 15:10:15 +03:00
2008-11-13 19:21:17 +03:00
// ------------------------------------------------------------------------
2010-06-25 15:24:54 +04:00
// Network Leases & Disk Images
2008-11-13 19:21:17 +03:00
// ------------------------------------------------------------------------
2008-11-14 22:51:33 +03:00
/**
* Get all network leases for this Virtual Machine
* @ return 0 if success
*/
2011-07-08 22:00:27 +04:00
int get_network_leases ( string & error_str ) ;
2009-03-06 15:10:15 +03:00
2008-11-13 19:21:17 +03:00
/**
* Releases all network leases taken by this Virtual Machine
*/
2009-03-06 18:54:57 +03:00
void release_network_leases ( ) ;
2009-03-06 15:10:15 +03:00
2010-06-25 15:24:54 +04:00
/**
* Get all disk images for this Virtual Machine
2011-03-22 20:40:03 +03:00
* @ param error_str Returns the error reason , if any
2010-06-25 15:24:54 +04:00
* @ return 0 if success
*/
2010-08-05 21:28:28 +04:00
int get_disk_images ( string & error_str ) ;
2010-06-25 15:24:54 +04:00
/**
* Releases all disk images taken by this Virtual Machine
*/
void release_disk_images ( ) ;
2009-03-06 18:54:57 +03:00
// ------------------------------------------------------------------------
// Context related functions
// ------------------------------------------------------------------------
2009-03-06 15:10:15 +03:00
/**
2009-03-07 03:56:26 +03:00
* Writes the context file for this VM , and gets the paths to be included
* in the context block device ( CBD )
* @ param files space separated list of paths to be included in the CBD
2009-03-06 15:10:15 +03:00
* @ return 0 if success
*/
2009-03-07 03:56:26 +03:00
int generate_context ( string & files ) ;
2009-03-06 15:10:15 +03:00
2010-07-21 20:06:40 +04:00
// ------------------------------------------------------------------------
2012-03-06 02:49:18 +04:00
// Datastore related functions
2010-07-21 20:06:40 +04:00
// ------------------------------------------------------------------------
/**
* Set the SAVE_AS attribute for the " disk_id " th disk .
* @ param disk_id Index of the disk to save
2012-03-06 02:49:18 +04:00
* @ param source to save the disk ( SAVE_AS_SOURCE )
* @ param img_id ID of the image this disk will be saved to ( SAVE_AS ) .
2010-07-21 20:06:40 +04:00
* @ return 0 if success
*/
2012-03-06 02:49:18 +04:00
int save_disk ( const string & disk_id ,
const string & source ,
int img_id ) ;
/**
* Get the original image id of the disk . It also checks that the disk can
* be saved_as .
* @ param disk_id Index of the disk to save
* @ param error_str describes the error
* @ return - 1 if failure
*/
int get_image_from_disk ( int disk_id , string & error_str ) ;
2010-07-21 20:06:40 +04:00
2011-06-01 14:41:46 +04:00
// ------------------------------------------------------------------------
// Authorization related functions
// ------------------------------------------------------------------------
/**
* Sets an authorization request for a VirtualMachine template based on
* the images and networks used
* @ param uid for template owner
* @ param ar the AuthRequest object
* @ param tmpl the virtual machine template
*/
static void set_auth_request ( int uid ,
AuthRequest & ar ,
VirtualMachineTemplate * tmpl ) ;
2012-06-13 20:42:42 +04:00
// ------------------------------------------------------------------------
// Hotplug related functions
// ------------------------------------------------------------------------
/**
2012-06-15 14:28:20 +04:00
* Collects information about VM DISKS
* @ param num_disks of the VM
* @ param used_targets by the DISKS of the VM
2012-06-13 20:42:42 +04:00
*/
2012-06-15 14:28:20 +04:00
void get_disk_info ( int & num_disks , set < string > & used_targets ) ;
2012-06-13 20:42:42 +04:00
2012-06-15 14:28:20 +04:00
/**
* Generate a DISK attributed to be attached to the VM .
* @ param tmpl Template containing a single DISK vector attribute .
* @ param used_targets targets in use by current DISKS
* @ param num_disks of the VM
* @ param uid of the VM owner
* @ param image_id returns the id of the aquired image
* @ param error_str describes the error
*
* @ return a new vectorattribute with the DISK ( should be freed if not
* added to the template ) , 0 in case of error ;
*/
static VectorAttribute * set_up_attach_disk (
VirtualMachineTemplate * tmpl ,
set < string > & used_targets ,
int num_disks ,
int uid ,
int & image_id ,
string & error_str ) ;
2012-06-13 20:42:42 +04:00
/**
* Returns the disk that is waiting for an attachment action
*
* @ return the disk waiting for an attachment action , or 0
*/
VectorAttribute * get_attach_disk ( ) ;
/**
2012-06-13 21:15:33 +04:00
* Cleans the ATTACH = YES attribute from the disks
2012-06-14 15:02:18 +04:00
*/
2012-06-15 14:28:20 +04:00
void clear_attach_disk ( ) ;
2012-06-14 15:02:18 +04:00
/**
2012-06-15 14:28:20 +04:00
* Deletes the DISK that was in the process of being attached
2012-06-14 15:02:18 +04:00
*
2012-06-15 14:28:20 +04:00
* @ return the DISK or 0 if no disk was deleted
2012-06-13 20:42:42 +04:00
*/
2012-06-15 14:28:20 +04:00
VectorAttribute * delete_attach_disk ( ) ;
/**
* Adds a new disk to the virtual machine template . The disk should be
* generated by the build_attach_disk
* @ param new_disk must be allocated in the heap
*/
2012-06-15 18:28:30 +04:00
void set_attach_disk ( VectorAttribute * new_disk )
2012-06-15 14:28:20 +04:00
{
new_disk - > replace ( " ATTACH " , " YES " ) ;
obj_template - > set ( new_disk ) ;
}
2012-06-13 20:42:42 +04:00
2012-06-15 18:28:30 +04:00
/**
* Sets the attach attribute to the given disk
* @ param disk_id of the DISK
* @ return 0 if the disk_id was found - 1 otherwise
*/
int set_attach_disk ( int disk_id ) ;
2012-06-14 19:45:41 +04:00
/**
* Cleans the ATTACH = YES attribute from the disks
*
* @ return 0 on success , - 1 otherwise
*/
int detach_success ( ) ;
/**
* Cleans the ATTACH = YES attribute from the disks
*
* @ return 0 on success , - 1 otherwise
*/
int detach_failure ( ) ;
2008-06-17 20:27:32 +04:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class VirtualMachinePool ;
// *************************************************************************
// Virtual Machine Attributes
// *************************************************************************
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
// -------------------------------------------------------------------------
// VM Scheduling & Managing Information
2009-03-06 15:10:15 +03:00
// -------------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
/**
* Last time ( in epoch ) that the VM was polled to get its status
*/
time_t last_poll ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
// -------------------------------------------------------------------------
// Virtual Machine Description
// -------------------------------------------------------------------------
/**
* The state of the virtual machine .
*/
VmState state ;
/**
* The state of the virtual machine ( in the Life - cycle Manager ) .
*/
LcmState lcm_state ;
2012-04-26 21:06:49 +04:00
/**
* Marks the VM as to be re - scheduled
*/
int resched ;
2008-06-17 20:27:32 +04:00
/**
* Start time , the VM enter the nebula system ( in epoch )
*/
time_t stime ;
/**
* Exit time , the VM leave the nebula system ( in epoch )
*/
time_t etime ;
/**
* Deployment specific identification string , as returned by the VM driver
*/
string deploy_id ;
/**
2012-05-08 18:18:24 +04:00
* Memory in Kilobytes used by the VM
2008-06-17 20:27:32 +04:00
*/
int memory ;
/**
* CPU usage ( percent )
*/
int cpu ;
/**
2012-05-08 18:18:24 +04:00
* Network usage , transmitted bytes
2008-06-17 20:27:32 +04:00
*/
int net_tx ;
/**
2012-05-08 18:18:24 +04:00
* Network usage , received bytes
2008-06-17 20:27:32 +04:00
*/
int net_rx ;
/**
2008-06-22 05:51:49 +04:00
* History record , for the current host
2008-06-17 20:27:32 +04:00
*/
History * history ;
2008-06-22 05:51:49 +04:00
/**
* History record , for the previous host
*/
History * previous_history ;
2009-03-06 15:10:15 +03:00
2011-06-25 03:29:44 +04:00
/**
* Complete set of history records for the VM
*/
vector < History * > history_records ;
2008-06-17 20:27:32 +04:00
// -------------------------------------------------------------------------
2012-02-25 02:13:22 +04:00
// Logging & Dirs
2008-06-17 20:27:32 +04:00
// -------------------------------------------------------------------------
/**
* Log class for the virtual machine , it writes log messages in
2010-04-11 00:16:47 +04:00
* $ ONE_LOCATION / var / $ VID / vm . log
2009-01-02 17:58:51 +03:00
* or , in case that OpenNebula is installed in root
2010-04-11 00:16:47 +04:00
* / var / log / one / $ VM_ID . log
2008-06-17 20:27:32 +04:00
*/
2012-02-25 02:13:22 +04:00
FileLog * _log ;
2008-06-17 20:27:32 +04:00
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
/**
* Bootstraps the database table ( s ) associated to the VirtualMachine
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * db )
2008-06-17 20:27:32 +04:00
{
2011-10-10 17:14:46 +04:00
int rc ;
2010-04-11 00:16:47 +04:00
ostringstream oss_vm ( VirtualMachine : : db_bootstrap ) ;
2012-05-16 18:09:27 +04:00
ostringstream oss_monit ( VirtualMachine : : monit_db_bootstrap ) ;
2010-04-11 00:16:47 +04:00
ostringstream oss_hist ( History : : db_bootstrap ) ;
2008-06-17 20:27:32 +04:00
2011-10-10 17:14:46 +04:00
rc = db - > exec ( oss_vm ) ;
2012-05-16 18:09:27 +04:00
rc + = db - > exec ( oss_monit ) ;
2011-10-10 17:14:46 +04:00
rc + = db - > exec ( oss_hist ) ;
return rc ;
2008-06-17 20:27:32 +04:00
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2010-04-11 00:16:47 +04:00
* Callback function to unmarshall a VirtualMachine object
* ( VirtualMachine : : select )
2008-06-17 20:27:32 +04:00
* @ param num the number of columns read from the DB
2009-05-22 04:46:52 +04:00
* @ param names the column names
* @ param vaues the column values
2008-06-17 20:27:32 +04:00
* @ return 0 on success
*/
2010-04-11 00:16:47 +04:00
int select_cb ( void * nil , int num , char * * names , char * * values ) ;
2010-06-25 15:24:54 +04:00
2010-04-29 20:11:04 +04:00
/**
2010-06-25 15:24:54 +04:00
* Execute an INSERT or REPLACE Sql query .
2010-04-29 20:11:04 +04:00
* @ param db The SQL DB
2010-06-25 15:24:54 +04:00
* @ param replace Execute an INSERT or a REPLACE
2011-12-19 20:07:32 +04:00
* @ param error_str Returns the error reason , if any
2010-04-29 20:11:04 +04:00
* @ return 0 one success
2011-12-19 20:07:32 +04:00
*/
int insert_replace ( SqlDB * db , bool replace , string & error_str ) ;
2009-07-09 18:34:34 +04:00
2008-06-17 20:27:32 +04:00
/**
* Updates the VM history record
* @ param db pointer to the db
2009-03-06 15:10:15 +03:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2010-04-11 00:16:47 +04:00
int update_history ( SqlDB * db )
2008-06-17 20:27:32 +04:00
{
if ( history ! = 0 )
{
2010-05-03 21:29:08 +04:00
return history - > update ( db ) ;
2008-06-17 20:27:32 +04:00
}
else
return - 1 ;
} ;
/**
2008-06-22 05:51:49 +04:00
* Updates the previous history record
2008-06-17 20:27:32 +04:00
* @ param db pointer to the db
2009-03-06 15:10:15 +03:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2010-04-11 00:16:47 +04:00
int update_previous_history ( SqlDB * db )
2008-06-22 05:51:49 +04:00
{
if ( previous_history ! = 0 )
{
2010-05-03 21:29:08 +04:00
return previous_history - > update ( db ) ;
2008-06-22 05:51:49 +04:00
}
else
return - 1 ;
} ;
2009-03-06 15:10:15 +03:00
2012-05-16 18:09:27 +04:00
/**
* Inserts the last monitoring , and deletes old monitoring entries .
*
* @ param db pointer to the db
* @ return 0 on success
*/
int update_monitoring ( SqlDB * db ) ;
2009-04-02 14:14:54 +04:00
// -------------------------------------------------------------------------
// Attribute Parser
// -------------------------------------------------------------------------
2009-03-06 15:10:15 +03:00
2009-04-02 14:14:54 +04:00
/**
* Mutex to perform just one attribute parse at a time
*/
static pthread_mutex_t lex_mutex ;
/**
2010-03-05 21:17:52 +03:00
* Parse the " CONTEXT " attribute of the template by substituting
* $ VARIABLE , $ VARIABLE [ ATTR ] and $ VARIABLE [ ATTR , ATTR = VALUE ]
2011-03-22 20:40:03 +03:00
* @ param error_str Returns the error reason , if any
2010-03-05 21:17:52 +03:00
* @ return 0 on success
*/
2011-03-22 20:40:03 +03:00
int parse_context ( string & error_str ) ;
2010-03-05 21:17:52 +03:00
/**
* Parse the " REQUIREMENTS " attribute of the template by substituting
* $ VARIABLE , $ VARIABLE [ ATTR ] and $ VARIABLE [ ATTR , ATTR = VALUE ]
2011-03-22 20:40:03 +03:00
* @ param error_str Returns the error reason , if any
2010-03-05 21:17:52 +03:00
* @ return 0 on success
*/
2011-03-22 20:40:03 +03:00
int parse_requirements ( string & error_str ) ;
2010-06-25 16:34:44 +04:00
2012-03-05 19:20:04 +04:00
/**
* Adds automatic placement requirements : Datastore and Cluster
*
* @ param error_str Returns the error reason , if any
* @ return 0 on success
*/
int automatic_requirements ( string & error_str ) ;
2010-03-05 21:17:52 +03:00
/**
* Parse the " GRAPHICS " attribute and generates a default PORT if not
* defined
2010-04-11 00:16:47 +04:00
*/
2010-03-05 21:17:52 +03:00
void parse_graphics ( ) ;
2010-04-11 00:16:47 +04:00
2011-06-25 03:29:44 +04:00
/**
* Function that renders the VM in XML format optinally including
* extended information ( all history records )
* @ param xml the resulting XML string
2012-05-08 17:42:24 +04:00
* @ param n_history Number of history records to include :
* 0 : none
* 1 : the last one
* 2 : all
2011-06-25 03:29:44 +04:00
* @ return a reference to the generated string
*/
2012-05-08 17:42:24 +04:00
string & to_xml_extended ( string & xml , int n_history ) const ;
2011-06-25 03:29:44 +04:00
2008-06-17 20:27:32 +04:00
protected :
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
//**************************************************************************
// Constructor
//**************************************************************************
2009-03-06 15:10:15 +03:00
2011-06-30 13:31:00 +04:00
VirtualMachine ( int id ,
int uid ,
int gid ,
const string & uname ,
const string & gname ,
VirtualMachineTemplate * _vm_template ) ;
2008-06-17 20:27:32 +04:00
virtual ~ VirtualMachine ( ) ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
// *************************************************************************
// DataBase implementation
// *************************************************************************
2012-05-05 05:18:25 +04:00
static const char * table ;
2008-06-17 20:27:32 +04:00
static const char * db_names ;
static const char * db_bootstrap ;
2009-03-06 15:10:15 +03:00
2012-05-16 18:09:27 +04:00
static const char * monit_table ;
static const char * monit_db_names ;
static const char * monit_db_bootstrap ;
2008-06-17 20:27:32 +04:00
/**
* Reads the Virtual Machine ( identified with its OID ) from the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2010-04-11 00:16:47 +04:00
int select ( SqlDB * db ) ;
2008-06-17 20:27:32 +04:00
/**
* Writes the Virtual Machine and its associated template in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2011-03-08 19:55:14 +03:00
int insert ( SqlDB * db , string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Writes / updates the Virtual Machine data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2011-03-08 19:55:14 +03:00
int update ( SqlDB * db )
{
2011-12-19 20:07:32 +04:00
string error_str ;
return insert_replace ( db , true , error_str ) ;
2011-03-08 19:55:14 +03:00
}
2010-04-11 00:16:47 +04:00
2008-06-17 20:27:32 +04:00
/**
2010-05-25 20:19:22 +04:00
* Deletes a VM from the database and all its associated information
2008-06-17 20:27:32 +04:00
* @ param db pointer to the db
2010-05-25 20:19:22 +04:00
* @ return - 1
2008-06-17 20:27:32 +04:00
*/
2011-03-08 19:55:14 +03:00
int drop ( SqlDB * db )
2009-03-06 15:10:15 +03:00
{
2010-05-25 20:19:22 +04:00
NebulaLog : : log ( " ONE " , Log : : ERROR , " VM Drop not implemented! " ) ;
return - 1 ;
2008-06-17 20:27:32 +04:00
}
} ;
# endif /*VIRTUAL_MACHINE_H_*/