]> sourceware.org Git - lvm2.git/blame - tools/vgchange.c
dev-type: detect mixed dos partition with gpt's PMBR
[lvm2.git] / tools / vgchange.c
CommitLineData
a381c45a 1/*
67cdbd7e 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
b90450b8 3 * Copyright (C) 2004-2013 Red Hat, Inc. All rights reserved.
a381c45a 4 *
6606c3ae 5 * This file is part of LVM2.
a381c45a 6 *
6606c3ae
AK
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
be684599 9 * of the GNU Lesser General Public License v.2.1.
a381c45a 10 *
be684599 11 * You should have received a copy of the GNU Lesser General Public License
6606c3ae 12 * along with this program; if not, write to the Free Software Foundation,
fcbef05a 13 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a381c45a
AK
14 */
15
16#include "tools.h"
8e62cbb1 17#include "lib/device/device_id.h"
62533ae3 18#include "lib/label/hints.h"
a381c45a 19
70bad998
DT
20struct vgchange_params {
21 int lock_start_count;
22 unsigned int lock_start_sanlock : 1;
d558b3ad 23 unsigned int vg_complete_to_activate : 1;
c609dedc 24 char *root_dm_uuid; /* dm uuid of LV under root fs */
70bad998
DT
25};
26
d1e8046f
AK
27/*
28 * Increments *count by the number of _new_ monitored devices.
29 */
8a37910d 30static int _monitor_lvs_in_vg(struct cmd_context *cmd,
d1e8046f 31 struct volume_group *vg, int reg, int *count)
3e3d5d85
AK
32{
33 struct lv_list *lvl;
34 struct logical_volume *lv;
176bbea0 35 int r = 1;
3e3d5d85 36
2c44337b 37 dm_list_iterate_items(lvl, &vg->lvs) {
3e3d5d85
AK
38 lv = lvl->lv;
39
2caa558e 40 if (!lv_info(cmd, lv, lv_is_thin_pool(lv) ? 1 : 0,
93fbef1a 41 NULL, 0, 0))
2caa558e 42 continue;
3e3d5d85
AK
43 /*
44 * FIXME: Need to consider all cases... PVMOVE, etc
45 */
2360ce35 46 if (lv_is_pvmove(lv))
3e3d5d85
AK
47 continue;
48
2d6fcbf6 49 if (!monitor_dev_for_events(cmd, lv, 0, reg)) {
176bbea0 50 r = 0;
3e3d5d85 51 continue;
03aeb867
ZK
52 }
53
54 (*count)++;
3e3d5d85
AK
55 }
56
d1e8046f 57 return r;
3e3d5d85
AK
58}
59
7a8fa6aa
MS
60static int _poll_lvs_in_vg(struct cmd_context *cmd,
61 struct volume_group *vg)
62{
63 struct lv_list *lvl;
64 struct logical_volume *lv;
7a8fa6aa
MS
65 int count = 0;
66
67 dm_list_iterate_items(lvl, &vg->lvs) {
68 lv = lvl->lv;
69
18259d55 70 if (lv_is_active(lv) &&
2360ce35 71 (lv_is_pvmove(lv) || lv_is_converting(lv) || lv_is_merging(lv))) {
df06d9ac
MS
72 lv_spawn_background_polling(cmd, lv);
73 count++;
74 }
7a8fa6aa
MS
75 }
76
77 /*
78 * returns the number of polled devices
79 * - there is no way to know if lv is already being polled
80 */
81
82 return count;
83}
84
95ced7a7
PR
85static int _activate_lvs_in_vg(struct cmd_context *cmd, struct volume_group *vg,
86 activation_change_t activate)
8c013da4 87{
f2b7349e 88 struct lv_list *lvl;
8c013da4 89 struct logical_volume *lv;
4c629a52 90 int count = 0, expected_count = 0, r = 1;
8c013da4 91
41449383 92 sigint_allow();
2c44337b 93 dm_list_iterate_items(lvl, &vg->lvs) {
41449383
ZK
94 if (sigint_caught())
95 return_0;
96
f2b7349e 97 lv = lvl->lv;
8c013da4 98
112846ce 99 if (!lv_is_visible(lv) && (!cmd->process_component_lvs || !lv_is_component(lv)))
b9ac4b0c
MB
100 continue;
101
89880365
MB
102 /* If LV is sparse, activate origin instead */
103 if (lv_is_cow(lv) && lv_is_virtual_origin(origin_from_cow(lv)))
104 lv = origin_from_cow(lv);
105
46a29c02 106 /* Only request activation of snapshot origin devices */
a8f5e1f2 107 if (lv_is_snapshot(lv) || lv_is_cow(lv))
a76ba817
AK
108 continue;
109
c8c24ccd 110 /* Only request activation of mirror LV */
a8f5e1f2 111 if (lv_is_mirror_image(lv) || lv_is_mirror_log(lv))
c8c24ccd
MB
112 continue;
113
12213445
ZK
114 if (lv_is_vdo_pool(lv))
115 continue;
116
7e671e5d 117 if (lv_activation_skip(lv, activate, arg_is_set(cmd, ignoreactivationskip_ARG)))
ab789c1b 118 continue;
ab789c1b 119
a81a2406
ZK
120 if ((activate == CHANGE_AAY) &&
121 !lv_passes_auto_activation_filter(cmd, lv))
31271606 122 continue;
215a314f 123
0a28e3c4
DT
124 /* vg NOAUTOACTIVATE flag was already checked */
125 if ((activate == CHANGE_AAY) && (lv->status & LV_NOAUTOACTIVATE))
126 continue;
127
155c608c
MB
128 expected_count++;
129
33703995
ZK
130 if (!lv_change_activate(cmd, lv, activate)) {
131 stack;
132 r = 0;
8c013da4 133 continue;
33703995 134 }
8c013da4 135
8c013da4
AK
136 count++;
137 }
138
41449383 139 sigint_restore();
4c629a52 140
218c5741
ZK
141 if (expected_count)
142 log_verbose("%sctivated %d logical volumes in volume group %s.",
143 is_change_activating(activate) ? "A" : "Dea",
144 count, vg->name);
145
146 /*
39b7d1ba 147 * After successful activation we need to initialise polling
218c5741
ZK
148 * for all activated LVs in a VG. Possible enhancement would
149 * be adding --poll y|n cmdline option for pvscan and call
150 * init_background_polling routine in autoactivation handler.
151 */
152 if (count && is_change_activating(activate) &&
153 !vgchange_background_polling(cmd, vg)) {
154 stack;
155 r = 0;
156 }
157
4c629a52
AK
158 /* Wait until devices are available */
159 if (!sync_local_dev_names(vg->cmd)) {
160 log_error("Failed to sync local devices for VG %s.", vg->name);
161 r = 0;
162 }
41449383 163
33703995 164 return r;
8c013da4
AK
165}
166
3e3d5d85
AK
167static int _vgchange_monitoring(struct cmd_context *cmd, struct volume_group *vg)
168{
176bbea0 169 int r = 1;
d1e8046f 170 int monitored = 0;
3e3d5d85 171
e78d473b 172 if (lvs_in_vg_activated(vg) &&
20db8ffc 173 dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE) {
176bbea0
AK
174 if (!_monitor_lvs_in_vg(cmd, vg, dmeventd_monitor_mode(), &monitored))
175 r = 0;
438e0050
AK
176 log_print_unless_silent("%d logical volume(s) in volume group "
177 "\"%s\" %smonitored",
178 monitored, vg->name, (dmeventd_monitor_mode()) ? "" : "un");
3e3d5d85
AK
179 }
180
176bbea0 181 return r;
3e3d5d85
AK
182}
183
40701af9 184int vgchange_background_polling(struct cmd_context *cmd, struct volume_group *vg)
7a8fa6aa
MS
185{
186 int polled;
187
4e8af1d3 188 if (background_polling()) {
218c5741
ZK
189 log_debug_activation("Starting background polling for volume group \"%s\".", vg->name);
190 polled = _poll_lvs_in_vg(cmd, vg);
178ff2be 191 if (polled)
438e0050
AK
192 log_print_unless_silent("Background polling started for %d logical volume(s) "
193 "in volume group \"%s\"",
194 polled, vg->name);
7a8fa6aa
MS
195 }
196
176bbea0 197 return 1;
7a8fa6aa
MS
198}
199
95ced7a7 200int vgchange_activate(struct cmd_context *cmd, struct volume_group *vg,
c609dedc 201 activation_change_t activate, int vg_complete_to_activate, char *root_dm_uuid)
a381c45a 202{
e6fd16f8 203 int lv_open, active, monitored = 0, r = 1;
2ada982e 204 const struct lv_list *lvl;
d558b3ad 205 struct pv_list *pvl;
e6fd16f8 206 int do_activate = is_change_activating(activate);
36cfd887 207
cccc2b29
DT
208 /*
209 * We can get here in the odd case where an LV is already active in
210 * a foreign VG, which allows the VG to be accessed by vgchange -a
211 * so the LV can be deactivated.
212 */
24774959
DT
213 if (vg->system_id && vg->system_id[0] &&
214 cmd->system_id && cmd->system_id[0] &&
cccc2b29 215 strcmp(vg->system_id, cmd->system_id) &&
439a579a 216 do_activate) {
cccc2b29 217 log_error("Cannot activate LVs in a foreign VG.");
70b159d1 218 return 0;
cccc2b29
DT
219 }
220
0a28e3c4
DT
221 if ((activate == CHANGE_AAY) && (vg->status & NOAUTOACTIVATE)) {
222 log_debug("Autoactivation is disabled for VG %s.", vg->name);
223 return 1;
224 }
225
d558b3ad
DT
226 if (do_activate && vg_complete_to_activate) {
227 dm_list_iterate_items(pvl, &vg->pvs) {
228 if (!pvl->pv->dev) {
229 log_print("VG %s is incomplete.", vg->name);
230 return 1;
231 }
232 }
233 }
234
19089ba3
PR
235 /*
236 * Safe, since we never write out new metadata here. Required for
237 * partial activation to work.
238 */
95ced7a7 239 cmd->handles_missing_pvs = 1;
88be836f 240
a381c45a 241 /* FIXME: Force argument to deactivate them? */
0da29600 242 if (!do_activate) {
2ada982e 243 dm_list_iterate_items(lvl, &vg->lvs)
0da29600
DT
244 label_scan_invalidate_lv(cmd, lvl->lv);
245
246 if ((lv_open = lvs_in_vg_opened(vg))) {
247 dm_list_iterate_items(lvl, &vg->lvs) {
12213445
ZK
248 if (lv_is_visible(lvl->lv) &&
249 !lv_is_vdo_pool(lvl->lv) && // FIXME: API skip flag missing
250 !lv_check_not_in_use(lvl->lv, 1)) {
0da29600
DT
251 log_error("Can't deactivate volume group \"%s\" with %d open logical volume(s)",
252 vg->name, lv_open);
253 return 0;
254 }
2ada982e 255 }
0da29600 256 }
a381c45a
AK
257 }
258
24e65464 259 /* FIXME Move into library where clvmd can use it */
95ced7a7 260 if (do_activate)
24e65464 261 check_current_backup(vg);
112846ce
ZK
262 else /* Component LVs might be active, support easy deactivation */
263 cmd->process_component_lvs = 1;
24e65464 264
95ced7a7 265 if (do_activate && (active = lvs_in_vg_activated(vg))) {
08907484 266 log_verbose("%d logical volume(s) in volume group \"%s\" "
8c013da4 267 "already active", active, vg->name);
20db8ffc 268 if (dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE) {
176bbea0
AK
269 if (!_monitor_lvs_in_vg(cmd, vg, dmeventd_monitor_mode(), &monitored))
270 r = 0;
20db8ffc
AK
271 log_verbose("%d existing logical volume(s) in volume "
272 "group \"%s\" %smonitored",
273 monitored, vg->name,
274 dmeventd_monitor_mode() ? "" : "un");
275 }
3e3d5d85 276 }
3080a754 277
4319e06a
ZK
278 if (!_activate_lvs_in_vg(cmd, vg, activate)) {
279 stack;
176bbea0 280 r = 0;
4319e06a 281 }
a381c45a 282
c609dedc
DT
283 /*
284 * Possibly trigger auto-generation of system.devices:
285 * - if root_dm_uuid contains vg->id, and
286 * - /etc/lvm/devices/auto-import-rootvg exists, and
287 * - /etc/lvm/devices/system.devices does not exist, then
288 * - create /run/lvm/lvm-devices-import to
289 * trigger lvm-devices-import.path and .service
290 * - lvm-devices-import will run vgimportdevices --rootvg
291 * to create system.devices
292 */
293 if (root_dm_uuid) {
294 char path[PATH_MAX];
295 struct stat info;
296 FILE *fp;
297
298 if (memcmp(root_dm_uuid + 4, &vg->id, ID_LEN))
299 goto out;
300
301 if (cmd->enable_devices_file || devices_file_exists(cmd))
302 goto out;
303
304 if (dm_snprintf(path, sizeof(path), "%s/devices/auto-import-rootvg", cmd->system_dir) < 0)
305 goto out;
306
307 if (stat(path, &info) < 0)
308 goto out;
309
310 log_debug("Found %s creating %s", path, DEVICES_IMPORT_PATH);
311
312 if (!(fp = fopen(DEVICES_IMPORT_PATH, "w"))) {
313 log_debug("failed to create %s", DEVICES_IMPORT_PATH);
314 goto out;
315 }
316 if (fclose(fp))
317 stack;
318 }
319out:
df7edc00 320 /* Print message only if there was not found a missing VG */
6ac1e04b
DT
321 log_print_unless_silent("%d logical volume(s) in volume group \"%s\" now active",
322 lvs_in_vg_activated(vg), vg->name);
176bbea0
AK
323 return r;
324}
325
326static int _vgchange_refresh(struct cmd_context *cmd, struct volume_group *vg)
327{
328 log_verbose("Refreshing volume group \"%s\"", vg->name);
329
47419d21
ZK
330 if (!vg_refresh_visible(cmd, vg))
331 return_0;
176bbea0
AK
332
333 return 1;
a381c45a
AK
334}
335
a0a23eff
AK
336static int _vgchange_alloc(struct cmd_context *cmd, struct volume_group *vg)
337{
338 alloc_policy_t alloc;
339
fbf6b89a 340 alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_NORMAL);
a0a23eff 341
5d623bde 342 /* FIXME: make consistent with vg_set_alloc_policy() */
a0a23eff
AK
343 if (alloc == vg->alloc) {
344 log_error("Volume group allocation policy is already %s",
345 get_alloc_string(vg->alloc));
176bbea0 346 return 0;
651ff9b3 347 }
a0a23eff 348
176bbea0
AK
349 if (!vg_set_alloc_policy(vg, alloc))
350 return_0;
a0a23eff 351
176bbea0 352 return 1;
a0a23eff
AK
353}
354
cfb7bfc7
AK
355static int _vgchange_resizeable(struct cmd_context *cmd,
356 struct volume_group *vg)
a381c45a 357{
237c5480 358 int resizeable = arg_int_value(cmd, resizeable_ARG, 0);
a381c45a 359
68fac97a 360 if (resizeable && vg_is_resizeable(vg)) {
6fda126d
AK
361 log_error("Volume group \"%s\" is already resizeable",
362 vg->name);
176bbea0 363 return 0;
a381c45a
AK
364 }
365
68fac97a 366 if (!resizeable && !vg_is_resizeable(vg)) {
08907484 367 log_error("Volume group \"%s\" is already not resizeable",
a381c45a 368 vg->name);
176bbea0 369 return 0;
651ff9b3 370 }
614a4508 371
b80f32dd
AK
372 if (resizeable)
373 vg->status |= RESIZEABLE_VG;
a381c45a 374 else
b80f32dd 375 vg->status &= ~RESIZEABLE_VG;
a381c45a 376
176bbea0 377 return 1;
a381c45a
AK
378}
379
0a28e3c4
DT
380static int _vgchange_autoactivation(struct cmd_context *cmd,
381 struct volume_group *vg)
382{
383 int aa_no_arg = !arg_int_value(cmd, setautoactivation_ARG, 0);
384 int aa_no_meta = (vg->status & NOAUTOACTIVATE) ? 1 : 0;
385
386 if ((aa_no_arg && aa_no_meta) || (!aa_no_arg && !aa_no_meta)) {
387 log_error("Volume group autoactivation is already %s.",
388 aa_no_arg ? "no" : "yes");
389 return 0;
390 }
391
392 if (aa_no_arg)
393 vg->status |= NOAUTOACTIVATE;
394 else
395 vg->status &= ~NOAUTOACTIVATE;
396
397 return 1;
398}
399
cfb7bfc7
AK
400static int _vgchange_logicalvolume(struct cmd_context *cmd,
401 struct volume_group *vg)
a381c45a 402{
8ef2b021 403 uint32_t max_lv = arg_uint_value(cmd, logicalvolume_ARG, 0);
a381c45a 404
176bbea0
AK
405 if (!vg_set_max_lv(vg, max_lv))
406 return_0;
a381c45a 407
176bbea0 408 return 1;
a381c45a 409}
5a52dca9 410
e95d8284
AK
411static int _vgchange_physicalvolumes(struct cmd_context *cmd,
412 struct volume_group *vg)
413{
414 uint32_t max_pv = arg_uint_value(cmd, maxphysicalvolumes_ARG, 0);
415
176bbea0
AK
416 if (!vg_set_max_pv(vg, max_pv))
417 return_0;
e95d8284 418
176bbea0 419 return 1;
e95d8284
AK
420}
421
66278a80
AK
422static int _vgchange_pesize(struct cmd_context *cmd, struct volume_group *vg)
423{
424 uint32_t extent_size;
425
be3510b2 426 if (arg_uint64_value(cmd, physicalextentsize_ARG, 0) > MAX_EXTENT_SIZE) {
81ef1fd0 427 log_warn("WARNING: Physical extent size cannot be larger than %s.",
f260f99d 428 display_size(cmd, (uint64_t) MAX_EXTENT_SIZE));
be3510b2
MB
429 return 1;
430 }
431
204a12e5 432 extent_size = arg_uint_value(cmd, physicalextentsize_ARG, 0);
de5dfec5 433 /* FIXME: remove check - redundant with vg_change_pesize */
66278a80 434 if (extent_size == vg->extent_size) {
81ef1fd0 435 log_warn("WARNING: Physical extent size of VG %s is already %s.",
f260f99d 436 vg->name, display_size(cmd, (uint64_t) extent_size));
176bbea0 437 return 1;
66278a80
AK
438 }
439
176bbea0
AK
440 if (!vg_set_extent_size(vg, extent_size))
441 return_0;
66278a80 442
32080c4f
PR
443 if (!vg_check_pv_dev_block_sizes(vg)) {
444 log_error("Failed to change physical extent size for VG %s.",
445 vg->name);
446 return 0;
447 }
448
176bbea0 449 return 1;
66278a80
AK
450}
451
176bbea0
AK
452static int _vgchange_addtag(struct cmd_context *cmd, struct volume_group *vg)
453{
b51cd542 454 return change_tag(cmd, vg, NULL, NULL, addtag_ARG);
176bbea0 455}
cf6dd251 456
176bbea0
AK
457static int _vgchange_deltag(struct cmd_context *cmd, struct volume_group *vg)
458{
b51cd542 459 return change_tag(cmd, vg, NULL, NULL, deltag_ARG);
cf6dd251
AK
460}
461
08f1ddea 462static int _vgchange_uuid(struct cmd_context *cmd __attribute__((unused)),
72b2cb61 463 struct volume_group *vg)
15e6db35
AK
464{
465 struct lv_list *lvl;
8e62cbb1 466 struct id old_vg_id;
15e6db35
AK
467
468 if (lvs_in_vg_activated(vg)) {
b1b60887 469 log_error("Volume group has active logical volumes.");
176bbea0 470 return 0;
651ff9b3 471 }
15e6db35 472
8e62cbb1
DT
473 memcpy(&old_vg_id, &vg->id, ID_LEN);
474
52f9afec
AK
475 if (!id_create(&vg->id)) {
476 log_error("Failed to generate new random UUID for VG %s.",
477 vg->name);
176bbea0 478 return 0;
52f9afec 479 }
15e6db35 480
2c44337b 481 dm_list_iterate_items(lvl, &vg->lvs) {
15e6db35
AK
482 memcpy(&lvl->lv->lvid, &vg->id, sizeof(vg->id));
483 }
484
8e62cbb1
DT
485 /*
486 * If any LVs in this VG have PVs stacked on them, then
487 * update the device_id of the stacked PV.
488 */
489 device_id_update_vg_uuid(cmd, vg, &old_vg_id);
490
176bbea0 491 return 1;
da1ba4ed
PR
492}
493
1779cc34
DW
494static int _vgchange_metadata_copies(struct cmd_context *cmd,
495 struct volume_group *vg)
496{
12eadbab
AK
497 uint32_t mda_copies = arg_uint_value(cmd, vgmetadatacopies_ARG, DEFAULT_VGMETADATACOPIES);
498
7eaa3ade
DT
499 log_debug("vgchange_metadata_copies new %u vg_mda_copies %u D %u",
500 mda_copies, vg_mda_copies(vg), DEFAULT_VGMETADATACOPIES);
1e2420bc 501
1779cc34 502 if (mda_copies == vg_mda_copies(vg)) {
12eadbab 503 if (vg_mda_copies(vg) == VGMETADATACOPIES_UNMANAGED)
81ef1fd0 504 log_warn("WARNING: Number of metadata copies for VG %s is already unmanaged.",
f260f99d 505 vg->name);
12eadbab 506 else
81ef1fd0 507 log_warn("WARNING: Number of metadata copies for VG %s is already %u.",
f260f99d 508 vg->name, mda_copies);
176bbea0 509 return 1;
1779cc34
DW
510 }
511
176bbea0
AK
512 if (!vg_set_mda_copies(vg, mda_copies))
513 return_0;
1779cc34 514
176bbea0 515 return 1;
1779cc34
DW
516}
517
0cd47947
PR
518static int _vgchange_profile(struct cmd_context *cmd,
519 struct volume_group *vg)
520{
521 const char *old_profile_name, *new_profile_name;
522 struct profile *new_profile;
523
524 old_profile_name = vg->profile ? vg->profile->name : "(no profile)";
525
7e671e5d 526 if (arg_is_set(cmd, detachprofile_ARG)) {
0cd47947
PR
527 new_profile_name = "(no profile)";
528 vg->profile = NULL;
529 } else {
7e671e5d 530 if (arg_is_set(cmd, metadataprofile_ARG))
9e3e4d69
PR
531 new_profile_name = arg_str_value(cmd, metadataprofile_ARG, NULL);
532 else
533 new_profile_name = arg_str_value(cmd, profile_ARG, NULL);
534 if (!(new_profile = add_profile(cmd, new_profile_name, CONFIG_PROFILE_METADATA)))
0cd47947
PR
535 return_0;
536 vg->profile = new_profile;
537 }
538
539 log_verbose("Changing configuration profile for VG %s: %s -> %s.",
540 vg->name, old_profile_name, new_profile_name);
541
542 return 1;
543}
544
8cdec4c4
DT
545/*
546 * This function will not be called unless the local host is allowed to use the
547 * VG. Either the VG has no system_id, or the VG and host have matching
548 * system_ids, or the host has the VG's current system_id in its
2fc29289 549 * extra_system_ids list. This function is not allowed to change the system_id
8cdec4c4
DT
550 * of a foreign VG (VG owned by another host).
551 */
8cdec4c4
DT
552static int _vgchange_system_id(struct cmd_context *cmd, struct volume_group *vg)
553{
b18feb98
AK
554 const char *system_id;
555 const char *system_id_arg_str = arg_str_value(cmd, systemid_ARG, NULL);
8cdec4c4 556
ac6a4cd7
AK
557 if (!(system_id = system_id_from_string(cmd, system_id_arg_str))) {
558 log_error("Unable to set system ID.");
8cdec4c4 559 return 0;
b18feb98
AK
560 }
561
b18feb98 562 if (!strcmp(vg->system_id, system_id)) {
458b0210 563 log_error("Volume Group system ID is already \"%s\".", vg->system_id);
b18feb98
AK
564 return 0;
565 }
8cdec4c4 566
458b0210
AK
567 if (!*system_id && cmd->system_id && strcmp(system_id, cmd->system_id)) {
568 log_warn("WARNING: Removing the system ID allows unsafe access from other hosts.");
ac6a4cd7 569
7e671e5d 570 if (!arg_is_set(cmd, yes_ARG) &&
458b0210
AK
571 yes_no_prompt("Remove system ID %s from volume group %s? [y/n]: ",
572 vg->system_id, vg->name) == 'n') {
573 log_error("System ID of volume group %s not changed.", vg->name);
574 return 0;
575 }
576 }
8cdec4c4 577
458b0210
AK
578 if (*system_id && (!cmd->system_id || strcmp(system_id, cmd->system_id))) {
579 if (lvs_in_vg_activated(vg)) {
580 log_error("Logical Volumes in VG %s must be deactivated before system ID can be changed.",
581 vg->name);
582 return 0;
583 }
8cdec4c4 584
458b0210
AK
585 if (cmd->system_id)
586 log_warn("WARNING: Requested system ID %s does not match local system ID %s.",
587 system_id, cmd->system_id ? : "");
588 else
589 log_warn("WARNING: No local system ID is set.");
590 log_warn("WARNING: Volume group %s might become inaccessible from this machine.",
591 vg->name);
592
7e671e5d 593 if (!arg_is_set(cmd, yes_ARG) &&
458b0210
AK
594 yes_no_prompt("Set foreign system ID %s on volume group %s? [y/n]: ",
595 system_id, vg->name) == 'n') {
596 log_error("Volume group %s system ID not changed.", vg->name);
597 return 0;
8cdec4c4
DT
598 }
599 }
600
458b0210 601 log_verbose("Changing system ID for VG %s from \"%s\" to \"%s\".",
b18feb98
AK
602 vg->name, vg->system_id, system_id);
603
8cdec4c4 604 vg->system_id = system_id;
c6a57dc4 605
8cdec4c4
DT
606 return 1;
607}
608
fe70b03d
DT
609static int _passes_lock_start_filter(struct cmd_context *cmd,
610 struct volume_group *vg,
611 const int cfg_id)
612{
613 const struct dm_config_node *cn;
614 const struct dm_config_value *cv;
615 const char *str;
616
617 /* undefined list means no restrictions, all vg names pass */
618
3b6840e0 619 cn = find_config_tree_array(cmd, cfg_id, NULL);
fe70b03d
DT
620 if (!cn)
621 return 1;
622
623 /* with a defined list, the vg name must be included to pass */
624
625 for (cv = cn->v; cv; cv = cv->next) {
626 if (cv->type == DM_CFG_EMPTY_ARRAY)
627 break;
628 if (cv->type != DM_CFG_STRING) {
b1b60887 629 log_error("Ignoring invalid string in lock_start list.");
fe70b03d
DT
630 continue;
631 }
632 str = cv->v.str;
633 if (!*str) {
b1b60887 634 log_error("Ignoring empty string in config file.");
fe70b03d
DT
635 continue;
636 }
637
638 /* ignoring tags for now */
639
640 if (!strcmp(str, vg->name))
641 return 1;
642 }
643
644 return 0;
645}
646
70bad998
DT
647static int _vgchange_lock_start(struct cmd_context *cmd, struct volume_group *vg,
648 struct vgchange_params *vp)
fe70b03d 649{
fe70b03d 650 int auto_opt = 0;
e158835a 651 int exists = 0;
70bad998 652 int r;
fe70b03d 653
b6f0f20d 654 if (!vg_is_shared(vg))
9ab6bdce
DT
655 return 1;
656
657 if (arg_is_set(cmd, force_ARG))
fe70b03d
DT
658 goto do_start;
659
9ab6bdce 660 /*
9ab6bdce
DT
661 * Any waiting is done at the end of vgchange.
662 */
943e9790 663 if ((cmd->lockopt & LOCKOPT_AUTO) || (cmd->lockopt & LOCKOPT_AUTONOWAIT))
fe70b03d
DT
664 auto_opt = 1;
665
666 if (!_passes_lock_start_filter(cmd, vg, activation_lock_start_list_CFG)) {
667 log_verbose("Not starting %s since it does not pass lock_start_list", vg->name);
668 return 1;
669 }
670
671 if (auto_opt && !_passes_lock_start_filter(cmd, vg, activation_auto_lock_start_list_CFG)) {
672 log_verbose("Not starting %s since it does not pass auto_lock_start_list", vg->name);
673 return 1;
674 }
675
676do_start:
9ea5ff3d 677 r = lockd_start_vg(cmd, vg, &exists);
70bad998
DT
678
679 if (r)
680 vp->lock_start_count++;
e158835a
DT
681 else if (exists)
682 vp->lock_start_count++;
70bad998
DT
683 if (!strcmp(vg->lock_type, "sanlock"))
684 vp->lock_start_sanlock = 1;
685
686 return r;
fe70b03d
DT
687}
688
689static int _vgchange_lock_stop(struct cmd_context *cmd, struct volume_group *vg)
690{
fe70b03d
DT
691 return lockd_stop_vg(cmd, vg);
692}
693
f1cc5b12
AK
694static int _vgchange_single(struct cmd_context *cmd, const char *vg_name,
695 struct volume_group *vg,
696 struct processing_handle *handle)
5a52dca9 697{
d558b3ad 698 struct vgchange_params *vp = (struct vgchange_params *)handle->custom_handle;
98414ca7 699 int ret = ECMD_PROCESSED;
c5d253f3 700 unsigned i;
70e3d0a6 701 activation_change_t activate;
17b27464 702 int changed = 0;
176bbea0 703
c5d253f3 704 static const struct {
176bbea0
AK
705 int arg;
706 int (*fn)(struct cmd_context *cmd, struct volume_group *vg);
707 } _vgchange_args[] = {
708 { logicalvolume_ARG, &_vgchange_logicalvolume },
709 { maxphysicalvolumes_ARG, &_vgchange_physicalvolumes },
710 { resizeable_ARG, &_vgchange_resizeable },
0a28e3c4 711 { setautoactivation_ARG, &_vgchange_autoactivation },
176bbea0
AK
712 { deltag_ARG, &_vgchange_deltag },
713 { addtag_ARG, &_vgchange_addtag },
714 { physicalextentsize_ARG, &_vgchange_pesize },
715 { uuid_ARG, &_vgchange_uuid },
716 { alloc_ARG, &_vgchange_alloc },
176bbea0 717 { vgmetadatacopies_ARG, &_vgchange_metadata_copies },
9e3e4d69 718 { metadataprofile_ARG, &_vgchange_profile },
8cdec4c4
DT
719 { profile_ARG, &_vgchange_profile },
720 { detachprofile_ARG, &_vgchange_profile },
176bbea0 721 };
cfb7bfc7 722
f407c8af
MS
723 /*
724 * FIXME: DEFAULT_BACKGROUND_POLLING should be "unspecified".
725 * If --poll is explicitly provided use it; otherwise polling
726 * should only be started if the LV is not already active. So:
727 * 1) change the activation code to say if the LV was actually activated
728 * 2) make polling of an LV tightly coupled with LV activation
d50c6d4b
PR
729 *
730 * Do not initiate any polling if --sysinit option is used.
f407c8af 731 */
7e671e5d 732 init_background_polling(arg_is_set(cmd, sysinit_ARG) ? 0 :
d50c6d4b
PR
733 arg_int_value(cmd, poll_ARG,
734 DEFAULT_BACKGROUND_POLLING));
7a8fa6aa 735
c5d253f3 736 for (i = 0; i < DM_ARRAY_SIZE(_vgchange_args); ++i) {
7e671e5d 737 if (arg_is_set(cmd, _vgchange_args[i].arg)) {
b90450b8
ZK
738 if (!_vgchange_args[i].fn(cmd, vg))
739 return_ECMD_FAILED;
17b27464 740 changed = 1;
176bbea0
AK
741 }
742 }
743
17b27464 744 if (changed) {
b90450b8
ZK
745 if (!vg_write(vg) || !vg_commit(vg))
746 return_ECMD_FAILED;
176bbea0 747
b1b60887 748 log_print_unless_silent("Volume group \"%s\" successfully changed.", vg->name);
176bbea0
AK
749 }
750
7e671e5d 751 if (arg_is_set(cmd, activate_ARG)) {
f38a5422 752 activate = (activation_change_t) arg_uint_value(cmd, activate_ARG, 0);
c609dedc 753 if (!vgchange_activate(cmd, vg, activate, vp->vg_complete_to_activate, vp->root_dm_uuid))
b90450b8 754 return_ECMD_FAILED;
a35098b1 755 } else if (arg_is_set(cmd, refresh_ARG)) {
178ff2be 756 /* refreshes the visible LVs (which starts polling) */
176bbea0 757 if (!_vgchange_refresh(cmd, vg))
b90450b8 758 return_ECMD_FAILED;
a35098b1 759 } else {
178ff2be 760 /* -ay* will have already done monitoring changes */
a35098b1
ZK
761 if (arg_is_set(cmd, monitor_ARG) &&
762 !_vgchange_monitoring(cmd, vg))
b90450b8 763 return_ECMD_FAILED;
3e3d5d85 764
39b7d1ba 765 /* When explicitly specified --poll */
a35098b1
ZK
766 if (arg_is_set(cmd, poll_ARG) &&
767 !vgchange_background_polling(cmd, vg))
768 return_ECMD_FAILED;
769 }
cf6dd251 770
07cd88dc 771 return ret;
5a52dca9
AK
772}
773
c609dedc
DT
774/*
775 * Automatic creation of system.devices for root VG on first boot
776 * is useful for OS images where the OS installer is not used to
777 * customize the OS for system.
778 *
779 * - OS image prep:
780 * . rm /etc/lvm/devices/system.devices (if it exists)
781 * . touch /etc/lvm/devices/auto-import-rootvg
782 * . enable lvm-devices-import.path
783 * . enable lvm-devices-import.service
784 *
785 * - lvchange -ay <rootvg>/<rootlv>
786 * . run by initrd so root fs can be mounted
787 * . does not use system.devices
788 * . named <rootvg>/<rootlv> comes from kernel command line rd.lvm
789 * . uses first device that appears containing the named root LV
790 *
791 * - vgchange -aay <rootvg>
792 * . triggered by udev when all PVs from root VG are online
793 * . activate LVs in root VG (in addition to the already active root LV)
794 * . check for /etc/lvm/devices/auto-import-rootvg (found)
795 * . check for /etc/lvm/devices/system.devices (not found)
796 * . create /run/lvm/lvm-devices-import because
797 * auto-import-rootvg was found and system.devices was not found
798 *
799 * - lvm-devices-import.path
800 * . triggered by /run/lvm/lvm-devices-import
801 * . start lvm-devices-import.service
802 *
803 * - lvm-devices-import.service
804 * . check for /etc/lvm/devices/system.devices, do nothing if found
805 * . run vgimportdevices --rootvg --auto
806 *
807 * - vgimportdevices --rootvg --auto
808 * . check for /etc/lvm/devices/auto-import-rootvg (found)
809 * . check for /etc/lvm/devices/system.devices (not found)
810 * . creates /etc/lvm/devices/system.devices for PVs in root VG
811 * . removes /etc/lvm/devices/auto-import-rootvg
812 * . removes /run/lvm/lvm-devices-import
813 *
814 * On future startup, /etc/lvm/devices/system.devices will exist,
815 * and /etc/lvm/devices/auto-import-rootvg will not exist, so
816 * vgchange -aay <rootvg> will not create /run/lvm/lvm-devices-import,
817 * and lvm-devices-import.path and lvm-device-import.service will not run.
818 *
819 * lvm-devices-import.path:
820 * [Path]
821 * PathExists=/run/lvm/lvm-devices-import
822 * Unit=lvm-devices-import.service
823 * ConditionPathExists=!/etc/lvm/devices/system.devices
824 *
825 * lvm-devices-import.service:
826 * [Service]
827 * Type=oneshot
828 * RemainAfterExit=no
829 * ExecStart=/usr/sbin/vgimportdevices --rootvg --auto
830 * ConditionPathExists=!/etc/lvm/devices/system.devices
831 */
c609dedc
DT
832static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out)
833{
834 char path[PATH_MAX];
c609dedc 835 struct stat info;
c609dedc
DT
836
837 if (cmd->enable_devices_file || devices_file_exists(cmd))
838 return;
839
840 if (dm_snprintf(path, sizeof(path), "%s/devices/auto-import-rootvg", cmd->system_dir) < 0)
841 return;
842
843 if (stat(path, &info) < 0)
844 return;
845
88e0060a
ZK
846 if (!get_rootvg_dev_uuid(cmd, dm_uuid_out))
847 stack;
c609dedc
DT
848}
849
92e741ed
DT
850static int _vgchange_autoactivation_setup(struct cmd_context *cmd,
851 struct vgchange_params *vp,
852 int *skip_command,
853 const char **vgname_ret,
854 uint32_t *flags)
d558b3ad
DT
855{
856 const char *aa;
92e741ed 857 char *vgname = NULL;
0e0faf30 858 int vg_locked = 0;
92e741ed 859 int found_none = 0, found_all = 0, found_incomplete = 0;
d558b3ad
DT
860
861 if (!(aa = arg_str_value(cmd, autoactivation_ARG, NULL)))
92e741ed 862 return_0;
d558b3ad
DT
863
864 if (strcmp(aa, "event")) {
865 log_print("Skip vgchange for unknown autoactivation value.");
866 *skip_command = 1;
867 return 1;
868 }
869
870 if (!find_config_tree_bool(cmd, global_event_activation_CFG, NULL)) {
871 log_print("Skip vgchange for event and event_activation=0.");
872 *skip_command = 1;
873 return 1;
874 }
875
876 vp->vg_complete_to_activate = 1;
62533ae3 877 cmd->use_hints = 0;
d558b3ad 878
92e741ed 879 /*
0e0faf30 880 * Add an option to skip the pvs_online optimization? e.g.
92e741ed
DT
881 * "online_skip" in --autoactivation / auto_activation_settings
882 *
0e0faf30
DT
883 * if (online_skip)
884 * return 1;
885 */
886
887 /* reads devices file, does not populate dev-cache */
888 if (!setup_devices_for_online_autoactivation(cmd))
889 return_0;
890
891 get_single_vgname_cmd_arg(cmd, NULL, &vgname);
892
c609dedc
DT
893 _get_rootvg_dev(cmd, &vp->root_dm_uuid);
894
0e0faf30
DT
895 /*
896 * Lock the VG before scanning the PVs so _vg_read can avoid the normal
897 * lock_vol+rescan (READ_WITHOUT_LOCK avoids the normal lock_vol and
898 * can_use_one_scan avoids the normal rescan.) If this early lock_vol
899 * fails, continue and use the normal lock_vol in _vg_read.
92e741ed 900 */
0e0faf30
DT
901 if (vgname) {
902 if (!lock_vol(cmd, vgname, LCK_VG_WRITE, NULL)) {
903 log_debug("Failed early VG locking for autoactivation.");
904 } else {
905 *flags |= READ_WITHOUT_LOCK;
906 cmd->can_use_one_scan = 1;
907 vg_locked = 1;
908 }
909 }
92e741ed
DT
910
911 /*
912 * Perform label_scan on PVs that are online (per /run/lvm files)
913 * for the given VG (or when no VG name is given, all online PVs.)
914 * If this fails, the caller will do a normal process_each_vg without
915 * optimizations (which will do a full label_scan.)
916 */
917 if (!label_scan_vg_online(cmd, vgname, &found_none, &found_all, &found_incomplete)) {
918 log_print("PVs online error%s%s, using all devices.", vgname ? " for VG " : "", vgname ?: "");
89c54db1 919 goto bad;
92e741ed
DT
920 }
921
922 /*
923 * Not the expected usage, activate any VGs that are complete based on
924 * pvs_online. Only online pvs are used.
925 */
926 if (!vgname) {
927 *flags |= PROCESS_SKIP_SCAN;
928 return 1;
929 }
930
931 /*
89c54db1
DT
932 * The expected and optimal usage, which is the purpose of
933 * this function. We expect online files to be found for
934 * all PVs because the udev rule calls
935 * vgchange -aay --autoactivation event <vgname>
936 * only after all PVs for vgname are found online.
92e741ed
DT
937 */
938 if (found_all) {
939 *flags |= PROCESS_SKIP_SCAN;
940 *vgname_ret = vgname;
941 return 1;
942 }
943
92e741ed
DT
944 /*
945 * Not expected usage, no online pvs for the vgname were found. The
946 * caller will fall back to process_each doing a full label_scan to
947 * look for the VG. (No optimization used.)
948 */
949 if (found_none) {
950 log_print("PVs online not found for VG %s, using all devices.", vgname);
89c54db1 951 goto bad;
92e741ed
DT
952 }
953
954 /*
955 * Not expected usage, only some online pvs for the vgname were found.
956 * The caller will fall back to process_each doing a full label_scan to
957 * look for all PVs in the VG. (No optimization used.)
958 */
959 if (found_incomplete) {
7da47cea 960 log_print("PVs online incomplete for VG %s, using all devices.", vgname);
89c54db1 961 goto bad;
92e741ed
DT
962 }
963
964 /*
965 * Shouldn't happen, the caller will fall back to standard
966 * process_each. (No optimization used.)
967 */
968 log_print("PVs online unknown for VG %s, using all devices.", vgname);
89c54db1
DT
969
970 bad:
971 /*
972 * The online scanning optimization didn't work, so undo the vg
973 * locking optimization before falling back to normal processing.
974 */
975 if (vg_locked) {
976 unlock_vg(cmd, NULL, vgname);
977 *flags &= ~READ_WITHOUT_LOCK;
978 cmd->can_use_one_scan = 0;
979 }
7ec97ed5
ZK
980
981 free(vgname);
982
d558b3ad 983 return 1;
89c54db1 984
d558b3ad
DT
985}
986
5a52dca9
AK
987int vgchange(struct cmd_context *cmd, int argc, char **argv)
988{
d558b3ad 989 struct vgchange_params vp = { 0 };
70bad998 990 struct processing_handle *handle;
92e741ed 991 const char *vgname = NULL;
3da88b89 992 uint32_t flags = 0;
fe70b03d
DT
993 int ret;
994
8cdec4c4 995 int noupdate =
7e671e5d 996 arg_is_set(cmd, activate_ARG) ||
7e671e5d
AK
997 arg_is_set(cmd, monitor_ARG) ||
998 arg_is_set(cmd, poll_ARG) ||
999 arg_is_set(cmd, refresh_ARG);
8cdec4c4 1000
3501f17f 1001 int update_partial_safe =
7e671e5d
AK
1002 arg_is_set(cmd, deltag_ARG) ||
1003 arg_is_set(cmd, addtag_ARG) ||
1004 arg_is_set(cmd, metadataprofile_ARG) ||
1005 arg_is_set(cmd, profile_ARG) ||
1006 arg_is_set(cmd, detachprofile_ARG);
8cdec4c4 1007
3501f17f 1008 int update_partial_unsafe =
7e671e5d
AK
1009 arg_is_set(cmd, logicalvolume_ARG) ||
1010 arg_is_set(cmd, maxphysicalvolumes_ARG) ||
1011 arg_is_set(cmd, resizeable_ARG) ||
0a28e3c4 1012 arg_is_set(cmd, setautoactivation_ARG) ||
7e671e5d
AK
1013 arg_is_set(cmd, uuid_ARG) ||
1014 arg_is_set(cmd, physicalextentsize_ARG) ||
7e671e5d 1015 arg_is_set(cmd, alloc_ARG) ||
1460cac2 1016 arg_is_set(cmd, vgmetadatacopies_ARG);
8cdec4c4 1017
3501f17f 1018 int update = update_partial_safe || update_partial_unsafe;
176bbea0 1019
8cdec4c4
DT
1020 if (!update && !noupdate) {
1021 log_error("Need one or more command options.");
5a52dca9
AK
1022 return EINVALID_CMD_LINE;
1023 }
1024
7e671e5d
AK
1025 if ((arg_is_set(cmd, profile_ARG) || arg_is_set(cmd, metadataprofile_ARG)) &&
1026 arg_is_set(cmd, detachprofile_ARG)) {
9e3e4d69 1027 log_error("Only one of --metadataprofile and --detachprofile permitted.");
0cd47947
PR
1028 return EINVALID_CMD_LINE;
1029 }
1030
7e671e5d 1031 if (arg_is_set(cmd, activate_ARG) && arg_is_set(cmd, refresh_ARG)) {
178ff2be
MS
1032 log_error("Only one of -a and --refresh permitted.");
1033 return EINVALID_CMD_LINE;
1034 }
1035
7e671e5d
AK
1036 if ((arg_is_set(cmd, ignorelockingfailure_ARG) ||
1037 arg_is_set(cmd, sysinit_ARG)) && update) {
b1b60887 1038 log_error("Only -a permitted with --ignorelockingfailure and --sysinit.");
d50c6d4b
PR
1039 return EINVALID_CMD_LINE;
1040 }
1041
7e671e5d
AK
1042 if (arg_is_set(cmd, activate_ARG) &&
1043 (arg_is_set(cmd, monitor_ARG) || arg_is_set(cmd, poll_ARG))) {
506ade80 1044 if (!is_change_activating((activation_change_t) arg_uint_value(cmd, activate_ARG, 0))) {
178ff2be
MS
1045 log_error("Only -ay* allowed with --monitor or --poll.");
1046 return EINVALID_CMD_LINE;
1047 }
1048 }
1049
7e671e5d 1050 if (arg_is_set(cmd, poll_ARG) && arg_is_set(cmd, sysinit_ARG)) {
d50c6d4b 1051 log_error("Only one of --poll and --sysinit permitted.");
5a52dca9
AK
1052 return EINVALID_CMD_LINE;
1053 }
1054
7e671e5d 1055 if (arg_is_set(cmd, maxphysicalvolumes_ARG) &&
fbf6b89a 1056 arg_sign_value(cmd, maxphysicalvolumes_ARG, SIGN_NONE) == SIGN_MINUS) {
b1b60887 1057 log_error("MaxPhysicalVolumes may not be negative.");
176bbea0
AK
1058 return EINVALID_CMD_LINE;
1059 }
1060
7e671e5d 1061 if (arg_is_set(cmd, physicalextentsize_ARG) &&
fbf6b89a 1062 arg_sign_value(cmd, physicalextentsize_ARG, SIGN_NONE) == SIGN_MINUS) {
b1b60887 1063 log_error("Physical extent size may not be negative.");
176bbea0
AK
1064 return EINVALID_CMD_LINE;
1065 }
1066
7e671e5d 1067 if (arg_is_set(cmd, clustered_ARG) && !argc && !arg_is_set(cmd, yes_ARG) &&
1a1d3a10
AK
1068 (yes_no_prompt("Change clustered property of all volumes groups? [y/n]: ") == 'n')) {
1069 log_error("No volume groups changed.");
1070 return ECMD_FAILED;
1071 }
1072
3501f17f
JB
1073 if (!update || !update_partial_unsafe)
1074 cmd->handles_missing_pvs = 1;
1075
87714a13
DT
1076 if (noupdate)
1077 cmd->ignore_device_name_mismatch = 1;
1078
8e62cbb1
DT
1079 /*
1080 * If the devices file includes PVs stacked on LVs, then
1081 * vgchange --uuid may need to update the devices file.
1082 * No PV-on-LV stacked is done without scan_lvs set.
1083 */
1084 if (arg_is_set(cmd, uuid_ARG) && cmd->scan_lvs)
1085 cmd->edit_devices_file = 1;
1086
fe70b03d
DT
1087 /*
1088 * Include foreign VGs that contain active LVs.
1089 * That shouldn't happen in general, but if it does by some
1090 * mistake, then we want to allow those LVs to be deactivated.
1091 */
cccc2b29
DT
1092 if (arg_is_set(cmd, activate_ARG))
1093 cmd->include_active_foreign_vgs = 1;
1094
1460cac2
DT
1095 /* The default vg lock mode is ex, but these options only need sh. */
1096 if ((cmd->command->command_enum == vgchange_activate_CMD) ||
1097 (cmd->command->command_enum == vgchange_refresh_CMD)) {
1098 cmd->lockd_vg_default_sh = 1;
1099 /* Allow deactivating if locks fail. */
1100 if (is_change_activating((activation_change_t)arg_uint_value(cmd, activate_ARG, CHANGE_AY)))
1101 cmd->lockd_vg_enforce_sh = 1;
1102 }
fe70b03d 1103
d558b3ad
DT
1104 if (arg_is_set(cmd, autoactivation_ARG)) {
1105 int skip_command = 0;
92e741ed 1106 if (!_vgchange_autoactivation_setup(cmd, &vp, &skip_command, &vgname, &flags))
d558b3ad
DT
1107 return ECMD_FAILED;
1108 if (skip_command)
1109 return ECMD_PROCESSED;
1110 }
1111
72f0b637
DT
1112 /*
1113 * Do not use udev for device listing or device info because
1114 * vgchange --monitor y is called during boot when udev is being
1115 * initialized and is not yet ready to be used.
1116 */
1117 if (arg_is_set(cmd, monitor_ARG) &&
1118 arg_int_value(cmd, monitor_ARG, DEFAULT_DMEVENTD_MONITOR)) {
1119 init_obtain_device_list_from_udev(0);
1120 init_external_device_info_source(DEV_EXT_NONE);
1121 }
1122
d16142f9 1123 if (update)
3da88b89 1124 flags |= READ_FOR_UPDATE;
65d23a1d 1125 else if (arg_is_set(cmd, activate_ARG) ||
1126 arg_is_set(cmd, refresh_ARG))
d16142f9 1127 flags |= READ_FOR_ACTIVATE;
3da88b89 1128
70bad998
DT
1129 if (!(handle = init_processing_handle(cmd, NULL))) {
1130 log_error("Failed to initialize processing handle.");
1131 return ECMD_FAILED;
1132 }
1133
d558b3ad
DT
1134 handle->custom_handle = &vp;
1135
92e741ed 1136 ret = process_each_vg(cmd, argc, argv, vgname, NULL, flags, 0, handle, &_vgchange_single);
fe70b03d 1137
70bad998 1138 destroy_processing_handle(cmd, handle);
fe70b03d 1139 return ret;
5a52dca9 1140}
f611b68f 1141
4dc009c8 1142static int _vgchange_locktype(struct cmd_context *cmd, struct volume_group *vg, int *no_change)
f611b68f
DT
1143{
1144 const char *lock_type = arg_str_value(cmd, locktype_ARG, NULL);
f611b68f
DT
1145 struct lv_list *lvl;
1146 struct logical_volume *lv;
1147 int lv_lock_count = 0;
1148
1149 /* Special recovery case. */
943e9790 1150 if (lock_type && !strcmp(lock_type, "none") && (cmd->lockopt & LOCKOPT_FORCE)) {
f611b68f
DT
1151 vg->status &= ~CLUSTERED;
1152 vg->lock_type = "none";
1153 vg->lock_args = NULL;
1154
1155 dm_list_iterate_items(lvl, &vg->lvs)
1156 lvl->lv->lock_args = NULL;
1157
1158 return 1;
1159 }
1160
1161 if (!vg->lock_type) {
1162 if (vg_is_clustered(vg))
1163 vg->lock_type = "clvm";
1164 else
1165 vg->lock_type = "none";
1166 }
1167
f331eb1c 1168 if (lock_type && !strcmp(vg->lock_type, lock_type)) {
81ef1fd0 1169 log_warn("WARNING: New lock type %s matches the current lock type %s.",
f611b68f 1170 lock_type, vg->lock_type);
4dc009c8 1171 *no_change = 1;
f611b68f
DT
1172 return 1;
1173 }
1174
1175 if (is_lockd_type(vg->lock_type) && is_lockd_type(lock_type)) {
1176 log_error("Cannot change lock type directly from \"%s\" to \"%s\".",
1177 vg->lock_type, lock_type);
1178 log_error("First change lock type to \"none\", then to \"%s\".",
1179 lock_type);
1180 return 0;
1181 }
1182
f611b68f
DT
1183 /*
1184 * When lvm is currently using lvmlockd, this function can:
1185 * - change none to lockd type
1186 * - change none to clvm (with warning about not being able to use it)
1187 * - change lockd type to none
1188 * - change lockd type to clvm (with warning about not being able to use it)
1189 * - change clvm to none
1190 * - change clvm to lockd type
1191 */
1192
1193 if (lvs_in_vg_activated(vg)) {
1194 log_error("Changing VG %s lock type not allowed with active LVs",
1195 vg->name);
1196 return 0;
1197 }
1198
f611b68f 1199 /* clvm to none */
8db2527c 1200 if (lock_type && !strcmp(vg->lock_type, "clvm") && !strcmp(lock_type, "none")) {
f611b68f
DT
1201 vg->status &= ~CLUSTERED;
1202 vg->lock_type = "none";
1203 return 1;
1204 }
1205
1206 /* clvm to ..., first undo clvm */
1207 if (!strcmp(vg->lock_type, "clvm")) {
1208 vg->status &= ~CLUSTERED;
1209 }
1210
1211 /*
1212 * lockd type to ..., first undo lockd type
1213 */
1214 if (is_lockd_type(vg->lock_type)) {
943e9790 1215 if (!lockd_free_vg_before(cmd, vg, 1, 0))
f611b68f
DT
1216 return 0;
1217
1218 lockd_free_vg_final(cmd, vg);
1219
1220 vg->status &= ~CLUSTERED;
1221 vg->lock_type = "none";
1222 vg->lock_args = NULL;
1223
1224 dm_list_iterate_items(lvl, &vg->lvs)
1225 lvl->lv->lock_args = NULL;
1226 }
1227
f611b68f
DT
1228 /* ... to lockd type */
1229 if (is_lockd_type(lock_type)) {
1230 /*
1231 * For lock_type dlm, lockd_init_vg() will do a single
1232 * vg_write() that sets lock_type, sets lock_args, clears
1233 * system_id, and sets all LV lock_args to dlm.
1234 * For lock_type sanlock, lockd_init_vg() needs to know
1235 * how many LV locks are needed so that it can make the
1236 * sanlock lv large enough.
1237 */
1238 dm_list_iterate_items(lvl, &vg->lvs) {
1239 lv = lvl->lv;
1240
1241 if (lockd_lv_uses_lock(lv)) {
1242 lv_lock_count++;
1243
1244 if (!strcmp(lock_type, "dlm"))
1245 lv->lock_args = "dlm";
1246 }
1247 }
1248
1249 /*
1250 * See below. We cannot set valid LV lock_args until stage 1
1251 * of the change is done, so we need to skip the validation of
1252 * the lock_args during stage 1.
1253 */
1254 if (!strcmp(lock_type, "sanlock"))
1255 vg->skip_validate_lock_args = 1;
1256
1257 vg->system_id = NULL;
1258
1259 if (!lockd_init_vg(cmd, vg, lock_type, lv_lock_count)) {
1260 log_error("Failed to initialize lock args for lock type %s", lock_type);
1261 return 0;
1262 }
1263
1264 /*
1265 * For lock_type sanlock, there must be multiple steps
1266 * because the VG needs an active lvmlock LV before
1267 * LV lock areas can be allocated, which must be done
1268 * before LV lock_args are written. So, the LV lock_args
1269 * remain unset during the first stage of the conversion.
1270 *
1271 * Stage 1:
1272 * lockd_init_vg() creates and activates the lvmlock LV,
1273 * then sets lock_type, sets lock_args, and clears system_id.
1274 *
1275 * Stage 2:
1276 * We get here, and can now set LV lock_args. This uses
1277 * the standard code path for allocating LV locks in
1278 * vg_write() by setting LV lock_args to "pending",
1279 * which tells vg_write() to call lockd_init_lv()
1280 * and sets the lv->lock_args value before writing the VG.
1281 */
1282 if (!strcmp(lock_type, "sanlock")) {
1283 dm_list_iterate_items(lvl, &vg->lvs) {
1284 lv = lvl->lv;
1285 if (lockd_lv_uses_lock(lv))
1286 lv->lock_args = "pending";
1287 }
1288
1289 vg->skip_validate_lock_args = 0;
1290 }
1291
1292 return 1;
1293 }
1294
1295 /* ... to none */
8db2527c 1296 if (lock_type && !strcmp(lock_type, "none")) {
f611b68f
DT
1297 vg->lock_type = NULL;
1298 vg->system_id = cmd->system_id ? dm_pool_strdup(vg->vgmem, cmd->system_id) : NULL;
1299 return 1;
1300 }
1301
1302 log_error("Cannot change to unknown lock type %s", lock_type);
1303 return 0;
1304}
1305
1306static int _vgchange_locktype_single(struct cmd_context *cmd, const char *vg_name,
1307 struct volume_group *vg,
1308 struct processing_handle *handle)
1309{
4dc009c8
DT
1310 int no_change = 0;
1311
1312 if (!_vgchange_locktype(cmd, vg, &no_change))
f611b68f
DT
1313 return_ECMD_FAILED;
1314
4dc009c8
DT
1315 if (no_change)
1316 return ECMD_PROCESSED;
1317
f611b68f
DT
1318 if (!vg_write(vg) || !vg_commit(vg))
1319 return_ECMD_FAILED;
1320
f6a54a50
DT
1321 /*
1322 * When init_vg_sanlock is called for vgcreate, the lockspace remains
1323 * started and lvmlock remains active, but when called for
1324 * vgchange --locktype sanlock, the lockspace is not started so the
1325 * lvmlock LV should be deactivated at the end. vg_write writes the
1326 * new leases to lvmlock, so we need to wait until after vg_write to
1327 * deactivate it.
1328 */
1329 if (vg->lock_type && !strcmp(vg->lock_type, "sanlock") &&
33fdeaf3
ZK
1330 (cmd->command->command_enum == vgchange_locktype_CMD)) {
1331 if (!deactivate_lv(cmd, vg->sanlock_lv)) {
7da47cea 1332 log_error("Failed to deactivate %s.",
33fdeaf3
ZK
1333 display_lvname(vg->sanlock_lv));
1334 return ECMD_FAILED;
1335 }
1336 }
f6a54a50 1337
b1b60887 1338 log_print_unless_silent("Volume group \"%s\" successfully changed.", vg->name);
f611b68f
DT
1339
1340 return ECMD_PROCESSED;
1341}
1342
1343int vgchange_locktype_cmd(struct cmd_context *cmd, int argc, char **argv)
1344{
1345 struct processing_handle *handle;
1346 const char *lock_type = arg_str_value(cmd, locktype_ARG, NULL);
f611b68f
DT
1347 int ret;
1348
943e9790 1349
f611b68f
DT
1350 /*
1351 * vgchange --locktype none --lockopt force VG
1352 *
1353 * This is a special/forced exception to change the lock type to none.
1354 * It's needed for recovery cases and skips the normal steps of undoing
1355 * the current lock type. It's a way to forcibly get access to a VG
1356 * when the normal locking mechanisms are not working.
1357 *
1358 * It ignores: the current lvm locking config, lvmlockd, the state of
1359 * the vg on other hosts, etc. It is meant to just remove any locking
1360 * related metadata from the VG (cluster/lock_type flags, lock_type,
1361 * lock_args).
1362 *
1363 * This can be necessary when manually recovering from certain failures.
1364 * e.g. when a pv is lost containing the lvmlock lv (holding sanlock
1365 * leases), the vg lock_type needs to be changed to none, and then
1366 * back to sanlock, which recreates the lvmlock lv and leases.
1367 *
1368 * Set lockd_gl_disable, lockd_vg_disable, lockd_lv_disable to
1369 * disable locking. lockd_gl(), lockd_vg() and lockd_lv() will
1370 * just return success when they see the disable flag set.
1371 */
943e9790 1372 if (cmd->lockopt & LOCKOPT_FORCE) {
ea9b2c21 1373 if (!arg_is_set(cmd, yes_ARG) &&
4dc009c8 1374 yes_no_prompt("Forcibly change VG lock type to %s? [y/n]: ", lock_type) == 'n') {
f611b68f
DT
1375 log_error("VG lock type not changed.");
1376 return 0;
1377 }
1378
1379 cmd->lockd_gl_disable = 1;
1380 cmd->lockd_vg_disable = 1;
1381 cmd->lockd_lv_disable = 1;
1382 cmd->handles_missing_pvs = 1;
e53cfc6a 1383 cmd->force_access_clustered = 1;
f611b68f
DT
1384 goto process;
1385 }
1386
1387 if (!lvmlockd_use()) {
1388 log_error("Using lock type requires lvmlockd.");
1389 return 0;
1390 }
1391
1392 /*
1393 * This is a special case where taking the global lock is
1394 * not needed to protect global state, because the change is
1395 * only to an existing VG. But, taking the global lock ex is
1396 * helpful in this case to trigger a global cache validation
1397 * on other hosts, to cause them to see the new system_id or
1398 * lock_type.
1399 */
8c87dda1 1400 if (!lockd_global(cmd, "ex"))
f611b68f
DT
1401 return 0;
1402
1403process:
1404 if (!(handle = init_processing_handle(cmd, NULL))) {
1405 log_error("Failed to initialize processing handle.");
1406 return ECMD_FAILED;
1407 }
1408
1409 ret = process_each_vg(cmd, argc, argv, NULL, NULL, READ_FOR_UPDATE, 0, handle, &_vgchange_locktype_single);
1410
1411 destroy_processing_handle(cmd, handle);
1412 return ret;
1413}
1414
07cd88dc
DT
1415static int _vgchange_lock_start_stop_single(struct cmd_context *cmd, const char *vg_name,
1416 struct volume_group *vg,
1417 struct processing_handle *handle)
1418{
1419 struct vgchange_params *vp = (struct vgchange_params *)handle->custom_handle;
1420
1421 if (arg_is_set(cmd, lockstart_ARG)) {
1422 if (!_vgchange_lock_start(cmd, vg, vp))
1423 return_ECMD_FAILED;
1424 } else if (arg_is_set(cmd, lockstop_ARG)) {
1425 if (!_vgchange_lock_stop(cmd, vg))
1426 return_ECMD_FAILED;
1427 }
1428
1429 return ECMD_PROCESSED;
1430}
1431
1432int vgchange_lock_start_stop_cmd(struct cmd_context *cmd, int argc, char **argv)
1433{
1434 struct processing_handle *handle;
1435 struct vgchange_params vp = { 0 };
1436 int ret;
1437
1438 if (!lvmlockd_use()) {
1439 log_error("Using lock start and lock stop requires lvmlockd.");
1440 return 0;
1441 }
1442
1443 if (!(handle = init_processing_handle(cmd, NULL))) {
1444 log_error("Failed to initialize processing handle.");
1445 return ECMD_FAILED;
1446 }
1447
1448 if (arg_is_set(cmd, lockstop_ARG))
1449 cmd->lockd_vg_default_sh = 1;
1450
1451 /*
1452 * Starting lockspaces. For VGs not yet started, locks are not
1453 * available to acquire, and for VGs already started, there's nothing
1454 * to do, so disable VG locks. Try to acquire the global lock sh to
1455 * validate the cache (if no gl is available, lockd_gl will force a
1456 * cache validation). If the global lock is available, it can be
39b7d1ba 1457 * beneficial to hold sh to serialize lock-start with vgremove of the
07cd88dc
DT
1458 * same VG from another host.
1459 */
1460 if (arg_is_set(cmd, lockstart_ARG)) {
1461 cmd->lockd_vg_disable = 1;
1462
8c87dda1 1463 if (!lockd_global(cmd, "sh"))
b1b60887 1464 log_debug("No global lock for lock start.");
07cd88dc
DT
1465
1466 /* Disable the lockd_gl in process_each_vg. */
1467 cmd->lockd_gl_disable = 1;
b4402bd8
DT
1468 } else {
1469 /* If the VG was started when it was exported, allow it to be stopped. */
1470 cmd->include_exported_vgs = 1;
07cd88dc
DT
1471 }
1472
1473 handle->custom_handle = &vp;
1474
b4402bd8 1475 ret = process_each_vg(cmd, argc, argv, NULL, NULL, 0, 0, handle, &_vgchange_lock_start_stop_single);
07cd88dc
DT
1476
1477 /* Wait for lock-start ops that were initiated in vgchange_lockstart. */
1478
1479 if (arg_is_set(cmd, lockstart_ARG) && vp.lock_start_count) {
8c87dda1 1480 if (!lockd_global(cmd, "un"))
07cd88dc
DT
1481 stack;
1482
943e9790
DT
1483 if ((cmd->lockopt & LOCKOPT_NOWAIT) || (cmd->lockopt & LOCKOPT_AUTONOWAIT)) {
1484 log_print_unless_silent("Starting locking. VG can only be read until locks are ready.");
1485 } else {
07cd88dc 1486 if (vp.lock_start_sanlock)
4d8fb10a 1487 log_print_unless_silent("Starting locking. Waiting for sanlock may take a few seconds to 3 min...");
07cd88dc
DT
1488 else
1489 log_print_unless_silent("Starting locking. Waiting until locks are ready...");
1490 lockd_start_wait(cmd);
07cd88dc
DT
1491 }
1492 }
1493
1494 destroy_processing_handle(cmd, handle);
1495 return ret;
1496}
1497
1460cac2
DT
1498static int _vgchange_systemid_single(struct cmd_context *cmd, const char *vg_name,
1499 struct volume_group *vg,
1500 struct processing_handle *handle)
1501{
aa85ed17
DT
1502 if (arg_is_set(cmd, majoritypvs_ARG)) {
1503 struct pv_list *pvl;
1504 int missing_pvs = 0;
1505 int found_pvs = 0;
1506
1507 dm_list_iterate_items(pvl, &vg->pvs) {
1508 if (!pvl->pv->dev)
1509 missing_pvs++;
1510 else
1511 found_pvs++;
1512 }
1513 if (found_pvs <= missing_pvs) {
b1b60887 1514 log_error("Cannot change system ID without the majority of PVs (found %d of %d).",
aa85ed17
DT
1515 found_pvs, found_pvs+missing_pvs);
1516 return ECMD_FAILED;
1517 }
1518 }
1519
1460cac2
DT
1520 if (!_vgchange_system_id(cmd, vg))
1521 return_ECMD_FAILED;
1522
1523 if (!vg_write(vg) || !vg_commit(vg))
1524 return_ECMD_FAILED;
1525
b1b60887 1526 log_print_unless_silent("Volume group \"%s\" successfully changed.", vg->name);
1460cac2
DT
1527
1528 return ECMD_PROCESSED;
1529}
1530
1531int vgchange_systemid_cmd(struct cmd_context *cmd, int argc, char **argv)
1532{
1533 struct processing_handle *handle;
1534 int ret;
1535
1460cac2
DT
1536 if (!(handle = init_processing_handle(cmd, NULL))) {
1537 log_error("Failed to initialize processing handle.");
1538 return ECMD_FAILED;
1539 }
1540
aa85ed17
DT
1541 if (arg_is_set(cmd, majoritypvs_ARG))
1542 cmd->handles_missing_pvs = 1;
1543
1460cac2
DT
1544 ret = process_each_vg(cmd, argc, argv, NULL, NULL, READ_FOR_UPDATE, 0, handle, &_vgchange_systemid_single);
1545
1546 destroy_processing_handle(cmd, handle);
1547 return ret;
1548}
1549
This page took 0.502294 seconds and 6 git commands to generate.