diff --git a/include/Backups.h b/include/Backups.h index e5b03f3f21..579f4bd4af 100644 --- a/include/Backups.h +++ b/include/Backups.h @@ -144,19 +144,40 @@ public: * backup: * - LAST_INCREMENT_ID * - INCREMENTAL_BACKUP_ID + * * @param tmpl Template to parse, the root element must be BACKUP_CONFIG - * @param can_increment VM disks support incremental backup * @param append Only append new values from tmpl - * @param eligible_disk_ids VM disk IDs eligible for backup + * @param disk_ids + * Input: eligible VM disk IDs. + * Output on success: configured selection after append/replace. + * Empty means all eligible disks. + * In append mode, an omitted DISK_IDS retains the existing selection, + * which may include disks that are no longer eligible. The backup + * transfer list is the intersection of that selection and eligible disks. * @param error_str Returns the error reason, if any + * + * Note: Call validate() after parsing to finalize the backup mode and check + * mode-dependent constraints + * * @return 0 success, -1 error */ int parse(Template *tmpl, - bool can_increment, bool append, - const std::vector& eligible_disk_ids, + std::vector& disk_ids, std::string& error_str); + /** + * Finalize the parsed configuration using the selected disks' capabilities. + * Falls back to FULL when incremental backups are unsupported, then checks + * mode-dependent constraints. + * + * @param can_increment Selected VM disks support incremental backup + * @param error_str Returns the error reason, if any + * + * @return 0 success, -1 error + */ + int validate(bool can_increment, std::string& error_str); + /** * @return true if Backup includes configuration attributes */ diff --git a/include/VirtualMachineDisk.h b/include/VirtualMachineDisk.h index 1a7cf6458a..0ddfb4c8a4 100644 --- a/include/VirtualMachineDisk.h +++ b/include/VirtualMachineDisk.h @@ -869,12 +869,12 @@ public: const std::vector& disk_ids); /** - * Returns true if all disks support incremental backups. This requires - * QCOW2 format. + * Returns true if the selected disks support incremental backups. * * @param do_volatile consider volatile disks for incremental backups + * @param disk_ids Selected disk IDs; empty means all disks */ - bool backup_increment(bool do_volatile); + bool backup_increment(bool do_volatile, const std::vector& disk_ids); /** * Returns list of disk IDs, ready for backup diff --git a/src/rm/api/VirtualMachineAPI.cc b/src/rm/api/VirtualMachineAPI.cc index 03eda5d151..aaded2e6c7 100644 --- a/src/rm/api/VirtualMachineAPI.cc +++ b/src/rm/api/VirtualMachineAPI.cc @@ -3643,6 +3643,22 @@ Request::ErrorCode VirtualMachineAPI::backup(int vid, return Request::INTERNAL; } + // Disk selection and snapshots may have changed since configuration. + if (mode == Backups::INCREMENT) + { + vector disk_ids; + + vm->backups().get_disk_ids(disk_ids); + + if (!vm->get_disks().backup_increment(vm->backups().do_volatile(), disk_ids) || + vm->has_snapshots()) + { + att.resp_msg = "VM cannot use backup increment mode, it has snapshots " + "or selected disks do not support incremental backups."; + return Request::ACTION; + } + } + for (auto *disk : vm->get_disks()) { if (disk->is_filesystem()) diff --git a/src/sam/ScheduledActionManager.cc b/src/sam/ScheduledActionManager.cc index 7077b4477f..ee1044f4f7 100644 --- a/src/sam/ScheduledActionManager.cc +++ b/src/sam/ScheduledActionManager.cc @@ -439,25 +439,36 @@ void ScheduledActionManager::backup_jobs() } auto& disks = vm->get_disks(); - bool inc = disks.backup_increment(bck_vol) && !vm->has_snapshots(); string err; Template tmpl; - bj->get_backup_config(tmpl); - + bool inc; vector disk_ids; + Request r("internal call"); + VirtualMachineAPI api(r); + + RequestAttributes ra(AuthRequest::ADMIN, + UserPool::ONEADMIN_ID, + GroupPool::ONEADMIN_ID, + PoolObjectSQL::VM); + Request::ErrorCode ec; + + bj->get_backup_config(tmpl); + vm->backup_disk_ids(bck_vol, disk_ids); - if ( backups.parse(&tmpl, inc, true, disk_ids, err) != 0 ) + if ( backups.parse(&tmpl, true, disk_ids, err) != 0 ) { - bj->set_template_error_message(err); + goto error_backup; + } - bj->add_error(vm_id); + inc = disks.backup_increment(bck_vol, disk_ids) && !vm->has_snapshots(); - NebulaLog::error("SCH", err); - continue; + if ( backups.validate(inc, err) != 0 ) + { + goto error_backup; } vm_pool->update(vm.get()); @@ -467,29 +478,16 @@ void ScheduledActionManager::backup_jobs() /* -------------------------------------------------------------- */ /* Launch backup, notify backup job */ /* -------------------------------------------------------------- */ - Request r("internal call"); - VirtualMachineAPI api(r); - - RequestAttributes ra(AuthRequest::ADMIN, - UserPool::ONEADMIN_ID, - GroupPool::ONEADMIN_ID, - PoolObjectSQL::VM); - NebulaLog::info("SCH", "Backup Job " + to_string(bj_id) + " executing backup for VM " + to_string(vm_id) ); - auto ec = api.backup(vm_id, ds_id, reset, bj_id, ra); + ec = api.backup(vm_id, ds_id, reset, bj_id, ra); if ( ec != Request::SUCCESS) { err = Request::failure_message(ec, ra, "backup"); - bj->set_template_error_message(err); - - bj->add_error(vm_id); - - NebulaLog::error("SCH", err); - continue; + goto error_backup; } bj->backup_started(vm_id); @@ -499,6 +497,13 @@ void ScheduledActionManager::backup_jobs() /* -------------------------------------------------------------- */ ++active_backups; ++host_backups[hid]; + + continue; + + error_backup: + bj->set_template_error_message(err); + bj->add_error(vm_id); + NebulaLog::error("SCH", err); } bj_pool->update(bj.get()); diff --git a/src/vm/Backups.cc b/src/vm/Backups.cc index 33f6314458..a5bc1291b3 100644 --- a/src/vm/Backups.cc +++ b/src/vm/Backups.cc @@ -203,9 +203,8 @@ int Backups::from_xml(const ObjectXML* xml) /* -------------------------------------------------------------------------- */ int Backups::parse(Template *tmpl, - bool can_increment, bool append, - const vector& eligible_ids, + vector& ids, std::string& error_str) { vector> cfg_a; @@ -222,6 +221,8 @@ int Backups::parse(Template *tmpl, if ( tmpl->remove("BACKUP_CONFIG", cfg_a) == 0 ) { + get_disk_ids(ids); + return 0; } @@ -277,7 +278,7 @@ int Backups::parse(Template *tmpl, for (int id : disk_ids) { - if (find(eligible_ids.begin(), eligible_ids.end(), id) == eligible_ids.end()) + if (find(ids.begin(), ids.end(), id) == ids.end()) { error_str = "Invalid DISK_IDS, disk " + to_string(id) + " cannot be backed up"; @@ -297,6 +298,10 @@ int Backups::parse(Template *tmpl, disk_ids_changed = !current_disk_ids.empty(); } + else + { + disk_ids = std::move(current_disk_ids); + } sattr = cfg->vector_value("FS_FREEZE"); @@ -316,53 +321,41 @@ int Backups::parse(Template *tmpl, config.replace("FS_FREEZE", "NONE"); } - if (!can_increment) //Only FULL backups for this VM + sattr = cfg->vector_value("MODE"); + + if (!sattr.empty() || !append) { - config.replace("INCREMENTAL_BACKUP_ID", -1); - config.replace("LAST_INCREMENT_ID", -1); + Mode new_mode = str_to_mode(sattr); - config.replace("MODE", mode_to_str(FULL)); - - config.erase("INCREMENT_MODE"); - } - else - { - sattr = cfg->vector_value("MODE"); - - if (!sattr.empty() || !append) + // Reset incremental backup pointers if mode changed to/from FULL + if (new_mode != INCREMENT || mode() != INCREMENT) { - Mode new_mode = str_to_mode(sattr); + config.replace("INCREMENTAL_BACKUP_ID", -1); + config.replace("LAST_INCREMENT_ID", -1); + } - // Reset incremental backup pointers if mode changed to/from FULL - if (new_mode != INCREMENT || mode() != INCREMENT) + config.replace("MODE", mode_to_str(new_mode)); + + sattr = cfg->vector_value("INCREMENT_MODE"); + + one_util::toupper(sattr); + + if ( !sattr.empty() ) + { + if ((sattr != "CBT") && (sattr != "SNAPSHOT")) { - config.replace("INCREMENTAL_BACKUP_ID", -1); - config.replace("LAST_INCREMENT_ID", -1); + sattr = "CBT"; } - config.replace("MODE", mode_to_str(new_mode)); - - sattr = cfg->vector_value("INCREMENT_MODE"); - - one_util::toupper(sattr); - - if ( !sattr.empty() ) - { - if ((sattr != "CBT") && (sattr != "SNAPSHOT")) - { - sattr = "CBT"; - } - - config.replace("INCREMENT_MODE", sattr); - } - else if (!append) - { - config.replace("INCREMENT_MODE", "CBT"); - } + config.replace("INCREMENT_MODE", sattr); + } + else if (!append) + { + config.replace("INCREMENT_MODE", "CBT"); } } - if (can_increment && (disk_ids_changed || volatile_changed)) + if (disk_ids_changed || volatile_changed) { // The effective disk set defines the incremental backup chain. config.replace("INCREMENTAL_BACKUP_ID", -1); @@ -378,6 +371,30 @@ int Backups::parse(Template *tmpl, config.replace("INTERACTIVE", "NO"); } + ids = std::move(disk_ids); + + return 0; +} + +/* -------------------------------------------------------------------------- */ + +int Backups::validate(bool can_increment, string& error_str) +{ + if (!configured()) + { + return 0; + } + + if (!can_increment) + { + config.replace("INCREMENTAL_BACKUP_ID", -1); + config.replace("LAST_INCREMENT_ID", -1); + config.replace("MODE", mode_to_str(FULL)); + config.erase("INCREMENT_MODE"); + } + + string sattr; + config.get("INCREMENT_MODE", sattr); one_util::toupper(sattr); diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 90fa29dc75..bb83d6602b 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -1188,11 +1188,14 @@ int VirtualMachine::insert(SqlDB * db, string& error_str) backup_disk_ids(do_volatile, disk_ids); - rc = _backups.parse(user_obj_template.get(), - disks.backup_increment(do_volatile), - false, - disk_ids, - error_str); + rc = _backups.parse(user_obj_template.get(), false, disk_ids, error_str); + + if ( rc != 0 ) + { + goto error_backup; + } + + rc = _backups.validate(disks.backup_increment(do_volatile, disk_ids), error_str); if ( rc != 0 ) { @@ -3516,12 +3519,22 @@ int VirtualMachine::updateconf(VirtualMachineTemplate* tmpl, string &err, do_volatile = false; } - bool increment = disks.backup_increment(do_volatile) && - !has_snapshots(); - string smode = backup_conf->vector_value("MODE"); Backups::Mode bmode = Backups::str_to_mode(smode); + vector disk_ids; + + backup_disk_ids(do_volatile, disk_ids); + + if ( _backups.parse(tmpl, append, disk_ids, err) != 0 ) + { + NebulaLog::log("ONE", Log::ERROR, err); + return -1; + } + + bool increment = disks.backup_increment(do_volatile, disk_ids) && + !has_snapshots(); + if (!smode.empty() && !increment && bmode == Backups::INCREMENT) { err = "VM cannot use backup increment mode"; @@ -3539,13 +3552,7 @@ int VirtualMachine::updateconf(VirtualMachineTemplate* tmpl, string &err, return -1; } - backup_conf = nullptr; - - vector disk_ids; - - backup_disk_ids(do_volatile, disk_ids); - - if ( _backups.parse(tmpl, increment, append, disk_ids, err) != 0 ) + if ( _backups.validate(increment, err) != 0 ) { NebulaLog::log("ONE", Log::ERROR, err); return -1; diff --git a/src/vm/VirtualMachineDisk.cc b/src/vm/VirtualMachineDisk.cc index 8388fd292a..060046ace9 100644 --- a/src/vm/VirtualMachineDisk.cc +++ b/src/vm/VirtualMachineDisk.cc @@ -1800,10 +1800,16 @@ long long VirtualMachineDisks::backup_size(Template &ds_quotas, /* -------------------------------------------------------------------------- */ -bool VirtualMachineDisks::backup_increment(bool do_volatile) +bool VirtualMachineDisks::backup_increment(bool do_volatile, + const vector& disk_ids) { - for (const auto disk : *this) + auto can_increment = [do_volatile](const VirtualMachineDisk* disk) { + if (disk == nullptr) + { + return true; + } + string type = disk->vector_value("TYPE"); one_util::toupper(type); @@ -1812,7 +1818,7 @@ bool VirtualMachineDisks::backup_increment(bool do_volatile) ((type == "FS") && !do_volatile) || (type == "FILESYSTEM")) { - continue; + return true; } string tm_mad = disk->vector_value("TM_MAD"); @@ -1838,6 +1844,29 @@ bool VirtualMachineDisks::backup_increment(bool do_volatile) { return false; } + + return true; + }; + + if (disk_ids.empty()) + { + for (const auto disk : *this) + { + if (!can_increment(disk)) + { + return false; + } + } + } + else + { + for (int id : disk_ids) + { + if (!can_increment(get_disk(id))) + { + return false; + } + } } return true;