summaryrefslogtreecommitdiffstats
path: root/libfdisk/samples
diff options
context:
space:
mode:
authorKarel Zak2017-02-17 16:15:03 +0100
committerKarel Zak2017-02-17 16:15:03 +0100
commit1c1d30c7501b264742f73b82338ce952f6709c20 (patch)
treeb8b586fd417bccdb530d91be925d0db2ee7f5c4a /libfdisk/samples
parentlibfdisk: (dos) make it possible to create primary/logical by template (diff)
downloadkernel-qcow2-util-linux-1c1d30c7501b264742f73b82338ce952f6709c20.tar.gz
kernel-qcow2-util-linux-1c1d30c7501b264742f73b82338ce952f6709c20.tar.xz
kernel-qcow2-util-linux-1c1d30c7501b264742f73b82338ce952f6709c20.zip
libfdisk: add sample-fdisk-mkpart binary
For example: # sample-fdisk-mkpart --device /dev/sdc 1M 2M 3M - 5M # fdisk -l /dev/sdc ... Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 4095 2048 1M 83 Linux /dev/sdc2 4096 8191 4096 2M 83 Linux /dev/sdc3 8192 14335 6144 3M 83 Linux /dev/sdc4 14336 204799 190464 93M 5 Extended /dev/sdc5 16384 26623 10240 5M 83 Linux Addresses: https://github.com/karelzak/util-linux/issues/204 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/samples')
-rw-r--r--libfdisk/samples/Makemodule.am11
-rw-r--r--libfdisk/samples/mkpart.c172
2 files changed, 183 insertions, 0 deletions
diff --git a/libfdisk/samples/Makemodule.am b/libfdisk/samples/Makemodule.am
new file mode 100644
index 000000000..d7c0cfa78
--- /dev/null
+++ b/libfdisk/samples/Makemodule.am
@@ -0,0 +1,11 @@
+
+check_PROGRAMS += \
+ sample-fdisk-mkpart
+
+sample_fdisk_cflags = $(AM_CFLAGS) $(NO_UNUSED_WARN_CFLAGS) \
+ -I$(ul_libfdisk_incdir)
+sample_fdisk_ldadd = $(LDADD) libfdisk.la
+
+sample_fdisk_mkpart_SOURCES = libfdisk/samples/mkpart.c
+sample_fdisk_mkpart_LDADD = $(sample_fdisk_ldadd) libcommon.la
+sample_fdisk_mkpart_CFLAGS = $(sample_fdisk_cflags)
diff --git a/libfdisk/samples/mkpart.c b/libfdisk/samples/mkpart.c
new file mode 100644
index 000000000..dc476dbe9
--- /dev/null
+++ b/libfdisk/samples/mkpart.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2017 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+#include "xalloc.h"
+
+#include "libfdisk.h"
+
+static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
+ struct fdisk_ask *ask,
+ void *data)
+{
+ switch(fdisk_ask_get_type(ask)) {
+ case FDISK_ASKTYPE_INFO:
+ fputs(fdisk_ask_print_get_mesg(ask), stdout);
+ fputc('\n', stdout);
+ break;
+ case FDISK_ASKTYPE_WARNX:
+ fflush(stdout);
+ fputs(fdisk_ask_print_get_mesg(ask), stderr);
+ fputc('\n', stderr);
+ break;
+ case FDISK_ASKTYPE_WARN:
+ fflush(stdout);
+ fputs(fdisk_ask_print_get_mesg(ask), stderr);
+ errno = fdisk_ask_print_get_errno(ask);
+ fprintf(stderr, ": %m\n");
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct fdisk_context *cxt;
+ struct fdisk_partition *pa;
+ const char *label = NULL, *device = NULL;
+ int n = 0, c;
+ unsigned int sectorsize;
+
+ static const struct option longopts[] = {
+ { "label", required_argument, 0, 'x' },
+ { "device", required_argument, 0, 'd' },
+ { "help", no_argument, 0, 'h' },
+ { NULL, 0, 0, 0 },
+ };
+
+ setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
+
+ fdisk_init_debug(0);
+
+ while((c = getopt_long(argc, argv, "x:d:h", longopts, NULL)) != -1) {
+ switch(c) {
+ case 'x':
+ label = optarg;
+ break;
+ case 'd':
+ device = optarg;
+ break;
+ case 'h':
+ printf("%s [options] <size> ...", program_invocation_short_name);
+ fputs(USAGE_SEPARATOR, stdout);
+ fputs("Make disklabel and partitions.\n", stdout);
+ fputs(USAGE_OPTIONS, stdout);
+ fputs(" -x, --label <dos,gpt,...> disk label type\n", stdout);
+ fputs(" -d, --device <path> block device\n", stdout);
+ fputs(" -h, --help this help\n", stdout);
+ fputs(USAGE_SEPARATOR, stdout);
+ return EXIT_SUCCESS;
+ }
+ }
+
+ if (!device)
+ errx(EXIT_FAILURE, "no device specified");
+ if (!label)
+ label = "dos";
+
+ cxt = fdisk_new_context();
+ if (!cxt)
+ err_oom();
+ fdisk_set_ask(cxt, ask_callback, NULL);
+
+ pa = fdisk_new_partition();
+ if (!pa)
+ err_oom();
+
+ if (fdisk_assign_device(cxt, device, 0))
+ err(EXIT_FAILURE, "failed to assign device");
+ if (fdisk_create_disklabel(cxt, "dos"))
+ err(EXIT_FAILURE, "failed to create disk label");
+
+ sectorsize = fdisk_get_sector_size(cxt);
+
+ while (optind < argc) {
+ int rc;
+ uint64_t size;
+ const char *str = argv[optind];
+
+ /* defaults */
+ fdisk_partition_start_follow_default(pa, 1);
+ fdisk_partition_end_follow_default(pa, 1);
+
+ /* set size */
+ if (isdigit(*str)) {
+ size = strtosize_or_err(argv[optind], "failed to parse partition size");
+ fdisk_partition_set_size(pa, size / sectorsize);
+ fdisk_partition_end_follow_default(pa, 0);
+
+ } else if (*str == '-') {
+ fdisk_partition_end_follow_default(pa, 1);
+ }
+
+ if (fdisk_is_label(cxt, DOS)) {
+ /* For MBR we want to avoid primary/logical dialog.
+ * This is possible by explicitly specified partition
+ * number, <4 means primary, >=4 means logical.
+ */
+ fdisk_partition_partno_follow_default(pa, 0);
+ fdisk_partition_set_partno(pa, n);
+
+ /* Make sure last primary partition is extended if user
+ * wants more than 4 partitions.
+ */
+ if (n == 3 && optind + 1 < argc) {
+ struct fdisk_parttype *type =
+ fdisk_label_parse_parttype(
+ fdisk_get_label(cxt, NULL), "05");
+ if (!type)
+ err_oom();
+ fdisk_partition_set_type(pa, type);
+ fdisk_unref_parttype(type);
+ }
+ } else
+ fdisk_partition_partno_follow_default(pa, 1);
+
+
+ rc = fdisk_add_partition(cxt, pa, NULL);
+ if (rc) {
+ errno = -rc;
+ err(EXIT_FAILURE, "failed to add #%d partition", n + 1);
+ }
+
+ fdisk_reset_partition(pa);
+ optind++;
+ n++;
+ }
+
+ if (fdisk_write_disklabel(cxt))
+ err(EXIT_FAILURE, "failed to write disk label");
+
+ fdisk_deassign_device(cxt, 1);
+ fdisk_unref_context(cxt);
+ fdisk_unref_partition(pa);
+
+ return EXIT_SUCCESS;
+}