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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "format-text.h"
20 #include "lvm-string.h"
22 #include "toolcontext.h"
27 struct archive_params {
30 unsigned int keep_days;
31 unsigned int keep_number;
34 struct backup_params {
39 int archive_init(struct cmd_context *cmd, const char *dir,
40 unsigned int keep_days, unsigned int keep_min,
45 if (!(cmd->archive_params = dm_pool_zalloc(cmd->libmem,
46 sizeof(*cmd->archive_params)))) {
47 log_error("archive_params alloc failed");
51 cmd->archive_params->dir = NULL;
56 if (!(cmd->archive_params->dir = dm_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 if (cmd->archive_params->dir)
73 dm_free(cmd->archive_params->dir);
74 memset(cmd->archive_params, 0, sizeof(*cmd->archive_params));
77 void archive_enable(struct cmd_context *cmd, int flag)
79 cmd->archive_params->enabled = flag;
82 static char *_build_desc(struct dm_pool *mem, const char *line, int before)
84 size_t len = strlen(line) + 32;
87 if (!(buffer = dm_pool_zalloc(mem, strlen(line) + 32)))
90 if (snprintf(buffer, len,
91 "Created %s executing '%s'",
92 before ? "*before*" : "*after*", line) < 0)
98 static int __archive(struct volume_group *vg)
102 if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 1)))
105 return archive_vg(vg, vg->cmd->archive_params->dir, desc,
106 vg->cmd->archive_params->keep_days,
107 vg->cmd->archive_params->keep_number);
110 int archive(struct volume_group *vg)
112 if (!vg->cmd->archive_params->enabled || !vg->cmd->archive_params->dir)
116 log_verbose("Test mode: Skipping archiving of volume group.");
120 if (!dm_create_dir(vg->cmd->archive_params->dir))
123 /* Trap a read-only file system */
124 if ((access(vg->cmd->archive_params->dir, R_OK | W_OK | X_OK) == -1) &&
128 log_verbose("Archiving volume group \"%s\" metadata (seqno %u).", vg->name,
130 if (!__archive(vg)) {
131 log_error("Volume group \"%s\" metadata archive failed.",
139 int archive_display(struct cmd_context *cmd, const char *vg_name)
143 r1 = archive_list(cmd, cmd->archive_params->dir, vg_name);
144 r2 = backup_list(cmd, cmd->backup_params->dir, vg_name);
149 int archive_display_file(struct cmd_context *cmd, const char *file)
153 r = archive_list_file(cmd, file);
158 int backup_init(struct cmd_context *cmd, const char *dir,
163 if (!(cmd->backup_params = dm_pool_zalloc(cmd->libmem,
164 sizeof(*cmd->backup_params)))) {
165 log_error("backup_params alloc failed");
169 cmd->backup_params->dir = NULL;
173 if (!(cmd->backup_params->dir = dm_strdup(dir))) {
174 log_error("Couldn't copy backup directory name.");
177 backup_enable(cmd, enabled);
182 void backup_exit(struct cmd_context *cmd)
184 if (!cmd->backup_params)
186 if (cmd->backup_params->dir)
187 dm_free(cmd->backup_params->dir);
188 memset(cmd->backup_params, 0, sizeof(*cmd->backup_params));
191 void backup_enable(struct cmd_context *cmd, int flag)
193 cmd->backup_params->enabled = flag;
196 static int __backup(struct volume_group *vg)
201 if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 0)))
204 if (dm_snprintf(name, sizeof(name), "%s/%s",
205 vg->cmd->backup_params->dir, vg->name) < 0) {
206 log_error("Failed to generate volume group metadata backup "
211 return backup_to_file(name, desc, vg);
214 int backup_locally(struct volume_group *vg)
216 if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
217 log_warn("WARNING: This metadata update is NOT backed up");
222 log_verbose("Test mode: Skipping volume group backup.");
226 if (!dm_create_dir(vg->cmd->backup_params->dir))
229 /* Trap a read-only file system */
230 if ((access(vg->cmd->backup_params->dir, R_OK | W_OK | X_OK) == -1) &&
235 log_error("Backup of volume group %s metadata failed.",
243 int backup(struct volume_group *vg)
245 if (vg_is_clustered(vg))
246 remote_backup_metadata(vg);
248 return backup_locally(vg);
251 int backup_remove(struct cmd_context *cmd, const char *vg_name)
255 if (dm_snprintf(path, sizeof(path), "%s/%s",
256 cmd->backup_params->dir, vg_name) < 0) {
257 log_error("Failed to generate backup filename (for removal).");
262 * Let this fail silently.
268 struct volume_group *backup_read_vg(struct cmd_context *cmd,
269 const char *vg_name, const char *file)
271 struct volume_group *vg = NULL;
272 struct format_instance *tf;
273 struct metadata_area *mda;
276 if (!(context = create_text_context(cmd, file,
278 !(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, NULL,
280 log_error("Couldn't create text format object.");
284 dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
285 if (!(vg = mda->ops->vg_read(tf, vg_name, mda)))
290 tf->fmt->ops->destroy_instance(tf);
294 /* ORPHAN and VG locks held before calling this */
295 int backup_restore_vg(struct cmd_context *cmd, struct volume_group *vg)
298 struct physical_volume *pv;
299 struct lvmcache_info *info;
302 * FIXME: Check that the PVs referenced in the backup are
303 * not members of other existing VGs.
306 /* Attempt to write out using currently active format */
307 if (!(vg->fid = cmd->fmt->ops->create_instance(cmd->fmt, vg->name,
309 log_error("Failed to allocate format instance");
314 * Setting vg->old_name to a blank value will explicitly
315 * disable any attempt to check VG name in existing metadata.
317 vg->old_name = dm_pool_strdup(vg->vgmem, "");
319 /* Add any metadata areas on the PVs */
320 dm_list_iterate_items(pvl, &vg->pvs) {
322 if (!(info = info_from_pvid(pv->dev->pvid, 0))) {
323 log_error("PV %s missing from cache",
327 if (cmd->fmt != info->fmt) {
328 log_error("PV %s is a different format (seqno %s)",
329 pv_dev_name(pv), info->fmt->name);
332 if (!vg->fid->fmt->ops->
333 pv_setup(vg->fid->fmt, UINT64_C(0), 0, 0, 0, 0, 0UL,
335 &vg->fid->metadata_areas_in_use, pv, vg)) {
336 log_error("Format-specific setup for %s failed",
342 if (!vg_write(vg) || !vg_commit(vg))
348 /* ORPHAN and VG locks held before calling this */
349 int backup_restore_from_file(struct cmd_context *cmd, const char *vg_name,
352 struct volume_group *vg;
353 int missing_pvs, r = 0;
356 * Read in the volume group from the text file.
358 if (!(vg = backup_read_vg(cmd, vg_name, file)))
361 missing_pvs = vg_missing_pv_count(vg);
362 if (missing_pvs == 0)
363 r = backup_restore_vg(cmd, vg);
365 log_error("Cannot restore Volume Group %s with %i PVs "
366 "marked as missing.", vg->name, missing_pvs);
372 int backup_restore(struct cmd_context *cmd, const char *vg_name)
376 if (dm_snprintf(path, sizeof(path), "%s/%s",
377 cmd->backup_params->dir, vg_name) < 0) {
378 log_error("Failed to generate backup filename (for restore).");
382 return backup_restore_from_file(cmd, vg_name, path);
385 int backup_to_file(const char *file, const char *desc, struct volume_group *vg)
388 struct format_instance *tf;
389 struct metadata_area *mda;
391 struct cmd_context *cmd;
395 log_verbose("Creating volume group backup \"%s\" (seqno %u).", file, vg->seqno);
397 if (!(context = create_text_context(cmd, file, desc)) ||
398 !(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, NULL,
400 log_error("Couldn't create backup object.");
404 if (!dm_list_size(&tf->metadata_areas_in_use)) {
405 log_error(INTERNAL_ERROR "No in use metadata areas to write.");
409 /* Write and commit the metadata area */
410 dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
411 if (!(r = mda->ops->vg_write(tf, vg, mda))) {
415 if (mda->ops->vg_commit &&
416 !(r = mda->ops->vg_commit(tf, vg, mda))) {
421 tf->fmt->ops->destroy_instance(tf);
426 * Update backup (and archive) if they're out-of-date or don't exist.
428 void check_current_backup(struct volume_group *vg)
431 struct volume_group *vg_backup;
434 if (vg_is_exported(vg))
437 if (dm_snprintf(path, sizeof(path), "%s/%s",
438 vg->cmd->backup_params->dir, vg->name) < 0) {
439 log_debug("Failed to generate backup filename.");
443 old_suppress = log_suppress(1);
444 /* Up-to-date backup exists? */
445 if ((vg_backup = backup_read_vg(vg->cmd, vg->name, path)) &&
446 (vg->seqno == vg_backup->seqno) &&
447 (id_equal(&vg->id, &vg_backup->id))) {
448 log_suppress(old_suppress);
452 log_suppress(old_suppress);