2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 static int lvchange_permission(struct cmd_context
*cmd
,
19 struct logical_volume
*lv
)
25 lv_access
= arg_uint_value(cmd
, permission_ARG
, 0);
27 if ((lv_access
& LVM_WRITE
) && (lv
->status
& LVM_WRITE
)) {
28 log_error("Logical volume \"%s\" is already writable",
33 if (!(lv_access
& LVM_WRITE
) && !(lv
->status
& LVM_WRITE
)) {
34 log_error("Logical volume \"%s\" is already read only",
39 if ((lv
->status
& MIRRORED
) && (vg_is_clustered(lv
->vg
)) &&
40 lv_info(cmd
, lv
, 0, &info
, 0, 0) && info
.exists
) {
41 log_error("Cannot change permissions of mirror \"%s\" "
42 "while active.", lv
->name
);
46 /* Not allowed to change permissions on RAID sub-LVs directly */
47 if ((lv
->status
& RAID_META
) || (lv
->status
& RAID_IMAGE
)) {
48 log_error("Cannot change permissions of RAID %s \"%s\"",
49 (lv
->status
& RAID_IMAGE
) ? "image" :
50 "metadata area", lv
->name
);
54 if (lv_access
& LVM_WRITE
) {
55 lv
->status
|= LVM_WRITE
;
56 log_verbose("Setting logical volume \"%s\" read/write",
59 lv
->status
&= ~LVM_WRITE
;
60 log_verbose("Setting logical volume \"%s\" read-only",
64 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
65 if (!vg_write(lv
->vg
))
68 if (!suspend_lv(cmd
, lv
)) {
69 log_error("Failed to lock %s", lv
->name
);
74 if (!vg_commit(lv
->vg
)) {
75 if (!resume_lv(cmd
, lv
))
80 log_very_verbose("Updating permissions for \"%s\" in kernel", lv
->name
);
81 if (!resume_lv(cmd
, lv
)) {
82 log_error("Problem reactivating %s", lv
->name
);
92 static int lvchange_monitoring(struct cmd_context
*cmd
,
93 struct logical_volume
*lv
)
97 if (!lv_info(cmd
, lv
, lv_is_thin_pool(lv
) ? 1 : 0,
98 &info
, 0, 0) || !info
.exists
) {
99 log_error("Logical volume, %s, is not active", lv
->name
);
103 /* do not monitor pvmove lv's */
104 if (lv
->status
& PVMOVE
)
107 if ((dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE
) &&
108 !monitor_dev_for_events(cmd
, lv
, 0, dmeventd_monitor_mode()))
114 static int lvchange_background_polling(struct cmd_context
*cmd
,
115 struct logical_volume
*lv
)
119 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || !info
.exists
) {
120 log_error("Logical volume, %s, is not active", lv
->name
);
124 if (background_polling())
125 lv_spawn_background_polling(cmd
, lv
);
130 static int lvchange_availability(struct cmd_context
*cmd
,
131 struct logical_volume
*lv
)
135 activate
= arg_uint_value(cmd
, available_ARG
, 0);
137 if (lv_is_cow(lv
) && !lv_is_virtual_origin(origin_from_cow(lv
)))
138 lv
= origin_from_cow(lv
);
140 if (activate
== CHANGE_ALN
) {
141 log_verbose("Deactivating logical volume \"%s\" locally",
143 if (!deactivate_lv_local(cmd
, lv
))
145 } else if (activate
== CHANGE_AN
) {
146 log_verbose("Deactivating logical volume \"%s\"", lv
->name
);
147 if (!deactivate_lv(cmd
, lv
))
150 if ((activate
== CHANGE_AE
) ||
152 lv_is_thin_type(lv
)) {
153 log_verbose("Activating logical volume \"%s\" "
154 "exclusively", lv
->name
);
155 if (!activate_lv_excl(cmd
, lv
))
157 } else if (activate
== CHANGE_ALY
) {
158 log_verbose("Activating logical volume \"%s\" locally",
160 if (!activate_lv_local(cmd
, lv
))
163 log_verbose("Activating logical volume \"%s\"",
165 if (!activate_lv(cmd
, lv
))
169 if (background_polling())
170 lv_spawn_background_polling(cmd
, lv
);
176 static int lvchange_refresh(struct cmd_context
*cmd
, struct logical_volume
*lv
)
178 log_verbose("Refreshing logical volume \"%s\" (if active)", lv
->name
);
180 return lv_refresh(cmd
, lv
);
183 static int lvchange_resync(struct cmd_context
*cmd
,
184 struct logical_volume
*lv
)
189 struct logical_volume
*log_lv
;
191 if (!(lv
->status
& MIRRORED
)) {
192 log_error("Unable to resync %s because it is not mirrored.",
197 if (lv
->status
& PVMOVE
) {
198 log_error("Unable to resync pvmove volume %s", lv
->name
);
202 if (lv
->status
& LOCKED
) {
203 log_error("Unable to resync locked volume %s", lv
->name
);
207 if (lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
208 if (info
.open_count
) {
209 log_error("Can't resync open logical volume \"%s\"",
215 if (!arg_count(cmd
, yes_ARG
) &&
216 yes_no_prompt("Do you really want to deactivate "
217 "logical volume %s to resync it? [y/n]: ",
219 log_error("Logical volume \"%s\" not resynced",
231 /* Activate exclusively to ensure no nodes still have LV active */
232 monitored
= dmeventd_monitor_mode();
233 init_dmeventd_monitor(0);
235 if (!deactivate_lv(cmd
, lv
)) {
236 log_error("Unable to deactivate %s for resync", lv
->name
);
240 if (vg_is_clustered(lv
->vg
) && lv_is_active(lv
)) {
241 log_error("Can't get exclusive access to clustered volume %s",
246 init_dmeventd_monitor(monitored
);
248 log_lv
= first_seg(lv
)->log_lv
;
250 log_very_verbose("Starting resync of %s%s%s mirror \"%s\"",
251 (active
) ? "active " : "",
252 vg_is_clustered(lv
->vg
) ? "clustered " : "",
253 (log_lv
) ? "disk-logged" : "core-logged",
257 * If this mirror has a core log (i.e. !log_lv),
258 * then simply deactivating/activating will cause
259 * it to reset the sync status. We only need to
260 * worry about persistent logs.
262 if (!log_lv
&& !(lv
->status
& LV_NOTSYNCED
)) {
263 if (active
&& !activate_lv(cmd
, lv
)) {
264 log_error("Failed to reactivate %s to resynchronize "
271 lv
->status
&= ~LV_NOTSYNCED
;
274 /* Separate mirror log so we can clear it */
275 detach_mirror_log(first_seg(lv
));
277 if (!vg_write(lv
->vg
)) {
278 log_error("Failed to write intermediate VG metadata.");
279 if (!attach_mirror_log(first_seg(lv
), log_lv
))
281 if (active
&& !activate_lv(cmd
, lv
))
286 if (!vg_commit(lv
->vg
)) {
287 log_error("Failed to commit intermediate VG metadata.");
288 if (!attach_mirror_log(first_seg(lv
), log_lv
))
290 if (active
&& !activate_lv(cmd
, lv
))
297 if (!activate_lv(cmd
, log_lv
)) {
298 log_error("Unable to activate %s for mirror log resync",
303 log_very_verbose("Clearing log device %s", log_lv
->name
);
304 if (!set_lv(cmd
, log_lv
, log_lv
->size
, 0)) {
305 log_error("Unable to reset sync status for %s", lv
->name
);
306 if (!deactivate_lv(cmd
, log_lv
))
307 log_error("Failed to deactivate log LV after "
312 if (!deactivate_lv(cmd
, log_lv
)) {
313 log_error("Unable to deactivate log LV %s after wiping "
314 "for resync", log_lv
->name
);
318 /* Put mirror log back in place */
319 if (!attach_mirror_log(first_seg(lv
), log_lv
))
323 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
324 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
)) {
325 log_error("Failed to update metadata on disk.");
329 if (active
&& !activate_lv(cmd
, lv
)) {
330 log_error("Failed to reactivate %s after resync", lv
->name
);
337 static int lvchange_alloc(struct cmd_context
*cmd
, struct logical_volume
*lv
)
339 int want_contiguous
= 0;
340 alloc_policy_t alloc
;
342 want_contiguous
= strcmp(arg_str_value(cmd
, contiguous_ARG
, "n"), "n");
343 alloc
= want_contiguous
? ALLOC_CONTIGUOUS
: ALLOC_INHERIT
;
344 alloc
= (alloc_policy_t
) arg_uint_value(cmd
, alloc_ARG
, alloc
);
346 if (alloc
== lv
->alloc
) {
347 log_error("Allocation policy of logical volume \"%s\" is "
348 "already %s", lv
->name
, get_alloc_string(alloc
));
354 /* FIXME If contiguous, check existing extents already are */
356 log_verbose("Setting contiguous allocation policy for \"%s\" to %s",
357 lv
->name
, get_alloc_string(alloc
));
359 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
361 /* No need to suspend LV for this change */
362 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
370 static int lvchange_readahead(struct cmd_context
*cmd
,
371 struct logical_volume
*lv
)
373 unsigned read_ahead
= 0;
374 unsigned pagesize
= (unsigned) lvm_getpagesize() >> SECTOR_SHIFT
;
377 read_ahead
= arg_uint_value(cmd
, readahead_ARG
, 0);
379 if (read_ahead
!= DM_READ_AHEAD_AUTO
&&
380 (lv
->vg
->fid
->fmt
->features
& FMT_RESTRICTED_READAHEAD
) &&
381 (read_ahead
< 2 || read_ahead
> 120)) {
382 log_error("Metadata only supports readahead values between 2 and 120.");
386 if (read_ahead
!= DM_READ_AHEAD_AUTO
&&
387 read_ahead
!= DM_READ_AHEAD_NONE
&& read_ahead
% pagesize
) {
388 if (read_ahead
< pagesize
)
389 read_ahead
= pagesize
;
391 read_ahead
= (read_ahead
/ pagesize
) * pagesize
;
392 log_warn("WARNING: Overriding readahead to %u sectors, a multiple "
393 "of %uK page size.", read_ahead
, pagesize
>> 1);
396 if (lv
->read_ahead
== read_ahead
) {
397 if (read_ahead
== DM_READ_AHEAD_AUTO
)
398 log_error("Read ahead is already auto for \"%s\"", lv
->name
);
400 log_error("Read ahead is already %u for \"%s\"",
401 read_ahead
, lv
->name
);
405 lv
->read_ahead
= read_ahead
;
407 log_verbose("Setting read ahead to %u for \"%s\"", read_ahead
,
410 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
411 if (!vg_write(lv
->vg
))
414 if (!suspend_lv(cmd
, lv
)) {
415 log_error("Failed to lock %s", lv
->name
);
420 if (!vg_commit(lv
->vg
)) {
421 if (!resume_lv(cmd
, lv
))
426 log_very_verbose("Updating permissions for \"%s\" in kernel", lv
->name
);
427 if (!resume_lv(cmd
, lv
)) {
428 log_error("Problem reactivating %s", lv
->name
);
438 static int lvchange_persistent(struct cmd_context
*cmd
,
439 struct logical_volume
*lv
)
444 if (!strcmp(arg_str_value(cmd
, persistent_ARG
, "n"), "n")) {
445 if (!(lv
->status
& FIXED_MINOR
)) {
446 log_error("Minor number is already not persistent "
447 "for \"%s\"", lv
->name
);
450 lv
->status
&= ~FIXED_MINOR
;
453 log_verbose("Disabling persistent device number for \"%s\"",
456 if (!arg_count(cmd
, minor_ARG
) && lv
->minor
< 0) {
457 log_error("Minor number must be specified with -My");
460 if (arg_count(cmd
, major_ARG
) > 1) {
461 log_error("Option -j/--major may not be repeated.");
464 if (arg_count(cmd
, minor_ARG
) > 1) {
465 log_error("Option --minor may not be repeated.");
468 if (!arg_count(cmd
, major_ARG
) && lv
->major
< 0) {
469 log_error("Major number must be specified with -My");
472 if (lv_info(cmd
, lv
, 0, &info
, 0, 0) && info
.exists
)
474 if (active
&& !arg_count(cmd
, force_ARG
) &&
475 yes_no_prompt("Logical volume %s will be "
476 "deactivated temporarily. "
477 "Continue? [y/n]: ", lv
->name
) == 'n') {
478 log_error("%s device number not changed.",
486 log_verbose("Ensuring %s is inactive.", lv
->name
);
487 if (!deactivate_lv(cmd
, lv
)) {
488 log_error("%s: deactivation failed", lv
->name
);
491 lv
->status
|= FIXED_MINOR
;
492 lv
->minor
= arg_int_value(cmd
, minor_ARG
, lv
->minor
);
493 lv
->major
= arg_int_value(cmd
, major_ARG
, lv
->major
);
494 log_verbose("Setting persistent device number to (%d, %d) "
495 "for \"%s\"", lv
->major
, lv
->minor
, lv
->name
);
499 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
500 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
506 log_verbose("Re-activating logical volume \"%s\"", lv
->name
);
507 if (!activate_lv(cmd
, lv
)) {
508 log_error("%s: reactivation failed", lv
->name
);
516 static int lvchange_tag(struct cmd_context
*cmd
, struct logical_volume
*lv
, int arg
)
518 if (!change_tag(cmd
, NULL
, lv
, NULL
, arg
))
521 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
523 /* No need to suspend LV for this change */
524 if (!vg_write(lv
->vg
) || !vg_commit(lv
->vg
))
532 static int lvchange_single(struct cmd_context
*cmd
, struct logical_volume
*lv
,
533 void *handle
__attribute__((unused
)))
535 int doit
= 0, docmds
= 0;
537 struct logical_volume
*origin
;
540 if (!(lv
->vg
->status
& LVM_WRITE
) &&
541 (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
542 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
543 arg_count(cmd
, alloc_ARG
))) {
544 log_error("Only -a permitted with read-only volume "
545 "group \"%s\"", lv
->vg
->name
);
546 return EINVALID_CMD_LINE
;
549 if (lv_is_origin(lv
) &&
550 (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
551 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
552 arg_count(cmd
, alloc_ARG
))) {
553 log_error("Can't change logical volume \"%s\" under snapshot",
558 if (lv_is_cow(lv
) && !lv_is_virtual_origin(origin
= origin_from_cow(lv
)) &&
559 arg_count(cmd
, available_ARG
)) {
560 if (origin
->origin_count
< 2)
562 else if (dm_snprintf(snaps_msg
, sizeof(snaps_msg
),
563 " and %u other snapshot(s)",
564 origin
->origin_count
- 1) < 0) {
565 log_error("Failed to prepare message.");
569 if (!arg_count(cmd
, yes_ARG
) &&
570 (yes_no_prompt("Change of snapshot %s will also change its"
571 " origin %s%s. Proceed? [y/n]: ", lv
->name
,
572 origin
->name
, snaps_msg
) == 'n')) {
573 log_error("Logical volume %s not changed.", lv
->name
);
578 if (lv
->status
& PVMOVE
) {
579 log_error("Unable to change pvmove LV %s", lv
->name
);
580 if (arg_count(cmd
, available_ARG
))
581 log_error("Use 'pvmove --abort' to abandon a pvmove");
585 if (lv
->status
& MIRROR_LOG
) {
586 log_error("Unable to change mirror log LV %s directly", lv
->name
);
590 if (lv
->status
& MIRROR_IMAGE
) {
591 log_error("Unable to change mirror image LV %s directly",
596 /* If LV is sparse, activate origin instead */
597 if (arg_count(cmd
, available_ARG
) && lv_is_cow(lv
) &&
598 lv_is_virtual_origin(origin
= origin_from_cow(lv
)))
601 if (!(lv_is_visible(lv
)) && !lv_is_virtual_origin(lv
)) {
602 log_error("Unable to change internal LV %s directly",
608 * FIXME: DEFAULT_BACKGROUND_POLLING should be "unspecified".
609 * If --poll is explicitly provided use it; otherwise polling
610 * should only be started if the LV is not already active. So:
611 * 1) change the activation code to say if the LV was actually activated
612 * 2) make polling of an LV tightly coupled with LV activation
614 * Do not initiate any polling if --sysinit option is used.
616 init_background_polling(arg_count(cmd
, sysinit_ARG
) ? 0 :
617 arg_int_value(cmd
, poll_ARG
,
618 DEFAULT_BACKGROUND_POLLING
));
620 /* access permission change */
621 if (arg_count(cmd
, permission_ARG
)) {
622 if (!archive(lv
->vg
)) {
627 doit
+= lvchange_permission(cmd
, lv
);
631 /* allocation policy change */
632 if (arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, alloc_ARG
)) {
633 if (!archived
&& !archive(lv
->vg
)) {
638 doit
+= lvchange_alloc(cmd
, lv
);
642 /* read ahead sector change */
643 if (arg_count(cmd
, readahead_ARG
)) {
644 if (!archived
&& !archive(lv
->vg
)) {
649 doit
+= lvchange_readahead(cmd
, lv
);
653 /* persistent device number change */
654 if (arg_count(cmd
, persistent_ARG
)) {
655 if (!archived
&& !archive(lv
->vg
)) {
660 doit
+= lvchange_persistent(cmd
, lv
);
662 if (sigint_caught()) {
669 if (arg_count(cmd
, addtag_ARG
)) {
670 if (!archived
&& !archive(lv
->vg
)) {
675 doit
+= lvchange_tag(cmd
, lv
, addtag_ARG
);
680 if (arg_count(cmd
, deltag_ARG
)) {
681 if (!archived
&& !archive(lv
->vg
)) {
686 doit
+= lvchange_tag(cmd
, lv
, deltag_ARG
);
691 log_print("Logical volume \"%s\" changed", lv
->name
);
693 if (arg_count(cmd
, resync_ARG
))
694 if (!lvchange_resync(cmd
, lv
)) {
699 /* availability change */
700 if (arg_count(cmd
, available_ARG
)) {
701 if (!lvchange_availability(cmd
, lv
)) {
707 if (arg_count(cmd
, refresh_ARG
))
708 if (!lvchange_refresh(cmd
, lv
)) {
713 if (!arg_count(cmd
, available_ARG
) &&
714 !arg_count(cmd
, refresh_ARG
) &&
715 arg_count(cmd
, monitor_ARG
)) {
716 if (!lvchange_monitoring(cmd
, lv
)) {
722 if (!arg_count(cmd
, available_ARG
) &&
723 !arg_count(cmd
, refresh_ARG
) &&
724 arg_count(cmd
, poll_ARG
)) {
725 if (!lvchange_background_polling(cmd
, lv
)) {
731 if (doit
!= docmds
) {
736 return ECMD_PROCESSED
;
739 int lvchange(struct cmd_context
*cmd
, int argc
, char **argv
)
741 int update
= /* options other than -a, --refresh, --monitor or --poll */
742 arg_count(cmd
, contiguous_ARG
) || arg_count(cmd
, permission_ARG
) ||
743 arg_count(cmd
, readahead_ARG
) || arg_count(cmd
, persistent_ARG
) ||
744 arg_count(cmd
, addtag_ARG
) || arg_count(cmd
, deltag_ARG
) ||
745 arg_count(cmd
, resync_ARG
) || arg_count(cmd
, alloc_ARG
);
748 !arg_count(cmd
, available_ARG
) && !arg_count(cmd
, refresh_ARG
) &&
749 !arg_count(cmd
, monitor_ARG
) && !arg_count(cmd
, poll_ARG
)) {
750 log_error("Need 1 or more of -a, -C, -M, -p, -r, "
751 "--resync, --refresh, --alloc, --addtag, --deltag, "
752 "--monitor or --poll");
753 return EINVALID_CMD_LINE
;
756 if (arg_count(cmd
, available_ARG
) && arg_count(cmd
, refresh_ARG
)) {
757 log_error("Only one of -a and --refresh permitted.");
758 return EINVALID_CMD_LINE
;
761 if ((arg_count(cmd
, ignorelockingfailure_ARG
) ||
762 arg_count(cmd
, sysinit_ARG
)) && update
) {
763 log_error("Only -a permitted with --ignorelockingfailure and --sysinit");
764 return EINVALID_CMD_LINE
;
768 cmd
->handles_missing_pvs
= 1;
771 log_error("Please give logical volume path(s)");
772 return EINVALID_CMD_LINE
;
775 if ((arg_count(cmd
, minor_ARG
) || arg_count(cmd
, major_ARG
)) &&
776 !arg_count(cmd
, persistent_ARG
)) {
777 log_error("--major and --minor require -My");
778 return EINVALID_CMD_LINE
;
781 if (arg_count(cmd
, minor_ARG
) && argc
!= 1) {
782 log_error("Only give one logical volume when specifying minor");
783 return EINVALID_CMD_LINE
;
786 if (arg_count(cmd
, contiguous_ARG
) && arg_count(cmd
, alloc_ARG
)) {
787 log_error("Only one of --alloc and --contiguous permitted");
788 return EINVALID_CMD_LINE
;
791 if (arg_count(cmd
, poll_ARG
) && arg_count(cmd
, sysinit_ARG
)) {
792 log_error("Only one of --poll and --sysinit permitted");
793 return EINVALID_CMD_LINE
;
796 return process_each_lv(cmd
, argc
, argv
,
797 update
? READ_FOR_UPDATE
: 0, NULL
,