4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
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.
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
20 static int lvchange_permission(struct cmd_context
*cmd
,
21 struct logical_volume
*lv
)
27 lv_access
= arg_uint_value(cmd
, permission_ARG
, 0);
29 if ((lv_access
& LVM_WRITE
) && (lv
->status
& LVM_WRITE
)) {
30 log_error("Logical volume \"%s\" is already writable",
35 if (!(lv_access
& LVM_WRITE
) && !(lv
->status
& LVM_WRITE
)) {
36 log_error("Logical volume \"%s\" is already read only",
41 if ((lv
->status
& MIRRORED
) && (vg_is_clustered(lv
->vg
)) &&
42 lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
43 log_error("Cannot change permissions of mirror \"%s\" "
44 "while active.", lv
->name
);
48 if (lv_access
& LVM_WRITE
) {
49 lv
->status
|= LVM_WRITE
;
50 log_verbose("Setting logical volume \"%s\" read/write",
53 lv
->status
&= ~LVM_WRITE
;
54 log_verbose("Setting logical volume \"%s\" read-only",
58 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
59 if (!vg_write(lv
->vg
))
62 if (!suspend_lv(cmd
, lv
)) {
63 log_error("Failed to lock %s", lv
->name
);
68 if (!vg_commit(lv
->vg
)) {
73 log_very_verbose("Updating permissions for \"%s\" in kernel", lv
->name
);
74 if (!resume_lv(cmd
, lv
)) {
75 log_error("Problem reactivating %s", lv
->name
);
85 static int lvchange_monitoring(struct cmd_context
*cmd
,
86 struct logical_volume
*lv
)
90 if (!lv_info(cmd
, lv
, &info
, 0, 0) || !info
.exists
) {
91 log_error("Logical volume, %s, is not active", lv
->name
);
95 /* do not monitor pvmove lv's */
96 if (lv
->status
& PVMOVE
)
99 if ((dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE
) &&
100 !monitor_dev_for_events(cmd
, lv
, dmeventd_monitor_mode()))
106 static int lvchange_availability(struct cmd_context
*cmd
,
107 struct logical_volume
*lv
)
111 activate
= arg_uint_value(cmd
, available_ARG
, 0);
113 if (activate
== CHANGE_ALN
) {
114 log_verbose("Deactivating logical volume \"%s\" locally",
116 if (!deactivate_lv_local(cmd
, lv
))
118 } else if (activate
== CHANGE_AN
) {
119 log_verbose("Deactivating logical volume \"%s\"", lv
->name
);
120 if (!deactivate_lv(cmd
, lv
))
123 if (lv_is_origin(lv
) || (activate
== CHANGE_AE
)) {
124 log_verbose("Activating logical volume \"%s\" "
125 "exclusively", lv
->name
);
126 if (!activate_lv_excl(cmd
, lv
))
128 } else if (activate
== CHANGE_ALY
) {
129 log_verbose("Activating logical volume \"%s\" locally",
131 if (!activate_lv_local(cmd
, lv
))
134 log_verbose("Activating logical volume \"%s\"",
136 if (!activate_lv(cmd
, lv
))
140 lv_spawn_background_polling(cmd
, lv
);
146 static int lvchange_refresh(struct cmd_context
*cmd
, struct logical_volume
*lv
)
148 log_verbose("Refreshing logical volume \"%s\" (if active)", lv
->name
);
149 return lv_refresh(cmd
, lv
);
152 static int lvchange_resync(struct cmd_context
*cmd
,
153 struct logical_volume
*lv
)
158 struct logical_volume
*log_lv
;
160 if (!(lv
->status
& MIRRORED
)) {
161 log_error("Unable to resync %s because it is not mirrored.",
166 if (lv
->status
& PVMOVE
) {
167 log_error("Unable to resync pvmove volume %s", lv
->name
);
171 if (lv
->status
& LOCKED
) {
172 log_error("Unable to resync locked volume %s", lv
->name
);
176 if (lv_info(cmd
, lv
, &info
, 1, 0)) {
177 if (info
.open_count
) {
178 log_error("Can't resync open logical volume \"%s\"",
184 if (!arg_count(cmd
, yes_ARG
) &&
185 yes_no_prompt("Do you really want to deactivate "
186 "logical volume %s to resync it? [y/n]: ",
188 log_print("Logical volume \"%s\" not resynced",
200 /* Activate exclusively to ensure no nodes still have LV active */
201 monitored
= dmeventd_monitor_mode();
202 init_dmeventd_monitor(0);
204 if (!deactivate_lv(cmd
, lv
)) {
205 log_error("Unable to deactivate %s for resync", lv
->name
);
209 if (vg_is_clustered(lv
->vg
) && lv_is_active(lv
)) {
210 log_error("Can't get exclusive access to clustered volume %s",
215 init_dmeventd_monitor(monitored
);
217 log_lv
= first_seg(lv
)->log_lv
;
219 log_very_verbose("Starting resync of %s%s%s mirror \"%s\"",
220 (active
) ? "active " : "",
221 vg_is_clustered(lv
->vg
) ? "clustered " : "",
222 (log_lv
) ? "disk-logged" : "core-logged",
226 * If this mirror has a core log (i.e. !log_lv),
227 * then simply deactivating/activating will cause
228 * it to reset the sync status. We only need to
229 * worry about persistent logs.
231 if (!log_lv
&& !(lv
->status
& MIRROR_NOTSYNCED
)) {
232 if (active
&& !activate_lv(cmd
, lv
)) {
233 log_error("Failed to reactivate %s to resynchronize "
240 lv
->status
&= ~MIRROR_NOTSYNCED
;
243 /* Separate mirror log so we can clear it */
244 detach_mirror_log(first_seg(lv
));
246 if (!vg_write(lv
->vg
)) {
247 log_error("Failed to write intermediate VG metadata.");
248 if (!attach_mirror_log(first_seg(lv
), log_lv
))
250 if (active
&& !activate_lv(cmd
, lv
))
255 if (!vg_commit(lv
->vg
)) {
256 log_error("Failed to commit intermediate VG metadata.");
257 if (!attach_mirror_log(first_seg(lv
), log_lv
))
259 if (active
&& !activate_lv(cmd
, lv
))
266 if (!activate_lv(cmd
, log_lv
)) {
267 log_error("Unable to activate %s for mirror log resync",
272 log_very_verbose("Clearing log device %s", log_lv
->name
);
273 if (!set_lv(cmd
, log_lv
, log_lv
->size
, 0)) {
274 log_error("Unable to reset sync status for %s", lv
->name
);
275 if (!deactivate_lv(cmd
, log_lv
))
276 log_error("Failed to deactivate log LV after "
281 if (!deactivate_lv(cmd
, log_lv
)) {
282 log_error("Unable to deactivate log LV %s after wiping "
283 "for resync", log_lv
->name
);
287 /* Put mirror log back in place */
288 if (!attach_mirror_log(first_seg(lv
), log_lv
))
292 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
293 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
)) {
294 log_error("Failed to update metadata on disk.");
298 if (active
&& !activate_lv(cmd
, lv
)) {
299 log_error("Failed to reactivate %s after resync", lv
->name
);
306 static int lvchange_alloc(struct cmd_context
*cmd
, struct logical_volume
*lv
)
308 int want_contiguous
= 0;
309 alloc_policy_t alloc
;
311 want_contiguous
= strcmp(arg_str_value(cmd
, contiguous_ARG
, "n"), "n");
312 alloc
= want_contiguous
? ALLOC_CONTIGUOUS
: ALLOC_INHERIT
;
313 alloc
= arg_uint_value(cmd
, alloc_ARG
, alloc
);
315 if (alloc
== lv
->alloc
) {
316 log_error("Allocation policy of logical volume \"%s\" is "
317 "already %s", lv
->name
, get_alloc_string(alloc
));
323 /* FIXME If contiguous, check existing extents already are */
325 log_verbose("Setting contiguous allocation policy for \"%s\" to %s",
326 lv
->name
, get_alloc_string(alloc
));
328 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
330 /* No need to suspend LV for this change */
331 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
339 static int lvchange_readahead(struct cmd_context
*cmd
,
340 struct logical_volume
*lv
)
342 unsigned read_ahead
= 0;
343 unsigned pagesize
= (unsigned) lvm_getpagesize() >> SECTOR_SHIFT
;
346 read_ahead
= arg_uint_value(cmd
, readahead_ARG
, 0);
348 if (read_ahead
!= DM_READ_AHEAD_AUTO
&&
349 (lv
->vg
->fid
->fmt
->features
& FMT_RESTRICTED_READAHEAD
) &&
350 (read_ahead
< 2 || read_ahead
> 120)) {
351 log_error("Metadata only supports readahead values between 2 and 120.");
355 if (read_ahead
!= DM_READ_AHEAD_AUTO
&&
356 read_ahead
!= DM_READ_AHEAD_NONE
&& read_ahead
% pagesize
) {
357 if (read_ahead
< pagesize
)
358 read_ahead
= pagesize
;
360 read_ahead
= (read_ahead
/ pagesize
) * pagesize
;
361 log_warn("WARNING: Overriding readahead to %u sectors, a multiple "
362 "of %uK page size.", read_ahead
, pagesize
>> 1);
365 if (lv
->read_ahead
== read_ahead
) {
366 if (read_ahead
== DM_READ_AHEAD_AUTO
)
367 log_error("Read ahead is already auto for \"%s\"", lv
->name
);
369 log_error("Read ahead is already %u for \"%s\"",
370 read_ahead
, lv
->name
);
374 lv
->read_ahead
= read_ahead
;
376 log_verbose("Setting read ahead to %u for \"%s\"", read_ahead
,
379 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
380 if (!vg_write(lv
->vg
))
383 if (!suspend_lv(cmd
, lv
)) {
384 log_error("Failed to lock %s", lv
->name
);
389 if (!vg_commit(lv
->vg
)) {
394 log_very_verbose("Updating permissions for \"%s\" in kernel", lv
->name
);
395 if (!resume_lv(cmd
, lv
)) {
396 log_error("Problem reactivating %s", lv
->name
);
406 static int lvchange_persistent(struct cmd_context
*cmd
,
407 struct logical_volume
*lv
)
412 if (!strcmp(arg_str_value(cmd
, persistent_ARG
, "n"), "n")) {
413 if (!(lv
->status
& FIXED_MINOR
)) {
414 log_error("Minor number is already not persistent "
415 "for \"%s\"", lv
->name
);
418 lv
->status
&= ~FIXED_MINOR
;
421 log_verbose("Disabling persistent device number for \"%s\"",
424 if (!arg_count(cmd
, minor_ARG
) && lv
->minor
< 0) {
425 log_error("Minor number must be specified with -My");
428 if (!arg_count(cmd
, major_ARG
) && lv
->major
< 0) {
429 log_error("Major number must be specified with -My");
432 if (lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
)
434 if (active
&& !arg_count(cmd
, force_ARG
) &&
435 yes_no_prompt("Logical volume %s will be "
436 "deactivated temporarily. "
437 "Continue? [y/n]: ", lv
->name
) == 'n') {
438 log_print("%s device number not changed.",
446 log_verbose("Ensuring %s is inactive.", lv
->name
);
447 if (!deactivate_lv(cmd
, lv
)) {
448 log_error("%s: deactivation failed", lv
->name
);
451 lv
->status
|= FIXED_MINOR
;
452 lv
->minor
= arg_int_value(cmd
, minor_ARG
, lv
->minor
);
453 lv
->major
= arg_int_value(cmd
, major_ARG
, lv
->major
);
454 log_verbose("Setting persistent device number to (%d, %d) "
455 "for \"%s\"", lv
->major
, lv
->minor
, lv
->name
);
459 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
460 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
466 log_verbose("Re-activating logical volume \"%s\"", lv
->name
);
467 if (!activate_lv(cmd
, lv
)) {
468 log_error("%s: reactivation failed", lv
->name
);
476 static int lvchange_tag(struct cmd_context
*cmd
, struct logical_volume
*lv
,
481 if (!(tag
= arg_str_value(cmd
, arg
, NULL
))) {
482 log_error("Failed to get tag");
486 if (!(lv
->vg
->fid
->fmt
->features
& FMT_TAGS
)) {
487 log_error("Logical volume %s/%s does not support tags",
488 lv
->vg
->name
, lv
->name
);
492 if ((arg
== addtag_ARG
)) {
493 if (!str_list_add(cmd
->mem
, &lv
->tags
, tag
)) {
494 log_error("Failed to add tag %s to %s/%s",
495 tag
, lv
->vg
->name
, lv
->name
);
499 if (!str_list_del(&lv
->tags
, tag
)) {
500 log_error("Failed to remove tag %s from %s/%s",
501 tag
, lv
->vg
->name
, lv
->name
);
506 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
508 /* No need to suspend LV for this change */
509 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
517 static int lvchange_single(struct cmd_context
*cmd
, struct logical_volume
*lv
,
518 void *handle
__attribute((unused
)))
520 int doit
= 0, docmds
= 0;
522 struct logical_volume
*origin
;
524 if (!(lv
->vg
->status
& LVM_WRITE
) &&
525 (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
526 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
527 arg_count(cmd
, alloc_ARG
))) {
528 log_error("Only -a permitted with read-only volume "
529 "group \"%s\"", lv
->vg
->name
);
530 return EINVALID_CMD_LINE
;
533 if (lv_is_origin(lv
) &&
534 (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
535 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
536 arg_count(cmd
, alloc_ARG
))) {
537 log_error("Can't change logical volume \"%s\" under snapshot",
542 if (lv_is_cow(lv
) && !lv_is_virtual_origin(origin_from_cow(lv
)) &&
543 arg_count(cmd
, available_ARG
)) {
544 log_error("Can't change snapshot logical volume \"%s\"",
549 if (lv
->status
& PVMOVE
) {
550 log_error("Unable to change pvmove LV %s", lv
->name
);
551 if (arg_count(cmd
, available_ARG
))
552 log_error("Use 'pvmove --abort' to abandon a pvmove");
556 if (lv
->status
& MIRROR_LOG
) {
557 log_error("Unable to change mirror log LV %s directly", lv
->name
);
561 if (lv
->status
& MIRROR_IMAGE
) {
562 log_error("Unable to change mirror image LV %s directly",
567 /* If LV is sparse, activate origin instead */
568 if (arg_count(cmd
, available_ARG
) && lv_is_cow(lv
) &&
569 lv_is_virtual_origin(origin
= origin_from_cow(lv
)))
572 if (!(lv_is_visible(lv
)) && !lv_is_virtual_origin(lv
)) {
573 log_error("Unable to change internal LV %s directly",
578 init_dmeventd_monitor(arg_int_value(cmd
, monitor_ARG
,
579 (is_static() || arg_count(cmd
, ignoremonitoring_ARG
)) ?
580 DMEVENTD_MONITOR_IGNORE
: DEFAULT_DMEVENTD_MONITOR
));
582 /* access permission change */
583 if (arg_count(cmd
, permission_ARG
)) {
584 if (!archive(lv
->vg
)) {
589 doit
+= lvchange_permission(cmd
, lv
);
593 /* allocation policy change */
594 if (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, alloc_ARG
)) {
595 if (!archived
&& !archive(lv
->vg
)) {
600 doit
+= lvchange_alloc(cmd
, lv
);
604 /* read ahead sector change */
605 if (arg_count(cmd
, readahead_ARG
)) {
606 if (!archived
&& !archive(lv
->vg
)) {
611 doit
+= lvchange_readahead(cmd
, lv
);
615 /* persistent device number change */
616 if (arg_count(cmd
, persistent_ARG
)) {
617 if (!archived
&& !archive(lv
->vg
)) {
622 doit
+= lvchange_persistent(cmd
, lv
);
624 if (sigint_caught()) {
631 if (arg_count(cmd
, addtag_ARG
)) {
632 if (!archived
&& !archive(lv
->vg
)) {
637 doit
+= lvchange_tag(cmd
, lv
, addtag_ARG
);
642 if (arg_count(cmd
, deltag_ARG
)) {
643 if (!archived
&& !archive(lv
->vg
)) {
648 doit
+= lvchange_tag(cmd
, lv
, deltag_ARG
);
653 log_print("Logical volume \"%s\" changed", lv
->name
);
655 if (arg_count(cmd
, resync_ARG
))
656 if (!lvchange_resync(cmd
, lv
)) {
661 /* availability change */
662 if (arg_count(cmd
, available_ARG
)) {
663 if (!lvchange_availability(cmd
, lv
)) {
669 if (arg_count(cmd
, refresh_ARG
))
670 if (!lvchange_refresh(cmd
, lv
)) {
675 if (!arg_count(cmd
, available_ARG
) &&
676 !arg_count(cmd
, refresh_ARG
) &&
677 arg_count(cmd
, monitor_ARG
)) {
678 if (!lvchange_monitoring(cmd
, lv
)) {
684 if (doit
!= docmds
) {
689 return ECMD_PROCESSED
;
692 int lvchange(struct cmd_context
*cmd
, int argc
, char **argv
)
694 if (!arg_count(cmd
, available_ARG
) && !arg_count(cmd
, contiguous_ARG
)
695 && !arg_count(cmd
, permission_ARG
) && !arg_count(cmd
, readahead_ARG
)
696 && !arg_count(cmd
, minor_ARG
) && !arg_count(cmd
, major_ARG
)
697 && !arg_count(cmd
, persistent_ARG
) && !arg_count(cmd
, addtag_ARG
)
698 && !arg_count(cmd
, deltag_ARG
) && !arg_count(cmd
, refresh_ARG
)
699 && !arg_count(cmd
, alloc_ARG
) && !arg_count(cmd
, monitor_ARG
)
700 && !arg_count(cmd
, resync_ARG
)) {
701 log_error("Need 1 or more of -a, -C, -j, -m, -M, -p, -r, "
702 "--resync, --refresh, --alloc, --addtag, --deltag "
704 return EINVALID_CMD_LINE
;
707 int avail_only
= /* i.e. only one of -a or --refresh is given */
708 !(arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
709 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
710 arg_count(cmd
, addtag_ARG
) || arg_count(cmd
, deltag_ARG
) ||
711 arg_count(cmd
, resync_ARG
) || arg_count(cmd
, alloc_ARG
));
713 if (arg_count(cmd
, ignorelockingfailure_ARG
) && !avail_only
) {
714 log_error("Only -a permitted with --ignorelockingfailure");
715 return EINVALID_CMD_LINE
;
719 cmd
->handles_missing_pvs
= 1;
722 log_error("Please give logical volume path(s)");
723 return EINVALID_CMD_LINE
;
726 if ((arg_count(cmd
, minor_ARG
) || arg_count(cmd
, major_ARG
)) &&
727 !arg_count(cmd
, persistent_ARG
)) {
728 log_error("--major and --minor require -My");
729 return EINVALID_CMD_LINE
;
732 if (arg_count(cmd
, minor_ARG
) && argc
!= 1) {
733 log_error("Only give one logical volume when specifying minor");
734 return EINVALID_CMD_LINE
;
737 if (arg_count(cmd
, contiguous_ARG
) && arg_count(cmd
, alloc_ARG
)) {
738 log_error("Only one of --alloc and --contiguous permitted");
739 return EINVALID_CMD_LINE
;
742 return process_each_lv(cmd
, argc
, argv
,
743 avail_only
? 0 : READ_FOR_UPDATE
, NULL
,