2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 #include "lib/misc/lib.h"
17 #include "lib/format_text/archiver.h"
18 #include "lib/format_text/format-text.h"
19 #include "lib/misc/lvm-string.h"
20 #include "lib/misc/lvm-signal.h"
21 #include "lib/cache/lvmcache.h"
22 #include "lib/mm/memlock.h"
23 #include "lib/commands/toolcontext.h"
24 #include "lib/locking/locking.h"
28 struct archive_params {
31 unsigned int keep_days;
32 unsigned int keep_number;
35 struct backup_params {
41 int archive_init(struct cmd_context *cmd, const char *dir,
42 unsigned int keep_days, unsigned int keep_min,
47 if (!(cmd->archive_params = dm_pool_zalloc(cmd->libmem,
48 sizeof(*cmd->archive_params)))) {
49 log_error("archive_params alloc failed");
56 if (!(cmd->archive_params->dir = strdup(dir))) {
57 log_error("Couldn't copy archive directory name.");
61 cmd->archive_params->keep_days = keep_days;
62 cmd->archive_params->keep_number = keep_min;
63 archive_enable(cmd, enabled);
68 void archive_exit(struct cmd_context *cmd)
70 if (!cmd->archive_params)
72 free(cmd->archive_params->dir);
73 memset(cmd->archive_params, 0, sizeof(*cmd->archive_params));
76 void archive_enable(struct cmd_context *cmd, int flag)
78 cmd->archive_params->enabled = flag;
81 static char *_build_desc(struct dm_pool *mem, const char *line, int before)
83 size_t len = strlen(line) + 32;
86 if (!(buffer = dm_pool_alloc(mem, len))) {
87 log_error("Failed to allocate desc.");
91 if (dm_snprintf(buffer, len, "Created %s executing '%s'",
92 before ? "*before*" : "*after*", line) < 0) {
93 log_error("Failed to build desc.");
100 static int _archive(struct volume_group *vg, int compulsory)
104 if (vg_is_archived(vg))
105 return 1; /* VG has been already archived */
107 /* Don't archive orphan VGs. */
108 if (is_orphan_vg(vg->name))
111 if (!vg->cmd->archive_params->enabled || !vg->cmd->archive_params->dir) {
112 vg->status |= ARCHIVED_VG;
117 vg->status |= ARCHIVED_VG;
118 log_verbose("Test mode: Skipping archiving of volume group.");
122 if (!dm_create_dir(vg->cmd->archive_params->dir)) {
128 /* Trap a read-only file system */
129 if ((access(vg->cmd->archive_params->dir, R_OK | W_OK | X_OK) == -1) &&
132 log_error("Cannot archive volume group metadata for %s to read-only filesystem.",
139 log_verbose("Archiving volume group \"%s\" metadata (seqno %u).", vg->name,
142 if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 1)))
145 if (!archive_vg(vg, vg->cmd->archive_params->dir, desc,
146 vg->cmd->archive_params->keep_days,
147 vg->cmd->archive_params->keep_number))
150 vg->status |= ARCHIVED_VG;
155 int archive(struct volume_group *vg)
166 int archive_display(struct cmd_context *cmd, const char *vg_name)
170 r1 = archive_list(cmd, cmd->archive_params->dir, vg_name);
171 r2 = backup_list(cmd, cmd->backup_params->dir, vg_name);
176 int archive_display_file(struct cmd_context *cmd, const char *file)
180 r = archive_list_file(cmd, file);
185 int backup_init(struct cmd_context *cmd, const char *dir,
190 if (!(cmd->backup_params = dm_pool_zalloc(cmd->libmem,
191 sizeof(*cmd->backup_params)))) {
192 log_error("backup_params alloc failed");
199 if (!(cmd->backup_params->dir = strdup(dir))) {
200 log_error("Couldn't copy backup directory name.");
203 backup_enable(cmd, enabled);
208 void backup_exit(struct cmd_context *cmd)
210 if (!cmd->backup_params)
212 free(cmd->backup_params->dir);
213 memset(cmd->backup_params, 0, sizeof(*cmd->backup_params));
216 void backup_enable(struct cmd_context *cmd, int flag)
218 cmd->backup_params->enabled = flag;
221 static int _backup(struct volume_group *vg)
227 if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 0)))
230 if (dm_snprintf(name, sizeof(name), "%s/%s",
231 vg->cmd->backup_params->dir, vg->name) < 0) {
232 log_error("Failed to generate volume group metadata backup "
238 r = backup_to_file(name, desc, vg);
244 int backup_locally(struct volume_group *vg)
246 if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
247 log_warn_suppress(vg->cmd->backup_params->suppress++,
248 "WARNING: This metadata update is NOT backed up.");
253 log_verbose("Test mode: Skipping backup of volume group.");
257 if (!dm_create_dir(vg->cmd->backup_params->dir))
260 /* Trap a read-only file system */
261 if ((access(vg->cmd->backup_params->dir, R_OK | W_OK | X_OK) == -1) &&
263 /* Will take a backup next time when FS is writable */
264 log_debug("Skipping backup of volume group on read-only filesystem.");
269 log_error("Backup of volume group %s metadata failed.",
277 int backup(struct volume_group *vg)
280 /* Unlock memory if possible */
281 memlock_unlock(vg->cmd);
283 /* Don't back up orphan VGs. */
284 if (is_orphan_vg(vg->name))
287 return backup_locally(vg);
290 int backup_remove(struct cmd_context *cmd, const char *vg_name)
294 if (dm_snprintf(path, sizeof(path), "%s/%s",
295 cmd->backup_params->dir, vg_name) < 0) {
296 log_error("Failed to generate backup filename (for removal).");
301 * Let this fail silently.
304 log_sys_debug("unlink", path);
309 struct volume_group *backup_read_vg(struct cmd_context *cmd,
310 const char *vg_name, const char *file)
312 struct volume_group *vg = NULL;
313 struct format_instance *tf;
314 struct format_instance_ctx fic;
315 struct text_context tc = {.path_live = file,
317 .desc = cmd->cmd_line};
318 struct metadata_area *mda;
320 fic.type = FMT_INSTANCE_PRIVATE_MDAS;
321 fic.context.private = &tc;
322 if (!(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, &fic))) {
323 log_error("Couldn't create text format object.");
327 dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
328 if (!(vg = mda->ops->vg_read(cmd, tf, vg_name, mda, NULL, NULL)))
334 set_pv_devices(tf, vg);
337 tf->fmt->ops->destroy_instance(tf);
342 static int _restore_vg_should_write_pv(struct physical_volume *pv, int do_pvcreate)
344 struct lvmcache_info *info;
349 if (!(pv->fmt->features & FMT_PV_FLAGS))
353 log_error("Failed to find device for PV.");
357 if (!(info = lvmcache_info_from_pvid(pv->dev->pvid, pv->dev, 0))) {
358 log_error("Failed to find cached info for PV %s.", pv_dev_name(pv));
363 * We're restoring a VG and if the PV_EXT_USED
364 * flag is not set yet in PV, we need to set it now!
365 * This may happen if we have plain PVs without a VG
366 * and we're restoring former VG from backup on top
369 if (!(lvmcache_ext_flags(info) & PV_EXT_USED))
375 /* ORPHAN and VG locks held before calling this */
376 int backup_restore_vg(struct cmd_context *cmd, struct volume_group *vg,
377 int do_pvcreate, struct pv_create_args *pva)
379 struct dm_list new_pvs;
380 struct pv_list *pvl, *new_pvl;
381 struct physical_volume *existing_pv, *pv;
382 struct dm_list *pvs = &vg->pvs;
383 struct format_instance *fid;
384 struct format_instance_ctx fic;
386 uint32_t tmp_extent_size;
389 * FIXME: Check that the PVs referenced in the backup are
390 * not members of other existing VGs.
393 /* Prepare new PVs if needed. */
395 dm_list_init(&new_pvs);
397 dm_list_iterate_items(pvl, &vg->pvs) {
398 existing_pv = pvl->pv;
400 pva->id = existing_pv->id;
402 pva->pe_start = pv_pe_start(existing_pv);
403 pva->extent_count = pv_pe_count(existing_pv);
404 pva->extent_size = pv_pe_size(existing_pv);
405 /* pe_end = pv_pe_count(existing_pv) * pv_pe_size(existing_pv) + pe_start - 1 */
407 if (!(pv = pv_create(cmd, pv_dev(existing_pv), pva))) {
408 log_error("Failed to setup physical volume \"%s\".",
409 pv_dev_name(existing_pv));
412 pv->vg_name = vg->name;
413 /* both are struct id */
414 memcpy(&pv->vg_id, &vg->id, sizeof(struct id));
416 if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
417 log_error("Failed to allocate PV list item for \"%s\".",
418 pv_dev_name(pvl->pv));
423 dm_list_add(&new_pvs, &new_pvl->list);
425 log_verbose("Set up physical volume for \"%s\" with " FMTu64
426 " available sectors.", pv_dev_name(pv), pv_size(pv));
432 /* Attempt to write out using currently active format */
433 fic.type = FMT_INSTANCE_AUX_MDAS;
434 fic.context.vg_ref.vg_name = vg->name;
435 fic.context.vg_ref.vg_id = NULL;
436 if (!(fid = cmd->fmt->ops->create_instance(cmd->fmt, &fic))) {
437 log_error("Failed to allocate format instance.");
442 log_verbose("Deleting existing metadata for VG %s.", vg->name);
443 if (!vg_remove_mdas(vg)) {
444 cmd->fmt->ops->destroy_instance(fid);
445 log_error("Removal of existing metadata for VG %s failed.", vg->name);
453 * Setting vg->old_name to a blank value will explicitly
454 * disable any attempt to check VG name in existing metadata.
456 if (!(vg->old_name = dm_pool_strdup(vg->vgmem, ""))) {
457 log_error("Failed to duplicate empty name.");
461 /* Add any metadata areas on the PVs */
462 dm_list_iterate_items(pvl, pvs) {
463 if ((should_write_pv = _restore_vg_should_write_pv(pvl->pv, do_pvcreate)) < 0)
466 if (should_write_pv) {
467 if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
468 log_error("Failed to allocate structure for scheduled "
469 "writing of PV '%s'.", pv_dev_name(pvl->pv));
473 new_pvl->pv = pvl->pv;
474 dm_list_add(&vg->pv_write_list, &new_pvl->list);
477 /* Add any metadata areas on the PV. */
478 tmp_extent_size = vg->extent_size;
480 if (!vg->fid->fmt->ops->pv_setup(vg->fid->fmt, pvl->pv, vg)) {
481 vg->extent_size = tmp_extent_size;
482 log_error("Format-specific setup for %s failed.",
483 pv_dev_name(pvl->pv));
486 vg->extent_size = tmp_extent_size;
490 dm_list_iterate_items(pvl, &vg->pv_write_list) {
491 struct device *dev = pv_dev(pvl->pv);
492 const char *pv_name = dev_name(dev);
494 if (!label_remove(dev)) {
495 log_error("Failed to wipe existing label on %s", pv_name);
499 log_verbose("Zeroing start of device %s", pv_name);
501 if (!dev_write_zeros(dev, 0, 2048)) {
502 log_error("%s not wiped: aborting", pv_name);
517 /* ORPHAN and VG locks held before calling this */
518 int backup_restore_from_file(struct cmd_context *cmd, const char *vg_name,
519 const char *file, int force)
521 struct volume_group *vg;
522 int missing_pvs, r = 0;
523 const struct lv_list *lvl;
526 * Read in the volume group from the text file.
528 if (!(vg = backup_read_vg(cmd, vg_name, file)))
531 /* FIXME: Restore support is missing for now */
532 dm_list_iterate_items(lvl, &vg->lvs) {
533 if (lv_is_thin_type(lvl->lv)) {
535 log_error("Consider using option --force to restore "
536 "Volume Group %s with thin volumes.",
540 log_warn("WARNING: Forced restore of Volume Group "
541 "%s with thin volumes.", vg->name);
547 missing_pvs = vg_missing_pv_count(vg);
548 if (missing_pvs == 0)
549 r = backup_restore_vg(cmd, vg, 0, NULL);
551 log_error("Cannot restore Volume Group %s with %i PVs "
552 "marked as missing.", vg->name, missing_pvs);
559 int backup_restore(struct cmd_context *cmd, const char *vg_name, int force)
563 if (dm_snprintf(path, sizeof(path), "%s/%s",
564 cmd->backup_params->dir, vg_name) < 0) {
565 log_error("Failed to generate backup filename (for restore).");
569 return backup_restore_from_file(cmd, vg_name, path, force);
572 int backup_to_file(const char *file, const char *desc, struct volume_group *vg)
575 struct format_instance *tf;
576 struct format_instance_ctx fic;
577 struct text_context tc = {.path_live = file,
580 struct metadata_area *mda;
581 struct cmd_context *cmd;
585 log_verbose("Creating volume group backup \"%s\" (seqno %u).", file, vg->seqno);
587 fic.type = FMT_INSTANCE_PRIVATE_MDAS;
588 fic.context.private = &tc;
589 if (!(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, &fic))) {
590 log_error("Couldn't create backup object.");
594 if (dm_list_empty(&tf->metadata_areas_in_use)) {
595 log_error(INTERNAL_ERROR "No in use metadata areas to write.");
596 tf->fmt->ops->destroy_instance(tf);
600 /* Write and commit the metadata area */
601 dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
602 if (!(r = mda->ops->vg_write(tf, vg, mda))) {
606 if (mda->ops->vg_commit &&
607 !(r = mda->ops->vg_commit(tf, vg, mda))) {
612 tf->fmt->ops->destroy_instance(tf);
617 * Update backup (and archive) if they're out-of-date or don't exist.
619 * This function is not supposed to log_error
620 * when the filesystem with archive/backup dir is read-only.
622 void check_current_backup(struct volume_group *vg)
625 struct volume_group *vg_backup;
628 if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
629 if (!vg->cmd->backup_disabled) {
630 log_debug("Skipping check for current backup, since backup is disabled.");
631 vg->cmd->backup_disabled = 1;
636 if (vg_is_exported(vg))
639 if (dm_snprintf(path, sizeof(path), "%s/%s",
640 vg->cmd->backup_params->dir, vg->name) < 0) {
641 log_warn("WARNING: Failed to generate backup pathname %s/%s.",
642 vg->cmd->backup_params->dir, vg->name);
646 old_suppress = log_suppress(1);
647 /* Up-to-date backup exists? */
648 if ((vg_backup = backup_read_vg(vg->cmd, vg->name, path)) &&
649 (vg->seqno == vg_backup->seqno) &&
650 (id_equal(&vg->id, &vg_backup->id))) {
651 log_suppress(old_suppress);
652 release_vg(vg_backup);
655 log_suppress(old_suppress);
658 if (!_archive(vg_backup, 0))
660 release_vg(vg_backup);
662 if (!_archive(vg, 0))
664 if (!backup_locally(vg))