Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of LVM2.
6 : : *
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.
10 : : *
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
14 : : */
15 : :
16 : : #include "tools.h"
17 : :
18 : 0 : static char *_expand_filename(const char *template, const char *vg_name,
19 : : char **last_filename)
20 : : {
21 : : char *filename;
22 : :
23 [ # # ]: 0 : if (security_level())
24 : 0 : return dm_strdup(template);
25 : :
26 [ # # ]: 0 : if (!(filename = dm_malloc(PATH_MAX))) {
27 : 0 : log_error("Failed to allocate filename.");
28 : 0 : return NULL;
29 : : }
30 : :
31 [ # # ]: 0 : if (snprintf(filename, PATH_MAX, template, vg_name) < 0) {
32 : 0 : log_error("Error processing filename template %s",
33 : : template);
34 : 0 : dm_free(filename);
35 : 0 : return NULL;
36 : : }
37 [ # # ][ # # ]: 0 : if (*last_filename && !strncmp(*last_filename, filename, PATH_MAX)) {
38 : 0 : log_error("VGs must be backed up into different files. "
39 : : "Use %%s in filename for VG name.");
40 : 0 : dm_free(filename);
41 : 0 : return NULL;
42 : : }
43 : :
44 : 0 : dm_free(*last_filename);
45 : 0 : *last_filename = filename;
46 : :
47 : 0 : return filename;
48 : : }
49 : :
50 : 0 : static int vg_backup_single(struct cmd_context *cmd, const char *vg_name,
51 : : struct volume_group *vg,
52 : : void *handle)
53 : : {
54 : 0 : char **last_filename = (char **)handle;
55 : : char *filename;
56 : :
57 [ # # ]: 0 : if (arg_count(cmd, file_ARG)) {
58 [ # # ]: 0 : if (!(filename = _expand_filename(arg_value(cmd, file_ARG),
59 : 0 : vg->name, last_filename))) {
60 : 0 : stack;
61 : 0 : return ECMD_FAILED;
62 : : }
63 : :
64 [ # # ]: 0 : if (!backup_to_file(filename, vg->cmd->cmd_line, vg)) {
65 : 0 : stack;
66 : 0 : return ECMD_FAILED;
67 : : }
68 : : } else {
69 [ # # ]: 0 : if (vg_read_error(vg) == FAILED_INCONSISTENT) {
70 : 0 : log_error("No backup taken: specify filename with -f "
71 : : "to backup an inconsistent VG");
72 : 0 : stack;
73 : 0 : return ECMD_FAILED;
74 : : }
75 : :
76 : : /* just use the normal backup code */
77 : 0 : backup_enable(cmd, 1); /* force a backup */
78 [ # # ]: 0 : if (!backup(vg)) {
79 : 0 : stack;
80 : 0 : return ECMD_FAILED;
81 : : }
82 : : }
83 : :
84 : 0 : log_print("Volume group \"%s\" successfully backed up.", vg_name);
85 : 0 : return ECMD_PROCESSED;
86 : : }
87 : :
88 : 0 : int vgcfgbackup(struct cmd_context *cmd, int argc, char **argv)
89 : : {
90 : : int ret;
91 : 0 : char *last_filename = NULL;
92 : :
93 : 0 : init_pvmove(1);
94 : :
95 : 0 : ret = process_each_vg(cmd, argc, argv, READ_ALLOW_INCONSISTENT,
96 : : &last_filename, &vg_backup_single);
97 : :
98 : 0 : dm_free(last_filename);
99 : :
100 : 0 : init_pvmove(0);
101 : :
102 : 0 : return ret;
103 : : }
|