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

o Use lvm_snprintf wherever return value is used

o Add parameters to set retention limits for backups
This commit is contained in:
Alasdair Kergon 2001-12-31 19:09:51 +00:00
parent 4624c6f845
commit 41b2fd5f4d
9 changed files with 65 additions and 39 deletions

View File

@ -9,6 +9,7 @@
#include "display.h" #include "display.h"
#include "log.h" #include "log.h"
#include "fs.h" #include "fs.h"
#include "lvm-string.h"
static void _build_lv_name(char *buffer, size_t s, struct logical_volume *lv) static void _build_lv_name(char *buffer, size_t s, struct logical_volume *lv)
{ {
@ -99,7 +100,7 @@ static int _emit_target(struct dm_task *dmt, struct stripe_segment *seg)
"Insufficient space to write target parameters."; "Insufficient space to write target parameters.";
if (stripes > 1) { if (stripes > 1) {
tw = snprintf(params, sizeof(params), "%u %u ", tw = lvm_snprintf(params, sizeof(params), "%u %u ",
stripes, seg->stripe_size); stripes, seg->stripe_size);
if (tw < 0) { if (tw < 0) {
@ -112,7 +113,7 @@ static int _emit_target(struct dm_task *dmt, struct stripe_segment *seg)
for (s = 0; s < stripes; s++, w += tw) { for (s = 0; s < stripes; s++, w += tw) {
tw = snprintf(params + w, sizeof(params) - w, tw = lvm_snprintf(params + w, sizeof(params) - w,
"%s %" PRIu64 "%s", "%s %" PRIu64 "%s",
dev_name(seg->area[s].pv->dev), dev_name(seg->area[s].pv->dev),
(seg->area[s].pv->pe_start + (seg->area[s].pv->pe_start +

View File

@ -131,7 +131,7 @@ static int _sectors_to_units(uint64_t sectors, char *buffer, size_t s)
/* FIXME: arrange so this doesn't print a /* FIXME: arrange so this doesn't print a
* decimal point unless we have a * decimal point unless we have a
* fractional part. */ * fractional part. */
return snprintf(buffer, s, "# %g %s", d, _units[i]) > 0; return lvm_snprintf(buffer, s, "# %g %s", d, _units[i]) > 0;
} }
/* /*

View File

@ -8,6 +8,7 @@
#include "pv_map.h" #include "pv_map.h"
#include "log.h" #include "log.h"
#include "dbg_malloc.h" #include "dbg_malloc.h"
#include "lvm-string.h"
#include <assert.h> #include <assert.h>
@ -353,7 +354,7 @@ static char *_generate_lv_name(struct volume_group *vg,
high = i; high = i;
} }
if ((s = snprintf(buffer, len, "lvol%d", high + 1)) < 0 || s >= len) if (lvm_snprintf(buffer, len, "lvol%d", high + 1) < 0)
return NULL; return NULL;
return buffer; return buffer;

View File

@ -46,8 +46,17 @@ is invoked. By default tools append messages to the log file.
.TP .TP
\fBbackups\fP \(em Configuration for metadata backups \fBbackups\fP \(em Configuration for metadata backups
.IP .IP
\fBdir\fP \(em Directory used for automatic metadata backups \fBdir\fP \(em Directory used for automatic metadata backups.
(defaults to "/etc/lvm") Defaults to "/etc/lvm/backup".
.IP
\fBkeep\fP \(em Minimum number of backups to keep.
Defaults to 10.
.IP
\fBdays\fP \(em Minimum number of days to keep backup files.
Defaults to 14.
.IP
\fBauto\fP \(em Whether or not tools make automatic backups after changing
metadata. Default is 1 (automatic backups enabled). Set to 0 to disable.
.TP .TP
\fBshell\fP \(em LVM2 built-in readline shell settings \fBshell\fP \(em LVM2 built-in readline shell settings
.IP .IP

View File

@ -233,13 +233,17 @@ int lvcreate(int argc, char **argv)
struct device *dev; struct device *dev;
char *name; char *name;
if (!(name = dbg_malloc(NAME_LEN))) { if (!(name = dbg_malloc(PATH_MAX))) {
log_error("Name allocation failed - device not zeroed"); log_error("Name allocation failed - device not zeroed");
return ECMD_FAILED; return ECMD_FAILED;
} }
snprintf(name, NAME_LEN, "%s%s/%s", fid->cmd->dev_dir, if (lvm_snprintf(name, PATH_MAX, "%s%s/%s", fid->cmd->dev_dir,
lv->vg->name, lv->name); lv->vg->name, lv->name) < 0) {
log_error("Name too long - device not zeroed (%s)",
lv->name);
return ECMD_FAILED;
}
log_verbose("Zeroing start of logical volume %s", name); log_verbose("Zeroing start of logical volume %s", name);

View File

@ -13,7 +13,6 @@
#include <libgen.h> #include <libgen.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#include "stub.h" #include "stub.h"
#include "vgcache.h" #include "vgcache.h"
@ -68,21 +67,25 @@ static int _debug;
static int _default_verbose; static int _default_verbose;
static int _verbose; static int _verbose;
/* /*
* The lvm_sys_dir contains: * The lvm_sys_dir contains:
* *
* o The lvm configuration (lvm.conf) * o The lvm configuration (lvm.conf)
* o The persistent filter cache (.cache) * o The persistent filter cache (.cache)
* o Volume group backups (backups) * o Volume group backups (backup/)
* *
*/ */
static char _sys_dir[PATH_MAX] = "/etc/lvm"; static char _sys_dir[PATH_MAX] = "/etc/lvm";
static char _backup_dir[PATH_MAX]; static char _backup_dir[PATH_MAX];
static char _dev_dir[PATH_MAX]; static char _dev_dir[PATH_MAX];
static int _backup_days = 14; /* Keep at least 14 days */
static int _backup_number = 10; /* Keep at least 10 backups */
static int _backup_auto = 1; /* Autobackups enabled by default */
#define DEFAULT_DEV_DIR "/dev" #define DEFAULT_DEV_DIR "/dev"
/* static functions */ /* static functions */
static void register_commands(void); static void register_commands(void);
static struct command *find_command(const char *name); static struct command *find_command(const char *name);
@ -548,7 +551,8 @@ static int process_common_commands(struct command *com)
/* Set autobackup if command takes this option */ /* Set autobackup if command takes this option */
for (l = 0; l < com->num_args; l++) for (l = 0; l < com->num_args; l++)
if (com->valid_args[l] == autobackup_ARG) if (com->valid_args[l] == autobackup_ARG)
if (!autobackup_init(_backup_dir)) if (!autobackup_init(_backup_dir, _backup_days,
_backup_number, _backup_auto))
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
/* Zero indicates it's OK to continue processing this command */ /* Zero indicates it's OK to continue processing this command */
@ -845,13 +849,22 @@ static int init(void)
dm_log_init(print_log); dm_log_init(print_log);
if (lvm_snprintf(_backup_dir, sizeof(_backup_dir), "%s", if (lvm_snprintf(_backup_dir, sizeof(_backup_dir), "%s/backup",
find_config_str(cmd->cf->root, "backup/dir", find_config_str(cmd->cf->root, "backup/dir",
'/', _sys_dir)) < 0) { '/', _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;
} }
_backup_days = find_config_int(cmd->cf->root, "backup/days", '/',
_backup_days);
_backup_number = find_config_int(cmd->cf->root, "backup/keep", '/',
_backup_number);
_backup_auto = find_config_int(cmd->cf->root, "backup/auto", '/',
_backup_auto);
if (!dev_cache_setup(cmd->cf)) if (!dev_cache_setup(cmd->cf))
return 0; return 0;

View File

@ -5,52 +5,45 @@
*/ */
#include "tools.h" #include "tools.h"
#include "format-text.h"
#include "metadata.h"
#include "lvm-string.h"
#include <ctype.h> static int _autobackup;
#include <limits.h>
static int _autobackup = 1;
static char _backup_dir[PATH_MAX]; static char _backup_dir[PATH_MAX];
static int _period = 7; /* backups will be kept for at least 7 days */ static int _keep_days; /* keep for at least this number of days */
static int _min_backups = 10; /* always have at least ten backups, even static int _keep_number; /* keep at least this number of backups */
* if they're older than the period */
/* /*
* Work out by looking at command line, config * Determine whether or not to do autobackup.
* file and environment variable whether we should * Cmd line overrides environment variable which in turn overrides config file
* do an autobackup.
*/ */
int autobackup_init(const char *system_dir) int autobackup_init(const char *backup_dir, int keep_days, int keep_number,
int autobackup)
{ {
char *lvm_autobackup; char *lvm_autobackup;
if (lvm_snprintf(_backup_dir, sizeof(_backup_dir), if (lvm_snprintf(_backup_dir, sizeof(_backup_dir),
"%s/backup", system_dir) < 0) { "%s", backup_dir) < 0) {
log_err("Backup directory (%s/backup) too long.", system_dir); log_error("Backup directory name too long.");
return 0; return 0;
} }
_keep_days = keep_days;
_keep_number = keep_number;
_autobackup = autobackup; /* Config file setting */
if (arg_count(autobackup_ARG)) { if (arg_count(autobackup_ARG)) {
_autobackup = !strcmp(arg_str_value(autobackup_ARG, "y"), "y"); _autobackup = !strcmp(arg_str_value(autobackup_ARG, "y"), "y");
return 1; return 1;
} }
_autobackup = 1; /* default */
lvm_autobackup = getenv("LVM_AUTOBACKUP"); lvm_autobackup = getenv("LVM_AUTOBACKUP");
if (!lvm_autobackup) if (!lvm_autobackup)
return 1; return 1;
log_print("using environment variable LVM_AUTOBACKUP " log_verbose("Setting autobackup from environment (LVM_AUTOBACKUP)");
"to set option A");
if (!strcasecmp(lvm_autobackup, "no")) if (!strcasecmp(lvm_autobackup, "no"))
_autobackup = 0; _autobackup = 0;
else if (strcasecmp(lvm_autobackup, "yes")) { else if (strcasecmp(lvm_autobackup, "yes")) {
log_error("environment variable LVM_AUTOBACKUP has " log_error("Environment variable LVM_AUTOBACKUP has "
"invalid value \"%s\"!", lvm_autobackup); "invalid value \"%s\"!", lvm_autobackup);
return 0; return 0;
} }
@ -78,8 +71,8 @@ static int __autobackup(struct volume_group *vg)
} }
if (!(backer = backup_format_create(vg->cmd, _backup_dir, if (!(backer = backup_format_create(vg->cmd, _backup_dir,
_period, _min_backups))) { _keep_days, _keep_number))) {
log_err("Couldn't create backup object."); log_error("Couldn't create backup object.");
return 0; return 0;
} }

View File

@ -22,7 +22,8 @@
#define _LVM_TOOLLIB_H #define _LVM_TOOLLIB_H
int autobackup_set(void); int autobackup_set(void);
int autobackup_init(const char *system_dir); int autobackup_init(const char *backup_dir, int keep_days, int keep_number,
int autobackup);
int autobackup(struct volume_group *vg); int autobackup(struct volume_group *vg);
int process_each_vg(int argc, char **argv, int process_each_vg(int argc, char **argv,

View File

@ -25,12 +25,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include "pool.h" #include "pool.h"
#include "dbg_malloc.h" #include "dbg_malloc.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "lvm-string.h"
#include "metadata.h" #include "metadata.h"
#include "config.h" #include "config.h"
#include "dev-cache.h" #include "dev-cache.h"
@ -42,6 +45,7 @@
#include "filter-composite.h" #include "filter-composite.h"
#include "filter-regex.h" #include "filter-regex.h"
#include "format1.h" #include "format1.h"
#include "format-text.h"
#include "toollib.h" #include "toollib.h"
#include "activate.h" #include "activate.h"