You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cvm/src/packages/exec-lvm/commands/create-volume-group.js

64 lines
2.4 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const execBinary = require("../../exec-binary");
const { errorResult } = require("../../text-parser");
const createRegexParser = require("../../text-parser-regex");
const unattendedFlags = require("../modifiers/unattended-flags");
const errors = require("../errors");
// TODO: Plural versions of handle modifiers? Or just have them accept an array of inputs?
module.exports = function ({ name, physicalVolumes }) {
return Promise.try(() => {
// FIXME: Validatem
if (/^[a-zA-Z0-9_][a-zA-Z0-9+_.-]*$/.test(name)) {
return execBinary("vgcreate", [name, ...physicalVolumes])
.asRoot()
.requireResult()
.withModifier(unattendedFlags)
.expectOnStderr(createRegexParser(/A volume group called ([^"]+) already exists\./, () => {
return errorResult(new errors.VolumeGroupExists(`A volume group with the name '${name}' already exists`, {
volumeGroupName: name
}));
}))
.expectOnStderr(createRegexParser(/WARNING: [a-z]+ signature detected on (.+) at offset/g, (matches) => {
let failedDevices = matches.map((match) => match.subMatches[0]);
let list = failedDevices.join(", ");
return errorResult(new errors.PartitionExists(`Refused to create a Volume Group, as partitions or partition tables already exist on the following devices: ${list}`, {
paths: failedDevices
}));
}))
.expectOnStderr(createRegexParser(/Device (.+) not found\./g, (matches) => {
let failedDevices = matches.map((match) => match.subMatches[0]);
let list = failedDevices.join(", ");
return errorResult(new errors.InvalidPath(`The following specified devices do not exist: ${list}`, {
paths: failedDevices
}));
}))
.expectOnStderr(createRegexParser(/Physical volume '([^']+)' is already in volume group '([^']+)'/g, (matches) => {
let failedItems = matches.map((match) => {
let [ device, volumeGroup ] = match.subMatches;
return { device, volumeGroup };
});
let list = failedItems
.map((item) => `${item.device} (${item.volumeGroup})`)
.join(", ");
return errorResult(new errors.PhysicalVolumeInUse(`The following specified Physical Volumes are already in use in another Volume Group: ${list}`, {
volumes: failedItems
}));
}))
.execute();
} else {
throw new errors.InvalidName(`The specified Volume Group name '${name}' contains invalid characters`);
}
}).then((_output) => {
return true;
});
};