]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libfdisk/samples/mkpart-fullspec.c
libfdisk: don't hardcode label type in samples
[thirdparty/util-linux.git] / libfdisk / samples / mkpart-fullspec.c
CommitLineData
5fb427c8
KZ
1/*
2 * Copyright (C) 2017 Karel Zak <kzak@redhat.com>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 *
7 *
8 * Libfdisk sample to create partitions by specify all required partition
9 * properties (partno, start and size). The defauls is only partition type
10 * (except MBR where 4th partition is extended).
11 */
12#include <stdlib.h>
13#include <unistd.h>
14#include <string.h>
15#include <errno.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <dirent.h>
19#include <getopt.h>
20
21#include "c.h"
22#include "nls.h"
23#include "strutils.h"
24#include "xalloc.h"
25
26#include "libfdisk.h"
27
28static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
29 struct fdisk_ask *ask,
30 void *data)
31{
32 switch(fdisk_ask_get_type(ask)) {
33 case FDISK_ASKTYPE_INFO:
34 fputs(fdisk_ask_print_get_mesg(ask), stdout);
35 fputc('\n', stdout);
36 break;
37 case FDISK_ASKTYPE_WARNX:
38 fflush(stdout);
39 fputs(fdisk_ask_print_get_mesg(ask), stderr);
40 fputc('\n', stderr);
41 break;
42 case FDISK_ASKTYPE_WARN:
43 fflush(stdout);
44 fputs(fdisk_ask_print_get_mesg(ask), stderr);
45 errno = fdisk_ask_print_get_errno(ask);
46 fprintf(stderr, ": %m\n");
47 break;
48 default:
49 break;
50 }
51 return 0;
52}
53
54int main(int argc, char *argv[])
55{
56 struct fdisk_context *cxt;
57 struct fdisk_partition *pa;
58 const char *label = NULL, *device = NULL;
59 int c;
60
61 static const struct option longopts[] = {
62 { "label", required_argument, NULL, 'x' },
63 { "device", required_argument, NULL, 'd' },
64 { "help", no_argument, NULL, 'h' },
65 { NULL, 0, NULL, 0 },
66 };
67
68 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
69
70 fdisk_init_debug(0);
71
72 while((c = getopt_long(argc, argv, "x:d:h", longopts, NULL)) != -1) {
73 switch(c) {
74 case 'x':
75 label = optarg;
76 break;
77 case 'd':
78 device = optarg;
79 break;
80 case 'h':
81 printf("%s [options] <partno,start,size> ...", program_invocation_short_name);
82 fputs(USAGE_SEPARATOR, stdout);
83 puts("Make disklabel and partitions.");
84 puts(" <partno> number from 1..n (4th is extended for MBR)");
85 puts(" <start> partition start offset in sectors");
86 puts(" <size> partition size in sectors");
87 fputs(USAGE_OPTIONS, stdout);
88 puts(" -x, --label <dos,gpt,...> disk label type (default MBR)");
89 puts(" -d, --device <path> block device");
90 puts(" -h, --help this help");
91 fputs(USAGE_SEPARATOR, stdout);
92 return EXIT_SUCCESS;
93 }
94 }
95
96 if (!device)
97 errx(EXIT_FAILURE, "no device specified");
98 if (!label)
99 label = "dos";
100
101 cxt = fdisk_new_context();
102 if (!cxt)
103 err_oom();
104 fdisk_set_ask(cxt, ask_callback, NULL);
105
106 pa = fdisk_new_partition();
107 if (!pa)
108 err_oom();
109
110 if (fdisk_assign_device(cxt, device, 0))
111 err(EXIT_FAILURE, "failed to assign device");
f2b6a0c0 112 if (fdisk_create_disklabel(cxt, label))
5fb427c8
KZ
113 err(EXIT_FAILURE, "failed to create disk label");
114
115 while (optind < argc) {
116 int rc;
117 unsigned int partno = 0;
118 uint64_t start = 0, size = 0;
119 const char *str = argv[optind];
120
121 if (sscanf(str, "%u,%ju,%ju", &partno, &start, &size) != 3)
122 errx(EXIT_FAILURE, "faild to parse %s", str);
123
124 /* disable defaults */
125 fdisk_partition_partno_follow_default(pa, 0);
126 fdisk_partition_end_follow_default(pa, 0);
127
128 /* set all */
129 fdisk_partition_set_partno(pa, partno - 1); /* library uses 0..n */
130 fdisk_partition_set_start(pa, start);
131 fdisk_partition_set_size(pa, size);
132
133 fprintf(stderr, "Requested partition: <partno=%zu,start=%ju,size=%ju>\n",
134 fdisk_partition_get_partno(pa),
135 fdisk_partition_get_start(pa),
136 fdisk_partition_get_size(pa));
137
138 if (fdisk_is_label(cxt, DOS)) {
139 /* Make sure last primary partition is extended if user
140 * wants more than 4 partitions.
141 */
142 if (partno == 4 && optind + 1 < argc) {
143 struct fdisk_parttype *type =
144 fdisk_label_parse_parttype(
145 fdisk_get_label(cxt, NULL), "05");
146 if (!type)
147 err_oom();
148 fdisk_partition_set_type(pa, type);
149 fdisk_unref_parttype(type);
150 }
151 }
152
153 rc = fdisk_add_partition(cxt, pa, NULL);
154 if (rc) {
155 errno = -rc;
156 err(EXIT_FAILURE, "failed to add #%d partition", partno);
157 }
158
159 fdisk_reset_partition(pa);
160 optind++;
161 }
162
163 if (fdisk_write_disklabel(cxt))
164 err(EXIT_FAILURE, "failed to write disk label");
165
166 fdisk_deassign_device(cxt, 1);
167 fdisk_unref_context(cxt);
168 fdisk_unref_partition(pa);
169
170 return EXIT_SUCCESS;
171}