4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2005 Zak Kipling. All rights reserved.
8 * This file is part of LVM2.
10 * This copyrighted material is made available to anyone wishing to use,
11 * modify, copy, or redistribute it subject to the terms and conditions
12 * of the GNU Lesser General Public License v.2.1.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 struct pvresize_params
{
28 static int _pv_resize_single(struct cmd_context
*cmd
,
29 struct volume_group
*vg
,
30 struct physical_volume
*pv
,
31 const uint64_t new_size
)
35 uint32_t new_pe_count
= 0;
38 const char *pv_name
= pv_dev_name(pv
);
40 struct lvmcache_info
*info
;
42 struct volume_group
*old_vg
= vg
;
46 if (is_orphan_vg(pv_vg_name(pv
))) {
48 if (!lock_vol(cmd
, vg_name
, LCK_VG_WRITE
)) {
49 log_error("Can't get lock for orphans");
53 if (!(pv
= pv_read(cmd
, pv_name
, &mdas
, NULL
, 1, 0))) {
54 unlock_vg(cmd
, vg_name
);
55 log_error("Unable to read PV \"%s\"", pv_name
);
59 mda_count
= dm_list_size(&mdas
);
61 vg_name
= pv_vg_name(pv
);
63 vg
= vg_read_for_update(cmd
, vg_name
, NULL
, 0);
65 if (vg_read_error(vg
))
68 if (!(pvl
= find_pv_in_vg(vg
, pv_name
))) {
69 log_error("Unable to find \"%s\" in volume group \"%s\"",
76 if (!(info
= info_from_pvid(pv
->dev
->pvid
, 0))) {
77 log_error("Can't get info for PV %s in volume group %s",
82 mda_count
= dm_list_size(&info
->mdas
);
88 /* FIXME Create function to test compatibility properly */
90 log_error("%s: too many metadata areas for pvresize", pv_name
);
94 if (!(pv
->fmt
->features
& FMT_RESIZE_PV
)) {
95 log_error("Physical volume %s format does not support resizing.",
101 if (!dev_get_size(pv_dev(pv
), &size
)) {
102 log_error("%s: Couldn't get size.", pv_name
);
108 log_warn("WARNING: %s: Overriding real size. "
109 "You could lose data.", pv_name
);
110 log_verbose("%s: Pretending size is %" PRIu64
" not %" PRIu64
111 " sectors.", pv_name
, new_size
, pv_size(pv
));
115 if (size
< PV_MIN_SIZE
) {
116 log_error("%s: Size must exceed minimum of %ld sectors.",
117 pv_name
, PV_MIN_SIZE
);
121 if (size
< pv_pe_start(pv
)) {
122 log_error("%s: Size must exceed physical extent start of "
123 "%" PRIu64
" sectors.", pv_name
, pv_pe_start(pv
));
130 pv
->size
-= pv_pe_start(pv
);
131 new_pe_count
= pv_size(pv
) / vg
->extent_size
;
134 log_error("%s: Size must leave space for at "
135 "least one physical extent of "
136 "%" PRIu32
" sectors.", pv_name
,
141 if (!pv_resize(pv
, vg
, new_pe_count
))
145 log_verbose("Resizing volume \"%s\" to %" PRIu64
" sectors.",
146 pv_name
, pv_size(pv
));
148 log_verbose("Updating physical volume \"%s\"", pv_name
);
149 if (!is_orphan_vg(pv_vg_name(pv
))) {
150 if (!vg_write(vg
) || !vg_commit(vg
)) {
151 log_error("Failed to store physical volume \"%s\" in "
152 "volume group \"%s\"", pv_name
, vg
->name
);
156 } else if (!(pv_write(cmd
, pv
, NULL
, INT64_C(-1)))) {
157 log_error("Failed to store physical volume \"%s\"",
162 log_print("Physical volume \"%s\" changed", pv_name
);
166 unlock_vg(cmd
, vg_name
);
172 static int _pvresize_single(struct cmd_context
*cmd
,
173 struct volume_group
*vg
,
174 struct physical_volume
*pv
,
177 struct pvresize_params
*params
= (struct pvresize_params
*) handle
;
181 if (!_pv_resize_single(cmd
, vg
, pv
, params
->new_size
)) {
188 return ECMD_PROCESSED
;
191 int pvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
193 struct pvresize_params params
;
197 log_error("Please supply physical volume(s)");
198 return EINVALID_CMD_LINE
;
201 if (arg_sign_value(cmd
, physicalvolumesize_ARG
, 0) == SIGN_MINUS
) {
202 log_error("Physical volume size may not be negative");
206 params
.new_size
= arg_uint64_value(cmd
, physicalvolumesize_ARG
,
212 ret
= process_each_pv(cmd
, argc
, argv
, NULL
, READ_FOR_UPDATE
, 0, ¶ms
,
215 log_print("%d physical volume(s) resized / %d physical volume(s) "
216 "not resized", params
.done
, params
.total
- params
.done
);