1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

Move archiver code from tools into library.

This commit is contained in:
Alasdair Kergon 2005-05-17 13:46:38 +00:00
parent 24e654645f
commit 7ac8c2389f
11 changed files with 150 additions and 130 deletions

View File

@ -1,5 +1,6 @@
Version 2.01.11 - Version 2.01.11 -
============================== ==============================
Move archiver code from tools into library.
vgscan/change/display/vgs automatically create metadata backups if needed. vgscan/change/display/vgs automatically create metadata backups if needed.
Fix contiguous allocation policy with linear. Fix contiguous allocation policy with linear.
Cope with missing format1 PVs again. Cope with missing format1 PVs again.

View File

@ -23,6 +23,7 @@
../lib/filters/filter.h ../lib/filters/filter.h
../lib/format1/format1.h ../lib/format1/format1.h
../lib/format_pool/format_pool.h ../lib/format_pool/format_pool.h
../lib/format_text/archiver.h
../lib/format_text/format-text.h ../lib/format_text/format-text.h
../lib/format_text/text_export.h ../lib/format_text/text_export.h
../lib/format_text/text_import.h ../lib/format_text/text_import.h

View File

@ -54,6 +54,7 @@ SOURCES =\
filters/filter-md.c \ filters/filter-md.c \
filters/filter.c \ filters/filter.c \
format_text/archive.c \ format_text/archive.c \
format_text/archiver.c \
format_text/export.c \ format_text/export.c \
format_text/flags.c \ format_text/flags.c \
format_text/format-text.c \ format_text/format-text.c \

View File

@ -36,6 +36,7 @@
#include "segtype.h" #include "segtype.h"
#include "lvmcache.h" #include "lvmcache.h"
#include "dev-cache.h" #include "dev-cache.h"
#include "archiver.h"
#ifdef HAVE_LIBDL #ifdef HAVE_LIBDL
#include "sharedlib.h" #include "sharedlib.h"
@ -814,6 +815,69 @@ static int _init_hostname(struct cmd_context *cmd)
return 1; return 1;
} }
static int _init_backup(struct cmd_context *cmd)
{
uint32_t days, min;
char default_dir[PATH_MAX];
const char *dir;
if (!cmd->sys_dir) {
log_warn("WARNING: Metadata changes will NOT be backed up");
backup_init(cmd, "");
archive_init(cmd, "", 0, 0);
return 1;
}
/* set up archiving */
cmd->default_settings.archive =
find_config_bool(cmd->cft->root, "backup/archive",
DEFAULT_ARCHIVE_ENABLED);
days = (uint32_t) find_config_int(cmd->cft->root, "backup/retain_days",
DEFAULT_ARCHIVE_DAYS);
min = (uint32_t) find_config_int(cmd->cft->root, "backup/retain_min",
DEFAULT_ARCHIVE_NUMBER);
if (lvm_snprintf
(default_dir, sizeof(default_dir), "%s/%s", cmd->sys_dir,
DEFAULT_ARCHIVE_SUBDIR) == -1) {
log_err("Couldn't create default archive path '%s/%s'.",
cmd->sys_dir, DEFAULT_ARCHIVE_SUBDIR);
return 0;
}
dir = find_config_str(cmd->cft->root, "backup/archive_dir",
default_dir);
if (!archive_init(cmd, dir, days, min)) {
log_debug("backup_init failed.");
return 0;
}
/* set up the backup */
cmd->default_settings.backup =
find_config_bool(cmd->cft->root, "backup/backup",
DEFAULT_BACKUP_ENABLED);
if (lvm_snprintf
(default_dir, sizeof(default_dir), "%s/%s", cmd->sys_dir,
DEFAULT_BACKUP_SUBDIR) == -1) {
log_err("Couldn't create default backup path '%s/%s'.",
cmd->sys_dir, DEFAULT_BACKUP_SUBDIR);
return 0;
}
dir = find_config_str(cmd->cft->root, "backup/backup_dir", default_dir);
if (!backup_init(cmd, dir)) {
log_debug("backup_init failed.");
return 0;
}
return 1;
}
/* Entry point */ /* Entry point */
struct cmd_context *create_toolcontext(struct arg *the_args) struct cmd_context *create_toolcontext(struct arg *the_args)
{ {
@ -902,6 +966,9 @@ struct cmd_context *create_toolcontext(struct arg *the_args)
if (!_init_segtypes(cmd)) if (!_init_segtypes(cmd))
goto error; goto error;
if (!_init_backup(cmd))
goto error;
cmd->current_settings = cmd->default_settings; cmd->current_settings = cmd->default_settings;
cmd->config_valid = 1; cmd->config_valid = 1;
@ -1012,6 +1079,8 @@ void destroy_toolcontext(struct cmd_context *cmd)
if (cmd->dump_filter) if (cmd->dump_filter)
persistent_filter_dump(cmd->filter); persistent_filter_dump(cmd->filter);
archive_exit(cmd);
backup_exit(cmd);
activation_exit(); activation_exit();
lvmcache_destroy(); lvmcache_destroy();
label_exit(); label_exit();

View File

@ -44,6 +44,8 @@ struct config_info {
}; };
struct config_tree; struct config_tree;
struct archive_params;
struct backup_params;
/* FIXME Split into tool & library contexts */ /* FIXME Split into tool & library contexts */
/* command-instance-related variables needed by library */ /* command-instance-related variables needed by library */
@ -73,6 +75,9 @@ struct cmd_context {
struct config_info default_settings; struct config_info default_settings;
struct config_info current_settings; struct config_info current_settings;
struct archive_params *archive_params;
struct backup_params *backup_params;
/* List of defined tags */ /* List of defined tags */
struct list tags; struct list tags;
int hosttags; int hosttags;

View File

@ -13,50 +13,63 @@
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include "tools.h" #include "lib.h"
#include "archiver.h"
#include "format-text.h"
#include "lvm-file.h"
#include "lvm-string.h"
#include "lvmcache.h"
#include "toolcontext.h"
static struct { #include <unistd.h>
struct archive_params {
int enabled; int enabled;
char *dir; char *dir;
unsigned int keep_days; unsigned int keep_days;
unsigned int keep_number; unsigned int keep_number;
};
} _archive_params; struct backup_params {
static struct {
int enabled; int enabled;
char *dir; char *dir;
};
} _backup_params; int archive_init(struct cmd_context *cmd, const char *dir,
unsigned int keep_days, unsigned int keep_min)
int archive_init(const char *dir, unsigned int keep_days, unsigned int keep_min)
{ {
_archive_params.dir = NULL; if (!(cmd->archive_params = pool_zalloc(cmd->mem, sizeof(*cmd->archive_params)))) {
log_error("archive_params alloc failed");
return 0;
}
cmd->archive_params->dir = NULL;
if (!*dir) if (!*dir)
return 1; return 1;
if (!(_archive_params.dir = dbg_strdup(dir))) { if (!(cmd->archive_params->dir = dbg_strdup(dir))) {
log_error("Couldn't copy archive directory name."); log_error("Couldn't copy archive directory name.");
return 0; return 0;
} }
_archive_params.keep_days = keep_days; cmd->archive_params->keep_days = keep_days;
_archive_params.keep_number = keep_min; cmd->archive_params->keep_number = keep_min;
_archive_params.enabled = 1; cmd->archive_params->enabled = 1;
return 1; return 1;
} }
void archive_exit(void) void archive_exit(struct cmd_context *cmd)
{ {
if (_archive_params.dir) if (cmd->archive_params->dir)
dbg_free(_archive_params.dir); dbg_free(cmd->archive_params->dir);
memset(&_archive_params, 0, sizeof(_archive_params)); memset(cmd->archive_params, 0, sizeof(*cmd->archive_params));
} }
void archive_enable(int flag) void archive_enable(struct cmd_context *cmd, int flag)
{ {
_archive_params.enabled = flag; cmd->archive_params->enabled = flag;
} }
static char *_build_desc(struct pool *mem, const char *line, int before) static char *_build_desc(struct pool *mem, const char *line, int before)
@ -88,14 +101,14 @@ static int __archive(struct volume_group *vg)
return 0; return 0;
} }
return archive_vg(vg, _archive_params.dir, desc, return archive_vg(vg, vg->cmd->archive_params->dir, desc,
_archive_params.keep_days, vg->cmd->archive_params->keep_days,
_archive_params.keep_number); vg->cmd->archive_params->keep_number);
} }
int archive(struct volume_group *vg) int archive(struct volume_group *vg)
{ {
if (!_archive_params.enabled || !_archive_params.dir) if (!vg->cmd->archive_params->enabled || !vg->cmd->archive_params->dir)
return 1; return 1;
if (test_mode()) { if (test_mode()) {
@ -103,11 +116,11 @@ int archive(struct volume_group *vg)
return 1; return 1;
} }
if (!create_dir(_archive_params.dir)) if (!create_dir(vg->cmd->archive_params->dir))
return 0; return 0;
/* Trap a read-only file system */ /* Trap a read-only file system */
if ((access(_archive_params.dir, R_OK | W_OK | X_OK) == -1) && if ((access(vg->cmd->archive_params->dir, R_OK | W_OK | X_OK) == -1) &&
(errno == EROFS)) (errno == EROFS))
return 0; return 0;
@ -127,20 +140,25 @@ int archive_display(struct cmd_context *cmd, const char *vg_name)
int r1, r2; int r1, r2;
init_partial(1); init_partial(1);
r1 = archive_list(cmd, _archive_params.dir, vg_name); r1 = archive_list(cmd, cmd->archive_params->dir, vg_name);
r2 = backup_list(cmd, _backup_params.dir, vg_name); r2 = backup_list(cmd, cmd->backup_params->dir, vg_name);
init_partial(0); init_partial(0);
return r1 && r2; return r1 && r2;
} }
int backup_init(const char *dir) int backup_init(struct cmd_context *cmd, const char *dir)
{ {
_backup_params.dir = NULL; if (!(cmd->backup_params = pool_zalloc(cmd->mem, sizeof(*cmd->archive_params)))) {
log_error("archive_params alloc failed");
return 0;
}
cmd->backup_params->dir = NULL;
if (!*dir) if (!*dir)
return 1; return 1;
if (!(_backup_params.dir = dbg_strdup(dir))) { if (!(cmd->backup_params->dir = dbg_strdup(dir))) {
log_error("Couldn't copy backup directory name."); log_error("Couldn't copy backup directory name.");
return 0; return 0;
} }
@ -148,16 +166,16 @@ int backup_init(const char *dir)
return 1; return 1;
} }
void backup_exit(void) void backup_exit(struct cmd_context *cmd)
{ {
if (_backup_params.dir) if (cmd->backup_params->dir)
dbg_free(_backup_params.dir); dbg_free(cmd->backup_params->dir);
memset(&_backup_params, 0, sizeof(_backup_params)); memset(cmd->backup_params, 0, sizeof(*cmd->backup_params));
} }
void backup_enable(int flag) void backup_enable(struct cmd_context *cmd, int flag)
{ {
_backup_params.enabled = flag; cmd->backup_params->enabled = flag;
} }
static int __backup(struct volume_group *vg) static int __backup(struct volume_group *vg)
@ -171,7 +189,7 @@ static int __backup(struct volume_group *vg)
} }
if (lvm_snprintf(name, sizeof(name), "%s/%s", if (lvm_snprintf(name, sizeof(name), "%s/%s",
_backup_params.dir, vg->name) < 0) { vg->cmd->backup_params->dir, vg->name) < 0) {
log_error("Failed to generate volume group metadata backup " log_error("Failed to generate volume group metadata backup "
"filename."); "filename.");
return 0; return 0;
@ -182,7 +200,7 @@ static int __backup(struct volume_group *vg)
int backup(struct volume_group *vg) int backup(struct volume_group *vg)
{ {
if (!_backup_params.enabled || !_backup_params.dir) { if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
log_print("WARNING: This metadata update is NOT backed up"); log_print("WARNING: This metadata update is NOT backed up");
return 1; return 1;
} }
@ -192,11 +210,11 @@ int backup(struct volume_group *vg)
return 1; return 1;
} }
if (!create_dir(_backup_params.dir)) if (!create_dir(vg->cmd->backup_params->dir))
return 0; return 0;
/* Trap a read-only file system */ /* Trap a read-only file system */
if ((access(_backup_params.dir, R_OK | W_OK | X_OK) == -1) && if ((access(vg->cmd->backup_params->dir, R_OK | W_OK | X_OK) == -1) &&
(errno == EROFS)) (errno == EROFS))
return 0; return 0;
@ -209,12 +227,12 @@ int backup(struct volume_group *vg)
return 1; return 1;
} }
int backup_remove(const char *vg_name) int backup_remove(struct cmd_context *cmd, const char *vg_name)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
if (lvm_snprintf(path, sizeof(path), "%s/%s", if (lvm_snprintf(path, sizeof(path), "%s/%s",
_backup_params.dir, vg_name) < 0) { cmd->backup_params->dir, vg_name) < 0) {
log_err("Failed to generate backup filename (for removal)."); log_err("Failed to generate backup filename (for removal).");
return 0; return 0;
} }
@ -323,7 +341,7 @@ int backup_restore(struct cmd_context *cmd, const char *vg_name)
char path[PATH_MAX]; char path[PATH_MAX];
if (lvm_snprintf(path, sizeof(path), "%s/%s", if (lvm_snprintf(path, sizeof(path), "%s/%s",
_backup_params.dir, vg_name) < 0) { cmd->backup_params->dir, vg_name) < 0) {
log_err("Failed to generate backup filename (for restore)."); log_err("Failed to generate backup filename (for restore).");
return 0; return 0;
} }
@ -378,7 +396,7 @@ void check_current_backup(struct volume_group *vg)
return; return;
if (lvm_snprintf(path, sizeof(path), "%s/%s", if (lvm_snprintf(path, sizeof(path), "%s/%s",
_backup_params.dir, vg->name) < 0) { vg->cmd->backup_params->dir, vg->name) < 0) {
log_debug("Failed to generate backup filename."); log_debug("Failed to generate backup filename.");
return; return;
} }

View File

@ -18,11 +18,6 @@
#include "metadata.h" #include "metadata.h"
/*
* FIXME: This file is going to merge with the archiving code in
* lib/format_text at some point.
*/
/* /*
* There are two operations that come under the general area of * There are two operations that come under the general area of
* backups. 'Archiving' occurs just before a volume group * backups. 'Archiving' occurs just before a volume group
@ -36,20 +31,20 @@
* Typically backups will be stored in /etc/lvm/backups. * Typically backups will be stored in /etc/lvm/backups.
*/ */
int archive_init(const char *dir, int archive_init(struct cmd_context *cmd, const char *dir,
unsigned int keep_days, unsigned int keep_min); unsigned int keep_days, unsigned int keep_min);
void archive_exit(void); void archive_exit(struct cmd_context *cmd);
void archive_enable(int flag); void archive_enable(struct cmd_context *cmd, int flag);
int archive(struct volume_group *vg); int archive(struct volume_group *vg);
int archive_display(struct cmd_context *cmd, const char *vg_name); int archive_display(struct cmd_context *cmd, const char *vg_name);
int backup_init(const char *dir); int backup_init(struct cmd_context *cmd, const char *dir);
void backup_exit(void); void backup_exit(struct cmd_context *cmd);
void backup_enable(int flag); void backup_enable(struct cmd_context *cmd, int flag);
int backup(struct volume_group *vg); int backup(struct volume_group *vg);
int backup_remove(const char *vg_name); int backup_remove(struct cmd_context *cmd, const char *vg_name);
struct volume_group *backup_read_vg(struct cmd_context *cmd, struct volume_group *backup_read_vg(struct cmd_context *cmd,
const char *vg_name, const char *file); const char *vg_name, const char *file);

View File

@ -21,7 +21,6 @@ ifeq ("@FSADM@", "yes")
endif endif
SOURCES =\ SOURCES =\
archiver.c \
dumpconfig.c \ dumpconfig.c \
formats.c \ formats.c \
lvchange.c \ lvchange.c \

View File

@ -758,8 +758,8 @@ static void _apply_settings(struct cmd_context *cmd)
init_msg_prefix(cmd->default_settings.msg_prefix); init_msg_prefix(cmd->default_settings.msg_prefix);
init_cmd_name(cmd->default_settings.cmd_name); init_cmd_name(cmd->default_settings.cmd_name);
archive_enable(cmd->current_settings.archive); archive_enable(cmd, cmd->current_settings.archive);
backup_enable(cmd->current_settings.backup); backup_enable(cmd, cmd->current_settings.backup);
set_activation(cmd->current_settings.activation); set_activation(cmd->current_settings.activation);
@ -912,69 +912,6 @@ static void _init_rand(void)
srand((unsigned int) time(NULL) + (unsigned int) getpid()); srand((unsigned int) time(NULL) + (unsigned int) getpid());
} }
static int _init_backup(struct cmd_context *cmd, struct config_tree *cft)
{
uint32_t days, min;
char default_dir[PATH_MAX];
const char *dir;
if (!cmd->sys_dir) {
log_warn("WARNING: Metadata changes will NOT be backed up");
backup_init("");
archive_init("", 0, 0);
return 1;
}
/* set up archiving */
cmd->default_settings.archive =
find_config_bool(cmd->cft->root, "backup/archive",
DEFAULT_ARCHIVE_ENABLED);
days = (uint32_t) find_config_int(cmd->cft->root, "backup/retain_days",
DEFAULT_ARCHIVE_DAYS);
min = (uint32_t) find_config_int(cmd->cft->root, "backup/retain_min",
DEFAULT_ARCHIVE_NUMBER);
if (lvm_snprintf
(default_dir, sizeof(default_dir), "%s/%s", cmd->sys_dir,
DEFAULT_ARCHIVE_SUBDIR) == -1) {
log_err("Couldn't create default archive path '%s/%s'.",
cmd->sys_dir, DEFAULT_ARCHIVE_SUBDIR);
return 0;
}
dir = find_config_str(cmd->cft->root, "backup/archive_dir",
default_dir);
if (!archive_init(dir, days, min)) {
log_debug("backup_init failed.");
return 0;
}
/* set up the backup */
cmd->default_settings.backup =
find_config_bool(cmd->cft->root, "backup/backup",
DEFAULT_BACKUP_ENABLED);
if (lvm_snprintf
(default_dir, sizeof(default_dir), "%s/%s", cmd->sys_dir,
DEFAULT_BACKUP_SUBDIR) == -1) {
log_err("Couldn't create default backup path '%s/%s'.",
cmd->sys_dir, DEFAULT_BACKUP_SUBDIR);
return 0;
}
dir = find_config_str(cmd->cft->root, "backup/backup_dir", default_dir);
if (!backup_init(dir)) {
log_debug("backup_init failed.");
return 0;
}
return 1;
}
static void _close_stray_fds(void) static void _close_stray_fds(void)
{ {
struct rlimit rlim; struct rlimit rlim;
@ -1012,9 +949,6 @@ static struct cmd_context *_init_lvm(void)
_init_rand(); _init_rand();
if (!_init_backup(cmd, cmd->cft))
return NULL;
_apply_settings(cmd); _apply_settings(cmd);
return cmd; return cmd;
@ -1032,10 +966,7 @@ static void _fin_commands(struct cmd_context *cmd)
static void _fin(struct cmd_context *cmd) static void _fin(struct cmd_context *cmd)
{ {
archive_exit();
backup_exit();
_fin_commands(cmd); _fin_commands(cmd);
destroy_toolcontext(cmd); destroy_toolcontext(cmd);
} }

View File

@ -76,7 +76,7 @@ static int vg_backup_single(struct cmd_context *cmd, const char *vg_name,
} }
/* just use the normal backup code */ /* just use the normal backup code */
backup_enable(1); /* force a backup */ backup_enable(cmd, 1); /* force a backup */
if (!backup(vg)) { if (!backup(vg)) {
stack; stack;
return ECMD_FAILED; return ECMD_FAILED;

View File

@ -73,7 +73,7 @@ static int vgremove_single(struct cmd_context *cmd, const char *vg_name,
} }
} }
backup_remove(vg_name); backup_remove(cmd, vg_name);
if (ret == ECMD_PROCESSED) if (ret == ECMD_PROCESSED)
log_print("Volume group \"%s\" successfully removed", vg_name); log_print("Volume group \"%s\" successfully removed", vg_name);