2010-05-20 18:58:17 +02:00
/* ------------------------------------------------------------------------ */
2023-01-09 12:23:19 +01:00
/* Copyright 2002-2023, OpenNebula Project, OpenNebula Systems */
2010-05-20 18:58:17 +02: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 IMAGE_H_
# define IMAGE_H_
# include "PoolSQL.h"
# include "ImageTemplate.h"
2012-10-25 01:32:35 +02:00
# include "ObjectCollection.h"
2015-05-30 13:33:06 +02:00
# include "Snapshots.h"
2022-11-06 22:54:17 +01:00
# include "BackupIncrements.h"
2010-05-20 18:58:17 +02:00
2016-12-11 21:05:07 +01:00
class VirtualMachineDisk ;
2010-05-20 18:58:17 +02:00
/**
* The Image class .
*/
class Image : public PoolObjectSQL
{
public :
/**
* Type of Images
*/
2010-06-24 16:12:45 +02:00
enum ImageType
{
OS = 0 , /** < Base OS image */
CDROM = 1 , /** < An ISO9660 image */
2012-11-16 11:29:52 +01:00
DATABLOCK = 2 , /** < User persistent data device */
2012-12-05 16:48:56 +01:00
KERNEL = 3 , /** < Kernel files */
RAMDISK = 4 , /** < Initrd files */
F #5516: New backup interface for OpenNebula
co-authored-by: Frederick Borges <fborges@opennebula.io>
co-authored-by: Neal Hansen <nhansen@opennebula.io>
co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.io>
co-authored-by: Pavel Czerný <pczerny@opennebula.systems>
BACKUP INTERFACE
=================
* Backups are exposed through a a special Datastore (BACKUP_DS) and
Image (BACKUP) types. These new types can only be used for backup'ing
up VMs. This approach allows to:
- Implement tier based backup policies (backups made on different
locations).
- Leverage access control and quota systems
- Support differnt storage and backup technologies
* Backup interface for the VMs:
- VM configures backups with BACKUP_CONFIG. This attribute can be set
in the VM template or updated with updateconf API call. It can include:
+ BACKUP_VOLATILE: To backup or not volatile disks
+ FS_FREEZE: How the FS is freeze for running VMs (qemu-agent,
suspend or none). When possible backups are crash consistent.
+ KEEP_LAST: keep only a given number of backups.
- Backups are initiated by the one.vm.backup API call that requires
the target Datastore to perform the backup (one-shot). This is
exposed by the onevm backup command.
- Backups can be periodic through scheduled actions.
- Backup configuration is updated with one.vm.updateconf API call.
* Restore interface:
- Restores are initiated by the one.image.restore API call. This is
exposed by oneimage restore command.
- Restore include configurable options for the VM template
+ NO_IP: to not preserve IP addresses (but keep the NICs and network
mapping)
+ NO_NIC: to not preserve network mappings
- Other template attributes:
+ Clean PCI devices, including network configuration in case of TYPE=NIC
attributes. By default it removes SHORT_ADDRESS and leave the "auto"
selection attributes.
+ Clean NUMA_NODE, removes node id and cpu sets. It keeps the NUMA node
- It is possible to restore single files stored in the repository by
using the backup specific URL.
* Sunstone (Ruby version) has been updated to expose this feautres.
BACKUP DRIVERS & IMPLEMENTATION
===============================
* Backup operation is implemented by a combination of 3 driver operations:
- VMM. New (internal oned <-> one_vmm_exec.rb) to orchestrate
backups for RUNNING VMs.
- TM. This commit introduces 2 new operations (and their
corresponding _live variants):
+ pre_backup(_live): Prepares the disks to be back'ed up in the
repository. It is specific to the driver: (i) ceph uses the export
operation; (ii) qcow2/raw uses snapshot-create-as and fs_freeze as
needed.
+ post_backup(_live): Performs cleanning operations, i.e. KVM
snapshots or tmp dirs.
- DATASTORE. Each backup technology is represented by its
corresponfing driver, that needs to implement:
+ backup: it takes the VM disks in file (qcow2) format and stores it
the backup repository.
+ restore: it takes a backup image and restores the associated disks
and VM template.
+ monitor: to gather available space in the repository
+ rm: to remove existing backups
+ stat: to return the "restored" size of a disk stored in a backup
+ downloader pseudo-URL handler: in the form
<backup_proto>://<driver_snapshot_id>/<disk filename>
BACKUP MANAGEMENT
=================
Backup actions may potentially take some time, leaving some vmm_exec threads in
use for a long time, stucking other vmm operations. Backups are planned
by the scheduler through the sched action interface.
Two attributes has been added to sched.conf:
* MAX_BACKUPS max active backup operations in the cloud. No more
backups will be started beyond this limit.
* MAX_BACKUPS_HOST max number of backups per host
* Fix onevm CLI to properly show and manage schedule actions. --schedule
supports now, as well as relative times +<seconds_from_stime>
onvm backup --schedule now -d 100 63
* Backup is added as VM_ADMIN_ACTIONS in oned.conf. Regular users needs
to use the batch interface or request specific permissions
Internal restructure of Scheduler:
- All sched_actions interface is now in SchedActionsXML class and files.
This class uses references to VM XML, and MUST be used in the same
lifetime scope.
- XMLRPC API calls for sched actions has been moved to ScheduledActionXML.cc as
static functions.
- VirtualMachineActionPool includes counters for active backups (total
and per host).
SUPPORTED PLATFORMS
====================
* hypervisor: KVM
* TM: qcow2/shared/ssh, ceph
* backup: restic, rsync
Notes on Ceph
* Ceph backups are performed in the following steps:
1. A snapshot of each disk is taken (group snapshots cannot be used as
it seems we cannot export the disks afterwards)
2. Disks are export to a file
3. File is converted to qcow2 format
4. Disk files are upload to the backup repo
TODO:
* Confirm crash consistent snapshots cannot be used in Ceph
TODO:
* Check if using VM dir instead of full path is better to accomodate
DS migrations i.e.:
- Current path: /var/lib/one/datastores/100/53/backup/disk.0
- Proposal: 53/backup/disk.0
RESTIC DRIVER
=============
Developed together with this feature is part of the EE edtion.
* It supports the SFTP protocol, the following attributes are
supported:
- RESTIC_SFTP_SERVER
- RESTIC_SFTP_USER: only if different from oneadmin
- RESTIC_PASSWORD
- RESTIC_IONICE: Run restic under a given ionice priority (class 2)
- RESTIC_NICE: Run restic under a given nice
- RESTIC_BWLIMIT: Limit restic upload/download BW
- RESTIC_COMPRESSION: Restic 0.14 implements compression (three modes:
off, auto, max). This requires repositories version 2. By default,
auto is used (average compression without to much CPU usage)
- RESTIC_CONNECTIONS: Sets the number of concurrent connections to a
backend (5 by default). For high-latency backends this number can be
increased.
* downloader URL: restic://<datastore_id>/<snapshot_id>/<file_name>
snapshot_id is the restic snapshot hash. To recover single disk images
from a backup. This URLs support:
- RESTIC_CONNECTIONS
- RESTIC_BWLIMIT
- RESTIC_IONICE
- RESTIC_NICE
These options needs to be defined in the associated datastore.
RSYNC DRIVER
=============
A rsync driver is included as part of the CE distribution. It uses the
rsync tool to store backups in a remote server through SSH:
* The following attributes are supported to configure the backup
datastore:
- RSYNC_HOST
- RSYNC_USER
- RSYNC_ARGS: Arguments to perform the rsync operatin (-aS by default)
* downloader URL: rsync://<ds_id>/<vmid>/<hash>/<file> can be used to recover
single files from an existing backup. (RSYNC_HOST and RSYN_USER needs
to be set in ds_id
EMULATOR_CPUS
=============
This commit includes a non related backup feature:
* Add EMULATOR_CPUS (KVM). This host (or cluster attribute) defines the
CPU IDs where the emulator threads will be pinned. If this value is
not defined the allocated CPU wll be used when using a PIN policy.
(cherry picked from commit a9e6a8e000e9a5a2f56f80ce622ad9ffc9fa032b)
F OpenNebula/one#5516: adding rsync backup driver
(cherry picked from commit fb52edf5d009dc02b071063afb97c6519b9e8305)
F OpenNebula/one#5516: update install.sh, add vmid to source, some polish
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 6fc6f8a67e435f7f92d5c40fdc3d1c825ab5581d)
F OpenNebula/one#5516: cleanup
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 12f4333b833f23098142cd4762eb9e6c505e1340)
F OpenNebula/one#5516: update downloader, default args, size check
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 510124ef2780a4e2e8c3d128c9a42945be38a305)
LL
(cherry picked from commit d4fcd134dc293f2b862086936db4d552792539fa)
2022-09-09 11:46:44 +02:00
CONTEXT = 5 , /** < Context files */
BACKUP = 6 , /** < VM Backup reference */
2010-06-24 16:12:45 +02:00
} ;
2012-03-05 23:49:18 +01:00
/**
* Return the string representation of an ImageType
* @ param ob the type
* @ return the string
2012-10-25 01:32:35 +02:00
*/
2020-07-02 22:42:10 +02:00
static std : : string type_to_str ( ImageType ob )
2012-03-05 23:49:18 +01:00
{
switch ( ob )
{
case OS : return " OS " ; break ;
case CDROM : return " CDROM " ; break ;
case DATABLOCK : return " DATABLOCK " ; break ;
2012-12-04 23:19:08 +01:00
case KERNEL : return " KERNEL " ; break ;
case RAMDISK : return " RAMDISK " ; break ;
case CONTEXT : return " CONTEXT " ; break ;
F #5516: New backup interface for OpenNebula
co-authored-by: Frederick Borges <fborges@opennebula.io>
co-authored-by: Neal Hansen <nhansen@opennebula.io>
co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.io>
co-authored-by: Pavel Czerný <pczerny@opennebula.systems>
BACKUP INTERFACE
=================
* Backups are exposed through a a special Datastore (BACKUP_DS) and
Image (BACKUP) types. These new types can only be used for backup'ing
up VMs. This approach allows to:
- Implement tier based backup policies (backups made on different
locations).
- Leverage access control and quota systems
- Support differnt storage and backup technologies
* Backup interface for the VMs:
- VM configures backups with BACKUP_CONFIG. This attribute can be set
in the VM template or updated with updateconf API call. It can include:
+ BACKUP_VOLATILE: To backup or not volatile disks
+ FS_FREEZE: How the FS is freeze for running VMs (qemu-agent,
suspend or none). When possible backups are crash consistent.
+ KEEP_LAST: keep only a given number of backups.
- Backups are initiated by the one.vm.backup API call that requires
the target Datastore to perform the backup (one-shot). This is
exposed by the onevm backup command.
- Backups can be periodic through scheduled actions.
- Backup configuration is updated with one.vm.updateconf API call.
* Restore interface:
- Restores are initiated by the one.image.restore API call. This is
exposed by oneimage restore command.
- Restore include configurable options for the VM template
+ NO_IP: to not preserve IP addresses (but keep the NICs and network
mapping)
+ NO_NIC: to not preserve network mappings
- Other template attributes:
+ Clean PCI devices, including network configuration in case of TYPE=NIC
attributes. By default it removes SHORT_ADDRESS and leave the "auto"
selection attributes.
+ Clean NUMA_NODE, removes node id and cpu sets. It keeps the NUMA node
- It is possible to restore single files stored in the repository by
using the backup specific URL.
* Sunstone (Ruby version) has been updated to expose this feautres.
BACKUP DRIVERS & IMPLEMENTATION
===============================
* Backup operation is implemented by a combination of 3 driver operations:
- VMM. New (internal oned <-> one_vmm_exec.rb) to orchestrate
backups for RUNNING VMs.
- TM. This commit introduces 2 new operations (and their
corresponding _live variants):
+ pre_backup(_live): Prepares the disks to be back'ed up in the
repository. It is specific to the driver: (i) ceph uses the export
operation; (ii) qcow2/raw uses snapshot-create-as and fs_freeze as
needed.
+ post_backup(_live): Performs cleanning operations, i.e. KVM
snapshots or tmp dirs.
- DATASTORE. Each backup technology is represented by its
corresponfing driver, that needs to implement:
+ backup: it takes the VM disks in file (qcow2) format and stores it
the backup repository.
+ restore: it takes a backup image and restores the associated disks
and VM template.
+ monitor: to gather available space in the repository
+ rm: to remove existing backups
+ stat: to return the "restored" size of a disk stored in a backup
+ downloader pseudo-URL handler: in the form
<backup_proto>://<driver_snapshot_id>/<disk filename>
BACKUP MANAGEMENT
=================
Backup actions may potentially take some time, leaving some vmm_exec threads in
use for a long time, stucking other vmm operations. Backups are planned
by the scheduler through the sched action interface.
Two attributes has been added to sched.conf:
* MAX_BACKUPS max active backup operations in the cloud. No more
backups will be started beyond this limit.
* MAX_BACKUPS_HOST max number of backups per host
* Fix onevm CLI to properly show and manage schedule actions. --schedule
supports now, as well as relative times +<seconds_from_stime>
onvm backup --schedule now -d 100 63
* Backup is added as VM_ADMIN_ACTIONS in oned.conf. Regular users needs
to use the batch interface or request specific permissions
Internal restructure of Scheduler:
- All sched_actions interface is now in SchedActionsXML class and files.
This class uses references to VM XML, and MUST be used in the same
lifetime scope.
- XMLRPC API calls for sched actions has been moved to ScheduledActionXML.cc as
static functions.
- VirtualMachineActionPool includes counters for active backups (total
and per host).
SUPPORTED PLATFORMS
====================
* hypervisor: KVM
* TM: qcow2/shared/ssh, ceph
* backup: restic, rsync
Notes on Ceph
* Ceph backups are performed in the following steps:
1. A snapshot of each disk is taken (group snapshots cannot be used as
it seems we cannot export the disks afterwards)
2. Disks are export to a file
3. File is converted to qcow2 format
4. Disk files are upload to the backup repo
TODO:
* Confirm crash consistent snapshots cannot be used in Ceph
TODO:
* Check if using VM dir instead of full path is better to accomodate
DS migrations i.e.:
- Current path: /var/lib/one/datastores/100/53/backup/disk.0
- Proposal: 53/backup/disk.0
RESTIC DRIVER
=============
Developed together with this feature is part of the EE edtion.
* It supports the SFTP protocol, the following attributes are
supported:
- RESTIC_SFTP_SERVER
- RESTIC_SFTP_USER: only if different from oneadmin
- RESTIC_PASSWORD
- RESTIC_IONICE: Run restic under a given ionice priority (class 2)
- RESTIC_NICE: Run restic under a given nice
- RESTIC_BWLIMIT: Limit restic upload/download BW
- RESTIC_COMPRESSION: Restic 0.14 implements compression (three modes:
off, auto, max). This requires repositories version 2. By default,
auto is used (average compression without to much CPU usage)
- RESTIC_CONNECTIONS: Sets the number of concurrent connections to a
backend (5 by default). For high-latency backends this number can be
increased.
* downloader URL: restic://<datastore_id>/<snapshot_id>/<file_name>
snapshot_id is the restic snapshot hash. To recover single disk images
from a backup. This URLs support:
- RESTIC_CONNECTIONS
- RESTIC_BWLIMIT
- RESTIC_IONICE
- RESTIC_NICE
These options needs to be defined in the associated datastore.
RSYNC DRIVER
=============
A rsync driver is included as part of the CE distribution. It uses the
rsync tool to store backups in a remote server through SSH:
* The following attributes are supported to configure the backup
datastore:
- RSYNC_HOST
- RSYNC_USER
- RSYNC_ARGS: Arguments to perform the rsync operatin (-aS by default)
* downloader URL: rsync://<ds_id>/<vmid>/<hash>/<file> can be used to recover
single files from an existing backup. (RSYNC_HOST and RSYN_USER needs
to be set in ds_id
EMULATOR_CPUS
=============
This commit includes a non related backup feature:
* Add EMULATOR_CPUS (KVM). This host (or cluster attribute) defines the
CPU IDs where the emulator threads will be pinned. If this value is
not defined the allocated CPU wll be used when using a PIN policy.
(cherry picked from commit a9e6a8e000e9a5a2f56f80ce622ad9ffc9fa032b)
F OpenNebula/one#5516: adding rsync backup driver
(cherry picked from commit fb52edf5d009dc02b071063afb97c6519b9e8305)
F OpenNebula/one#5516: update install.sh, add vmid to source, some polish
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 6fc6f8a67e435f7f92d5c40fdc3d1c825ab5581d)
F OpenNebula/one#5516: cleanup
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 12f4333b833f23098142cd4762eb9e6c505e1340)
F OpenNebula/one#5516: update downloader, default args, size check
Signed-off-by: Neal Hansen <nhansen@opennebula.io>
(cherry picked from commit 510124ef2780a4e2e8c3d128c9a42945be38a305)
LL
(cherry picked from commit d4fcd134dc293f2b862086936db4d552792539fa)
2022-09-09 11:46:44 +02:00
case BACKUP : return " BACKUP " ; break ;
2012-03-05 23:49:18 +01:00
default : return " " ;
}
} ;
2012-06-01 19:18:49 +02:00
/**
* Return the string representation of an ImageType
* @ param ob the type
* @ return the string
2012-10-25 01:32:35 +02:00
*/
2020-07-02 22:42:10 +02:00
static ImageType str_to_type ( std : : string & str_type ) ;
2012-06-01 19:18:49 +02:00
2012-04-19 11:44:19 +02:00
/**
2012-10-25 01:32:35 +02:00
* Type of Disks ( used by the VMM_MAD ) . Values : BLOCK , CDROM or
2012-04-19 11:44:19 +02:00
* FILE
*/
enum DiskType
{
2015-12-15 11:23:25 +01:00
FILE = 0 , /** < File-based disk */
CD_ROM = 1 , /** < An ISO9660 disk */
BLOCK = 2 , /** < Block-device disk */
RBD = 3 , /** < CEPH RBD disk */
RBD_CDROM = 4 , /** < CEPH RBD CDROM disk */
GLUSTER = 5 , /** < Gluster Block Device */
GLUSTER_CDROM = 6 , /** < Gluster CDROM Device Device */
SHEEPDOG = 7 , /** < Sheepdog Block Device */
2014-11-22 18:09:10 +01:00
SHEEPDOG_CDROM = 8 , /** < Sheepdog CDROM Device Device */
2015-12-15 11:23:25 +01:00
ISCSI = 9 , /** < iSCSI Volume (Devices Datastore) */
NONE = 255 /** < No disk type, error situation */
2012-04-19 11:44:19 +02:00
} ;
/**
* Return the string representation of a DiskType
* @ param ob the type
* @ return the string
2012-10-25 01:32:35 +02:00
*/
2020-07-02 22:42:10 +02:00
static std : : string disk_type_to_str ( DiskType ob )
2012-04-19 11:44:19 +02:00
{
switch ( ob )
{
2015-06-09 23:25:10 +02:00
case FILE : return " FILE " ; break ;
case CD_ROM : return " CDROM " ; break ;
case BLOCK : return " BLOCK " ; break ;
case RBD : return " RBD " ; break ;
case RBD_CDROM : return " RBD_CDROM " ; break ;
case GLUSTER : return " GLUSTER " ; break ;
case GLUSTER_CDROM : return " GLUSTER_CDROM " ; break ;
2015-07-01 15:15:40 -04:00
case SHEEPDOG : return " SHEEPDOG " ; break ;
2015-06-09 23:25:10 +02:00
case SHEEPDOG_CDROM : return " SHEEPDOG_CDROM " ; break ;
2015-12-15 11:23:25 +01:00
case ISCSI : return " ISCSI " ; break ;
2015-06-09 23:25:10 +02:00
default : return " " ;
2012-04-19 11:44:19 +02:00
}
} ;
2013-10-02 16:21:46 +02:00
/**
* Return the string representation of a DiskType
* @ param s_disk_type string representing the DiskTypr
* @ return the DiskType ( defaults to FILE )
*/
2020-07-02 22:42:10 +02:00
static DiskType str_to_disk_type ( std : : string & s_disk_type ) ;
2013-10-02 16:21:46 +02:00
2010-06-24 16:12:45 +02:00
/**
* Image State
*/
enum ImageState
{
INIT = 0 , /** < Initialization state */
2010-07-21 17:50:04 +02:00
READY = 1 , /** < Image ready to use */
USED = 2 , /** < Image in use */
2011-03-22 18:21:09 +01:00
DISABLED = 3 , /** < Image can not be instantiated by a VM */
LOCKED = 4 , /** < FS operation for the Image in process */
2012-06-12 18:59:23 +02:00
ERROR = 5 , /** < Error state the operation FAILED*/
CLONE = 6 , /** < Image is being cloned */
DELETE = 7 , /** < DS is deleting the image */
2016-04-18 17:44:47 +02:00
USED_PERS = 8 , /** < Image is in use and persistent */
LOCKED_USED = 9 , /** < FS operation in progress, VMs waiting */
LOCKED_USED_PERS = 10 /** < FS operation in progress, VMs waiting. Persistent */
2010-06-24 16:12:45 +02:00
} ;
2012-05-25 12:56:51 +02:00
/**
* Returns the string representation of an ImageState
* @ param state The state
* @ return the string representation
*/
2020-07-02 22:42:10 +02:00
static std : : string state_to_str ( ImageState state )
2012-05-25 12:56:51 +02:00
{
2019-09-03 16:31:51 +02:00
switch ( state )
2012-05-25 12:56:51 +02:00
{
2016-04-18 17:44:47 +02:00
case INIT : return " INIT " ; break ;
case READY : return " READY " ; break ;
case USED : return " USED " ; break ;
case DISABLED : return " DISABLED " ; break ;
case LOCKED : return " LOCKED " ; break ;
case ERROR : return " ERROR " ; break ;
case CLONE : return " CLONE " ; break ;
case DELETE : return " DELETE " ; break ;
case USED_PERS : return " USED " ; break ;
case LOCKED_USED : return " LOCKED_USED " ; break ;
case LOCKED_USED_PERS : return " LOCKED_USED " ; break ;
default : return " " ;
2012-05-25 12:56:51 +02:00
}
} ;
2021-02-08 16:10:34 +01:00
static ImageState str_to_state ( std : : string & str_state ) ;
2010-05-20 18:58:17 +02:00
// *************************************************************************
2010-05-28 18:56:35 +02:00
// Image Public Methods
2010-05-20 18:58:17 +02:00
// *************************************************************************
2020-09-10 09:08:29 +02:00
virtual ~ Image ( ) = default ;
2010-05-28 18:56:35 +02:00
/**
* Function to print the Image object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 22:42:10 +02:00
std : : string & to_xml ( std : : string & xml ) const override ;
2010-05-20 18:58:17 +02:00
/**
2011-02-25 19:02:29 +01:00
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
2010-05-20 18:58:17 +02:00
*/
2020-07-02 22:42:10 +02:00
int from_xml ( const std : : string & xml_str ) override ;
2010-06-24 16:12:45 +02:00
2010-08-03 19:22:13 +02:00
/**
* Returns true if the image is persistent
* @ return true if the image is persistent
*/
2015-06-09 23:25:10 +02:00
bool is_persistent ( ) const
2010-08-03 19:22:13 +02:00
{
return ( persistent_img = = 1 ) ;
} ;
2010-05-28 18:56:35 +02:00
2019-01-10 10:04:43 +01:00
/**
* @ return true if the image is in a locked state
*/
bool is_locked ( ) const
{
return state = = LOCKED | | state = = LOCKED_USED | | state = = LOCKED_USED_PERS ;
} ;
2017-03-14 17:34:53 +01:00
/**
* Check the PERSISTENT attribute in an image Template , if not set the
* DEFAULT_IMAGE_PERSISTENT and DEFAULT_IMAGE_PERSISTENT_NEW are check in
* user / group / oned . conf to set the attribute in the image .
* @ param image_template
* @ param uid of the user making the request
* @ param gid of the group of the user making the request
* @ param is_allocate true for one . image . allocate API Calls
* @ return true if the image is set to persistent , false otherwise
*/
static bool test_set_persistent ( Template * image_template , int uid , int gid ,
bool is_allocate ) ;
2011-03-24 16:37:15 +01:00
/**
* Returns the source path of the image
* @ return source of image
*/
2020-07-02 22:42:10 +02:00
const std : : string & get_source ( ) const
2011-03-24 16:37:15 +01:00
{
return source ;
}
2011-10-11 16:05:32 +02:00
/**
* Returns the original path of the image
* @ return path of image
*/
2020-07-02 22:42:10 +02:00
const std : : string & get_path ( ) const
2011-10-11 16:05:32 +02:00
{
return path ;
}
/**
2020-09-25 12:08:42 +02:00
* Returns the format of the image ( defined for datablocks )
* @ return format
2011-10-11 16:05:32 +02:00
*/
2020-09-25 12:08:42 +02:00
const std : : string & get_format ( ) const
2011-10-11 16:05:32 +02:00
{
2020-09-25 12:08:42 +02:00
return format ;
}
/**
* Sets the format of the image
*/
void set_format ( const std : : string & _format )
{
format = _format ;
2011-10-11 16:05:32 +02:00
}
/**
2012-10-25 01:32:35 +02:00
* Returns the size of the image
2013-10-17 12:35:19 +02:00
* @ return size in MB
2011-10-11 16:05:32 +02:00
*/
2013-10-17 12:35:19 +02:00
long long get_size ( ) const
2011-10-11 16:05:32 +02:00
{
return size_mb ;
}
2015-06-02 23:42:19 +02:00
2011-05-07 02:49:07 +02:00
/**
2011-08-31 16:21:18 +02:00
* Sets the source path of the image
2011-05-07 02:49:07 +02:00
*/
2020-07-02 22:42:10 +02:00
void set_source ( const std : : string & _source )
2011-05-07 02:49:07 +02:00
{
source = _source ;
}
2011-08-31 16:21:18 +02:00
/**
* Sets the size for the image
*/
2013-10-17 12:35:19 +02:00
void set_size ( long long _size_mb )
2011-08-31 16:21:18 +02:00
{
size_mb = _size_mb ;
}
2011-03-24 18:27:19 +01:00
/**
* Returns the type of the image
* @ return type
*/
2012-06-12 18:59:23 +02:00
ImageType get_type ( ) const
2011-03-24 18:27:19 +01:00
{
return type ;
}
2010-05-25 18:19:22 +02:00
/**
2011-03-22 18:21:09 +01:00
* Returns the image state
* @ return state of image
2010-06-24 16:12:45 +02:00
*/
2012-05-25 12:56:51 +02:00
ImageState get_state ( ) const
2010-05-25 18:19:22 +02:00
{
2011-03-22 18:21:09 +01:00
return state ;
2010-05-25 18:19:22 +02:00
}
2010-05-20 18:58:17 +02:00
/**
2011-03-22 18:21:09 +01:00
* Sets the image state
* @ param state of image
2010-05-21 19:02:08 +02:00
*/
2016-04-18 17:44:47 +02:00
void set_state ( ImageState _state ) ;
2021-02-08 16:10:34 +01:00
/**
* Sets the previous state
*/
void set_prev_state ( )
{
prev_state = state ;
}
/**
* Test if the Image has changed state since last time prev state was set
* @ return true if state changed
*/
bool has_changed_state ( ) const
{
return prev_state ! = state ;
}
2016-04-18 17:44:47 +02:00
/**
* Moves the image from the locked * states to ready , used , used_persistent
*/
void set_state_unlock ( ) ;
2010-05-28 18:56:35 +02:00
2011-03-22 18:21:09 +01:00
/**
2012-06-12 18:59:23 +02:00
* Return the ID of the image we are cloning this one from ( if any )
2011-03-22 18:21:09 +01:00
*/
2012-06-12 18:59:23 +02:00
int get_cloning_id ( ) const
2011-03-22 18:21:09 +01:00
{
2012-06-12 18:59:23 +02:00
return cloning_id ;
2011-03-22 18:21:09 +01:00
}
2010-05-21 19:02:08 +02:00
/**
2012-06-12 18:59:23 +02:00
* Sets the ID of the image we are cloning this one from ( if any )
2010-05-20 18:58:17 +02:00
*/
2012-06-12 18:59:23 +02:00
void set_cloning_id ( int id )
2011-03-22 18:21:09 +01:00
{
2012-06-12 18:59:23 +02:00
cloning_id = id ;
2011-03-22 18:21:09 +01:00
}
2010-05-28 18:56:35 +02:00
2011-03-25 00:11:10 +01:00
/**
2012-06-12 18:59:23 +02:00
* Clear the cloning state of the image
2011-03-25 00:11:10 +01:00
*/
2012-06-12 18:59:23 +02:00
void clear_cloning_id ( )
2011-03-25 00:11:10 +01:00
{
2012-06-12 18:59:23 +02:00
cloning_id = - 1 ;
2012-10-25 01:32:35 +02:00
}
2012-06-12 18:59:23 +02:00
/* ---------------------------------------------------------------------- */
2012-10-19 12:52:57 +02:00
/* Access Image Counters (running vms and cloning operations ) */
2012-06-12 18:59:23 +02:00
/* ---------------------------------------------------------------------- */
2011-03-25 00:11:10 +01:00
2019-09-03 16:31:51 +02:00
int dec_running ( int vm_id )
2012-05-25 12:56:51 +02:00
{
2016-03-01 23:31:31 +01:00
if ( vm_collection . del ( vm_id ) = = 0 )
2012-10-25 01:32:35 +02:00
{
running_vms - - ;
}
return running_vms ;
2012-05-25 12:56:51 +02:00
}
2012-10-25 01:32:35 +02:00
int inc_running ( int vm_id )
2012-05-25 12:56:51 +02:00
{
2016-03-01 23:31:31 +01:00
if ( vm_collection . add ( vm_id ) = = 0 )
2012-10-25 01:32:35 +02:00
{
running_vms + + ;
}
return running_vms ;
2012-05-25 12:56:51 +02:00
}
2012-06-12 18:59:23 +02:00
int get_running ( ) const
2012-05-25 12:56:51 +02:00
{
2011-03-25 00:11:10 +01:00
return running_vms ;
2012-05-25 12:56:51 +02:00
}
2020-07-05 22:01:32 +02:00
const std : : set < int > & get_running_ids ( ) const
2016-04-18 17:44:47 +02:00
{
2020-07-05 22:01:32 +02:00
return vm_collection . get_collection ( ) ;
2016-04-18 17:44:47 +02:00
}
2012-06-12 18:59:23 +02:00
int get_cloning ( ) const
2012-05-25 12:56:51 +02:00
{
2012-06-12 18:59:23 +02:00
return cloning_ops ;
2012-05-25 12:56:51 +02:00
}
2016-03-04 16:31:10 +01:00
int dec_cloning ( PoolObjectSQL : : ObjectType ot , int oid )
2012-05-25 12:56:51 +02:00
{
2016-03-04 16:31:10 +01:00
int rc = - 1 ;
if ( ot = = PoolObjectSQL : : IMAGE )
{
rc = img_clone_collection . del ( oid ) ;
}
else //if (ot == PoolObjectSQL::MARKETPLACEAPP)
{
rc = app_clone_collection . del ( oid ) ;
}
if ( rc = = 0 )
2012-10-31 17:50:16 +01:00
{
cloning_ops - - ;
}
return cloning_ops ;
2012-05-25 12:56:51 +02:00
}
2016-03-04 16:31:10 +01:00
int inc_cloning ( PoolObjectSQL : : ObjectType ot , int oid )
2012-05-25 12:56:51 +02:00
{
2016-03-04 16:31:10 +01:00
int rc = - 1 ;
if ( ot = = PoolObjectSQL : : IMAGE )
{
rc = img_clone_collection . add ( oid ) ;
}
else //if (ot == PoolObjectSQL::MARKETPLACEAPP)
{
rc = app_clone_collection . add ( oid ) ;
}
if ( rc = = 0 )
2012-10-31 17:50:16 +01:00
{
cloning_ops + + ;
}
return cloning_ops ;
2012-05-25 12:56:51 +02:00
}
2010-06-24 16:22:05 +02:00
/**
2011-10-21 18:50:04 +02:00
* Sets the Image type .
*
* @ param _type the new type . It will be transformed to upper case
* @ return 0 on success , - 1 otherwise
2010-06-24 16:22:05 +02:00
*/
2020-07-02 22:42:10 +02:00
int set_type ( std : : string & _type , std : : string & error ) ;
2010-06-24 16:22:05 +02:00
2012-03-05 23:49:18 +01:00
/**
* Check if the image is used for saving_as a current one
* @ return true if the image will be used to save an existing image .
*/
2015-06-09 23:25:10 +02:00
bool is_saving ( )
2013-03-01 19:04:56 +01:00
{
2020-09-15 11:16:00 +02:00
return static_cast < ImageTemplate * > ( obj_template . get ( ) ) - > is_saving ( ) ;
2022-01-18 12:00:17 +01:00
}
void clear_saving ( )
{
static_cast < ImageTemplate * > ( obj_template . get ( ) ) - > clear_saving ( ) ;
2013-03-01 19:04:56 +01:00
}
2010-08-03 19:22:13 +02:00
/**
2010-08-30 18:21:21 +02:00
* Set / Unset an image as persistent
* @ param persistent true to make an image persistent
2011-07-26 19:35:58 +02:00
* @ param error_str Returns the error reason , if any
*
2010-08-05 19:28:28 +02:00
* @ return 0 on success
2010-08-03 19:22:13 +02:00
*/
2020-07-02 22:42:10 +02:00
int persistent ( bool persis , std : : string & error_str )
2010-08-03 19:22:13 +02:00
{
2020-07-02 22:42:10 +02:00
std : : ostringstream oss ;
2012-10-25 01:32:35 +02:00
2015-06-02 20:45:42 +02:00
if ( ( snapshots . size ( ) > 0 ) & & ! persis )
{
error_str = " Image has snapshots. " ;
return - 1 ;
}
2019-09-03 16:31:51 +02:00
switch ( state )
2010-08-03 19:22:13 +02:00
{
2012-10-30 18:37:37 +01:00
case USED :
case CLONE :
case USED_PERS :
2016-04-18 17:44:47 +02:00
case LOCKED_USED :
case LOCKED_USED_PERS :
2015-06-02 20:45:42 +02:00
oss < < " Image cannot be in state " < < state_to_str ( state ) < < " . " ;
error_str = oss . str ( ) ;
return - 1 ;
2012-10-30 18:37:37 +01:00
case INIT :
case READY :
case DISABLED :
case LOCKED :
case ERROR :
case DELETE :
if ( persis = = true )
{
persistent_img = 1 ;
}
else
{
persistent_img = 0 ;
}
break ;
2010-08-03 19:22:13 +02:00
}
2010-08-04 17:42:53 +02:00
2011-07-26 19:35:58 +02:00
return 0 ;
2010-06-24 16:22:05 +02:00
}
2010-05-28 18:56:35 +02:00
/**
2010-05-31 15:08:24 +02:00
* Modifies the given disk attribute adding the following attributes :
* * SOURCE : the file - path .
2012-04-26 16:53:11 +02:00
* * TARGET : will only be set if the Image ' s definition includes it .
*
2010-06-24 18:35:18 +02:00
* @ param disk attribute for the VM template
2012-04-25 16:47:12 +02:00
* @ param img_type will be set to the used image ' s type
2012-04-26 16:53:11 +02:00
* @ param dev_prefix will be set to the defined dev_prefix ,
* or the default one
2013-11-14 11:49:53 +01:00
* @ param inherit_attrs Attributes to be inherited from the image template
* into the disk
2012-04-26 16:53:11 +02:00
*
2010-05-28 18:56:35 +02:00
*/
2020-07-02 22:42:10 +02:00
void disk_attribute ( VirtualMachineDisk * disk ,
ImageType & img_type ,
std : : string & dev_prefix ,
const std : : vector < std : : string > & inherit_attrs ) ;
2011-06-01 23:53:09 +02:00
/**
* Factory method for image templates
*/
2020-09-15 11:16:00 +02:00
std : : unique_ptr < Template > get_new_template ( ) const override
2011-06-01 23:53:09 +02:00
{
2020-09-15 11:16:00 +02:00
return std : : make_unique < ImageTemplate > ( ) ;
2011-06-01 23:53:09 +02:00
}
2012-02-10 19:28:18 +01:00
/**
* Returns the Datastore ID
*/
2012-03-05 23:49:18 +01:00
int get_ds_id ( ) const
2012-02-10 19:28:18 +01:00
{
return ds_id ;
} ;
2012-03-05 23:49:18 +01:00
/**
2013-09-02 12:53:54 +02:00
* Returns the Datastore name
2012-03-05 23:49:18 +01:00
*/
2020-07-02 22:42:10 +02:00
const std : : string & get_ds_name ( ) const
2012-03-05 23:49:18 +01:00
{
return ds_name ;
} ;
2012-05-25 12:56:51 +02:00
2013-09-02 12:53:54 +02:00
/**
* Updates the Datastore name
*/
2020-07-02 22:42:10 +02:00
void set_ds_name ( const std : : string & name )
2013-09-02 12:53:54 +02:00
{
ds_name = name ;
} ;
2012-05-25 12:56:51 +02:00
/**
2012-10-25 01:32:35 +02:00
* Clones this image template including image specific attributes : NAME ,
2020-09-25 12:08:42 +02:00
* TYPE , PATH , FORMAT , SIZE and PERSISTENT
2012-05-25 12:56:51 +02:00
* @ param new_name Value for the NAME attribute
2012-06-12 18:59:23 +02:00
* @ return Pointer to the new tempalte 0 in case of success
2012-05-25 12:56:51 +02:00
*/
2021-02-08 16:10:34 +01:00
std : : unique_ptr < ImageTemplate > clone_template ( const std : : string & nn ) const ;
2012-05-25 12:56:51 +02:00
2015-06-05 03:15:39 +02:00
/* ---------------------------------------------------------------------- */
/* Snapshots functions */
/* ---------------------------------------------------------------------- */
/**
* Return the snapshot list of this image
*/
const Snapshots & get_snapshots ( ) const
{
return snapshots ;
} ;
2015-06-05 16:01:06 +02:00
/**
* Clear all the snapshots in the list
*/
void clear_snapshots ( )
{
snapshots . clear ( ) ;
}
2015-05-30 13:33:06 +02:00
/**
* Set the snapshots for this image
* @ param snapshot list
*/
void set_snapshots ( const Snapshots & s )
{
snapshots = s ;
snapshots . clear_disk_id ( ) ;
2015-06-05 03:15:39 +02:00
} ;
void delete_snapshot ( int snap_id )
{
snapshots . delete_snapshot ( snap_id ) ;
} ;
void revert_snapshot ( int snap_id )
{
2018-12-24 13:58:27 +01:00
snapshots . active_snapshot ( snap_id , true ) ;
2015-06-05 03:15:39 +02:00
} ;
void set_target_snapshot ( int snap_id )
{
target_snapshot = snap_id ;
} ;
2020-07-05 22:01:32 +02:00
int get_target_snapshot ( ) const
2015-06-05 03:15:39 +02:00
{
return target_snapshot ;
} ;
void clear_target_snapshot ( )
{
target_snapshot = - 1 ;
} ;
2015-05-30 13:33:06 +02:00
2022-11-06 22:54:17 +01:00
/* ---------------------------------------------------------------------- */
/* Incremental backups interface */
/* ---------------------------------------------------------------------- */
int add_increment ( std : : string source , long long size , Increment : : Type type )
{
int rc = increments . add_increment ( source , size , type ) ;
if ( rc = = - 1 )
{
return - 1 ;
}
size_mb = increments . total_size ( ) ;
return 0 ;
}
int last_increment_id ( )
{
return increments . last_increment_id ( ) ;
}
2010-05-20 18:58:17 +02:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class ImagePool ;
// -------------------------------------------------------------------------
// Image Description
// -------------------------------------------------------------------------
/**
* Type of the Image
*/
2011-08-31 16:21:18 +02:00
ImageType type ;
2010-06-07 16:21:58 +02:00
2012-04-19 11:44:19 +02:00
/**
* Type for the Disk
*/
DiskType disk_type ;
2010-08-03 19:22:13 +02:00
/**
* Persistency of the Image
*/
2011-08-31 16:21:18 +02:00
int persistent_img ;
2010-06-07 16:21:58 +02:00
2010-05-20 18:58:17 +02:00
/**
* Registration time
*/
2011-08-31 16:21:18 +02:00
time_t regtime ;
2010-06-24 16:12:45 +02:00
2010-05-20 18:58:17 +02:00
/**
* Path to the image
*/
2020-07-02 22:42:10 +02:00
std : : string source ;
2011-08-31 16:21:18 +02:00
2011-10-11 16:05:32 +02:00
/**
* Original Path to the image ( optional if source is given or datablock )
*/
2020-07-02 22:42:10 +02:00
std : : string path ;
2011-10-11 16:05:32 +02:00
/**
2020-09-25 12:08:42 +02:00
* Format of the image file ( e . g qcow2 , raw , . . . )
*/
std : : string format ;
/**
* Filesystem of the image file ( e . g ext4 , xfs , . . . )
2011-10-11 16:05:32 +02:00
*/
2020-09-25 12:08:42 +02:00
std : : string fs ;
2011-10-11 16:05:32 +02:00
2011-08-31 16:21:18 +02:00
/**
* Size of the image in MB
*/
2013-10-17 12:35:19 +02:00
long long size_mb ;
2010-05-26 12:38:21 +02:00
2010-05-21 19:02:08 +02:00
/**
* Image state
*/
2011-08-31 16:21:18 +02:00
ImageState state ;
2010-06-24 16:12:45 +02:00
2021-02-08 16:10:34 +01:00
ImageState prev_state ;
2010-05-21 19:02:08 +02:00
/**
* Number of VMs using the image
*/
2010-06-24 16:12:45 +02:00
int running_vms ;
2010-05-20 18:58:17 +02:00
2012-05-25 12:56:51 +02:00
/**
2016-03-04 16:31:10 +01:00
* Number of pending cloning operations , for both images and apps
2012-05-25 12:56:51 +02:00
*/
int cloning_ops ;
/**
* Indicates if this Image is a clone of another one .
* Once the clone process is complete , it should be set to - 1
*/
2012-06-12 18:59:23 +02:00
int cloning_id ;
2012-05-25 12:56:51 +02:00
2012-02-10 19:28:18 +01:00
/**
* Datastore ID
*/
int ds_id ;
/**
* Datastore name
*/
2020-07-02 22:42:10 +02:00
std : : string ds_name ;
2012-02-10 19:28:18 +01:00
2012-10-25 01:32:35 +02:00
/**
* Stores a collection with the VMs using the image
*/
ObjectCollection vm_collection ;
2012-10-31 17:50:16 +01:00
/**
* Stores a collection with the Images cloning this image
*/
ObjectCollection img_clone_collection ;
2016-03-04 16:31:10 +01:00
/**
* Stores a collection with the Marketplace apps cloning this image
*/
ObjectCollection app_clone_collection ;
2015-05-30 13:33:06 +02:00
/**
* Snapshot list for this image
*/
Snapshots snapshots ;
2022-11-06 22:54:17 +01:00
/**
* List of backup increments ( only relevant for BACKUP images , of type
* incremental )
*/
BackupIncrements increments ;
2015-06-05 03:15:39 +02:00
/**
* ID of the snapshot being processed ( if any )
*/
int target_snapshot ;
2010-05-20 18:58:17 +02:00
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
/**
* Execute an INSERT or REPLACE Sql query .
* @ param db The SQL DB
* @ param replace Execute an INSERT or a REPLACE
2011-12-19 17:07:32 +01:00
* @ param error_str Returns the error reason , if any
2010-05-20 18:58:17 +02:00
* @ return 0 on success
2011-12-19 17:07:32 +01:00
*/
2020-07-02 22:42:10 +02:00
int insert_replace ( SqlDB * db , bool replace , std : : string & error_str ) ;
2010-05-20 18:58:17 +02:00
/**
* Bootstraps the database table ( s ) associated to the Image
2011-10-10 06:14:46 -07:00
* @ return 0 on success
2010-05-20 18:58:17 +02:00
*/
2020-06-29 12:14:00 +02:00
static int bootstrap ( SqlDB * db ) ;
2010-05-20 18:58:17 +02:00
2010-06-30 13:10:07 +02:00
/**
2019-03-01 12:30:24 +01:00
* " Encrypts " the password with SHA256 digest
2010-06-30 13:10:07 +02:00
* @ param password
2019-03-01 12:30:24 +01:00
* @ return sha256 encrypted password
2010-06-30 13:10:07 +02:00
*/
2020-07-02 22:42:10 +02:00
static std : : string sha256_digest ( const std : : string & pass ) ;
2010-06-30 13:10:07 +02:00
2010-05-20 18:58:17 +02:00
protected :
// *************************************************************************
// Constructor
// *************************************************************************
2020-07-02 22:42:10 +02:00
Image ( int uid ,
int gid ,
const std : : string & uname ,
const std : : string & gname ,
int umask ,
2020-09-15 11:16:00 +02:00
std : : unique_ptr < ImageTemplate > img_template ) ;
2010-05-20 18:58:17 +02:00
// *************************************************************************
// DataBase implementation
// *************************************************************************
/**
* Writes the Image in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2020-07-02 22:42:10 +02:00
int insert ( SqlDB * db , std : : string & error_str ) override ;
2010-05-20 18:58:17 +02:00
/**
* Writes / updates the Images data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2019-09-03 16:31:51 +02:00
int update ( SqlDB * db ) override ;
2010-05-20 18:58:17 +02:00
} ;
# endif /*IMAGE_H_*/