File: evms_lvremove.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 (226 lines) | stat: -rw-r--r-- 5,641 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *   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_lvremove.c
 *
 *	Emulates LVM's 'lvremove' utility using the EVMS Engine. All options
 *	and several status messages are based on the original lvremove command
 *	from Heinz Mauelshagen and Sistina Software (www.sistina.com).
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.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_region_handle.c"
#include "helpers/is_lvm_region.c"
#include "helpers/open_engine.c"
#include "helpers/remove_duplicates.c"
#include "helpers/revert_volume.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 - Logical Volume Remove\n\n", cmd);
	return 0;
}


static int showhelp( void )
{
	showheader();
	printf("Synopsis:\n");
	printf("---------\n\n");
	printf("%s\n", cmd);
	printf("\t[-A/--autobackup y/n]\n");
	printf("\t[-d/--debug]\n");
	printf("\t[-f/--force]\n");
	printf("\t[-h/-?/--help]\n");
	printf("\t[-v/--verbose]\n");
	printf("\t[-V/--version]\n");
	printf("\tLogicalVolume[Path] [LogicalVolume[Path]...]\n\n");
	return 0;
}


static int parse_options( int		argc,
			char		** argv )
{
	int		c;
	char		* short_opts = "Adfh?vV";
	struct option	long_opts[] = {
				{ "autobackup",	required_argument, NULL, 'A'},
				{ "debug",	no_argument,       NULL, 'd'},
				{ "force",	no_argument,       NULL, 'f'},
				{ "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 'A':
			// -A is ignored by EVMS
			if ( optarg[0] != 'n' && optarg[0] != 'y' ) {
				printf( "%s -- ERROR option %c argument \"%s\"\n", cmd, c, optarg);
				return EINVAL;
			}
			break;
		case 'd':
			opts.debug++;
			opts.verbose++;
			break;
		case 'f':
			// -f is ignored by EVMS
			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		lv_handle;
	char			* lv_names[256]	= {0};
	int			number_of_lvs;
	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 LV names
	if ( optind == argc) {
		printf("%s -- please enter a logical volume path\n\n", cmd);
		return EINVAL;
	}
	number_of_lvs = argc - optind;

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

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

	// For each LV specified on the command line, find the Engine handle
	// and make sure it belongs to LVM. If the LV is a compatibility
	// volume, call evms_destroy. If it has no volume, call evms_remove.
	for ( i = 0; i < number_of_lvs; i++ ) {
		rc = get_region_handle(lv_names[i], &lv_handle);
		if (rc) {
			evms_close_engine();
			return rc;
		}

		// Does this region belong to LVM?
		if ( ! is_lvm_region(lv_handle) ) {
			printf("%s -- %s is not an LVM LV...skipping\n", cmd, lv_names[i]);
			continue;
		}

		// If the region has a compatibility volume, the volume must be
		// destroyed. If there is no volume, just remove the region. If
		// there is an EVMS volume, don't delete.
		rc = revert_volume_from_region(lv_handle);
		if (rc) {
			printf("%s -- Error removing LV %s (%d)\n", cmd, lv_names[i], rc);
			evms_close_engine();
			return rc;
		}

		printf("%s -- logical volume %s successfully removed\n", cmd, lv_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 changes committed to disk\n", cmd);
	}

	evms_close_engine();
	return 0;
}