summaryrefslogtreecommitdiff
path: root/tools/lvmdevices.c
blob: a82f4d990c8ef3b151a9ac0b69759b0051a34093 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2020 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "tools.h"
#include "lib/cache/lvmcache.h"
#include "lib/filters/filter.h"
#include "lib/device/device_id.h"

int lvmdevices(struct cmd_context *cmd, int argc, char **argv)
{
	struct device *dev;
	struct use_id *uid;

	if (!setup_devices_file(cmd))
		return ECMD_FAILED;

	if (!cmd->enable_devices_file) {
		log_error("Devices file not enabled.");
		return ECMD_FAILED;
	}

	if (arg_is_set(cmd, update_ARG) ||
	    arg_is_set(cmd, adddev_ARG) || arg_is_set(cmd, deldev_ARG) ||
	    arg_is_set(cmd, addpvid_ARG) || arg_is_set(cmd, delpvid_ARG)) {
		if (!lock_devices_file(cmd, LOCK_EX)) {
			log_error("Failed to lock the devices file to create.");
			return ECMD_FAILED;
		}
		if (!devices_file_exists(cmd)) {
			if (!devices_file_touch(cmd)) {
				log_error("Failed to create the devices file.");
				return ECMD_FAILED;
			}
		}

		/*
		 * The hint file is associated with the default/system devices file,
		 * so don't clear hints when using a different --devicesfile.
		 */
		if (!cmd->devicesfile)
			clear_hint_file(cmd);
	} else {
		if (!lock_devices_file(cmd, LOCK_SH)) {
			log_error("Failed to lock the devices file.");
			return ECMD_FAILED;
		}
		if (!devices_file_exists(cmd)) {
			log_error("Devices file does not exist.");
			return ECMD_FAILED;
		}
	}

	if (!device_ids_read(cmd)) {
		log_error("Failed to read the devices file.");
		return ECMD_FAILED;
	}
	dev_cache_scan();
	device_ids_match(cmd);

	if (arg_is_set(cmd, check_ARG)) {
		/* For each cmd->use_device_ids:
		   reads pvid from header, sets dev->pvid and uid->pvid */
		label_scan_setup_bcache();
		device_ids_read_pvids(cmd);
		goto out;
	}

	if (arg_is_set(cmd, update_ARG)) {
		/* For each cmd->use_device_ids:
		   reads pvid from header, sets dev->pvid and uid->pvid */
		label_scan_setup_bcache();
		device_ids_read_pvids(cmd);

		/* Any uid fields found/set/fixed will be written. */
		if (!device_ids_write(cmd))
			goto_bad;
		goto out;
	}

	if (arg_is_set(cmd, adddev_ARG)) {
		const char *devname;

		if (!(devname = arg_str_value(cmd, adddev_ARG, NULL)))
			goto_bad;

		/*
		 * addev will add a device to devices_file even if that device
		 * is excluded by filters.
		 */

		/*
		 * No filter applied here (only the non-data filters would
		 * be applied since we haven't read the device yet.
		 */
		if (!(dev = dev_cache_get(cmd, devname, NULL))) {
			log_error("No device found for %s.", devname);
			goto_bad;
		}

		/*
		 * reads pvid from dev header, sets dev->pvid.
		 * (it's ok if the device is not a PV and has no PVID)
		 */
		label_scan_setup_bcache();
		device_id_read_pvid(cmd, dev);

		/*
		 * Allow filtered devices to be added to devices_file, but
		 * check if it's excluded by filters to print a warning.
		 * Since device_id_read_pvid has read the first 4K of the device,
		 * the filters should not for the most part need to do any further
		 * reading of the device.
		 *
		 * (This is the first time filters are being run, so we do
		 * not need to wipe filters of any previous result that was
		 * based on filter_deviceid_skip=0.)
		 */
		cmd->filter_deviceid_skip = 1;
		cmd->filter_regex_with_devices_file = 1;

		if (!cmd->filter->passes_filter(cmd, cmd->filter, dev, NULL)) {
			/* FIXME: print which filters */
			log_warn("WARNING: %s is currently excluded by filters.", dev_name(dev));
		}

		if (!device_id_add(cmd, dev, dev->pvid,
			      arg_str_value(cmd, deviceidtype_ARG, NULL),
			      arg_str_value(cmd, deviceid_ARG, NULL)))
			goto_bad;
		if (!device_ids_write(cmd))
			goto_bad;
		goto out;
	}

	if (arg_is_set(cmd, addpvid_ARG)) {
		struct dev_iter *iter;
		struct id id;
		char pvid[ID_LEN+1] = { 0 };
		const char *pvid_arg;
		struct device_list *devl, *safe;
		struct dm_list devs;

		dm_list_init(&devs);

		/*
		 * Iterate through all devs on the system, reading the
		 * pvid of each to check if it has this pvid.
		 * Devices that are excluded by no-data filters will not
		 * be checked for the PVID.
		 * addpvid will not add a device to devices_file if it's
		 * excluded by filters.
		 */

		pvid_arg = arg_str_value(cmd, addpvid_ARG, NULL);
		if (!id_read_format_try(&id, pvid_arg)) {
			log_error("Invalid PVID.");
			goto bad;
		}
		memcpy(pvid, &id.uuid, ID_LEN);

		if ((uid = get_uid_for_pvid(cmd, pvid))) {
			log_error("PVID already exists in devices_file for %s.", dev_name(uid->dev));
			goto bad;
		}

		/*
		 * Create a list of all devices on the system, without applying
		 * any filters, since we do not want filters to read any of the
		 * devices yet.
		 */
		if (!(iter = dev_iter_create(NULL, 0)))
			goto_bad;
		while ((dev = dev_iter_get(cmd, iter))) {
			if (!(devl = zalloc(sizeof(*devl))))
				continue;
			devl->dev = dev;
			dm_list_add(&devs, &devl->list);
                }
		dev_iter_destroy(iter);

		/*
		 * Apply the filters that do not require reading the devices
		 * (bcache is not set up yet, so any filter that requires
		 * reading the device will return EAGAIN.) The regex filter
		 * will be used and filter-deviceid not used.
		 */
		log_debug("Filtering devices without data");
		cmd->filter_deviceid_skip = 1;
		cmd->filter_regex_with_devices_file = 1;
		dm_list_iterate_items_safe(devl, safe, &devs) {
			if (!cmd->filter->passes_filter(cmd, cmd->filter, devl->dev, NULL))
				dm_list_del(&devl->list);
		}

		label_scan_setup_bcache();
		dev = NULL;
		dm_list_iterate_items(devl, &devs) {
			if (!device_id_read_pvid(cmd, devl->dev))
				continue;
			if (!strcmp(devl->dev->pvid, pvid)) {
				dev = devl->dev;
				break;
			}
		}

		if (!dev) {
			log_error("PVID %s not found on any devices.", pvid);
			goto bad;
		}

		/*
		 * Now that the device has been read, apply the filters again
		 * which will now include filters that read data from the device.
		 * N.B. we've already skipped devs that were excluded by the
		 * no-data filters, so if the PVID exists on one of those devices
		 * no warning is printed.
		 */
		log_debug("Filtering device with data");
		cmd->filter_deviceid_skip = 1;
		cmd->filter_regex_with_devices_file = 1;
		cmd->filter->wipe(cmd, cmd->filter, dev, NULL);
		if (!cmd->filter->passes_filter(cmd, cmd->filter, dev, NULL)) {
			/* FIXME: print which filters */
			log_error("PVID %s found on %s which is excluded by filters",
				 pvid, dev_name(dev));
			goto_bad;
		}

		if (!device_id_add(cmd, dev, dev->pvid, NULL, NULL))
			goto_bad;
		if (!device_ids_write(cmd))
			goto_bad;
		goto out;
	}

	if (arg_is_set(cmd, deldev_ARG)) {
		const char *devname;

		if (!(devname = arg_str_value(cmd, deldev_ARG, NULL)))
			goto_bad;

		/* we don't need to filter_deviceid_skip since we're
		   removing a dev from devices_file, that dev should
		   be in the devices_file and pass the filter */
		if (!(dev = dev_cache_get(cmd, devname, cmd->filter))) {
			log_error("No device found for %s.", devname);
			goto bad;
		}

		/* dev_cache_scan uses sysfs to check if an LV is using each dev
		   and sets this flag is so. */
		if (dev->flags & DEV_USED_FOR_LV) {
			if (!arg_count(cmd, yes_ARG) &&
			    yes_no_prompt("Device %s is used by an active LV, continue to remove? ", devname) == 'n') {
				log_error("Device not removed.");
				goto bad;
			}
		}

		if (!(uid = get_uid_for_dev(cmd, dev))) {
			log_error("Device not found in devices_file.");
			goto bad;
		}

		dm_list_del(&uid->list);
		free_uid(uid);
		device_ids_write(cmd);
		goto out;
	}

	if (arg_is_set(cmd, delpvid_ARG)) {
		struct id id;
		char pvid[ID_LEN+1] = { 0 };
		const char *pvid_arg;

		pvid_arg = arg_str_value(cmd, delpvid_ARG, NULL);
		if (!id_read_format_try(&id, pvid_arg)) {
			log_error("Invalid PVID.");
			goto bad;
		}
		memcpy(pvid, &id.uuid, ID_LEN);

		if (!(uid = get_uid_for_pvid(cmd, pvid))) {
			log_error("PVID not found in devices_file.");
			goto_bad;
		}

		if (uid->devname && (uid->devname[0] != '.')) {
			if ((dev = dev_cache_get(cmd, uid->devname, cmd->filter)) &&
			    (dev->flags & DEV_USED_FOR_LV)) {
				if (!arg_count(cmd, yes_ARG) &&
			    	    yes_no_prompt("Device %s is used by an active LV, continue to remove? ", uid->devname) == 'n') {
					log_error("Device not removed.");
					goto bad;
				}
			}
		}

		dm_list_del(&uid->list);
		free_uid(uid);
		device_ids_write(cmd);
		goto out;
	}

	/* If no options, print use_device_ids list */

	dm_list_iterate_items(uid, &cmd->use_device_ids) {
		char part_buf[64] = { 0 };

		if (uid->part)
			snprintf(part_buf, 63, " PART=%d", uid->part);

		log_print("Device %s IDTYPE=%s IDNAME=%s DEVNAME=%s PVID=%s%s",
			  uid->dev ? dev_name(uid->dev) : ".",
			  uid->idtype ? idtype_to_str(uid->idtype) : ".",
			  uid->idname ? uid->idname : ".",
			  uid->devname ? uid->devname : ".",
			  uid->pvid ? (char *)uid->pvid : ".",
			  part_buf);
	}

out:
	return ECMD_PROCESSED;

bad:
	return ECMD_FAILED;
}