File: evms_vgremove.c

package info (click to toggle)
evms 1.0.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,168 kB
  • ctags: 5,853
  • sloc: ansic: 87,317; makefile: 691; sh: 238
file content (203 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or 
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software 
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: LvmUtils
 * File: evms_vgremove.c
 *
 *	Emulates LVM's 'vgremove' utility using the EVMS Engine. All options
 *	and several status messages are based on the original vgremove command
 *	from Heinz Mauelshagen and Sistina Software (www.sistina.com).
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <ctype.h>
#include <frontend.h>

typedef struct cmd_options_s {
	int debug;
	int help;
	int verbose;
	int version;
} cmd_options_t;

static char * cmd = NULL;
static cmd_options_t opts;

#include "helpers/get_container_handle.c"
#include "helpers/open_engine.c"
#include "helpers/remove_duplicates.c"


static int showheader( void )
{
	// VERSION and DATE are defined in the top-level make.rules
	printf("Enterprise Volume Management System\n");
	printf("International Business Machines  %s\n", DATE);
	printf("LVM Emulation Utilities %s\n\n", VERSION);
	printf("%s --  Volume Group Remove\n",cmd);
	return 0;
}


static int showhelp( void )
{
	showheader();
	printf("Synopsis:\n");
	printf("---------\n\n");
	printf("%s\n", cmd);
	printf("\t[-d/--debug]\n");
	printf("\t[-h/--help]\n");
	printf("\t[-v/--verbose]\n");
	printf("\t[-V/--version]\n");
	printf("\tVolumeGroupName [VolumeGroupName...]\n");
	return 0;
}


static int parse_options( int		argc,
			char		** argv )
{
	int		c;
	char		* short_opts = "dh?vV";
	struct option	long_opts[] = {
				{ "debug",	no_argument, NULL, 'd'},
				{ "help",	no_argument, NULL, 'h'},
				{ "verbose",	no_argument, NULL, 'v'},
				{ "version",	no_argument, NULL, 'V'},
				{ NULL, 0, NULL, 0} };

    while ( (c = getopt_long(argc, argv, short_opts,
				long_opts, NULL)) != EOF ) {
		switch (c) {
		case 'd':
			opts.debug++;
			opts.verbose++;
			break;
		case 'h':
		case '?':
			opts.help++;
			break;
		case 'v':
			opts.verbose++;
			break;
		case 'V':
			opts.version++;
			break;
		default:
			printf("%s -- unrecognized option \"%c\"\n\n", cmd, c);
			return EINVAL;
		}
	}

	return 0;
}


int main( int argc, char *argv[] )
{
	object_handle_t		container;
	char			* vg_names[256] = {0};
	int			number_of_vgs;
	int			log_level = DEFAULT;
	int			i, rc;

	memset(&opts, 0, sizeof(cmd_options_t));
	cmd = basename(argv[0]);

	// Get the command line options.
	rc = parse_options(argc, argv);
	if (rc) {
		showhelp();
		return rc;
	}
	if ( opts.help ) {
		showhelp();
		return 0;
	}
	if ( opts.version ) {
		showheader();
		return 0;
	}
	if ( opts.verbose ) {
		log_level = DEBUG;
	}
	if ( opts.debug ) {
		log_level = ENTRY_EXIT;
	}

	// Check for volume group name.
	if ( optind == argc ) {
		printf("%s -- please enter a volume group name\n\n", cmd);
		return EINVAL;
	}
	number_of_vgs = argc - optind;

	// Copy the VG names from the command line, detecting any duplicates.
	rc = remove_duplicates(&argv[optind], vg_names, number_of_vgs);
	if (rc) {
		printf("%s -- Duplicate VGs specified. Please only specify each VG once\n", cmd);
		return rc;
	}

	// Open the EVMS Engine.
	rc = open_engine(ENGINE_READWRITE, log_level);
	if (rc) {
		return rc;
	}

	// Now we can actually remove each volume group, one by one. For now, if
	// any one fails, exit without committing, so nothing is changed.
	for ( i = 0; i < number_of_vgs; i++ ) {
		// Get the handle for the group to be deleted.
		rc = get_container_handle(vg_names[i], &container);
		if (rc) {
			evms_close_engine();
			return rc;
		}

		// Perform the removal.
		rc = evms_destroy(container);
		if (rc) {
			printf("%s -- Error removing container %s (%d)\n", cmd, vg_names[i], rc);
			evms_close_engine();
			return rc;
		}
		printf("%s -- volume group \"%s\" successfully removed\n", cmd, vg_names[i]);
	}

	// Write everything to disk.
	rc = evms_commit_changes(NULL);
	if (rc) {
		printf("%s -- Error committing changes to disk (%d)\n", cmd, rc);
		evms_close_engine();
		return rc;
	}

	if ( opts.verbose ) {
		printf("%s -- All VG changes committed to disk\n", cmd);
	}

	evms_close_engine();

	return 0;
}