summaryrefslogtreecommitdiff
path: root/tools/toollib.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/toollib.c')
-rw-r--r--tools/toollib.c854
1 files changed, 728 insertions, 126 deletions
diff --git a/tools/toollib.c b/tools/toollib.c
index 728e61d4d..8dab3c612 100644
--- a/tools/toollib.c
+++ b/tools/toollib.c
@@ -1136,121 +1136,6 @@ void lv_spawn_background_polling(struct cmd_context *cmd,
}
}
-/*
- * Intial sanity checking of non-recovery related command-line arguments.
- *
- * Output arguments:
- * pp: structure allocated by caller, fields written / validated here
- */
-int pvcreate_params_validate(struct cmd_context *cmd, int argc,
- struct pvcreate_params *pp)
-{
- if (!argc) {
- log_error("Please enter a physical volume path.");
- return 0;
- }
-
- pp->yes = arg_count(cmd, yes_ARG);
- pp->force = (force_t) arg_count(cmd, force_ARG);
-
- if (arg_int_value(cmd, labelsector_ARG, 0) >= LABEL_SCAN_SECTORS) {
- log_error("labelsector must be less than %lu.",
- LABEL_SCAN_SECTORS);
- return 0;
- } else {
- pp->labelsector = arg_int64_value(cmd, labelsector_ARG,
- DEFAULT_LABELSECTOR);
- }
-
- if (!(cmd->fmt->features & FMT_MDAS) &&
- (arg_count(cmd, pvmetadatacopies_ARG) ||
- arg_count(cmd, metadatasize_ARG) ||
- arg_count(cmd, dataalignment_ARG) ||
- arg_count(cmd, dataalignmentoffset_ARG))) {
- log_error("Metadata and data alignment parameters only "
- "apply to text format.");
- return 0;
- }
-
- if (!(cmd->fmt->features & FMT_BAS) &&
- arg_count(cmd, bootloaderareasize_ARG)) {
- log_error("Bootloader area parameters only "
- "apply to text format.");
- return 0;
- }
-
- if (arg_count(cmd, metadataignore_ARG))
- pp->metadataignore = arg_int_value(cmd, metadataignore_ARG,
- DEFAULT_PVMETADATAIGNORE);
- else
- pp->metadataignore = find_config_tree_bool(cmd, metadata_pvmetadataignore_CFG, NULL);
-
- if (arg_count(cmd, pvmetadatacopies_ARG) &&
- !arg_int_value(cmd, pvmetadatacopies_ARG, -1) &&
- pp->metadataignore) {
- log_error("metadataignore only applies to metadatacopies > 0");
- return 0;
- }
-
- pp->zero = arg_int_value(cmd, zero_ARG, 1);
-
- if (arg_sign_value(cmd, dataalignment_ARG, SIGN_NONE) == SIGN_MINUS) {
- log_error("Physical volume data alignment may not be negative.");
- return 0;
- }
- pp->data_alignment = arg_uint64_value(cmd, dataalignment_ARG, UINT64_C(0));
-
- if (pp->data_alignment > UINT32_MAX) {
- log_error("Physical volume data alignment is too big.");
- return 0;
- }
-
- if (arg_sign_value(cmd, dataalignmentoffset_ARG, SIGN_NONE) == SIGN_MINUS) {
- log_error("Physical volume data alignment offset may not be negative");
- return 0;
- }
- pp->data_alignment_offset = arg_uint64_value(cmd, dataalignmentoffset_ARG, UINT64_C(0));
-
- if (pp->data_alignment_offset > UINT32_MAX) {
- log_error("Physical volume data alignment offset is too big.");
- return 0;
- }
-
- if ((pp->data_alignment + pp->data_alignment_offset) &&
- (pp->rp.pe_start != PV_PE_START_CALC)) {
- if ((pp->data_alignment ? pp->rp.pe_start % pp->data_alignment : pp->rp.pe_start) != pp->data_alignment_offset) {
- log_warn("WARNING: Ignoring data alignment %s"
- " incompatible with restored pe_start value %s)",
- display_size(cmd, pp->data_alignment + pp->data_alignment_offset),
- display_size(cmd, pp->rp.pe_start));
- pp->data_alignment = 0;
- pp->data_alignment_offset = 0;
- }
- }
-
- if (arg_sign_value(cmd, metadatasize_ARG, SIGN_NONE) == SIGN_MINUS) {
- log_error("Metadata size may not be negative.");
- return 0;
- }
-
- if (arg_sign_value(cmd, bootloaderareasize_ARG, SIGN_NONE) == SIGN_MINUS) {
- log_error("Bootloader area size may not be negative.");
- return 0;
- }
-
- pp->pvmetadatasize = arg_uint64_value(cmd, metadatasize_ARG, UINT64_C(0));
- if (!pp->pvmetadatasize)
- pp->pvmetadatasize = find_config_tree_int(cmd, metadata_pvmetadatasize_CFG, NULL);
-
- pp->pvmetadatacopies = arg_int_value(cmd, pvmetadatacopies_ARG, -1);
- if (pp->pvmetadatacopies < 0)
- pp->pvmetadatacopies = find_config_tree_int(cmd, metadata_pvmetadatacopies_CFG, NULL);
-
- pp->rp.ba_size = arg_uint64_value(cmd, bootloaderareasize_ARG, pp->rp.ba_size);
-
- return 1;
-}
-
int get_activation_monitoring_mode(struct cmd_context *cmd,
int *monitoring_mode)
{
@@ -1737,8 +1622,6 @@ static int _get_arg_vgnames(struct cmd_context *cmd,
int ret_max = ECMD_PROCESSED;
const char *vg_name;
- log_verbose("Using volume group(s) on command line.");
-
if (one_vgname) {
if (!str_list_add(cmd->mem, arg_vgnames,
dm_pool_strdup(cmd->mem, one_vgname))) {
@@ -2482,8 +2365,6 @@ static int _get_arg_lvnames(struct cmd_context *cmd,
const char *vgname_def;
unsigned dev_dir_found;
- log_verbose("Using logical volume(s) on command line.");
-
for (; opt < argc; opt++) {
lv_name = argv[opt];
dev_dir_found = 0;
@@ -2798,8 +2679,6 @@ static int _get_arg_pvnames(struct cmd_context *cmd,
char *arg_name;
int ret_max = ECMD_PROCESSED;
- log_verbose("Using physical volume(s) on command line.");
-
for (; opt < argc; opt++) {
arg_name = argv[opt];
@@ -2864,6 +2743,8 @@ static int _get_all_devices(struct cmd_context *cmd, struct dm_list *all_devices
struct device_id_list *dil;
int r = ECMD_FAILED;
+ log_very_verbose("Getting list of all devices");
+
lvmcache_seed_infos_from_lvmetad(cmd);
if (!(iter = dev_iter_create(cmd->full_filter, 1))) {
@@ -2989,6 +2870,8 @@ static int _process_device_list(struct cmd_context *cmd, struct dm_list *all_dev
int ret_max = ECMD_PROCESSED;
int ret = 0;
+ log_verbose("Processing devices that are not PVs");
+
/*
* Pretend that each device is a PV with dummy values.
* FIXME Formalise this extension or find an alternative.
@@ -3246,6 +3129,8 @@ static int _process_pvs_in_vgs(struct cmd_context *cmd, uint32_t read_flags,
continue;
}
+ log_verbose("Processing PVs in VG %s", vg_name);
+
vg = vg_read(cmd, vg_name, vg_uuid, read_flags, lockd_state);
if (_ignore_vg(vg, vg_name, NULL, read_flags, &skip, &notfound)) {
stack;
@@ -3286,9 +3171,8 @@ endvg:
}
int process_each_pv(struct cmd_context *cmd,
- int argc, char **argv,
- const char *only_this_vgname,
- uint32_t read_flags,
+ int argc, char **argv, const char *only_this_vgname,
+ int all_is_set, uint32_t read_flags,
struct processing_handle *handle,
process_single_pv_fn_t process_single_pv)
{
@@ -3342,8 +3226,7 @@ int process_each_pv(struct cmd_context *cmd,
process_all_pvs = dm_list_empty(&arg_pvnames) && dm_list_empty(&arg_tags);
- process_all_devices = process_all_pvs && (cmd->command->flags & ENABLE_ALL_DEVS) &&
- arg_count(cmd, all_ARG);
+ process_all_devices = process_all_pvs && (cmd->command->flags & ENABLE_ALL_DEVS) && all_is_set;
/* Needed for a current listing of the global VG namespace. */
if (!only_this_vgname && !lockd_gl(cmd, "sh", 0))
@@ -3355,6 +3238,7 @@ int process_each_pv(struct cmd_context *cmd,
* so that get_vgnameids() will look at any new devices.
*/
if (!trust_cache()) {
+ log_verbose("Scanning for available devices");
dev_cache_full_scan(cmd->full_filter);
lvmcache_destroy(cmd, 1, 0);
}
@@ -3498,3 +3382,721 @@ int lvremove_single(struct cmd_context *cmd, struct logical_volume *lv,
return ECMD_PROCESSED;
}
+
+void pvcreate_each_params_set_defaults(struct pvcreate_each_params *pp)
+{
+ memset(pp, 0, sizeof(*pp));
+
+ pp->zero = 1;
+ pp->size = 0;
+ pp->data_alignment = UINT64_C(0);
+ pp->data_alignment_offset = UINT64_C(0);
+ pp->pvmetadatacopies = DEFAULT_PVMETADATACOPIES;
+ pp->pvmetadatasize = DEFAULT_PVMETADATASIZE;
+ pp->labelsector = DEFAULT_LABELSECTOR;
+ pp->force = PROMPT;
+ pp->yes = 0;
+ pp->metadataignore = DEFAULT_PVMETADATAIGNORE;
+ pp->restorefile = NULL;
+ pp->uuid_str = NULL;
+ pp->ba_start = 0;
+ pp->ba_size = 0;
+ pp->pe_start = PV_PE_START_CALC;
+ pp->extent_count = 0;
+ pp->extent_size = 0;
+
+ dm_list_init(&pp->prompts);
+ dm_list_init(&pp->arg_devices);
+ dm_list_init(&pp->arg_create);
+ dm_list_init(&pp->arg_fail);
+ dm_list_init(&pp->pvs);
+}
+
+int pvcreate_each_params_from_args(struct cmd_context *cmd, struct pvcreate_each_params *pp)
+{
+ pp->yes = arg_count(cmd, yes_ARG);
+ pp->force = (force_t) arg_count(cmd, force_ARG);
+
+ if (arg_int_value(cmd, labelsector_ARG, 0) >= LABEL_SCAN_SECTORS) {
+ log_error("labelsector must be less than %lu.",
+ LABEL_SCAN_SECTORS);
+ return 0;
+ } else {
+ pp->labelsector = arg_int64_value(cmd, labelsector_ARG,
+ DEFAULT_LABELSECTOR);
+ }
+
+ if (!(cmd->fmt->features & FMT_MDAS) &&
+ (arg_count(cmd, pvmetadatacopies_ARG) ||
+ arg_count(cmd, metadatasize_ARG) ||
+ arg_count(cmd, dataalignment_ARG) ||
+ arg_count(cmd, dataalignmentoffset_ARG))) {
+ log_error("Metadata and data alignment parameters only "
+ "apply to text format.");
+ return 0;
+ }
+
+ if (!(cmd->fmt->features & FMT_BAS) &&
+ arg_count(cmd, bootloaderareasize_ARG)) {
+ log_error("Bootloader area parameters only "
+ "apply to text format.");
+ return 0;
+ }
+
+ if (arg_count(cmd, metadataignore_ARG))
+ pp->metadataignore = arg_int_value(cmd, metadataignore_ARG,
+ DEFAULT_PVMETADATAIGNORE);
+ else
+ pp->metadataignore = find_config_tree_bool(cmd, metadata_pvmetadataignore_CFG, NULL);
+
+ if (arg_count(cmd, pvmetadatacopies_ARG) &&
+ !arg_int_value(cmd, pvmetadatacopies_ARG, -1) &&
+ pp->metadataignore) {
+ log_error("metadataignore only applies to metadatacopies > 0");
+ return 0;
+ }
+
+ pp->zero = arg_int_value(cmd, zero_ARG, 1);
+
+ if (arg_sign_value(cmd, dataalignment_ARG, SIGN_NONE) == SIGN_MINUS) {
+ log_error("Physical volume data alignment may not be negative.");
+ return 0;
+ }
+ pp->data_alignment = arg_uint64_value(cmd, dataalignment_ARG, UINT64_C(0));
+
+ if (pp->data_alignment > UINT32_MAX) {
+ log_error("Physical volume data alignment is too big.");
+ return 0;
+ }
+
+ if (arg_sign_value(cmd, dataalignmentoffset_ARG, SIGN_NONE) == SIGN_MINUS) {
+ log_error("Physical volume data alignment offset may not be negative");
+ return 0;
+ }
+ pp->data_alignment_offset = arg_uint64_value(cmd, dataalignmentoffset_ARG, UINT64_C(0));
+
+ if (pp->data_alignment_offset > UINT32_MAX) {
+ log_error("Physical volume data alignment offset is too big.");
+ return 0;
+ }
+
+ if ((pp->data_alignment + pp->data_alignment_offset) &&
+ (pp->pe_start != PV_PE_START_CALC)) {
+ if ((pp->data_alignment ? pp->pe_start % pp->data_alignment : pp->pe_start) != pp->data_alignment_offset) {
+ log_warn("WARNING: Ignoring data alignment %s"
+ " incompatible with restored pe_start value %s)",
+ display_size(cmd, pp->data_alignment + pp->data_alignment_offset),
+ display_size(cmd, pp->pe_start));
+ pp->data_alignment = 0;
+ pp->data_alignment_offset = 0;
+ }
+ }
+
+ if (arg_sign_value(cmd, metadatasize_ARG, SIGN_NONE) == SIGN_MINUS) {
+ log_error("Metadata size may not be negative.");
+ return 0;
+ }
+
+ if (arg_sign_value(cmd, bootloaderareasize_ARG, SIGN_NONE) == SIGN_MINUS) {
+ log_error("Bootloader area size may not be negative.");
+ return 0;
+ }
+
+ pp->pvmetadatasize = arg_uint64_value(cmd, metadatasize_ARG, UINT64_C(0));
+ if (!pp->pvmetadatasize)
+ pp->pvmetadatasize = find_config_tree_int(cmd, metadata_pvmetadatasize_CFG, NULL);
+
+ pp->pvmetadatacopies = arg_int_value(cmd, pvmetadatacopies_ARG, -1);
+ if (pp->pvmetadatacopies < 0)
+ pp->pvmetadatacopies = find_config_tree_int(cmd, metadata_pvmetadatacopies_CFG, NULL);
+
+ if (pp->pvmetadatacopies > 2) {
+ log_error("Metadatacopies may only be 0, 1 or 2");
+ return 0;
+ }
+
+ pp->ba_size = arg_uint64_value(cmd, bootloaderareasize_ARG, pp->ba_size);
+
+ return 1;
+}
+
+enum {
+ PROMPT_PVCREATE_PV_IN_VG = 1,
+};
+
+/*
+ * When a prompt entry is created, save any strings or info
+ * in this struct that are needed for the prompt messages.
+ * The VG/PV structs are not be available when the prompt
+ * is run.
+ */
+struct pvcreate_prompt {
+ struct dm_list list;
+ uint32_t type;
+ const char *pv_name;
+ const char *vg_name;
+ struct device *dev;
+ unsigned answer : 1;
+ unsigned abort_command : 1;
+};
+
+struct pvcreate_device {
+ struct dm_list list;
+ const char *name;
+ struct device *dev;
+ char pvid[ID_LEN + 1];
+ int wiped;
+ int found_existing_label;
+};
+
+static void _run_pvcreate_prompt(struct cmd_context *cmd,
+ struct pvcreate_each_params *pp,
+ struct pvcreate_prompt *prompt)
+{
+ if (prompt->type == PROMPT_PVCREATE_PV_IN_VG) {
+ if (pp->force != DONT_PROMPT_OVERRIDE) {
+ prompt->answer = 0;
+
+ /* FIXME: vgcreate/vgextend have different error messages than pvcreate. */
+ if (!strcmp(command_name(cmd), "pvcreate")) {
+ log_error("Can't initialize physical volume \"%s\" of volume group \"%s\" without -ff",
+ prompt->pv_name, prompt->vg_name);
+ } else {
+ log_error("Physical volume '%s' is already in volume group '%s'",
+ prompt->pv_name, prompt->vg_name);
+ log_error("Unable to add physical volume '%s' to volume group '%s'",
+ prompt->pv_name, prompt->vg_name);
+ }
+ } else if (pp->yes) {
+ prompt->answer = 1;
+ } else {
+ if (yes_no_prompt("Really INITIALIZE physical volume \"%s\" of volume group \"%s\" [y/n]? ",
+ prompt->pv_name, prompt->vg_name) == 'n') {
+ prompt->answer = 0;
+ log_error("%s: physical volume not initialized", prompt->pv_name);
+ } else {
+ prompt->answer = 1;
+ log_warn("WARNING: Forcing physical volume creation on %s of volume group \"%s\"",
+ prompt->pv_name, prompt->vg_name);
+ }
+ }
+ }
+}
+
+static struct pvcreate_device *_pvcreate_list_find_dev(struct dm_list *devices, struct device *dev)
+{
+ struct pvcreate_device *pd;
+
+ dm_list_iterate_items(pd, devices) {
+ if (pd->dev == dev)
+ return pd;
+ }
+
+ return NULL;
+}
+
+/*
+ * If this function decides that a arg_devices entry cannot be used, but the
+ * command might be able to continue without it, then it moves that entry from
+ * arg_devices to arg_fail and returns 1 (success).
+ *
+ * If this function decides that an arg_devices entry could be used (possibly
+ * requiring a prompt), then it moves the entry from arg_devices to arg_create
+ * and returns 1 (success).
+ *
+ * Any arg_devices entries that are not moved to arg_fail or arg_create were
+ * not found. The caller will decide if the command can continue if any
+ * arg_devices entries were not found, or if any were moved to arg_fail.
+ */
+
+static int _pvcreate_check1_single(struct cmd_context *cmd,
+ struct volume_group *vg,
+ struct physical_volume *pv,
+ struct processing_handle *handle)
+{
+ struct pvcreate_each_params *pp = (struct pvcreate_each_params *) handle->custom_handle;
+ struct pvcreate_device *pd;
+ struct pvcreate_prompt *prompt;
+ struct device *dev;
+ int found = 0;
+
+ /*
+ * Check if pv uuid matches a uuid specified by the command.
+ */
+ if (pp->uuid_str && id_equal(&pv->id, &pp->id)) {
+ log_error("uuid %s already in use on \"%s\"", pp->uuid_str, pv_dev_name(pv));
+ pp->check_failed = 1;
+ return 0;
+ }
+
+ /*
+ * Check if one of the command args in arg_devices
+ * matches this device.
+ *
+ * (Possible optimization: the first time this _single
+ * function is called, we could iterate through all
+ * arg_devices entries, do the name to dev lookup
+ * with dev_cache_get() and set the pd->dev fields.
+ * Subsequent _single calls would just compare devs
+ * and not do any dev_cache_get(). This would avoid
+ * repeating dev_cache_get() for arg_devices entries.)
+ */
+ dm_list_iterate_items(pd, &pp->arg_devices) {
+ dev = dev_cache_get(pd->name, cmd->full_filter);
+ if (dev != pv->dev)
+ continue;
+
+ pd->dev = pv->dev;
+ if (pv->dev->pvid[0])
+ strncpy(pd->pvid, pv->dev->pvid, ID_LEN);
+ found = 1;
+ break;
+ }
+
+ /*
+ * The command is not interested in this device.
+ */
+ if (!found)
+ return 1;
+
+ log_verbose("Checking device %s for pvcreate %s",
+ pv_dev_name(pv), pv->dev->pvid[0] ? pv->dev->pvid : "");
+
+ /*
+ * This test will fail if the device belongs to an MD array.
+ */
+ if (!dev_test_excl(pv->dev)) {
+ /* FIXME Detect whether device-mapper itself is still using it */
+ log_error("Can't open %s exclusively. Mounted filesystem?",
+ pv_dev_name(pv));
+ dm_list_move(&pp->arg_fail, &pd->list);
+ return 1;
+ }
+
+ if (is_orphan(pv))
+ pd->found_existing_label = 1;
+
+ /*
+ * pvcreate is being run on this device, and it's not a PV,
+ * or is an orphan PV. Neither case requires a prompt.
+ */
+ if (!vg || is_orphan(pv)) {
+ pd->dev = pv->dev;
+ dm_list_move(&pp->arg_create, &pd->list);
+ return 1;
+ }
+
+ /*
+ * pvcreate is being run on this device, but the device is already
+ * a PV in a VG. A prompt or force option is required to use it.
+ */
+
+ if (!(prompt = dm_pool_zalloc(cmd->mem, sizeof(*prompt)))) {
+ log_error("prompt alloc failed");
+ pp->check_failed = 1;
+ return 0;
+ }
+ prompt->dev = pd->dev;
+ prompt->pv_name = dm_pool_strdup(cmd->mem, pd->name);
+ prompt->vg_name = dm_pool_strdup(cmd->mem, vg->name);
+ prompt->type = PROMPT_PVCREATE_PV_IN_VG;
+ dm_list_add(&pp->prompts, &prompt->list);
+
+ pd->dev = pv->dev;
+ dm_list_move(&pp->arg_create, &pd->list);
+ return 1;
+}
+
+static int _pvcreate_check2_single(struct cmd_context *cmd,
+ const char *vg_name,
+ struct volume_group *vg,
+ struct processing_handle *handle)
+{
+ struct pvcreate_each_params *pp = (struct pvcreate_each_params *) handle->custom_handle;
+ struct pvcreate_device *pd, *pd2;
+ struct pv_list *pvl;
+
+ /*
+ * Ensure arg_create entries were not used in a real VG since the first
+ * check.
+ */
+ dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) {
+ dm_list_iterate_items(pvl, &vg->pvs) {
+ if (pvl->pv->dev != pd->dev)
+ continue;
+
+ log_error("Cannot use device %s: PV was changed during command.", pd->name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ break;
+ }
+ }
+ return 1;
+}
+
+/*
+ * This can be used by pvcreate, vgcreate and vgextend to create PVs. The
+ * callers need to set up the pvcreate_each_params structure based on command
+ * line args. This includes the pv_names field which specifies the devices to
+ * create PVs on.
+ *
+ * This uses process_each_pv() and should be called from a high level in the
+ * command -- the same level at which other instances of process_each are
+ * called.
+ *
+ * This function returns 0 (failed) if the caller requires all specified
+ * devices to be created, and any of those devices are not found, or any of
+ * them cannot be created.
+ *
+ * This function returns 1 (success) if the caller requires all specified
+ * devices to be created, and all are created, or if the caller does not
+ * require all specified devices to be created and one or more were created.
+ *
+ * When this function returns 1 (success), it returns to the caller with the
+ * VG_ORPHANS write lock held.
+ */
+
+int pvcreate_each_device(struct cmd_context *cmd,
+ struct processing_handle *handle,
+ struct pvcreate_each_params *pp)
+{
+ struct pvcreate_restorable_params rp;
+ struct pvcreate_device *pd, *pd2;
+ struct pvcreate_prompt *prompt, *prompt2;
+ struct physical_volume *pv;
+ struct volume_group *orphan_vg;
+ struct pv_list *pvl;
+ struct pv_list *vgpvl;
+ const char *pv_name;
+ int consistent = 0;
+ int must_use_all = (cmd->command->flags & MUST_USE_ALL_ARGS);
+ int found;
+ int i;
+
+ handle->custom_handle = pp;
+
+ /*
+ * Create a list entry for each name arg.
+ */
+ for (i = 0; i < pp->pv_count; i++) {
+ dm_unescape_colons_and_at_signs(pp->pv_names[i], NULL, NULL);
+
+ pv_name = pp->pv_names[i];
+
+ if (!(pd = dm_pool_zalloc(cmd->mem, sizeof(*pd)))) {
+ log_error("alloc failed");
+ return 0;
+ }
+
+ if (!(pd->name = dm_pool_strdup(cmd->mem, pv_name))) {
+ log_error("strdup failed");
+ return 0;
+ }
+
+ dm_list_add(&pp->arg_devices, &pd->list);
+ }
+
+ /*
+ * process all existing PVs and devices (not just args).
+ *
+ * This is a slightly different way to use process_each_pv, because the
+ * command args (arg_devices) are not being processed directly by
+ * process_each_pv (argc and argv are not passed). Instead,
+ * process_each_pv is processing all existing PVs and devices, and the
+ * single function is matching each of those against the command args
+ * (arg_devices).
+ *
+ * If an arg_devices entry is found during process_each, it's moved to
+ * arg_create if it can be used, or arg_fail if it cannot be used. If
+ * it's added to arg_create but needs a prompt or force option, then a
+ * corresponding prompt entry is added to pp->prompts.
+ */
+ cmd->command->flags |= ENABLE_ALL_DEVS;
+
+ /*
+ * FIXME: when checking against existing PVs, we don't want to
+ * skip foreign, clustered or shared VGs.
+ */
+ process_each_pv(cmd, 0, NULL, NULL, 1, 0, handle, _pvcreate_check1_single);
+
+ log_debug("Checking devices for pvcreate found %u to use", dm_list_size(&pp->arg_create));
+
+ /*
+ * A fatal error was found while checking.
+ */
+ if (pp->check_failed)
+ goto_out;
+
+ /*
+ * Check if all arg_devices were found by process_each_pv.
+ */
+ dm_list_iterate_items(pd, &pp->arg_devices)
+ log_error("Device %s not found (or ignored by filtering).", pd->name);
+
+ /*
+ * Can the command continue if some specified devices were not found?
+ */
+ if (!dm_list_empty(&pp->arg_devices) && must_use_all)
+ goto_out;
+
+ /*
+ * Can the command continue if some specified devices cannot be used?
+ */
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ goto_out;
+
+ /*
+ * The command cannot continue if there are no devices to create.
+ */
+ if (dm_list_empty(&pp->arg_create)) {
+ log_error("No devices found.");
+ goto_out;
+ }
+
+ /*
+ * Process any prompts that were gathered during the process_each_pv
+ * checks. Options can override/force/skip prompts.
+ */
+ dm_list_iterate_items_safe(prompt, prompt2, &pp->prompts) {
+ _run_pvcreate_prompt(cmd, pp, prompt);
+
+ if (!prompt->answer) {
+ /* Remove the corresponding entry from arg_create. */
+
+ if ((pd = _pvcreate_list_find_dev(&pp->arg_create, prompt->dev))) {
+ dm_list_move(&pp->arg_fail, &pd->list);
+ } else {
+ /* should never happen */
+ goto_out;
+ }
+ }
+
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ goto_out;
+
+ if (sigint_caught())
+ goto_out;
+
+ if (prompt->abort_command)
+ goto_out;
+ }
+
+ /*
+ * The orphans lock was acquired and released during the
+ * process_each_pv read/check, and is now reacquired to write. The
+ * devices being created could have been changed while the lock was not
+ * held (during the prompts). So, each device in arg_create should now
+ * be checked again to verify it was not changed while the lock was not
+ * held. lvmcache is destroyed after the prompts, and orphans are
+ * reread here. The pvids from the rescanned info are compared with
+ * the pvids seen during the check.
+ *
+ * FIXME: a possible optimization might be to take the VG_ORPHANS write
+ * lock before the check1 loop, then if no prompts are collected during
+ * check1, skip the check2 loop. If prompts were found, then the
+ * orphans lock would be released, prompts processed, the orphans lock
+ * reacquired, and the check2 loop run. This would especially help
+ * vgcreate/vgextend which never use pvcreate prompts.
+ */
+
+ lvmcache_destroy(cmd, 1, 0);
+
+ /*
+ * The lock ordering rules do not want the orphans lock held while VG
+ * locks are taken, so the ordering check is disabled. With the
+ * orphans lock held, process_each_vg locks/unlocks each VG, and then
+ * vgcreate/vgextend will also lock the VG being extended to write it.
+ */
+ lvmcache_lock_ordering(0);
+
+ if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) {
+ log_error("Can't get lock for orphan PVs");
+ goto_out;
+ }
+
+ /*
+ * Check if the arg_create entries were used during the prompts while
+ * the orphans lock was not held. A PV with no MDAs could have been
+ * used in a VG during prompts, and we wouldn't be able to tell without
+ * reading all VGs.
+ */
+ cmd->command->flags |= ALL_VGS_IS_DEFAULT;
+
+ /*
+ * FIXME: when checking if a PV is used, we don't want to skip
+ * foreign or cluster or lockd VGs.
+ */
+ process_each_vg(cmd, 0, NULL, NULL, 0, handle, _pvcreate_check2_single);
+
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ goto_bad;
+
+ /*
+ * Wipe signatures on devices being created.
+ */
+ dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) {
+ log_verbose("Wiping signatures on new PV %s", pd->name);
+
+ if (!wipe_known_signatures(cmd, pd->dev, pd->name, TYPE_LVM1_MEMBER | TYPE_LVM2_MEMBER,
+ 0, pp->yes, pp->force, &pd->wiped)) {
+ dm_list_move(&pp->arg_fail, &pd->list);
+ }
+
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ goto_bad;
+
+ if (sigint_caught())
+ goto_bad;
+ }
+
+ /*
+ * pvcreate does not preserve existing PVs, and always writes a new PV
+ * over an existing orphan.
+ */
+ if (!pp->preserve_existing)
+ goto do_create;
+
+ /*
+ * Find existing orphan PVs that vgcreate or vgextend want to use.
+ * "preserve_existing" means that the command wants to use existing
+ * PVs and not recreate a new PV on top of an existing PV.
+ */
+ log_verbose("Using existing orphan PVs");
+
+ if (!(orphan_vg = vg_read_internal(cmd, cmd->fmt->orphan_vg_name, NULL, 0, &consistent))) {
+ log_error("Cannot read orphans");
+ goto_bad;
+ }
+
+ dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) {
+ if (!pd->found_existing_label)
+ continue;
+
+ if (!(pvl = dm_pool_alloc(cmd->mem, sizeof(*pvl)))) {
+ log_error("alloc pvl failed");
+ dm_list_move(&pp->arg_fail, &pd->list);
+ continue;
+ }
+
+ found = 0;
+ dm_list_iterate_items(vgpvl, &orphan_vg->pvs) {
+ if (vgpvl->pv->dev == pd->dev) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (found) {
+ log_verbose("Using existing orphan PV %s", pv_dev_name(vgpvl->pv));
+ pvl->pv = vgpvl->pv;
+ dm_list_add(&pp->pvs, &pvl->list);
+ } else {
+ log_error("Failed to find PV %s", pd->name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ }
+ }
+
+do_create:
+ /*
+ * Create PVs on devices. Either create a new PV on top of an existing
+ * one (e.g. for pvcreate), or create a new PV on a device that is not
+ * a PV.
+ */
+ dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) {
+ if (pp->preserve_existing && pd->found_existing_label)
+ continue;
+
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ break;
+
+ if (!(pvl = dm_pool_alloc(cmd->mem, sizeof(*pvl)))) {
+ log_error("alloc pvl failed");
+ dm_list_move(&pp->arg_fail, &pd->list);
+ }
+
+ pv_name = pd->name;
+
+ log_verbose("Creating a new PV on %s", pv_name);
+
+ /* FIXME: get rid of rp usage in pv_create to avoid this. */
+ memset(&rp, 0, sizeof(rp));
+ rp.restorefile = pp->restorefile;
+ if (pp->uuid_str) {
+ rp.id = pp->id;
+ rp.idp = &pp->id;
+ }
+ rp.ba_start = pp->ba_start;
+ rp.ba_size = pp->ba_size;
+ rp.pe_start = pp->pe_start;
+ rp.extent_count = pp->extent_count;
+ rp.extent_size = pp->extent_size;
+
+ if (!(pv = pv_create(cmd, pd->dev, pp->size, pp->data_alignment,
+ pp->data_alignment_offset, pp->labelsector,
+ pp->pvmetadatacopies, pp->pvmetadatasize,
+ pp->metadataignore, &rp))) {
+ log_error("Failed to setup physical volume \"%s\"", pv_name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ continue;
+ }
+
+ log_verbose("Set up physical volume for \"%s\" with %" PRIu64
+ " available sectors", pv_name, pv_size(pv));
+
+ if (!label_remove(pv->dev)) {
+ log_error("Failed to wipe existing label on %s", pv_name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ continue;
+ }
+
+ if (pp->zero) {
+ log_verbose("Zeroing start of device %s", pv_name);
+
+ if (!dev_open_quiet(pv->dev)) {
+ log_error("%s not opened: device not zeroed", pv_name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ continue;
+ }
+
+ if (!dev_set(pv->dev, UINT64_C(0), (size_t) 2048, 0)) {
+ log_error("%s not wiped: aborting", pv_name);
+ if (!dev_close(pv->dev))
+ stack;
+ dm_list_move(&pp->arg_fail, &pd->list);
+ continue;
+ }
+ if (!dev_close(pv->dev))
+ stack;
+ }
+
+ log_verbose("Writing physical volume data to disk \"%s\"", pv_name);
+
+ if (!pv_write(cmd, pv, 0)) {
+ log_error("Failed to write physical volume \"%s\"", pv_name);
+ dm_list_move(&pp->arg_fail, &pd->list);
+ }
+
+ log_print_unless_silent("Physical volume \"%s\" successfully created", pv_name);
+
+ pvl->pv = pv;
+ dm_list_add(&pp->pvs, &pvl->list);
+ }
+
+ if (!dm_list_empty(&pp->arg_fail) && must_use_all)
+ goto_bad;
+
+ dm_list_iterate_items(pvl, &pp->pvs) {
+ log_verbose("pvcreate complete for %s", pv_dev_name(pvl->pv));
+ }
+
+ /*
+ * Returns with VG_ORPHANS write lock held because vgcreate and
+ * vgextend want to use the newly created PVs.
+ */
+ return 1;
+
+bad:
+ unlock_vg(cmd, VG_ORPHANS);
+out:
+ return 0;
+}
+