2008-02-20 15:52:17 +00:00
|
|
|
/*
|
|
|
|
* parthelper.c: Helper program to talk to parted with.
|
|
|
|
*
|
|
|
|
* This helper exists because parted is GPLv3+, while libvirt is LGPLv2+.
|
|
|
|
* Thus we can't link to parted in libvirt.so without the combined work
|
|
|
|
* being GPLv3+. Thus we separate via an external command. NB, this source
|
|
|
|
* code is still LGPLv2+, but the binary helper is effectively GPLv3+
|
|
|
|
*
|
|
|
|
* The existing 'parted' command line tool is also incredibly hard to parse
|
|
|
|
* in a reliable fashion if merely after a list of partitions & sizes,
|
|
|
|
* though it is fine for creating partitions.
|
|
|
|
*
|
storage: Add new flag for libvirt_parthelper
https://bugzilla.redhat.com/show_bug.cgi?id=1265694
In order to be able to process disk storage pool's using a multipath
device to handle the partitions, libvirt_parthelper will need a way to
not automatically add a partition separator "p" to the generated device
name for each partition found. This is designed to mimic the multipath
features known as 'user_friendly_names' and custom 'alias' name.
If the part_separator attribute is set to "no", then generation of the
multipath partition name will not include the "p" partition separator
unless the source device path name ends with a number. The generated
partition names that get passed back to libvirt are processed in order
to find the device mapper multipath (dm-#) path device.
For example, device path "/dev/mapper/mpatha" would create partitions
"/dev/mapper/mpatha1", "/dev/mapper/mpatha2", etc. instead of
"/dev/mapper/mpathap1", "/dev/mapper/mpathap2", etc. If the device
path ends with a number "/dev/mapper/mpatha1", then the algorithm
to generate names "/dev/mapper/mpatha1p1", "/dev/mapper/mpatha1p2", etc.
would be utilized.
Signed-off-by: John Ferlan <jferlan@redhat.com>
2016-01-07 12:34:51 +00:00
|
|
|
* Copyright (C) 2007-2008, 2010, 2013, 2016 Red Hat, Inc.
|
2008-02-20 15:52:17 +00:00
|
|
|
* Copyright (C) 2007-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-02-20 15:52:17 +00:00
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <parted/parted.h>
|
|
|
|
#include <stdio.h>
|
2009-12-10 16:53:55 +00:00
|
|
|
#include <string.h>
|
2010-07-08 22:04:26 +00:00
|
|
|
#include <libdevmapper.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2008-02-20 15:52:17 +00:00
|
|
|
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2010-06-08 17:27:24 +00:00
|
|
|
#include "c-ctype.h"
|
2013-05-03 12:49:08 +00:00
|
|
|
#include "virstring.h"
|
2016-04-12 22:29:52 +00:00
|
|
|
#include "virgettext.h"
|
2010-06-08 17:27:24 +00:00
|
|
|
|
2009-06-26 16:18:59 +00:00
|
|
|
/* we don't need to include the full internal.h just for this */
|
2014-08-22 12:59:28 +00:00
|
|
|
#define STREQ(a, b) (strcmp(a, b) == 0)
|
2009-06-26 16:18:59 +00:00
|
|
|
|
2008-04-29 13:20:46 +00:00
|
|
|
/* Make the comparisons below fail if your parted headers
|
|
|
|
are so old that they lack the definition. */
|
|
|
|
#ifndef PED_PARTITION_PROTECTED
|
|
|
|
# define PED_PARTITION_PROTECTED 0
|
|
|
|
#endif
|
|
|
|
|
2009-06-26 16:18:59 +00:00
|
|
|
enum diskCommand {
|
|
|
|
DISK_LAYOUT = 0,
|
|
|
|
DISK_GEOMETRY
|
|
|
|
};
|
|
|
|
|
2008-02-20 15:52:17 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
PedDevice *dev;
|
|
|
|
PedDisk *disk;
|
|
|
|
PedPartition *part;
|
2009-06-26 16:18:59 +00:00
|
|
|
int cmd = DISK_LAYOUT;
|
2010-06-08 17:27:24 +00:00
|
|
|
const char *path;
|
2010-07-08 22:04:26 +00:00
|
|
|
char *canonical_path;
|
2010-06-08 17:27:24 +00:00
|
|
|
const char *partsep;
|
2016-05-09 18:57:17 +00:00
|
|
|
bool devmap_partsep = false;
|
2008-02-20 15:52:17 +00:00
|
|
|
|
2016-04-12 22:29:52 +00:00
|
|
|
if (virGettextInitialize() < 0)
|
2010-11-16 19:01:37 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2009-07-10 01:50:20 +00:00
|
|
|
if (argc == 3 && STREQ(argv[2], "-g")) {
|
2009-06-26 16:18:59 +00:00
|
|
|
cmd = DISK_GEOMETRY;
|
storage: Add new flag for libvirt_parthelper
https://bugzilla.redhat.com/show_bug.cgi?id=1265694
In order to be able to process disk storage pool's using a multipath
device to handle the partitions, libvirt_parthelper will need a way to
not automatically add a partition separator "p" to the generated device
name for each partition found. This is designed to mimic the multipath
features known as 'user_friendly_names' and custom 'alias' name.
If the part_separator attribute is set to "no", then generation of the
multipath partition name will not include the "p" partition separator
unless the source device path name ends with a number. The generated
partition names that get passed back to libvirt are processed in order
to find the device mapper multipath (dm-#) path device.
For example, device path "/dev/mapper/mpatha" would create partitions
"/dev/mapper/mpatha1", "/dev/mapper/mpatha2", etc. instead of
"/dev/mapper/mpathap1", "/dev/mapper/mpathap2", etc. If the device
path ends with a number "/dev/mapper/mpatha1", then the algorithm
to generate names "/dev/mapper/mpatha1p1", "/dev/mapper/mpatha1p2", etc.
would be utilized.
Signed-off-by: John Ferlan <jferlan@redhat.com>
2016-01-07 12:34:51 +00:00
|
|
|
} else if (argc == 3 && STREQ(argv[2], "-p")) {
|
2016-05-09 18:57:17 +00:00
|
|
|
devmap_partsep = true;
|
2009-06-26 16:18:59 +00:00
|
|
|
} else if (argc != 2) {
|
storage: Add new flag for libvirt_parthelper
https://bugzilla.redhat.com/show_bug.cgi?id=1265694
In order to be able to process disk storage pool's using a multipath
device to handle the partitions, libvirt_parthelper will need a way to
not automatically add a partition separator "p" to the generated device
name for each partition found. This is designed to mimic the multipath
features known as 'user_friendly_names' and custom 'alias' name.
If the part_separator attribute is set to "no", then generation of the
multipath partition name will not include the "p" partition separator
unless the source device path name ends with a number. The generated
partition names that get passed back to libvirt are processed in order
to find the device mapper multipath (dm-#) path device.
For example, device path "/dev/mapper/mpatha" would create partitions
"/dev/mapper/mpatha1", "/dev/mapper/mpatha2", etc. instead of
"/dev/mapper/mpathap1", "/dev/mapper/mpathap2", etc. If the device
path ends with a number "/dev/mapper/mpatha1", then the algorithm
to generate names "/dev/mapper/mpatha1p1", "/dev/mapper/mpatha1p2", etc.
would be utilized.
Signed-off-by: John Ferlan <jferlan@redhat.com>
2016-01-07 12:34:51 +00:00
|
|
|
fprintf(stderr, _("syntax: %s DEVICE [-g]|[-p]\n"), argv[0]);
|
2008-02-20 15:52:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-26 12:53:57 +00:00
|
|
|
/* NB: Changes to the following algorithm will need corresponding
|
|
|
|
* changes to virStorageBackendDiskDeleteVol */
|
2010-06-08 17:27:24 +00:00
|
|
|
path = argv[1];
|
2011-02-17 07:29:07 +00:00
|
|
|
if (virIsDevMapperDevice(path)) {
|
2016-05-09 18:57:17 +00:00
|
|
|
/* If the path ends with a number or we explicitly request it for
|
|
|
|
* path, then append the "p" partition separator. Otherwise, if
|
|
|
|
* the path ends with a letter already, then no need for a separator.
|
storage: Add new flag for libvirt_parthelper
https://bugzilla.redhat.com/show_bug.cgi?id=1265694
In order to be able to process disk storage pool's using a multipath
device to handle the partitions, libvirt_parthelper will need a way to
not automatically add a partition separator "p" to the generated device
name for each partition found. This is designed to mimic the multipath
features known as 'user_friendly_names' and custom 'alias' name.
If the part_separator attribute is set to "no", then generation of the
multipath partition name will not include the "p" partition separator
unless the source device path name ends with a number. The generated
partition names that get passed back to libvirt are processed in order
to find the device mapper multipath (dm-#) path device.
For example, device path "/dev/mapper/mpatha" would create partitions
"/dev/mapper/mpatha1", "/dev/mapper/mpatha2", etc. instead of
"/dev/mapper/mpathap1", "/dev/mapper/mpathap2", etc. If the device
path ends with a number "/dev/mapper/mpatha1", then the algorithm
to generate names "/dev/mapper/mpatha1p1", "/dev/mapper/mpatha1p2", etc.
would be utilized.
Signed-off-by: John Ferlan <jferlan@redhat.com>
2016-01-07 12:34:51 +00:00
|
|
|
*/
|
2016-05-09 18:57:17 +00:00
|
|
|
if (c_isdigit(path[strlen(path)-1]) || devmap_partsep)
|
storage: Add new flag for libvirt_parthelper
https://bugzilla.redhat.com/show_bug.cgi?id=1265694
In order to be able to process disk storage pool's using a multipath
device to handle the partitions, libvirt_parthelper will need a way to
not automatically add a partition separator "p" to the generated device
name for each partition found. This is designed to mimic the multipath
features known as 'user_friendly_names' and custom 'alias' name.
If the part_separator attribute is set to "no", then generation of the
multipath partition name will not include the "p" partition separator
unless the source device path name ends with a number. The generated
partition names that get passed back to libvirt are processed in order
to find the device mapper multipath (dm-#) path device.
For example, device path "/dev/mapper/mpatha" would create partitions
"/dev/mapper/mpatha1", "/dev/mapper/mpatha2", etc. instead of
"/dev/mapper/mpathap1", "/dev/mapper/mpathap2", etc. If the device
path ends with a number "/dev/mapper/mpatha1", then the algorithm
to generate names "/dev/mapper/mpatha1p1", "/dev/mapper/mpatha1p2", etc.
would be utilized.
Signed-off-by: John Ferlan <jferlan@redhat.com>
2016-01-07 12:34:51 +00:00
|
|
|
partsep = "p";
|
|
|
|
else
|
|
|
|
partsep = "";
|
2013-05-03 12:49:08 +00:00
|
|
|
if (VIR_STRDUP_QUIET(canonical_path, path) < 0)
|
2010-07-08 22:04:26 +00:00
|
|
|
return 2;
|
|
|
|
} else {
|
2014-11-13 14:25:27 +00:00
|
|
|
if (virFileResolveLink(path, &canonical_path) != 0)
|
2010-07-08 22:04:26 +00:00
|
|
|
return 2;
|
|
|
|
|
|
|
|
partsep = *canonical_path &&
|
|
|
|
c_isdigit(canonical_path[strlen(canonical_path)-1]) ? "p" : "";
|
|
|
|
}
|
2010-06-08 17:27:24 +00:00
|
|
|
|
|
|
|
if ((dev = ped_device_get(path)) == NULL) {
|
2010-11-16 19:01:37 +00:00
|
|
|
fprintf(stderr, _("unable to access device %s\n"), path);
|
2008-02-20 15:52:17 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2009-06-26 16:18:59 +00:00
|
|
|
/* return the geometry of the disk and then exit */
|
2012-10-17 09:23:12 +00:00
|
|
|
if (cmd == DISK_GEOMETRY) {
|
2009-06-26 20:14:18 +00:00
|
|
|
printf("%d%c%d%c%d%c",
|
2009-06-26 16:18:59 +00:00
|
|
|
dev->hw_geom.cylinders, '\0',
|
|
|
|
dev->hw_geom.heads, '\0',
|
|
|
|
dev->hw_geom.sectors, '\0');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 15:52:17 +00:00
|
|
|
if ((disk = ped_disk_new(dev)) == NULL) {
|
2010-11-16 19:01:37 +00:00
|
|
|
fprintf(stderr, _("unable to access disk %s\n"), argv[1]);
|
2008-02-20 15:52:17 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the first partition, and then iterate over all */
|
2008-08-20 13:20:01 +00:00
|
|
|
part = ped_disk_next_partition(disk, NULL);
|
2008-02-20 15:52:17 +00:00
|
|
|
while (part) {
|
|
|
|
const char *type;
|
|
|
|
const char *content;
|
|
|
|
if (part->type & PED_PARTITION_LOGICAL) {
|
|
|
|
type = "logical";
|
|
|
|
if (part->type & PED_PARTITION_FREESPACE)
|
|
|
|
content = "free";
|
|
|
|
else if (part->type & PED_PARTITION_METADATA)
|
|
|
|
content = "metadata";
|
2013-01-22 14:41:04 +00:00
|
|
|
/* coverity[dead_error_condition] - not true if defined */
|
2008-02-20 15:52:17 +00:00
|
|
|
else if (part->type & PED_PARTITION_PROTECTED)
|
|
|
|
content = "protected";
|
|
|
|
else
|
|
|
|
content = "data";
|
|
|
|
} else if (part->type == PED_PARTITION_EXTENDED) {
|
|
|
|
type = "extended";
|
|
|
|
content = "metadata";
|
|
|
|
} else {
|
|
|
|
type = "normal";
|
|
|
|
if (part->type & PED_PARTITION_FREESPACE)
|
|
|
|
content = "free";
|
|
|
|
else if (part->type & PED_PARTITION_METADATA)
|
|
|
|
content = "metadata";
|
2013-01-22 14:41:04 +00:00
|
|
|
/* coverity[dead_error_condition] - not true if defined */
|
2008-02-20 15:52:17 +00:00
|
|
|
else if (part->type & PED_PARTITION_PROTECTED)
|
|
|
|
content = "protected";
|
|
|
|
else
|
|
|
|
content = "data";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We do +1 on geom.end, because we want end of the last sector
|
|
|
|
* in bytes, not the last sector number
|
|
|
|
*/
|
|
|
|
if (part->num != -1) {
|
2010-06-08 17:27:24 +00:00
|
|
|
printf("%s%s%d%c%s%c%s%c%llu%c%llu%c%llu%c",
|
2010-07-08 22:04:26 +00:00
|
|
|
canonical_path, partsep,
|
2008-02-20 15:52:17 +00:00
|
|
|
part->num, '\0',
|
|
|
|
type, '\0',
|
|
|
|
content, '\0',
|
2011-08-23 14:25:28 +00:00
|
|
|
part->geom.start * dev->sector_size, '\0',
|
2012-10-17 09:23:12 +00:00
|
|
|
(part->geom.end + 1) * dev->sector_size, '\0',
|
2011-08-23 14:25:28 +00:00
|
|
|
part->geom.length * dev->sector_size, '\0');
|
2008-02-20 15:52:17 +00:00
|
|
|
} else {
|
|
|
|
printf("%s%c%s%c%s%c%llu%c%llu%c%llu%c",
|
|
|
|
"-", '\0',
|
|
|
|
type, '\0',
|
|
|
|
content, '\0',
|
2011-08-23 14:25:28 +00:00
|
|
|
part->geom.start * dev->sector_size, '\0',
|
2012-10-17 09:23:12 +00:00
|
|
|
(part->geom.end + 1) * dev->sector_size, '\0',
|
2011-08-23 14:25:28 +00:00
|
|
|
part->geom.length * dev->sector_size, '\0');
|
2008-02-20 15:52:17 +00:00
|
|
|
}
|
|
|
|
part = ped_disk_next_partition(disk, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|