Import lvm2 from NetBSD
[dragonfly.git] / contrib / lvm2 / dist / tools / vgcfgrestore.c
1 /*      $NetBSD: vgcfgrestore.c,v 1.1.1.2 2009/12/02 00:25:56 haad Exp $        */
2
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #include "tools.h"
19
20 int vgcfgrestore(struct cmd_context *cmd, int argc, char **argv)
21 {
22         char *vg_name = NULL;
23
24         if (argc == 1) {
25                 vg_name = skip_dev_dir(cmd, argv[0], NULL);
26                 if (!validate_name(vg_name)) {
27                         log_error("Volume group name \"%s\" is invalid", vg_name);
28                         return ECMD_FAILED;
29                 }
30         } else if (!(arg_count(cmd, list_ARG) && arg_count(cmd, file_ARG))) {
31                 log_error("Please specify a *single* volume group to restore.");
32                 return ECMD_FAILED;
33         }
34
35         /*
36          * FIXME: overloading the -l arg for now to display a
37          * list of archive files for a particular vg
38          */
39         if (arg_count(cmd, list_ARG)) {
40                 if (!(arg_count(cmd,file_ARG) ?
41                             archive_display_file(cmd,
42                                 arg_str_value(cmd, file_ARG, "")) :
43                             archive_display(cmd, vg_name))) {
44                         stack;
45                         return ECMD_FAILED;
46                 }
47                 return ECMD_PROCESSED;
48         }
49
50         if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
51                 log_error("Unable to lock volume group %s", vg_name);
52                 return ECMD_FAILED;
53         }
54
55         if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
56                 log_error("Unable to lock orphans");
57                 unlock_vg(cmd, vg_name);
58                 return ECMD_FAILED;
59         }
60
61         cmd->handles_unknown_segments = 1;
62
63         if (!(arg_count(cmd, file_ARG) ?
64               backup_restore_from_file(cmd, vg_name,
65                                        arg_str_value(cmd, file_ARG, "")) :
66               backup_restore(cmd, vg_name))) {
67                 unlock_vg(cmd, VG_ORPHANS);
68                 unlock_vg(cmd, vg_name);
69                 log_error("Restore failed.");
70                 return ECMD_FAILED;
71         }
72
73         log_print("Restored volume group %s", vg_name);
74
75         unlock_vg(cmd, VG_ORPHANS);
76         unlock_vg(cmd, vg_name);
77         return ECMD_PROCESSED;
78 }