]>
Commit | Line | Data |
---|---|---|
22853e4a | 1 | /* |
c4ecaf21 | 2 | * partx: tell the kernel about your disk's partitions |
22853e4a KZ |
3 | * [This is not an fdisk - adding and removing partitions |
4 | * is not a change of the disk, but just telling the kernel | |
5 | * about presence and numbering of on-disk partitions.] | |
6 | * | |
22853e4a | 7 | * aeb, 2000-03-21 -- sah is 42 now |
c4ecaf21 DB |
8 | * |
9 | * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> | |
a9485396 | 10 | * Rewritten to use libblkid for util-linux |
c4ecaf21 | 11 | * based on ideas from Karel Zak <kzak@redhat.com> |
22853e4a KZ |
12 | */ |
13 | ||
14 | #include <stdio.h> | |
22853e4a KZ |
15 | #include <errno.h> |
16 | #include <stdlib.h> | |
c4ecaf21 | 17 | #include <ctype.h> |
22853e4a | 18 | #include <getopt.h> |
1d4ad1de | 19 | #include <unistd.h> |
c4ecaf21 | 20 | #include <assert.h> |
c4ecaf21 DB |
21 | #include <dirent.h> |
22 | ||
23 | #include <blkid.h> | |
0d518f34 | 24 | |
1aff9b62 | 25 | #include "c.h" |
c4ecaf21 DB |
26 | #include "pathnames.h" |
27 | #include "nls.h" | |
28 | #include "tt.h" | |
0d518f34 | 29 | #include "blkdev.h" |
c4ecaf21 DB |
30 | #include "strutils.h" |
31 | #include "xalloc.h" | |
22853e4a | 32 | #include "partx.h" |
a88268b8 | 33 | #include "sysfs.h" |
0e938113 | 34 | #include "loopdev.h" |
741a5b10 | 35 | #include "at.h" |
22853e4a | 36 | |
c4ecaf21 DB |
37 | /* this is the default upper limit, could be modified by --nr */ |
38 | #define SLICES_MAX 256 | |
39 | ||
40 | /* all the columns (-o option) */ | |
41 | enum { | |
42 | COL_PARTNO, | |
43 | COL_START, | |
44 | COL_END, | |
45 | COL_SECTORS, | |
46 | COL_SIZE, | |
47 | COL_NAME, | |
48 | COL_UUID, | |
49 | COL_TYPE, | |
5de966b3 KZ |
50 | COL_FLAGS, |
51 | COL_SCHEME, | |
c4ecaf21 | 52 | }; |
22853e4a | 53 | |
c4ecaf21 DB |
54 | enum { |
55 | ACT_LIST = 1, | |
56 | ACT_SHOW, | |
57 | ACT_ADD, | |
58 | ACT_DELETE | |
59 | }; | |
60 | ||
61 | enum { | |
62 | FL_BYTES = (1 << 1) | |
63 | }; | |
64 | ||
65 | /* column names */ | |
66 | struct colinfo { | |
67 | const char *name; /* header */ | |
68 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
69 | int flags; /* TT_FL_* */ | |
70 | const char *help; | |
71 | }; | |
22853e4a | 72 | |
c4ecaf21 | 73 | /* columns descriptions */ |
10ebfeea | 74 | struct colinfo infos[] = { |
5de966b3 | 75 | [COL_PARTNO] = { "NR", 0.25, TT_FL_RIGHT, N_("partition number") }, |
c4ecaf21 DB |
76 | [COL_START] = { "START", 0.30, TT_FL_RIGHT, N_("start of the partition in sectors") }, |
77 | [COL_END] = { "END", 0.30, TT_FL_RIGHT, N_("end of the partition in sectors") }, | |
78 | [COL_SECTORS] = { "SECTORS", 0.30, TT_FL_RIGHT, N_("number of sectors") }, | |
79 | [COL_SIZE] = { "SIZE", 0.30, TT_FL_RIGHT, N_("human readable size") }, | |
80 | [COL_NAME] = { "NAME", 0.30, TT_FL_TRUNC, N_("partition name") }, | |
81 | [COL_UUID] = { "UUID", 36, 0, N_("partition UUID")}, | |
5de966b3 KZ |
82 | [COL_SCHEME] = { "SCHEME", 0.1, TT_FL_TRUNC, N_("partition table type (dos, gpt, ...)")}, |
83 | [COL_FLAGS] = { "FLAGS", 0.1, TT_FL_TRUNC, N_("partition flags")}, | |
84 | [COL_TYPE] = { "TYPE", 1, TT_FL_RIGHT, N_("partition type hex or uuid")}, | |
c4ecaf21 | 85 | }; |
10ebfeea DB |
86 | |
87 | #define NCOLS ARRAY_SIZE(infos) | |
88 | ||
c4ecaf21 | 89 | /* array with IDs of enabled columns */ |
10ebfeea | 90 | static int columns[NCOLS], ncolumns; |
c4ecaf21 DB |
91 | |
92 | static int verbose; | |
93 | static int partx_flags; | |
0e938113 DB |
94 | static struct loopdev_cxt lc; |
95 | static int loopdev; | |
22853e4a | 96 | |
0e938113 DB |
97 | static void assoc_loopdev(const char *fname) |
98 | { | |
99 | int rc; | |
100 | ||
101 | loopcxt_init(&lc, 0); | |
102 | ||
103 | rc = loopcxt_find_unused(&lc); | |
104 | if (rc) | |
105 | err(EXIT_FAILURE, _("%s: failed to find unused loop device"), | |
106 | fname); | |
107 | ||
108 | if (verbose) | |
109 | printf(_("Trying to use '%s' for the loop device\n"), | |
110 | loopcxt_get_device(&lc)); | |
111 | ||
112 | if (loopcxt_set_backing_file(&lc, fname)) | |
113 | err(EXIT_FAILURE, _("%s: failed to set backing file"), fname); | |
114 | ||
115 | rc = loopcxt_setup_device(&lc); | |
116 | ||
117 | if (rc == -EBUSY) | |
118 | err(EXIT_FAILURE, _("%s: failed to setup loop device"), fname); | |
119 | ||
120 | loopdev = 1; | |
121 | } | |
22853e4a | 122 | |
c4ecaf21 | 123 | static inline int get_column_id(int num) |
1d4ad1de | 124 | { |
10ebfeea | 125 | assert(ARRAY_SIZE(columns) == NCOLS); |
c4ecaf21 | 126 | assert(num < ncolumns); |
10ebfeea | 127 | assert(columns[num] < (int) NCOLS); |
c4ecaf21 | 128 | return columns[num]; |
22853e4a KZ |
129 | } |
130 | ||
c4ecaf21 | 131 | static inline struct colinfo *get_column_info(int num) |
1d4ad1de | 132 | { |
c4ecaf21 | 133 | return &infos[ get_column_id(num) ]; |
22853e4a KZ |
134 | } |
135 | ||
c4ecaf21 DB |
136 | static int column_name_to_id(const char *name, size_t namesz) |
137 | { | |
10ebfeea | 138 | size_t i; |
22853e4a | 139 | |
c4ecaf21 | 140 | assert(name); |
22853e4a | 141 | |
10ebfeea | 142 | for (i = 0; i < NCOLS; i++) { |
c4ecaf21 | 143 | const char *cn = infos[i].name; |
22853e4a | 144 | |
c4ecaf21 DB |
145 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) |
146 | return i; | |
22853e4a | 147 | } |
c4ecaf21 DB |
148 | warnx(_("unknown column: %s"), name); |
149 | return -1; | |
150 | } | |
22853e4a | 151 | |
c4ecaf21 DB |
152 | /* |
153 | * Given a partition return the corresponding partition number. | |
154 | * | |
155 | * Note that this function tries to use sysfs, otherwise it assumes that the | |
156 | * last characters are always numeric (sda1, sdc20, etc). | |
c4ecaf21 DB |
157 | */ |
158 | static int get_partno_from_device(char *partition, dev_t devno) | |
159 | { | |
160 | int partno = 0; | |
161 | size_t sz; | |
162 | char *p, *end = NULL; | |
163 | ||
164 | assert(partition); | |
165 | ||
166 | if (devno) { | |
a88268b8 DB |
167 | struct sysfs_cxt cxt; |
168 | ||
169 | sysfs_init(&cxt, devno, NULL); | |
4190aaf6 DB |
170 | if (sysfs_read_int(&cxt, "partition", &partno) >= 0) { |
171 | sysfs_deinit(&cxt); | |
c4ecaf21 | 172 | return partno; |
4190aaf6 | 173 | } |
c4ecaf21 | 174 | } |
22853e4a | 175 | |
c4ecaf21 DB |
176 | sz = strlen(partition); |
177 | p = partition + sz - 1; | |
22853e4a | 178 | |
c4ecaf21 DB |
179 | if (!isdigit((unsigned int) *p)) |
180 | goto err; | |
22853e4a | 181 | |
c4ecaf21 DB |
182 | while (isdigit((unsigned int) *(p - 1))) p--; |
183 | ||
184 | errno = 0; | |
185 | partno = strtol(p, &end, 10); | |
186 | if (errno || !end || *end || p == end) | |
187 | goto err; | |
188 | ||
189 | return partno; | |
190 | err: | |
191 | errx(EXIT_FAILURE, _("%s: failed to get partition number"), partition); | |
192 | } | |
193 | ||
194 | static int get_max_partno(const char *disk, dev_t devno) | |
195 | { | |
741a5b10 | 196 | char path[PATH_MAX], *parent, *dirname = NULL; |
c4ecaf21 DB |
197 | struct stat st; |
198 | DIR *dir; | |
199 | struct dirent *d; | |
200 | int partno = 0; | |
201 | ||
202 | if (!devno && !stat(disk, &st)) | |
203 | devno = st.st_rdev; | |
204 | if (!devno) | |
205 | goto dflt; | |
206 | parent = strrchr(disk, '/'); | |
207 | if (!parent) | |
208 | goto dflt; | |
209 | parent++; | |
210 | ||
211 | snprintf(path, sizeof(path), _PATH_SYS_DEVBLOCK "/%d:%d/", | |
212 | major(devno), minor(devno)); | |
213 | ||
214 | dir = opendir(path); | |
215 | if (!dir) | |
216 | goto dflt; | |
217 | ||
741a5b10 KZ |
218 | dirname = xstrdup(path); |
219 | ||
220 | while ((d = readdir(dir))) { | |
c4ecaf21 DB |
221 | int fd; |
222 | ||
223 | if (!strcmp(d->d_name, ".") || | |
224 | !strcmp(d->d_name, "..")) | |
225 | continue; | |
226 | #ifdef _DIRENT_HAVE_D_TYPE | |
227 | if (d->d_type != DT_DIR) | |
228 | continue; | |
229 | #endif | |
230 | if (strncmp(parent, d->d_name, strlen(parent))) | |
231 | continue; | |
232 | snprintf(path, sizeof(path), "%s/partition", d->d_name); | |
233 | ||
741a5b10 | 234 | fd = open_at(dirfd(dir), dirname, path, O_RDONLY); |
c4ecaf21 DB |
235 | if (fd) { |
236 | int x = 0; | |
237 | FILE *f = fdopen(fd, "r"); | |
238 | if (f) { | |
239 | if (fscanf(f, "%d", &x) == 1 && x > partno) | |
240 | partno = x; | |
241 | fclose(f); | |
22853e4a | 242 | } |
22853e4a | 243 | } |
22853e4a KZ |
244 | } |
245 | ||
741a5b10 | 246 | free(dirname); |
c4ecaf21 DB |
247 | closedir(dir); |
248 | return partno; | |
249 | dflt: | |
250 | return SLICES_MAX; | |
251 | } | |
252 | ||
253 | static void del_parts_warnx(const char *device, int first, int last) | |
254 | { | |
255 | if (first == last) | |
256 | warnx(_("%s: error deleting partition %d"), device, first); | |
257 | else | |
258 | warnx(_("%s: error deleting partitions %d-%d"), | |
259 | device, first, last); | |
260 | } | |
261 | ||
262 | static int del_parts(int fd, const char *device, dev_t devno, | |
263 | int lower, int upper) | |
264 | { | |
265 | int rc = 0, i, errfirst = 0, errlast = 0; | |
266 | ||
267 | assert(fd >= 0); | |
268 | assert(device); | |
269 | ||
270 | if (!lower) | |
271 | lower = 1; | |
272 | if (!upper || lower < 0 || upper < 0) { | |
273 | int n = get_max_partno(device, devno); | |
274 | if (!upper) | |
275 | upper = n; | |
276 | else if (upper < 0) | |
277 | upper = n + upper + 1; | |
278 | if (lower < 0) | |
279 | lower = n + lower + 1; | |
280 | } | |
281 | if (lower > upper) { | |
144df9a2 | 282 | warnx(_("specified range <%d:%d> " |
c4ecaf21 DB |
283 | "does not make sense"), lower, upper); |
284 | return -1; | |
285 | } | |
286 | ||
287 | for (i = lower; i <= upper; i++) { | |
ab025087 PS |
288 | rc = partx_del_partition(fd, i); |
289 | if (rc == 0) { | |
c4ecaf21 DB |
290 | if (verbose) |
291 | printf(_("%s: partition #%d removed\n"), device, i); | |
292 | continue; | |
ab025087 PS |
293 | } else if (errno == ENXIO) { |
294 | if (verbose) | |
295 | printf(_("%s: partition #%d already doesn't exist\n"), device, i); | |
296 | continue; | |
c4ecaf21 DB |
297 | } |
298 | rc = -1; | |
299 | if (verbose) | |
144df9a2 | 300 | warn(_("%s: deleting partition #%d failed"), device, i); |
c4ecaf21 DB |
301 | if (!errfirst) |
302 | errlast = errfirst = i; | |
303 | else if (errlast + 1 == i) | |
304 | errlast++; | |
305 | else { | |
306 | del_parts_warnx(device, errfirst, errlast); | |
307 | errlast = errfirst = i; | |
22853e4a | 308 | } |
22853e4a KZ |
309 | } |
310 | ||
c4ecaf21 DB |
311 | if (errfirst) |
312 | del_parts_warnx(device, errfirst, errlast); | |
313 | return rc; | |
314 | } | |
315 | ||
0e938113 | 316 | |
c4ecaf21 DB |
317 | static void add_parts_warnx(const char *device, int first, int last) |
318 | { | |
319 | if (first == last) | |
320 | warnx(_("%s: error adding partition %d"), device, first); | |
321 | else | |
322 | warnx(_("%s: error adding partitions %d-%d"), | |
323 | device, first, last); | |
324 | } | |
325 | ||
326 | static int add_parts(int fd, const char *device, | |
0e938113 | 327 | blkid_partlist ls, int lower, int upper) |
c4ecaf21 DB |
328 | { |
329 | int i, nparts, rc = 0, errfirst = 0, errlast = 0; | |
330 | ||
331 | assert(fd >= 0); | |
332 | assert(device); | |
333 | assert(ls); | |
334 | ||
335 | nparts = blkid_partlist_numof_partitions(ls); | |
336 | ||
337 | for (i = 0; i < nparts; i++) { | |
338 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
339 | int n = blkid_partition_get_partno(par); | |
340 | uintmax_t start, size; | |
341 | ||
342 | if (lower && n < lower) | |
343 | continue; | |
344 | if (upper && n > upper) | |
345 | continue; | |
346 | ||
347 | start = blkid_partition_get_start(par); | |
348 | size = blkid_partition_get_size(par); | |
349 | ||
350 | if (blkid_partition_is_extended(par)) | |
351 | /* | |
144df9a2 BS |
352 | * Let's follow the Linux kernel and reduce |
353 | * DOS extended partition to 1 or 2 sectors. | |
c4ecaf21 DB |
354 | */ |
355 | size = min(size, (uintmax_t) 2); | |
356 | ||
357 | if (partx_add_partition(fd, n, start, size) == 0) { | |
358 | if (verbose) | |
359 | printf(_("%s: partition #%d added\n"), device, n); | |
360 | continue; | |
361 | } | |
362 | rc = -1; | |
363 | if (verbose) | |
144df9a2 | 364 | warn(_("%s: adding partition #%d failed"), device, n); |
c4ecaf21 DB |
365 | if (!errfirst) |
366 | errlast = errfirst = n; | |
367 | else if (errlast + 1 == n) | |
368 | errlast++; | |
369 | else { | |
370 | add_parts_warnx(device, errfirst, errlast); | |
371 | errlast = errfirst = n; | |
372 | } | |
22853e4a | 373 | } |
c4ecaf21 DB |
374 | |
375 | if (errfirst) | |
376 | add_parts_warnx(device, errfirst, errlast); | |
0e938113 | 377 | |
59d749c3 KZ |
378 | /* |
379 | * The kernel with enabled partitions scanner for loop devices add *all* | |
380 | * partitions, so we should delete any extra, unwanted ones, when the -n | |
381 | * option is passed. | |
382 | */ | |
383 | if (loopdev && loopcxt_is_partscan(&lc) && (lower || upper)) { | |
0e938113 DB |
384 | for (i = 0; i < nparts; i++) { |
385 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
386 | int n = blkid_partition_get_partno(par); | |
387 | ||
388 | if (n < lower || n > upper) | |
389 | partx_del_partition(fd, n); | |
390 | } | |
391 | } | |
392 | ||
c4ecaf21 DB |
393 | return rc; |
394 | } | |
395 | ||
396 | static int list_parts(blkid_partlist ls, int lower, int upper) | |
397 | { | |
398 | int i, nparts; | |
399 | ||
400 | assert(ls); | |
401 | ||
402 | nparts = blkid_partlist_numof_partitions(ls); | |
403 | ||
404 | for (i = 0; i < nparts; i++) { | |
405 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
406 | int n = blkid_partition_get_partno(par); | |
407 | uintmax_t start, size; | |
408 | ||
409 | if (lower && n < lower) | |
410 | continue; | |
411 | if (upper && n > upper) | |
412 | continue; | |
413 | ||
414 | start = blkid_partition_get_start(par); | |
415 | size = blkid_partition_get_size(par); | |
416 | ||
144df9a2 | 417 | printf(_("#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n"), |
c4ecaf21 DB |
418 | n, start, start + size -1, |
419 | size, (size << 9) / 1000000); | |
22853e4a | 420 | } |
c4ecaf21 DB |
421 | return 0; |
422 | } | |
423 | ||
424 | static void add_tt_line(struct tt *tt, blkid_partition par) | |
425 | { | |
426 | struct tt_line *line; | |
427 | int i; | |
428 | ||
429 | assert(tt); | |
430 | assert(par); | |
22853e4a | 431 | |
c4ecaf21 DB |
432 | line = tt_add_line(tt, NULL); |
433 | if (!line) { | |
434 | warn(_("failed to add line to output")); | |
435 | return; | |
22853e4a | 436 | } |
22853e4a | 437 | |
c4ecaf21 DB |
438 | for (i = 0; i < ncolumns; i++) { |
439 | char *str = NULL; | |
440 | int rc = 0; | |
441 | ||
442 | switch (get_column_id(i)) { | |
443 | case COL_PARTNO: | |
444 | rc = asprintf(&str, "%d", | |
445 | blkid_partition_get_partno(par)); | |
446 | break; | |
447 | case COL_START: | |
448 | rc = asprintf(&str, "%ju", | |
449 | blkid_partition_get_start(par)); | |
450 | break; | |
451 | case COL_END: | |
452 | rc = asprintf(&str, "%ju", | |
453 | blkid_partition_get_start(par) + | |
454 | blkid_partition_get_size(par) - 1); | |
455 | break; | |
456 | case COL_SECTORS: | |
457 | rc = asprintf(&str, "%ju", | |
458 | blkid_partition_get_size(par)); | |
459 | break; | |
460 | case COL_SIZE: | |
461 | if (partx_flags & FL_BYTES) | |
462 | rc = asprintf(&str, "%ju", (uintmax_t) | |
463 | blkid_partition_get_size(par) << 9); | |
5de966b3 | 464 | else |
5d2a9849 | 465 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, |
c4ecaf21 | 466 | blkid_partition_get_size(par) << 9); |
c4ecaf21 DB |
467 | break; |
468 | case COL_NAME: | |
469 | str = (char *) blkid_partition_get_name(par); | |
5de966b3 KZ |
470 | if (str) |
471 | str = xstrdup(str); | |
c4ecaf21 DB |
472 | break; |
473 | case COL_UUID: | |
474 | str = (char *) blkid_partition_get_uuid(par); | |
5de966b3 KZ |
475 | if (str) |
476 | str = xstrdup(str); | |
c4ecaf21 DB |
477 | break; |
478 | case COL_TYPE: | |
5de966b3 KZ |
479 | str = (char *) blkid_partition_get_type_string(par); |
480 | if (str) | |
481 | str = xstrdup(str); | |
482 | else | |
483 | rc = asprintf(&str, "0x%x", | |
484 | blkid_partition_get_type(par)); | |
c4ecaf21 | 485 | break; |
5de966b3 KZ |
486 | case COL_FLAGS: |
487 | rc = asprintf(&str, "0x%llx", blkid_partition_get_flags(par)); | |
488 | break; | |
489 | case COL_SCHEME: | |
490 | { | |
491 | blkid_parttable tab = blkid_partition_get_table(par); | |
492 | if (tab) { | |
493 | str = (char *) blkid_parttable_get_type(tab); | |
494 | if (str) | |
495 | str = xstrdup(str); | |
496 | } | |
497 | break; | |
498 | } | |
c4ecaf21 DB |
499 | default: |
500 | break; | |
501 | } | |
502 | ||
5de966b3 | 503 | if (rc || str) |
c4ecaf21 | 504 | tt_line_set_data(line, i, str); |
22853e4a | 505 | } |
c4ecaf21 | 506 | } |
22853e4a | 507 | |
c4ecaf21 DB |
508 | static int show_parts(blkid_partlist ls, int tt_flags, int lower, int upper) |
509 | { | |
510 | int i, rc = -1; | |
511 | struct tt *tt; | |
512 | int nparts; | |
22853e4a | 513 | |
c4ecaf21 DB |
514 | assert(ls); |
515 | ||
516 | nparts = blkid_partlist_numof_partitions(ls); | |
517 | if (!nparts) | |
518 | return 0; | |
519 | ||
520 | tt = tt_new_table(tt_flags); | |
521 | if (!tt) { | |
522 | warn(_("failed to initialize output table")); | |
523 | return -1; | |
22853e4a | 524 | } |
22853e4a | 525 | |
c4ecaf21 DB |
526 | for (i = 0; i < ncolumns; i++) { |
527 | struct colinfo *col = get_column_info(i); | |
528 | ||
529 | if (!tt_define_column(tt, col->name, col->whint, col->flags)) { | |
530 | warnx(_("failed to initialize output column")); | |
531 | goto done; | |
22853e4a KZ |
532 | } |
533 | } | |
534 | ||
c4ecaf21 DB |
535 | for (i = 0; i < nparts; i++) { |
536 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
537 | int n = blkid_partition_get_partno(par); | |
22853e4a | 538 | |
c4ecaf21 DB |
539 | if (lower && n < lower) |
540 | continue; | |
541 | if (upper && n > upper) | |
542 | continue; | |
22853e4a | 543 | |
c4ecaf21 | 544 | add_tt_line(tt, par); |
22853e4a | 545 | } |
c4ecaf21 DB |
546 | |
547 | rc = 0; | |
548 | tt_print_table(tt); | |
549 | done: | |
550 | tt_free_table(tt); | |
551 | return rc; | |
22853e4a KZ |
552 | } |
553 | ||
c4ecaf21 DB |
554 | static blkid_partlist get_partlist(blkid_probe pr, |
555 | const char *device, char *type) | |
556 | { | |
557 | blkid_partlist ls; | |
558 | blkid_parttable tab; | |
559 | ||
560 | assert(pr); | |
561 | assert(device); | |
562 | ||
563 | if (type) { | |
564 | char *name[] = { type, NULL }; | |
565 | ||
566 | if (blkid_probe_filter_partitions_type(pr, | |
567 | BLKID_FLTR_ONLYIN, name)) { | |
568 | warnx(_("failed to initialize blkid " | |
144df9a2 | 569 | "filter for '%s'"), type); |
c4ecaf21 DB |
570 | return NULL; |
571 | } | |
572 | } | |
573 | ||
574 | ls = blkid_probe_get_partitions(pr); | |
575 | if (!ls) { | |
576 | warnx(_("%s: failed to read partition table"), device); | |
577 | return NULL; | |
578 | } | |
579 | ||
580 | tab = blkid_partlist_get_table(ls); | |
83ae4c24 | 581 | if (verbose && tab) { |
144df9a2 | 582 | printf(_("%s: partition table type '%s' detected\n"), |
83ae4c24 | 583 | device, blkid_parttable_get_type(tab)); |
c4ecaf21 | 584 | |
83ae4c24 KZ |
585 | if (!blkid_partlist_numof_partitions(ls)) |
586 | printf(_("%s: partition table with no partitions"), device); | |
22853e4a | 587 | } |
83ae4c24 | 588 | |
c4ecaf21 DB |
589 | return ls; |
590 | } | |
591 | ||
abafd686 | 592 | static void __attribute__((__noreturn__)) usage(FILE *out) |
c4ecaf21 | 593 | { |
10ebfeea | 594 | size_t i; |
c4ecaf21 | 595 | |
4ded3823 | 596 | fputs(USAGE_HEADER, out); |
e96fd176 | 597 | fprintf(out, |
b37155e2 | 598 | _(" %s [-a|-d|-s] [--nr <n:m> | <partition>] <disk>\n"), |
c4ecaf21 DB |
599 | program_invocation_short_name); |
600 | ||
8275b732 | 601 | fputs(USAGE_OPTIONS, out); |
e96fd176 | 602 | fputs(_(" -a, --add add specified partitions or all of them\n" |
c4ecaf21 DB |
603 | " -d, --delete delete specified partitions or all of them\n" |
604 | " -l, --list list partitions (DEPRECATED)\n" | |
605 | " -s, --show list partitions\n\n" | |
606 | ||
607 | " -b, --bytes print SIZE in bytes rather than in human readable format\n" | |
608 | " -g, --noheadings don't print headings for --show\n" | |
8275b732 KZ |
609 | " -n, --nr <n:m> specify the range of partitions (e.g. --nr 2:4)\n" |
610 | " -o, --output <type> define which output columns to use\n" | |
8ed48785 KZ |
611 | " -P, --pairs use key=\"value\" output format\n" |
612 | " -r, --raw use raw output format\n" | |
b37155e2 | 613 | " -t, --type <type> specify the partition type (dos, bsd, solaris, etc.)\n" |
8275b732 KZ |
614 | " -v, --verbose verbose mode\n"), out); |
615 | ||
616 | fputs(USAGE_SEPARATOR, out); | |
617 | fputs(USAGE_HELP, out); | |
618 | fputs(USAGE_VERSION, out); | |
c4ecaf21 | 619 | |
e96fd176 | 620 | fputs(_("\nAvailable columns (for --show, --raw or --pairs):\n"), out); |
c4ecaf21 | 621 | |
10ebfeea | 622 | for (i = 0; i < NCOLS; i++) |
d015794e | 623 | fprintf(out, " %10s %s\n", infos[i].name, _(infos[i].help)); |
c4ecaf21 | 624 | |
4ded3823 | 625 | fprintf(out, USAGE_MAN_TAIL("partx(8)")); |
c4ecaf21 DB |
626 | |
627 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
628 | } | |
629 | ||
abafd686 | 630 | static void __attribute__((__noreturn__)) |
c4ecaf21 DB |
631 | errx_mutually_exclusive(const char *opts) |
632 | { | |
144df9a2 | 633 | errx(EXIT_FAILURE, _("the options %s are mutually exclusive"), opts); |
22853e4a KZ |
634 | } |
635 | ||
c4ecaf21 DB |
636 | int main(int argc, char **argv) |
637 | { | |
638 | int fd, c, what = 0, lower = 0, upper = 0, rc = 0; | |
639 | int tt_flags = 0; | |
640 | char *type = NULL; | |
1d6a5daa | 641 | char *device = NULL; /* pointer to argv[], ie: /dev/sda1 */ |
c4ecaf21 DB |
642 | char *wholedisk = NULL; /* allocated, ie: /dev/sda */ |
643 | dev_t disk_devno = 0, part_devno = 0; | |
644 | ||
645 | static const struct option long_opts[] = { | |
646 | { "bytes", no_argument, NULL, 'b' }, | |
647 | { "noheadings", no_argument, NULL, 'g' }, | |
648 | { "raw", no_argument, NULL, 'r' }, | |
649 | { "list", no_argument, NULL, 'l' }, | |
650 | { "show", no_argument, NULL, 's' }, | |
651 | { "add", no_argument, NULL, 'a' }, | |
652 | { "delete", no_argument, NULL, 'd' }, | |
653 | { "type", required_argument, NULL, 't' }, | |
654 | { "nr", required_argument, NULL, 'n' }, | |
655 | { "output", required_argument, NULL, 'o' }, | |
8ed48785 | 656 | { "pairs", no_argument, NULL, 'P' }, |
c4ecaf21 | 657 | { "help", no_argument, NULL, 'h' }, |
8275b732 | 658 | { "verbose", no_argument, NULL, 'v' }, |
c4ecaf21 DB |
659 | { NULL, 0, NULL, 0 } |
660 | }; | |
661 | ||
5c87aa1a BS |
662 | setlocale(LC_ALL, ""); |
663 | bindtextdomain(PACKAGE, LOCALEDIR); | |
664 | textdomain(PACKAGE); | |
665 | ||
8ed48785 KZ |
666 | while ((c = getopt_long(argc, argv, |
667 | "abdglrsvn:t:o:Ph", long_opts, NULL)) != -1) { | |
668 | ||
c4ecaf21 DB |
669 | switch(c) { |
670 | case 'a': | |
8ed48785 KZ |
671 | case 'd': |
672 | case 'l': | |
673 | case 'r': | |
674 | case 'P': | |
675 | case 's': | |
c4ecaf21 | 676 | if (what) |
8ed48785 KZ |
677 | errx_mutually_exclusive("--{add,delete,show,list,raw,pairs}"); |
678 | break; | |
679 | } | |
680 | ||
681 | switch(c) { | |
682 | case 'a': | |
c4ecaf21 DB |
683 | what = ACT_ADD; |
684 | break; | |
685 | case 'b': | |
686 | partx_flags |= FL_BYTES; | |
687 | break; | |
688 | case 'd': | |
c4ecaf21 DB |
689 | what = ACT_DELETE; |
690 | break; | |
691 | case 'g': | |
692 | tt_flags |= TT_FL_NOHEADINGS; | |
693 | break; | |
694 | case 'l': | |
c4ecaf21 DB |
695 | what = ACT_LIST; |
696 | break; | |
697 | case 'n': | |
af7df9ee | 698 | if (parse_range(optarg, &lower, &upper, 0)) |
c4ecaf21 DB |
699 | errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range")); |
700 | break; | |
c4ecaf21 | 701 | case 'o': |
c87638ad | 702 | ncolumns = string_to_idarray(optarg, |
bdc3ed66 KZ |
703 | columns, ARRAY_SIZE(columns), |
704 | column_name_to_id); | |
705 | if (ncolumns < 0) | |
c4ecaf21 DB |
706 | return EXIT_FAILURE; |
707 | break; | |
8ed48785 KZ |
708 | case 'P': |
709 | tt_flags |= TT_FL_EXPORT; | |
710 | what = ACT_SHOW; | |
711 | break; | |
c4ecaf21 DB |
712 | case 'r': |
713 | tt_flags |= TT_FL_RAW; | |
c4ecaf21 DB |
714 | what = ACT_SHOW; |
715 | break; | |
c4ecaf21 | 716 | case 's': |
c4ecaf21 DB |
717 | what = ACT_SHOW; |
718 | break; | |
719 | case 't': | |
720 | type = optarg; | |
721 | break; | |
722 | case 'v': | |
723 | verbose = 1; | |
724 | break; | |
725 | case 'h': | |
726 | usage(stdout); | |
727 | case '?': | |
728 | default: | |
729 | usage(stderr); | |
730 | } | |
731 | } | |
732 | ||
733 | /* -o <list> enables --show mode by default */ | |
734 | if (ncolumns && !what) | |
735 | what = ACT_SHOW; | |
736 | ||
737 | /* backwardly compatible default */ | |
738 | if (!what) | |
739 | what = ACT_LIST; | |
740 | ||
741 | /* --show default, could by modified by -o */ | |
742 | if (what == ACT_SHOW && !ncolumns) { | |
743 | columns[ncolumns++] = COL_PARTNO; | |
744 | columns[ncolumns++] = COL_START; | |
745 | columns[ncolumns++] = COL_END; | |
746 | columns[ncolumns++] = COL_SECTORS; | |
747 | columns[ncolumns++] = COL_SIZE; | |
748 | columns[ncolumns++] = COL_NAME; | |
749 | columns[ncolumns++] = COL_UUID; | |
750 | } | |
751 | ||
752 | /* | |
753 | * Note that 'partx /dev/sda1' == 'partx /dev/sda1 /dev/sda' | |
754 | * so assume that the device and/or disk are always the last | |
755 | * arguments to be passed to partx. | |
756 | */ | |
757 | if (optind == argc - 2) { | |
758 | /* passed 2 arguments: | |
759 | * /dev/sda1 /dev/sda : partition + whole-disk | |
760 | * -- /dev/sda1 : partition that should be used as a whole-disk | |
761 | */ | |
762 | device = argv[optind]; | |
763 | ||
764 | if (strcmp(device, "-") == 0) { | |
765 | device = NULL; | |
766 | wholedisk = xstrdup(argv[optind + 1]); | |
767 | } else { | |
768 | device = argv[optind]; | |
769 | wholedisk = xstrdup(argv[optind + 1]); | |
770 | } | |
771 | } else if (optind == argc - 1) { | |
772 | /* passed only one arg (ie: /dev/sda3 or /dev/sda) */ | |
773 | struct stat sb; | |
774 | ||
775 | device = argv[optind]; | |
776 | ||
777 | if (stat(device, &sb)) | |
778 | err(EXIT_FAILURE, _("%s: stat failed"), device); | |
779 | ||
780 | part_devno = sb.st_rdev; | |
781 | ||
782 | if (blkid_devno_to_wholedisk(part_devno, | |
783 | NULL, 0, &disk_devno) == 0 && | |
784 | part_devno != disk_devno) | |
785 | wholedisk = blkid_devno_to_devname(disk_devno); | |
786 | ||
787 | if (!wholedisk) { | |
788 | wholedisk = xstrdup(device); | |
789 | disk_devno = part_devno; | |
790 | device = NULL; | |
791 | part_devno = 0; | |
22853e4a | 792 | } |
22853e4a | 793 | } else |
c4ecaf21 DB |
794 | usage(stderr); |
795 | ||
796 | if (device && (upper || lower)) | |
e22d8b95 | 797 | errx(EXIT_FAILURE, _("--nr and <partition> are mutually exclusive")); |
c4ecaf21 DB |
798 | |
799 | assert(wholedisk); | |
800 | ||
801 | if (device) { | |
802 | /* use partno from given partition instead of --nr range, e.g: | |
803 | * partx -d /dev/sda3 | |
804 | * is the same like: | |
805 | * partx -d --nr 3 /dev/sda | |
806 | */ | |
807 | struct stat sb; | |
808 | ||
809 | if (!part_devno && !stat(device, &sb)) | |
810 | part_devno = sb.st_rdev; | |
811 | ||
812 | lower = upper = get_partno_from_device(device, part_devno); | |
813 | } | |
814 | ||
815 | if (verbose) | |
144df9a2 | 816 | printf(_("partition: %s, disk: %s, lower: %d, upper: %d\n"), |
2e2b6bb1 | 817 | device ? device : "none", wholedisk, lower, upper); |
c4ecaf21 DB |
818 | |
819 | if (what == ACT_ADD || what == ACT_DELETE) { | |
820 | struct stat x; | |
821 | ||
0e938113 DB |
822 | if (stat(wholedisk, &x)) |
823 | errx(EXIT_FAILURE, "%s", wholedisk); | |
824 | ||
825 | if (S_ISREG(x.st_mode)) { | |
826 | /* not a blkdev, try to associate it to a loop device */ | |
827 | if (what == ACT_DELETE) | |
828 | errx(EXIT_FAILURE, _("%s: cannot delete partitions"), | |
829 | wholedisk); | |
59d749c3 | 830 | if (!loopmod_supports_partscan()) |
0e938113 DB |
831 | errx(EXIT_FAILURE, _("%s: partitioned loop devices unsupported"), |
832 | wholedisk); | |
833 | assoc_loopdev(wholedisk); | |
834 | wholedisk = xstrdup(lc.device); | |
835 | } else if (!S_ISBLK(x.st_mode)) | |
c4ecaf21 DB |
836 | errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk); |
837 | } | |
838 | if ((fd = open(wholedisk, O_RDONLY)) == -1) | |
839 | err(EXIT_FAILURE, _("%s: open failed"), wholedisk); | |
840 | ||
841 | if (what == ACT_DELETE) | |
842 | rc = del_parts(fd, wholedisk, disk_devno, lower, upper); | |
843 | else { | |
844 | blkid_probe pr = blkid_new_probe(); | |
845 | blkid_partlist ls = NULL; | |
846 | ||
847 | if (!pr || blkid_probe_set_device(pr, fd, 0, 0)) | |
848 | warnx(_("%s: failed to initialize blkid prober"), | |
849 | wholedisk); | |
850 | else | |
851 | ls = get_partlist(pr, wholedisk, type); | |
852 | ||
853 | if (ls) { | |
854 | int n = blkid_partlist_numof_partitions(ls); | |
855 | ||
856 | if (lower < 0) | |
857 | lower = n + lower + 1; | |
858 | if (upper < 0) | |
859 | upper = n + upper + 1; | |
860 | if (lower > upper) { | |
144df9a2 | 861 | warnx(_("specified range <%d:%d> " |
c4ecaf21 DB |
862 | "does not make sense"), lower, upper); |
863 | rc = -1, what = 0; | |
864 | } | |
865 | ||
866 | switch (what) { | |
867 | case ACT_SHOW: | |
868 | rc = show_parts(ls, tt_flags, lower, upper); | |
869 | break; | |
870 | case ACT_LIST: | |
871 | rc = list_parts(ls, lower, upper); | |
872 | break; | |
873 | case ACT_ADD: | |
874 | rc = add_parts(fd, wholedisk, ls, lower, upper); | |
875 | break; | |
876 | } | |
877 | } | |
878 | blkid_free_probe(pr); | |
879 | } | |
880 | ||
0e938113 DB |
881 | if (loopdev) |
882 | loopcxt_deinit(&lc); | |
883 | ||
c4ecaf21 DB |
884 | close(fd); |
885 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; | |
22853e4a | 886 | } |
c4ecaf21 | 887 |