2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 * Copyright (C) 2005 Zak Kipling. All rights reserved.
6 * This file is part of LVM2.
8 * This copyrighted material is made available to anyone wishing to use,
9 * modify, copy, or redistribute it subject to the terms and conditions
10 * of the GNU Lesser General Public License v.2.1.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 struct pvresize_params {
26 static int _pv_resize_single(struct cmd_context *cmd,
27 struct volume_group *vg,
28 struct physical_volume *pv,
29 const uint64_t new_size)
33 uint32_t new_pe_count = 0;
36 const char *pv_name = pv_dev_name(pv);
37 const char *vg_name = pv_vg_name(pv);
38 struct lvmcache_info *info;
40 struct volume_group *old_vg = vg;
44 if (is_orphan_vg(vg_name)) {
45 if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
46 log_error("Can't get lock for orphans");
50 if (!(pv = pv_read(cmd, pv_name, &mdas, NULL, 1, 0))) {
51 unlock_vg(cmd, vg_name);
52 log_error("Unable to read PV \"%s\"", pv_name);
56 mda_count = dm_list_size(&mdas);
58 vg = vg_read_for_update(cmd, vg_name, NULL, 0);
60 if (vg_read_error(vg)) {
62 log_error("Unable to read volume group \"%s\".",
67 if (!(pvl = find_pv_in_vg(vg, pv_name))) {
68 log_error("Unable to find \"%s\" in volume group \"%s\"",
75 if (!(info = info_from_pvid(pv->dev->pvid, 0))) {
76 log_error("Can't get info for PV %s in volume group %s",
81 mda_count = dm_list_size(&info->mdas);
87 /* FIXME Create function to test compatibility properly */
89 log_error("%s: too many metadata areas for pvresize", pv_name);
93 if (!(pv->fmt->features & FMT_RESIZE_PV)) {
94 log_error("Physical volume %s format does not support resizing.",
100 if (!dev_get_size(pv_dev(pv), &size)) {
101 log_error("%s: Couldn't get size.", pv_name);
107 log_warn("WARNING: %s: Overriding real size. "
108 "You could lose data.", pv_name);
109 log_verbose("%s: Pretending size is %" PRIu64 " not %" PRIu64
110 " sectors.", pv_name, new_size, pv_size(pv));
114 if (size < PV_MIN_SIZE) {
115 log_error("%s: Size must exceed minimum of %ld sectors.",
116 pv_name, PV_MIN_SIZE);
120 if (size < pv_pe_start(pv)) {
121 log_error("%s: Size must exceed physical extent start of "
122 "%" PRIu64 " sectors.", pv_name, pv_pe_start(pv));
129 pv->size -= pv_pe_start(pv);
130 new_pe_count = pv_size(pv) / vg->extent_size;
133 log_error("%s: Size must leave space for at "
134 "least one physical extent of "
135 "%" PRIu32 " sectors.", pv_name,
140 if (!pv_resize(pv, vg, new_pe_count))
144 log_verbose("Resizing volume \"%s\" to %" PRIu64 " sectors.",
145 pv_name, pv_size(pv));
147 log_verbose("Updating physical volume \"%s\"", pv_name);
148 if (!is_orphan_vg(vg_name)) {
149 if (!vg_write(vg) || !vg_commit(vg)) {
150 log_error("Failed to store physical volume \"%s\" in "
151 "volume group \"%s\"", pv_name, vg_name);
155 } else if (!(pv_write(cmd, pv, NULL, INT64_C(-1)))) {
156 log_error("Failed to store physical volume \"%s\"",
161 log_print("Physical volume \"%s\" changed", pv_name);
165 unlock_vg(cmd, vg_name);
171 static int _pvresize_single(struct cmd_context *cmd,
172 struct volume_group *vg,
173 struct physical_volume *pv,
176 struct pvresize_params *params = (struct pvresize_params *) handle;
180 if (!_pv_resize_single(cmd, vg, pv, params->new_size)) {
187 return ECMD_PROCESSED;
190 int pvresize(struct cmd_context *cmd, int argc, char **argv)
192 struct pvresize_params params;
196 log_error("Please supply physical volume(s)");
197 return EINVALID_CMD_LINE;
200 if (arg_sign_value(cmd, physicalvolumesize_ARG, 0) == SIGN_MINUS) {
201 log_error("Physical volume size may not be negative");
205 params.new_size = arg_uint64_value(cmd, physicalvolumesize_ARG,
211 ret = process_each_pv(cmd, argc, argv, NULL, READ_FOR_UPDATE, 0, ¶ms,
214 log_print("%d physical volume(s) resized / %d physical volume(s) "
215 "not resized", params.done, params.total - params.done);