1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-18 06:03:39 +03:00
one/include/Image.h

813 lines
21 KiB
C
Raw Normal View History

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"
#include "ObjectCollection.h"
#include "Snapshots.h"
F #5516: Incremental backups for qcow2 disk images - Adds new configuration attribute MODE: FULL or INCREMENTAL for BACKUP_CONFIG. FULL backups uses a differen backup image. - INCREMENTAL backup information is together with the backup image. Example: <BACKUP_INCREMENTS> <INCREMENT> <DATE><![CDATA[1667770552]]></DATE> <ID><![CDATA[0]]></ID> <PARENT_ID><![CDATA[-1]]></PARENT_ID> <SIZE><![CDATA[172]]></SIZE> <SOURCE><![CDATA[bb828060]]></SOURCE> <TYPE><![CDATA[FULL]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770604]]></DATE> <ID><![CDATA[1]]></ID> <PARENT_ID><![CDATA[0]]></PARENT_ID> <SIZE><![CDATA[1]]></SIZE> <SOURCE><![CDATA[ca0de5f6]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770700]]></DATE> <ID><![CDATA[2]]></ID> <PARENT_ID><![CDATA[1]]></PARENT_ID> <SIZE><![CDATA[39]]></SIZE> <SOURCE><![CDATA[e9897d6a]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> </BACKUP_INCREMENTS> This information only appears on incremental backups - Internal BACKUP_CONFIG data includes information about the current active backup and the last increment id. - Backup operation includes a new parameter: reset. This "closes" the current active incremental chain and creates a new FULL backup. - Backup drivers store backups with increment index (0 = FULL) e.g. disk.0.0. - Incremental backups are only allowed for VMs using all disks in qcow2 format. - Backup configuration cannot be changed while doing a VM backup. - Downloader strings includes backup chains <inc_id>:<backup_ref>,... - Restic downloader has been updated to support backup chains. Disk images are rebased across increments.
2022-11-06 22:54:17 +01:00
#include "BackupIncrements.h"
2010-05-20 18:58:17 +02:00
class VirtualMachineDisk;
2010-05-20 18:58:17 +02:00
/**
* The Image class.
*/
class Image : public PoolObjectSQL
{
public:
/**
* Type of Images
*/
enum ImageType
{
OS = 0, /** < Base OS image */
CDROM = 1, /** < An ISO9660 image */
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 */
};
2012-03-05 23:49:18 +01:00
/**
* Return the string representation of an ImageType
* @param ob the type
* @return the string
*/
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;
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 "";
}
};
/**
* Return the string representation of an ImageType
* @param ob the type
* @return the string
*/
static ImageType str_to_type(std::string& str_type);
/**
* Type of Disks (used by the VMM_MAD). Values: BLOCK, CDROM or
* FILE
*/
enum DiskType
{
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 */
SHEEPDOG_CDROM = 8, /** < Sheepdog CDROM Device Device */
ISCSI = 9, /** < iSCSI Volume (Devices Datastore) */
NONE = 255 /** < No disk type, error situation */
};
/**
* Return the string representation of a DiskType
* @param ob the type
* @return the string
*/
static std::string disk_type_to_str(DiskType ob)
{
switch (ob)
{
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;
case SHEEPDOG_CDROM: return "SHEEPDOG_CDROM" ; break;
case ISCSI: return "ISCSI" ; break;
default: return "";
}
};
/**
* Return the string representation of a DiskType
* @param s_disk_type string representing the DiskTypr
* @return the DiskType (defaults to FILE)
*/
static DiskType str_to_disk_type(std::string& s_disk_type);
/**
* Image State
*/
enum ImageState
{
INIT = 0, /** < Initialization state */
READY = 1, /** < Image ready to use */
USED = 2, /** < Image in use */
DISABLED = 3, /** < Image can not be instantiated by a VM */
LOCKED = 4, /** < FS operation for the Image in process */
ERROR = 5, /** < Error state the operation FAILED*/
CLONE = 6, /** < Image is being cloned */
DELETE = 7, /** < DS is deleting the image */
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 */
};
/**
* Returns the string representation of an ImageState
* @param state The state
* @return the string representation
*/
static std::string state_to_str(ImageState state)
{
switch (state)
{
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 "";
}
};
static ImageState str_to_state(std::string& str_state);
2010-05-20 18:58:17 +02:00
// *************************************************************************
// Image Public Methods
2010-05-20 18:58:17 +02:00
// *************************************************************************
virtual ~Image() = default;
/**
* 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
*/
std::string& to_xml(std::string& xml) const override;
2010-05-20 18:58:17 +02: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
*/
int from_xml(const std::string &xml_str) override;
2010-08-03 19:22:13 +02:00
/**
* Returns true if the image is persistent
* @return true if the image is persistent
*/
bool is_persistent() const
2010-08-03 19:22:13 +02:00
{
return (persistent_img == 1);
};
/**
* @return true if the image is in a locked state
*/
bool is_locked() const
{
return state == LOCKED || state == LOCKED_USED || state == LOCKED_USED_PERS;
};
/**
* 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);
/**
* Returns the source path of the image
* @return source of image
*/
const std::string& get_source() const
{
return source;
}
/**
* Returns the original path of the image
* @return path of image
*/
const std::string& get_path() const
{
return path;
}
/**
* Returns the format of the image (defined for datablocks)
* @return format
*/
const std::string& get_format() const
{
return format;
}
/**
* Sets the format of the image
*/
void set_format(const std::string& _format)
{
format = _format;
}
/**
* Returns the size of the image
* @return size in MB
*/
long long get_size() const
{
return size_mb;
}
/**
* Sets the source path of the image
*/
void set_source(const std::string& _source)
{
source = _source;
}
/**
* Sets the size for the image
*/
void set_size(long long _size_mb)
{
size_mb = _size_mb;
}
/**
* Returns the type of the image
* @return type
*/
ImageType get_type() const
{
return type;
}
2010-05-25 18:19:22 +02:00
/**
* Returns the image state
* @return state of image
*/
ImageState get_state() const
2010-05-25 18:19:22 +02:00
{
return state;
2010-05-25 18:19:22 +02:00
}
2010-05-20 18:58:17 +02:00
/**
* Sets the image state
* @param state of image
*/
void set_state(ImageState _state);
/**
* 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;
}
/**
* Moves the image from the locked* states to ready, used, used_persistent
*/
void set_state_unlock();
/**
* Return the ID of the image we are cloning this one from (if any)
*/
int get_cloning_id() const
{
return cloning_id;
}
/**
* Sets the ID of the image we are cloning this one from (if any)
2010-05-20 18:58:17 +02:00
*/
void set_cloning_id(int id)
{
cloning_id = id;
}
/**
* Clear the cloning state of the image
*/
void clear_cloning_id()
{
cloning_id = -1;
}
/* ---------------------------------------------------------------------- */
/* Access Image Counters (running vms and cloning operations ) */
/* ---------------------------------------------------------------------- */
int dec_running(int vm_id)
{
if ( vm_collection.del(vm_id) == 0 )
{
running_vms--;
}
return running_vms;
}
int inc_running(int vm_id)
{
if ( vm_collection.add(vm_id) == 0 )
{
running_vms++;
}
return running_vms;
}
int get_running() const
{
return running_vms;
}
const std::set<int>& get_running_ids() const
{
return vm_collection.get_collection();
}
int get_cloning() const
{
return cloning_ops;
}
int dec_cloning(PoolObjectSQL::ObjectType ot, int oid)
{
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 )
{
cloning_ops--;
}
return cloning_ops;
}
int inc_cloning(PoolObjectSQL::ObjectType ot, int oid)
{
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 )
{
cloning_ops++;
}
return cloning_ops;
}
/**
* Sets the Image type.
*
* @param _type the new type. It will be transformed to upper case
* @return 0 on success, -1 otherwise
*/
int set_type(std::string& _type, std::string& error);
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.
*/
bool is_saving()
{
return static_cast<ImageTemplate*>(obj_template.get())->is_saving();
}
void clear_saving()
{
static_cast<ImageTemplate*>(obj_template.get())->clear_saving();
}
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
* @param error_str Returns the error reason, if any
*
* @return 0 on success
2010-08-03 19:22:13 +02:00
*/
int persistent(bool persis, std::string& error_str)
2010-08-03 19:22:13 +02:00
{
std::ostringstream oss;
if ((snapshots.size() > 0) && !persis)
{
error_str = "Image has snapshots.";
return -1;
}
switch (state)
2010-08-03 19:22:13 +02:00
{
case USED:
case CLONE:
case USED_PERS:
case LOCKED_USED:
case LOCKED_USED_PERS:
oss << "Image cannot be in state " << state_to_str(state) <<".";
error_str = oss.str();
return -1;
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
}
return 0;
}
/**
* Modifies the given disk attribute adding the following attributes:
* * SOURCE: the file-path.
* * TARGET: will only be set if the Image's definition includes it.
*
* @param disk attribute for the VM template
* @param img_type will be set to the used image's type
* @param dev_prefix will be set to the defined dev_prefix,
* or the default one
* @param inherit_attrs Attributes to be inherited from the image template
* into the disk
*
*/
void disk_attribute(VirtualMachineDisk * disk,
ImageType& img_type,
std::string& dev_prefix,
const std::vector<std::string>& inherit_attrs);
/**
* Factory method for image templates
*/
std::unique_ptr<Template> get_new_template() const override
{
return std::make_unique<ImageTemplate>();
}
/**
* Returns the Datastore ID
*/
2012-03-05 23:49:18 +01:00
int get_ds_id() const
{
return ds_id;
};
2012-03-05 23:49:18 +01:00
/**
* Returns the Datastore name
2012-03-05 23:49:18 +01:00
*/
const std::string& get_ds_name() const
2012-03-05 23:49:18 +01:00
{
return ds_name;
};
/**
* Updates the Datastore name
*/
void set_ds_name(const std::string& name)
{
ds_name = name;
};
/**
* Clones this image template including image specific attributes: NAME,
* TYPE, PATH, FORMAT, SIZE and PERSISTENT
* @param new_name Value for the NAME attribute
* @return Pointer to the new tempalte 0 in case of success
*/
std::unique_ptr<ImageTemplate> clone_template(const std::string& nn) const;
/* ---------------------------------------------------------------------- */
/* Snapshots functions */
/* ---------------------------------------------------------------------- */
/**
* Return the snapshot list of this image
*/
const Snapshots& get_snapshots() const
{
return snapshots;
};
/**
* Clear all the snapshots in the list
*/
void clear_snapshots()
{
snapshots.clear();
}
/**
* Set the snapshots for this image
* @param snapshot list
*/
void set_snapshots(const Snapshots& s)
{
snapshots = s;
snapshots.clear_disk_id();
};
void delete_snapshot(int snap_id)
{
snapshots.delete_snapshot(snap_id);
};
void revert_snapshot(int snap_id)
{
snapshots.active_snapshot(snap_id, true);
};
void set_target_snapshot(int snap_id)
{
target_snapshot = snap_id;
};
int get_target_snapshot() const
{
return target_snapshot;
};
void clear_target_snapshot()
{
target_snapshot = -1;
};
F #5516: Incremental backups for qcow2 disk images - Adds new configuration attribute MODE: FULL or INCREMENTAL for BACKUP_CONFIG. FULL backups uses a differen backup image. - INCREMENTAL backup information is together with the backup image. Example: <BACKUP_INCREMENTS> <INCREMENT> <DATE><![CDATA[1667770552]]></DATE> <ID><![CDATA[0]]></ID> <PARENT_ID><![CDATA[-1]]></PARENT_ID> <SIZE><![CDATA[172]]></SIZE> <SOURCE><![CDATA[bb828060]]></SOURCE> <TYPE><![CDATA[FULL]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770604]]></DATE> <ID><![CDATA[1]]></ID> <PARENT_ID><![CDATA[0]]></PARENT_ID> <SIZE><![CDATA[1]]></SIZE> <SOURCE><![CDATA[ca0de5f6]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770700]]></DATE> <ID><![CDATA[2]]></ID> <PARENT_ID><![CDATA[1]]></PARENT_ID> <SIZE><![CDATA[39]]></SIZE> <SOURCE><![CDATA[e9897d6a]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> </BACKUP_INCREMENTS> This information only appears on incremental backups - Internal BACKUP_CONFIG data includes information about the current active backup and the last increment id. - Backup operation includes a new parameter: reset. This "closes" the current active incremental chain and creates a new FULL backup. - Backup drivers store backups with increment index (0 = FULL) e.g. disk.0.0. - Incremental backups are only allowed for VMs using all disks in qcow2 format. - Backup configuration cannot be changed while doing a VM backup. - Downloader strings includes backup chains <inc_id>:<backup_ref>,... - Restic downloader has been updated to support backup chains. Disk images are rebased across increments.
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
*/
ImageType type;
/**
* Type for the Disk
*/
DiskType disk_type;
2010-08-03 19:22:13 +02:00
/**
* Persistency of the Image
*/
int persistent_img;
2010-05-20 18:58:17 +02:00
/**
* Registration time
*/
time_t regtime;
2010-05-20 18:58:17 +02:00
/**
* Path to the image
*/
std::string source;
/**
* Original Path to the image (optional if source is given or datablock)
*/
std::string path;
/**
* Format of the image file (e.g qcow2, raw, ...)
*/
std::string format;
/**
* Filesystem of the image file (e.g ext4, xfs, ...)
*/
std::string fs;
/**
* Size of the image in MB
*/
long long size_mb;
/**
* Image state
*/
ImageState state;
ImageState prev_state;
/**
* Number of VMs using the image
*/
int running_vms;
2010-05-20 18:58:17 +02:00
/**
* Number of pending cloning operations, for both images and apps
*/
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
*/
int cloning_id;
/**
* Datastore ID
*/
int ds_id;
/**
* Datastore name
*/
std::string ds_name;
/**
* Stores a collection with the VMs using the image
*/
ObjectCollection vm_collection;
/**
* Stores a collection with the Images cloning this image
*/
ObjectCollection img_clone_collection;
/**
* Stores a collection with the Marketplace apps cloning this image
*/
ObjectCollection app_clone_collection;
/**
* Snapshot list for this image
*/
Snapshots snapshots;
F #5516: Incremental backups for qcow2 disk images - Adds new configuration attribute MODE: FULL or INCREMENTAL for BACKUP_CONFIG. FULL backups uses a differen backup image. - INCREMENTAL backup information is together with the backup image. Example: <BACKUP_INCREMENTS> <INCREMENT> <DATE><![CDATA[1667770552]]></DATE> <ID><![CDATA[0]]></ID> <PARENT_ID><![CDATA[-1]]></PARENT_ID> <SIZE><![CDATA[172]]></SIZE> <SOURCE><![CDATA[bb828060]]></SOURCE> <TYPE><![CDATA[FULL]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770604]]></DATE> <ID><![CDATA[1]]></ID> <PARENT_ID><![CDATA[0]]></PARENT_ID> <SIZE><![CDATA[1]]></SIZE> <SOURCE><![CDATA[ca0de5f6]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> <INCREMENT> <DATE><![CDATA[1667770700]]></DATE> <ID><![CDATA[2]]></ID> <PARENT_ID><![CDATA[1]]></PARENT_ID> <SIZE><![CDATA[39]]></SIZE> <SOURCE><![CDATA[e9897d6a]]></SOURCE> <TYPE><![CDATA[INCREMENT]]></TYPE> </INCREMENT> </BACKUP_INCREMENTS> This information only appears on incremental backups - Internal BACKUP_CONFIG data includes information about the current active backup and the last increment id. - Backup operation includes a new parameter: reset. This "closes" the current active incremental chain and creates a new FULL backup. - Backup drivers store backups with increment index (0 = FULL) e.g. disk.0.0. - Incremental backups are only allowed for VMs using all disks in qcow2 format. - Backup configuration cannot be changed while doing a VM backup. - Downloader strings includes backup chains <inc_id>:<backup_ref>,... - Restic downloader has been updated to support backup chains. Disk images are rebased across increments.
2022-11-06 22:54:17 +01:00
/**
* List of backup increments (only relevant for BACKUP images, of type
* incremental)
*/
BackupIncrements increments;
/**
* 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
* @param error_str Returns the error reason, if any
2010-05-20 18:58:17 +02:00
* @return 0 on success
*/
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
* @return 0 on success
2010-05-20 18:58:17 +02:00
*/
static int bootstrap(SqlDB * db);
2010-05-20 18:58:17 +02:00
/**
* "Encrypts" the password with SHA256 digest
* @param password
* @return sha256 encrypted password
*/
static std::string sha256_digest(const std::string& pass);
2010-05-20 18:58:17 +02:00
protected:
// *************************************************************************
// Constructor
// *************************************************************************
Image(int uid,
int gid,
const std::string& uname,
const std::string& gname,
int umask,
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
*/
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
*/
int update(SqlDB *db) override;
2010-05-20 18:58:17 +02:00
};
#endif /*IMAGE_H_*/