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

o Add autobackup support to tools (follows most vg_write calls).

o Skip autobackup when in test mode.
o Set test mode from config file.
o Create system/backup dirs if not present (unless LVM_SYSTEM_DIR holds "").
This commit is contained in:
Alasdair Kergon 2001-12-31 21:27:39 +00:00
parent 129c5d50a1
commit cc21948339
17 changed files with 101 additions and 61 deletions

View File

@ -39,6 +39,8 @@ void init_verbose(int level) {
void init_test(int level) { void init_test(int level) {
_test = level; _test = level;
if (_test)
log_print("Test mode. Metadata will NOT be updated.");
} }
int test_mode() { int test_mode() {

View File

@ -89,8 +89,6 @@ static int lvchange_single(struct logical_volume *lv)
log_print("Logical volume %s changed", lv->name); log_print("Logical volume %s changed", lv->name);
/* FIXME autobackup */
return 0; return 0;
} }
@ -122,6 +120,8 @@ static int lvchange_permission(struct logical_volume *lv)
if (!fid->ops->vg_write(fid, lv->vg)) if (!fid->ops->vg_write(fid, lv->vg))
return 0; return 0;
autobackup(lv->vg);
log_very_verbose("Updating permissions for %s in kernel", lv->name); log_very_verbose("Updating permissions for %s in kernel", lv->name);
if (!lv_update_write_access(lv)) if (!lv_update_write_access(lv))
return 0; return 0;
@ -158,6 +158,7 @@ static int lvchange_availability(struct logical_volume *lv)
lv->name); lv->name);
if (!fid->ops->vg_write(fid, lv->vg)) if (!fid->ops->vg_write(fid, lv->vg))
return 0; return 0;
autobackup(lv->vg);
} }
if ((lv_stat & ACTIVE) && (active & ACTIVE)) if ((lv_stat & ACTIVE) && (active & ACTIVE))
@ -221,6 +222,8 @@ static int lvchange_contiguous(struct logical_volume *lv)
if (!fid->ops->vg_write(fid, lv->vg)) if (!fid->ops->vg_write(fid, lv->vg))
return 0; return 0;
autobackup(lv->vg);
return 1; return 1;
} }
@ -251,5 +254,7 @@ static int lvchange_readahead(struct logical_volume *lv)
if (!fid->ops->vg_write(fid, lv->vg)) if (!fid->ops->vg_write(fid, lv->vg))
return 0; return 0;
autobackup(lv->vg);
return 1; return 1;
} }

View File

@ -6,6 +6,7 @@
*/ */
#include "tools.h" #include "tools.h"
#include <fcntl.h> #include <fcntl.h>
int lvcreate(int argc, char **argv) int lvcreate(int argc, char **argv)
@ -224,6 +225,8 @@ int lvcreate(int argc, char **argv)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return ECMD_FAILED; return ECMD_FAILED;
autobackup(vg);
log_print("Logical volume %s created", lv->name); log_print("Logical volume %s created", lv->name);
if (!lv_activate(lv)) if (!lv_activate(lv))
@ -259,10 +262,5 @@ int lvcreate(int argc, char **argv)
} else } else
log_print("WARNING: %s not zeroed", lv->name); log_print("WARNING: %s not zeroed", lv->name);
/******** FIXME backup
if ((ret = do_autobackup(vg_name, vg)))
return ret;
***********/
return 0; return 0;
} }

View File

@ -65,6 +65,9 @@ static int _debug;
static int _default_verbose; static int _default_verbose;
static int _verbose; static int _verbose;
static int _default_test;
static int _test;
/* /*
* The lvm_sys_dir contains: * The lvm_sys_dir contains:
* *
@ -509,31 +512,22 @@ static int process_common_commands(struct command *com)
if (arg_count(suspend_ARG)) if (arg_count(suspend_ARG))
kill(getpid(), SIGSTOP); kill(getpid(), SIGSTOP);
/*
* debug
*/
_debug = _default_debug; _debug = _default_debug;
if (arg_count(debug_ARG)) if (arg_count(debug_ARG))
_debug = arg_count(debug_ARG); _debug = arg_count(debug_ARG);
/*
* verbose
*/
_verbose = _default_verbose; _verbose = _default_verbose;
if (arg_count(verbose_ARG)) if (arg_count(verbose_ARG))
_verbose = arg_count(verbose_ARG); _verbose = arg_count(verbose_ARG);
if (arg_count(quiet_ARG)) { if (arg_count(quiet_ARG)) {
_debug = 0; _debug = 0;
_verbose = 0; _verbose = 0;
} }
_test = _default_test;
if ((l = arg_count(test_ARG))) { if (arg_count(test_ARG))
log_error("Test mode. Metadata will NOT be updated."); _test = arg_count(test_ARG);
init_test(l);
}
if (arg_count(help_ARG)) { if (arg_count(help_ARG)) {
usage(com->name); usage(com->name);
@ -602,6 +596,7 @@ static int run_command(int argc, char **argv)
init_debug(_debug); init_debug(_debug);
init_verbose(_verbose); init_verbose(_verbose);
init_test(_test);
ret = the_command->fn(argc, argv); ret = the_command->fn(argc, argv);
@ -611,6 +606,7 @@ static int run_command(int argc, char **argv)
*/ */
init_debug(_default_debug); init_debug(_default_debug);
init_verbose(_default_verbose); init_verbose(_default_verbose);
init_test(_default_test);
/* /*
* free off any memory the command used. * free off any memory the command used.
@ -674,6 +670,9 @@ static void __init_log(struct config_file *cf)
_default_verbose = find_config_int(cf->root, "log/verbose", '/', 0); _default_verbose = find_config_int(cf->root, "log/verbose", '/', 0);
init_verbose(_default_verbose); init_verbose(_default_verbose);
_default_test = find_config_int(cf->root, "log/test", '/', 0);
init_test(_default_test);
} }
static int dev_cache_setup(struct config_file *cf) static int dev_cache_setup(struct config_file *cf)
@ -772,6 +771,9 @@ static struct dev_filter *filter_setup(struct config_file *cf)
if (find_config_int(cf->root, "devices/write_cache_state", '/', 1)) if (find_config_int(cf->root, "devices/write_cache_state", '/', 1))
_dump_filter = 1; _dump_filter = 1;
if (!*_sys_dir)
_dump_filter = 0;
if (!stat(lvm_cache, &st) && !persistent_filter_load(f4)) if (!stat(lvm_cache, &st) && !persistent_filter_load(f4))
log_verbose("Failed to load existing device cache from %s", log_verbose("Failed to load existing device cache from %s",
lvm_cache); lvm_cache);
@ -783,6 +785,7 @@ static int _get_env_vars(void)
{ {
const char *e; const char *e;
/* Set to "" to avoid using any system directory */
if ((e = getenv("LVM_SYSTEM_DIR"))) { if ((e = getenv("LVM_SYSTEM_DIR"))) {
if (lvm_snprintf(_sys_dir, sizeof(_sys_dir), "%s", e) < 0) { if (lvm_snprintf(_sys_dir, sizeof(_sys_dir), "%s", e) < 0) {
log_error("LVM_SYSTEM_DIR environment variable " log_error("LVM_SYSTEM_DIR environment variable "
@ -797,11 +800,15 @@ static int _get_env_vars(void)
static int init(void) static int init(void)
{ {
struct stat info; struct stat info;
char config_file[PATH_MAX]; char config_file[PATH_MAX] = "";
if (!_get_env_vars()) if (!_get_env_vars())
return 0; return 0;
/* Create system directory if it doesn't already exist */
if (!create_dir(_sys_dir))
return 0;
if (!(cmd = dbg_malloc(sizeof(*cmd)))) { if (!(cmd = dbg_malloc(sizeof(*cmd)))) {
log_error("Failed to allocate command context"); log_error("Failed to allocate command context");
return 0; return 0;
@ -818,7 +825,7 @@ static int init(void)
/* send log messages to stderr for now */ /* send log messages to stderr for now */
init_log(stderr); init_log(stderr);
if (lvm_snprintf(config_file, sizeof(config_file), if (*_sys_dir && lvm_snprintf(config_file, sizeof(config_file),
"%s/lvm.conf", _sys_dir) < 0) { "%s/lvm.conf", _sys_dir) < 0) {
log_error("lvm_sys_dir was too long"); log_error("lvm_sys_dir was too long");
return 0; return 0;
@ -847,13 +854,19 @@ static int init(void)
dm_log_init(print_log); dm_log_init(print_log);
if (lvm_snprintf(_backup_dir, sizeof(_backup_dir), "%s/backup", if (!*strncpy(_backup_dir,
find_config_str(cmd->cf->root, "backup/dir", find_config_str(cmd->cf->root, "backup/dir", '/', ""),
'/', _sys_dir)) < 0) { sizeof(_backup_dir)) &&
*_sys_dir &&
lvm_snprintf(_backup_dir, sizeof(_backup_dir), "%s/backup",
_sys_dir) < 0) {
log_error("Backup directory given in config file too long"); log_error("Backup directory given in config file too long");
return 0; return 0;
} }
if (!create_dir(_backup_dir))
return 0;
_backup_days = find_config_int(cmd->cf->root, "backup/days", '/', _backup_days = find_config_int(cmd->cf->root, "backup/days", '/',
_backup_days); _backup_days);

View File

@ -87,10 +87,7 @@ static int lvremove_single(struct logical_volume *lv)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return ECMD_FAILED; return ECMD_FAILED;
/******** FIXME autobackup(vg);
if ((ret = do_autobackup(vg->name, vg)))
return ret;
**********/
log_print("logical volume %s successfully removed", lv->name); log_print("logical volume %s successfully removed", lv->name);
return 0; return 0;

View File

@ -122,11 +122,11 @@ int lvrename(int argc, char **argv)
return ECMD_FAILED; return ECMD_FAILED;
} }
autobackup(lv->vg);
/* FIXME Update symlink. */ /* FIXME Update symlink. */
lv_reactivate(lv); lv_reactivate(lv);
/* FIXME backup */
log_print("Renamed %s to %s in volume group %s%s", log_print("Renamed %s to %s in volume group %s%s",
lv_name_old, lv_name_new, fid->cmd->dev_dir, vg_name); lv_name_old, lv_name_new, fid->cmd->dev_dir, vg_name);

View File

@ -345,17 +345,14 @@ int lvresize(int argc, char **argv)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return ECMD_FAILED; return ECMD_FAILED;
autobackup(vg);
/* FIXME Ensure it always displays errors? */ /* FIXME Ensure it always displays errors? */
if (!lv_reactivate(lv)) if (!lv_reactivate(lv))
return ECMD_FAILED; return ECMD_FAILED;
/********* FIXME Resume *********/ /********* FIXME Resume *********/
/********* FIXME Backup
if ((ret = do_autobackup(vg_name, vg)))
return ret;
************/
log_print("Logical volume %s successfully resized", lv_name); log_print("Logical volume %s successfully resized", lv_name);
return 0; return 0;

View File

@ -73,11 +73,6 @@ int pvchange(int argc, char **argv)
} }
} }
/******* FIXME Backup
if ((ret = do_autobackup(vg_name, vg)))
return ret;
*********/
log_print("%d physical volume(s) changed / %d physical volume(s) " log_print("%d physical volume(s) changed / %d physical volume(s) "
"not changed", done, total - done); "not changed", done, total - done);
@ -144,6 +139,7 @@ int pvchange_single(struct physical_volume *pv)
"volume group %s", pv_name, vg->name); "volume group %s", pv_name, vg->name);
return 0; return 0;
} }
autobackup(vg);
} else { } else {
if (!(fid->ops->pv_write(fid, pv))) { if (!(fid->ops->pv_write(fid, pv))) {
log_error("Failed to store physical volume %s", log_error("Failed to store physical volume %s",

View File

@ -6,6 +6,8 @@
#include "tools.h" #include "tools.h"
#include <sys/stat.h>
static int _autobackup; static int _autobackup;
static char _backup_dir[PATH_MAX]; static char _backup_dir[PATH_MAX];
static int _keep_days; /* keep for at least this number of days */ static int _keep_days; /* keep for at least this number of days */
@ -87,25 +89,52 @@ static int __autobackup(struct volume_group *vg)
int autobackup(struct volume_group *vg) int autobackup(struct volume_group *vg)
{ {
if (!_autobackup) { if (!_autobackup || !*_backup_dir) {
log_print("WARNING: You don't have an automatic backup of %s", log_print("WARNING: You don't have an automatic backup of %s",
vg->name); vg->name);
return 1; return 1;
} }
if (test_mode()) {
log_print("Test mode: Skipping automatic backup");
return 1;
}
log_print("Creating automatic backup of volume group \"%s\" ...", log_print("Creating automatic backup of volume group \"%s\" ...",
vg->name); vg->name);
if (!__autobackup(vg)) { if (!__autobackup(vg)) {
log_err("Autobackup failed."); log_error("Autobackup failed.");
return 0; return 0;
} }
log_err("Autobackup not implemented yet.");
return 1; return 1;
} }
int create_dir(const char *dir)
{
struct stat info;
if (!*dir)
return 1;
if (stat(dir, &info) < 0 ) {
log_verbose("Creating directory %s", dir);
if (!mkdir(dir, S_IRWXU))
return 1;
log_sys_error("mkdir", dir);
return 0;
}
if (S_ISDIR(info.st_mode))
return 1;
log_error("Directory %s not found", dir);
return 0;
}
int process_each_lv_in_vg(struct volume_group *vg, int process_each_lv_in_vg(struct volume_group *vg,
int (*process_single) (struct logical_volume *lv)) int (*process_single) (struct logical_volume *lv))
{ {

View File

@ -26,6 +26,8 @@ int autobackup_init(const char *backup_dir, int keep_days, int keep_number,
int autobackup); int autobackup);
int autobackup(struct volume_group *vg); int autobackup(struct volume_group *vg);
int create_dir(const char *dir);
int process_each_vg(int argc, char **argv, int process_each_vg(int argc, char **argv,
int (*process_single) (const char *vg_name)); int (*process_single) (const char *vg_name));

View File

@ -117,6 +117,8 @@ void vgchange_available(struct volume_group *vg)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return; return;
autobackup(vg);
if (available && (lv_open = activate_lvs_in_vg(vg))) if (available && (lv_open = activate_lvs_in_vg(vg)))
log_verbose("Activated %d logical volumes in " log_verbose("Activated %d logical volumes in "
"volume group %s", lv_open, vg->name); "volume group %s", lv_open, vg->name);
@ -153,11 +155,9 @@ void vgchange_allocation(struct volume_group *vg)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return; return;
log_print("Volume group %s successfully changed", vg->name); autobackup(vg);
/********FIXME log_print("Volume group %s successfully changed", vg->name);
do_autobackup(vg->name, vg);
*********/
return; return;
} }
@ -207,10 +207,9 @@ void vgchange_logicalvolume(struct volume_group *vg)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return; return;
autobackup(vg);
log_print("Volume group %s successfully changed", vg->name); log_print("Volume group %s successfully changed", vg->name);
/*******FIXME
do_autobackup(vg->name, vg);
********/
return; return;
} }

View File

@ -88,6 +88,8 @@ int vgcreate(int argc, char **argv)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return ECMD_FAILED; return ECMD_FAILED;
autobackup(vg);
log_print("Volume group %s successfully created", vg->name); log_print("Volume group %s successfully created", vg->name);
return 0; return 0;

View File

@ -75,10 +75,7 @@ int vgextend(int argc, char **argv)
if (!fid->ops->vg_write(fid, vg)) if (!fid->ops->vg_write(fid, vg))
return ECMD_FAILED; return ECMD_FAILED;
/********* FIXME autobackup(vg);
if ((ret = do_autobackup(vg_name, vg)))
return ret;
*********/
log_print("Volume group '%s' successfully extended", vg_name); log_print("Volume group '%s' successfully extended", vg_name);

View File

@ -146,6 +146,8 @@ int vgmerge_single(const char *vg_name_to, const char *vg_name_from)
/* FIXME Remove /dev/vgfrom */ /* FIXME Remove /dev/vgfrom */
autobackup(vg_to);
log_print("Volume group %s successfully merged into %s", log_print("Volume group %s successfully merged into %s",
vg_from->name, vg_to->name); vg_from->name, vg_to->name);
return 0; return 0;

View File

@ -130,6 +130,8 @@ static int vgreduce_single(struct volume_group *vg, struct physical_volume *pv)
return ECMD_FAILED; return ECMD_FAILED;
} }
autobackup(vg);
log_print("Removed %s from volume group %s", name, vg->name); log_print("Removed %s from volume group %s", name, vg->name);
return 0; return 0;

View File

@ -74,6 +74,8 @@ static int vgremove_single(const char *vg_name)
} }
} }
autobackup(vg);
if (!ret) if (!ret)
log_print("Volume group %s successfully removed", vg_name); log_print("Volume group %s successfully removed", vg_name);
else else

View File

@ -120,10 +120,7 @@ int vgrename(int argc, char **argv)
/******* FIXME Any LV things to update? */ /******* FIXME Any LV things to update? */
/*********** autobackup(vg_old);
if ((ret = do_autobackup(vg_name_new, vg_old)))
return ECMD_FAILED;
**********/
log_print("Volume group %s successfully renamed to %s", log_print("Volume group %s successfully renamed to %s",
vg_name_old, vg_name_new); vg_name_old, vg_name_new);