]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * SPDX-License-Identifier: GPL-2.0-or-later | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * | |
10 | * partx: tell the kernel about your disk's partitions | |
11 | * [This is not an fdisk - adding and removing partitions | |
12 | * is not a change of the disk, but just telling the kernel | |
13 | * about presence and numbering of on-disk partitions.] | |
14 | * | |
15 | * aeb, 2000-03-21 -- sah is 42 now | |
16 | * | |
17 | * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> | |
18 | * Rewritten to use libblkid for util-linux | |
19 | * based on ideas from Karel Zak <kzak@redhat.com> | |
20 | */ | |
21 | #include <stdio.h> | |
22 | #include <errno.h> | |
23 | #include <stdlib.h> | |
24 | #include <ctype.h> | |
25 | #include <getopt.h> | |
26 | #include <unistd.h> | |
27 | #include <assert.h> | |
28 | #include <dirent.h> | |
29 | ||
30 | #include <blkid.h> | |
31 | #include <libsmartcols.h> | |
32 | ||
33 | #include "c.h" | |
34 | #include "pathnames.h" | |
35 | #include "nls.h" | |
36 | #include "blkdev.h" | |
37 | #include "strutils.h" | |
38 | #include "xalloc.h" | |
39 | #include "partx.h" | |
40 | #include "sysfs.h" | |
41 | #include "loopdev.h" | |
42 | #include "closestream.h" | |
43 | #include "optutils.h" | |
44 | ||
45 | /* this is the default upper limit, could be modified by --nr */ | |
46 | #define SLICES_MAX 256 | |
47 | ||
48 | /* basic table settings */ | |
49 | enum { | |
50 | PARTX_RAW = (1 << 0), | |
51 | PARTX_NOHEADINGS = (1 << 1), | |
52 | PARTX_EXPORT = (1 << 2), | |
53 | }; | |
54 | ||
55 | /* all the columns (-o option) */ | |
56 | enum { | |
57 | COL_PARTNO, | |
58 | COL_START, | |
59 | COL_END, | |
60 | COL_SECTORS, | |
61 | COL_SIZE, | |
62 | COL_NAME, | |
63 | COL_UUID, | |
64 | COL_TYPE, | |
65 | COL_FLAGS, | |
66 | COL_SCHEME, | |
67 | }; | |
68 | ||
69 | #define ACT_ERROR "--{add,delete,show,list,raw,pairs}" | |
70 | enum { | |
71 | ACT_NONE, | |
72 | ACT_LIST, | |
73 | ACT_SHOW, | |
74 | ACT_ADD, | |
75 | ACT_UPD, | |
76 | ACT_DELETE | |
77 | }; | |
78 | ||
79 | enum { | |
80 | FL_BYTES = (1 << 1) | |
81 | }; | |
82 | ||
83 | /* column names */ | |
84 | struct colinfo { | |
85 | const char * const name; /* header */ | |
86 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
87 | int flags; /* SCOLS_FL_* */ | |
88 | const char *help; | |
89 | }; | |
90 | ||
91 | /* columns descriptions */ | |
92 | static const struct colinfo infos[] = { | |
93 | [COL_PARTNO] = { "NR", 0.25, SCOLS_FL_RIGHT, N_("partition number") }, | |
94 | [COL_START] = { "START", 0.30, SCOLS_FL_RIGHT, N_("start of the partition in sectors") }, | |
95 | [COL_END] = { "END", 0.30, SCOLS_FL_RIGHT, N_("end of the partition in sectors") }, | |
96 | [COL_SECTORS] = { "SECTORS", 0.30, SCOLS_FL_RIGHT, N_("number of sectors") }, | |
97 | [COL_SIZE] = { "SIZE", 0.30, SCOLS_FL_RIGHT, N_("human readable size") }, | |
98 | [COL_NAME] = { "NAME", 0.30, SCOLS_FL_TRUNC, N_("partition name") }, | |
99 | [COL_UUID] = { "UUID", 36, 0, N_("partition UUID")}, | |
100 | [COL_SCHEME] = { "SCHEME", 0.1, SCOLS_FL_TRUNC, N_("partition table type (dos, gpt, ...)")}, | |
101 | [COL_FLAGS] = { "FLAGS", 0.1, SCOLS_FL_TRUNC, N_("partition flags")}, | |
102 | [COL_TYPE] = { "TYPE", 1, SCOLS_FL_RIGHT, N_("partition type (a string, a UUID, or hex)")}, | |
103 | }; | |
104 | ||
105 | #define NCOLS ARRAY_SIZE(infos) | |
106 | ||
107 | /* array with IDs of enabled columns */ | |
108 | static int columns[NCOLS]; | |
109 | static size_t ncolumns; | |
110 | ||
111 | static int verbose; | |
112 | static int partx_flags; | |
113 | static struct loopdev_cxt lc; | |
114 | static int loopdev; | |
115 | ||
116 | static void assoc_loopdev(const char *fname) | |
117 | { | |
118 | int rc; | |
119 | ||
120 | if (loopcxt_init(&lc, 0)) | |
121 | err(EXIT_FAILURE, _("failed to initialize loopcxt")); | |
122 | ||
123 | rc = loopcxt_find_unused(&lc); | |
124 | if (rc) | |
125 | err(EXIT_FAILURE, _("%s: failed to find unused loop device"), | |
126 | fname); | |
127 | ||
128 | if (verbose) | |
129 | printf(_("Trying to use '%s' for the loop device\n"), | |
130 | loopcxt_get_device(&lc)); | |
131 | ||
132 | if (loopcxt_set_backing_file(&lc, fname)) | |
133 | err(EXIT_FAILURE, _("%s: failed to set backing file"), fname); | |
134 | ||
135 | rc = loopcxt_setup_device(&lc); | |
136 | ||
137 | if (rc == -EBUSY) | |
138 | err(EXIT_FAILURE, _("%s: failed to set up loop device"), fname); | |
139 | ||
140 | loopdev = 1; | |
141 | } | |
142 | ||
143 | static inline int get_column_id(int num) | |
144 | { | |
145 | assert(ARRAY_SIZE(columns) == NCOLS); | |
146 | assert((size_t)num < ncolumns); | |
147 | assert(columns[num] < (int) NCOLS); | |
148 | return columns[num]; | |
149 | } | |
150 | ||
151 | static inline const struct colinfo *get_column_info(int num) | |
152 | { | |
153 | return &infos[ get_column_id(num) ]; | |
154 | } | |
155 | ||
156 | static int column_name_to_id(const char *name, size_t namesz) | |
157 | { | |
158 | size_t i; | |
159 | ||
160 | assert(name); | |
161 | ||
162 | for (i = 0; i < NCOLS; i++) { | |
163 | const char *cn = infos[i].name; | |
164 | ||
165 | if (!c_strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
166 | return i; | |
167 | } | |
168 | warnx(_("unknown column: %s"), name); | |
169 | return -1; | |
170 | } | |
171 | ||
172 | /* | |
173 | * Given a partition return the corresponding partition number. | |
174 | * | |
175 | * Note that this function tries to use sysfs, otherwise it assumes that the | |
176 | * last characters are always numeric (sda1, sdc20, etc). | |
177 | */ | |
178 | static int get_partno_from_device(char *partition, dev_t devno) | |
179 | { | |
180 | int partno = 0; | |
181 | size_t sz; | |
182 | char *p, *end = NULL; | |
183 | ||
184 | assert(partition); | |
185 | ||
186 | if (devno) { | |
187 | struct path_cxt *pc; | |
188 | int rc; | |
189 | ||
190 | pc = ul_new_sysfs_path(devno, NULL, NULL); | |
191 | if (!pc) | |
192 | goto err; | |
193 | ||
194 | rc = ul_path_read_s32(pc, &partno, "partition"); | |
195 | ul_unref_path(pc); | |
196 | ||
197 | if (rc == 0) | |
198 | return partno; | |
199 | } | |
200 | ||
201 | sz = strlen(partition); | |
202 | p = partition + sz - 1; | |
203 | ||
204 | if (!isdigit((unsigned char) *p)) | |
205 | goto err; | |
206 | ||
207 | while (isdigit((unsigned char) *(p - 1))) p--; | |
208 | ||
209 | errno = 0; | |
210 | partno = strtol(p, &end, 10); | |
211 | if (errno || !end || *end || p == end) | |
212 | goto err; | |
213 | ||
214 | return partno; | |
215 | err: | |
216 | errx(EXIT_FAILURE, _("%s: failed to get partition number"), partition); | |
217 | } | |
218 | ||
219 | static int get_max_partno(const char *disk, dev_t devno) | |
220 | { | |
221 | char path[PATH_MAX], *parent, *dirname = NULL; | |
222 | struct stat st; | |
223 | DIR *dir; | |
224 | struct dirent *d; | |
225 | int partno = 0; | |
226 | ||
227 | if (!devno && !stat(disk, &st)) | |
228 | devno = st.st_rdev; | |
229 | if (!devno) | |
230 | goto dflt; | |
231 | parent = strrchr(disk, '/'); | |
232 | if (!parent) | |
233 | goto dflt; | |
234 | parent++; | |
235 | ||
236 | snprintf(path, sizeof(path), _PATH_SYS_DEVBLOCK "/%d:%d/", | |
237 | major(devno), minor(devno)); | |
238 | ||
239 | dir = opendir(path); | |
240 | if (!dir) | |
241 | goto dflt; | |
242 | ||
243 | dirname = xstrdup(path); | |
244 | ||
245 | while ((d = readdir(dir))) { | |
246 | int fd; | |
247 | ||
248 | if (!strcmp(d->d_name, ".") || | |
249 | !strcmp(d->d_name, "..")) | |
250 | continue; | |
251 | #ifdef _DIRENT_HAVE_D_TYPE | |
252 | if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN) | |
253 | continue; | |
254 | #endif | |
255 | if (strncmp(parent, d->d_name, strlen(parent)) != 0) | |
256 | continue; | |
257 | snprintf(path, sizeof(path), "%s/partition", d->d_name); | |
258 | ||
259 | fd = openat(dirfd(dir), path, O_RDONLY); | |
260 | if (fd) { | |
261 | int x = 0; | |
262 | FILE *f = fdopen(fd, "r"); | |
263 | if (f) { | |
264 | if (fscanf(f, "%d", &x) == 1 && x > partno) | |
265 | partno = x; | |
266 | fclose(f); | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | free(dirname); | |
272 | closedir(dir); | |
273 | return partno; | |
274 | dflt: | |
275 | return SLICES_MAX; | |
276 | } | |
277 | ||
278 | static int recount_range_by_pt(blkid_partlist ls, int *lower, int *upper) | |
279 | { | |
280 | int n = 0, i, nparts = blkid_partlist_numof_partitions(ls); | |
281 | ||
282 | for (i = 0; i < nparts; i++) { | |
283 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
284 | int partno = blkid_partition_get_partno(par); | |
285 | n = max(partno, n); | |
286 | } | |
287 | ||
288 | if (*lower < 0) | |
289 | *lower = n + *lower + 1; | |
290 | if (*upper < 0) | |
291 | *upper = n + *upper + 1; | |
292 | ||
293 | if (*lower > *upper && *upper != 0) { | |
294 | warnx(_("specified range <%d:%d> does not make sense"), *lower, *upper); | |
295 | return -EINVAL; | |
296 | } | |
297 | if (verbose) | |
298 | printf(_("range recount: max partno=%d, lower=%d, upper=%d\n"), n, *lower, *upper); | |
299 | return 0; | |
300 | } | |
301 | ||
302 | static void del_parts_warnx(const char *device, int first, int last) | |
303 | { | |
304 | if (first == last) | |
305 | warnx(_("%s: error deleting partition %d"), device, first); | |
306 | else | |
307 | warnx(_("%s: error deleting partitions %d-%d"), | |
308 | device, first, last); | |
309 | } | |
310 | ||
311 | static int del_parts(int fd, const char *device, dev_t devno, | |
312 | int lower, int upper) | |
313 | { | |
314 | int rc = 0, i, errfirst = 0, errlast = 0; | |
315 | ||
316 | assert(fd >= 0); | |
317 | assert(device); | |
318 | ||
319 | /* recount range by information in /sys */ | |
320 | if (!lower) | |
321 | lower = 1; | |
322 | if (!upper || lower < 0 || upper < 0) { | |
323 | int n = get_max_partno(device, devno); | |
324 | if (!upper) | |
325 | upper = n; | |
326 | else if (upper < 0) | |
327 | upper = n + upper + 1; | |
328 | if (lower < 0) | |
329 | lower = n + lower + 1; | |
330 | } | |
331 | if (lower > upper) { | |
332 | warnx(_("specified range <%d:%d> " | |
333 | "does not make sense"), lower, upper); | |
334 | return -1; | |
335 | } | |
336 | ||
337 | for (i = lower; i <= upper; i++) { | |
338 | if (partx_del_partition(fd, i) == 0) { | |
339 | if (verbose) | |
340 | printf(_("%s: partition #%d removed\n"), device, i); | |
341 | continue; | |
342 | } | |
343 | ||
344 | if (errno == ENXIO) { | |
345 | if (verbose) | |
346 | printf(_("%s: partition #%d doesn't exist\n"), device, i); | |
347 | continue; | |
348 | } | |
349 | rc = -1; | |
350 | if (verbose) | |
351 | warn(_("%s: deleting partition #%d failed"), device, i); | |
352 | if (!errfirst) | |
353 | errlast = errfirst = i; | |
354 | else if (errlast + 1 == i) | |
355 | errlast++; | |
356 | else { | |
357 | del_parts_warnx(device, errfirst, errlast); | |
358 | errlast = errfirst = i; | |
359 | } | |
360 | } | |
361 | ||
362 | if (errfirst) | |
363 | del_parts_warnx(device, errfirst, errlast); | |
364 | return rc; | |
365 | } | |
366 | ||
367 | ||
368 | static void add_parts_warnx(const char *device, int first, int last) | |
369 | { | |
370 | if (first == last) | |
371 | warnx(_("%s: error adding partition %d"), device, first); | |
372 | else | |
373 | warnx(_("%s: error adding partitions %d-%d"), | |
374 | device, first, last); | |
375 | } | |
376 | ||
377 | static int add_parts(int fd, const char *device, | |
378 | blkid_partlist ls, int lower, int upper) | |
379 | { | |
380 | int i, nparts, rc, errfirst = 0, errlast = 0; | |
381 | ||
382 | assert(fd >= 0); | |
383 | assert(device); | |
384 | assert(ls); | |
385 | ||
386 | rc = recount_range_by_pt(ls, &lower, &upper); | |
387 | if (rc) | |
388 | return rc; | |
389 | ||
390 | nparts = blkid_partlist_numof_partitions(ls); | |
391 | ||
392 | for (i = 0; i < nparts; i++) { | |
393 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
394 | int n = blkid_partition_get_partno(par); | |
395 | uintmax_t start, size; | |
396 | ||
397 | if (lower && n < lower) | |
398 | continue; | |
399 | if (upper && n > upper) | |
400 | continue; | |
401 | ||
402 | start = blkid_partition_get_start(par); | |
403 | size = blkid_partition_get_size(par); | |
404 | ||
405 | if (blkid_partition_is_extended(par)) | |
406 | /* | |
407 | * Let's follow the Linux kernel and reduce | |
408 | * DOS extended partition to 1 or 2 sectors. | |
409 | */ | |
410 | size = min(size, (uintmax_t) 2); | |
411 | ||
412 | if (partx_add_partition(fd, n, start, size) == 0) { | |
413 | if (verbose) | |
414 | printf(_("%s: partition #%d added\n"), device, n); | |
415 | continue; | |
416 | } | |
417 | rc = -1; | |
418 | if (verbose) | |
419 | warn(_("%s: adding partition #%d failed"), device, n); | |
420 | if (!errfirst) | |
421 | errlast = errfirst = n; | |
422 | else if (errlast + 1 == n) | |
423 | errlast++; | |
424 | else { | |
425 | add_parts_warnx(device, errfirst, errlast); | |
426 | errlast = errfirst = n; | |
427 | } | |
428 | } | |
429 | ||
430 | if (errfirst) | |
431 | add_parts_warnx(device, errfirst, errlast); | |
432 | ||
433 | /* | |
434 | * The kernel with enabled partitions scanner for loop devices add *all* | |
435 | * partitions, so we should delete any extra, unwanted ones, when the -n | |
436 | * option is passed. | |
437 | */ | |
438 | if (loopdev && loopcxt_is_partscan(&lc) && (lower || upper)) { | |
439 | for (i = 0; i < nparts; i++) { | |
440 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
441 | int n = blkid_partition_get_partno(par); | |
442 | ||
443 | if (n < lower || n > upper) | |
444 | partx_del_partition(fd, n); | |
445 | } | |
446 | } | |
447 | ||
448 | return rc; | |
449 | } | |
450 | ||
451 | static void upd_parts_warnx(const char *device, int first, int last) | |
452 | { | |
453 | if (first == last) | |
454 | warnx(_("%s: error updating partition %d"), device, first); | |
455 | else | |
456 | warnx(_("%s: error updating partitions %d-%d"), | |
457 | device, first, last); | |
458 | } | |
459 | ||
460 | static int upd_parts(int fd, const char *device, dev_t devno, | |
461 | blkid_partlist ls, int lower, int upper) | |
462 | { | |
463 | int n, nparts, rc = 0, errfirst = 0, errlast = 0, err; | |
464 | blkid_partition par; | |
465 | uintmax_t start, size; | |
466 | ||
467 | assert(fd >= 0); | |
468 | assert(device); | |
469 | assert(ls); | |
470 | ||
471 | /* recount range by information in /sys, if on disk number of | |
472 | * partitions is greater than in /sys the use on-disk limit */ | |
473 | nparts = blkid_partlist_numof_partitions(ls); | |
474 | if (!lower) | |
475 | lower = 1; | |
476 | if (!upper || lower < 0 || upper < 0) { | |
477 | n = get_max_partno(device, devno); | |
478 | if (!upper) | |
479 | upper = n > nparts ? n : nparts; | |
480 | else if (upper < 0) | |
481 | upper = n + upper + 1; | |
482 | if (lower < 0) | |
483 | lower = n + lower + 1; | |
484 | } | |
485 | if (lower > upper) { | |
486 | warnx(_("specified range <%d:%d> " | |
487 | "does not make sense"), lower, upper); | |
488 | return -1; | |
489 | } | |
490 | ||
491 | for (n = lower; n <= upper; n++) { | |
492 | par = blkid_partlist_get_partition_by_partno(ls, n); | |
493 | if (!par) { | |
494 | if (verbose) | |
495 | warn(_("%s: no partition #%d"), device, n); | |
496 | continue; | |
497 | } | |
498 | ||
499 | start = blkid_partition_get_start(par); | |
500 | size = blkid_partition_get_size(par); | |
501 | if (blkid_partition_is_extended(par)) | |
502 | /* | |
503 | * Let's follow the Linux kernel and reduce | |
504 | * DOS extended partition to 1 or 2 sectors. | |
505 | */ | |
506 | size = min(size, (uintmax_t) 2); | |
507 | ||
508 | err = partx_del_partition(fd, n); | |
509 | if (err == -1 && errno == ENXIO) | |
510 | err = 0; /* good, it already doesn't exist */ | |
511 | if (err == -1 && errno == EBUSY) | |
512 | { | |
513 | /* try to resize */ | |
514 | err = partx_resize_partition(fd, n, start, size); | |
515 | if (verbose) | |
516 | printf(_("%s: partition #%d resized\n"), device, n); | |
517 | if (err == 0) | |
518 | continue; | |
519 | } | |
520 | if (err == 0 && partx_add_partition(fd, n, start, size) == 0) { | |
521 | if (verbose) | |
522 | printf(_("%s: partition #%d added\n"), device, n); | |
523 | continue; | |
524 | } | |
525 | ||
526 | if (err == 0) | |
527 | continue; | |
528 | rc = -1; | |
529 | if (verbose) | |
530 | warn(_("%s: updating partition #%d failed"), device, n); | |
531 | if (!errfirst) | |
532 | errlast = errfirst = n; | |
533 | else if (errlast + 1 == n) | |
534 | errlast++; | |
535 | else { | |
536 | upd_parts_warnx(device, errfirst, errlast); | |
537 | errlast = errfirst = n; | |
538 | } | |
539 | } | |
540 | ||
541 | if (errfirst) | |
542 | upd_parts_warnx(device, errfirst, errlast); | |
543 | return rc; | |
544 | } | |
545 | ||
546 | static int list_parts(blkid_partlist ls, int lower, int upper) | |
547 | { | |
548 | int i, nparts, rc; | |
549 | ||
550 | assert(ls); | |
551 | ||
552 | rc = recount_range_by_pt(ls, &lower, &upper); | |
553 | if (rc) | |
554 | return rc; | |
555 | ||
556 | nparts = blkid_partlist_numof_partitions(ls); | |
557 | ||
558 | for (i = 0; i < nparts; i++) { | |
559 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
560 | int n = blkid_partition_get_partno(par); | |
561 | uintmax_t start, size; | |
562 | ||
563 | if (lower && n < lower) | |
564 | continue; | |
565 | if (upper && n > upper) | |
566 | continue; | |
567 | ||
568 | start = blkid_partition_get_start(par); | |
569 | size = blkid_partition_get_size(par); | |
570 | ||
571 | printf(P_("#%2d: %9ju-%9ju (%9ju sector, %6ju MB)\n", | |
572 | "#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n", | |
573 | size), | |
574 | n, start, start + size -1, | |
575 | size, (size << 9) / 1000000); | |
576 | } | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static int add_scols_line(struct libscols_table *table, blkid_partition par) | |
581 | { | |
582 | struct libscols_line *line; | |
583 | int i, rc = 0; | |
584 | ||
585 | assert(table); | |
586 | assert(par); | |
587 | ||
588 | line = scols_table_new_line(table, NULL); | |
589 | if (!line) { | |
590 | warn(_("failed to allocate output line")); | |
591 | return -ENOMEM; | |
592 | } | |
593 | ||
594 | for (i = 0; (size_t)i < ncolumns; i++) { | |
595 | char *str = NULL; /* allocated string */ | |
596 | const char *cstr = NULL; /* foreign string */ | |
597 | ||
598 | switch (get_column_id(i)) { | |
599 | case COL_PARTNO: | |
600 | xasprintf(&str, "%d", blkid_partition_get_partno(par)); | |
601 | break; | |
602 | case COL_START: | |
603 | xasprintf(&str, "%ju", blkid_partition_get_start(par)); | |
604 | break; | |
605 | case COL_END: | |
606 | xasprintf(&str, "%ju", | |
607 | blkid_partition_get_start(par) + | |
608 | blkid_partition_get_size(par) - 1); | |
609 | break; | |
610 | case COL_SECTORS: | |
611 | xasprintf(&str, "%ju", blkid_partition_get_size(par)); | |
612 | break; | |
613 | case COL_SIZE: | |
614 | if (partx_flags & FL_BYTES) | |
615 | xasprintf(&str, "%ju", (uintmax_t) | |
616 | blkid_partition_get_size(par) << 9); | |
617 | else | |
618 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, | |
619 | blkid_partition_get_size(par) << 9); | |
620 | break; | |
621 | case COL_NAME: | |
622 | cstr = blkid_partition_get_name(par); | |
623 | break; | |
624 | case COL_UUID: | |
625 | cstr = blkid_partition_get_uuid(par); | |
626 | break; | |
627 | case COL_TYPE: | |
628 | if (blkid_partition_get_type_string(par)) | |
629 | cstr = blkid_partition_get_type_string(par); | |
630 | else | |
631 | xasprintf(&str, "0x%x", | |
632 | blkid_partition_get_type(par)); | |
633 | break; | |
634 | case COL_FLAGS: | |
635 | xasprintf(&str, "0x%llx", blkid_partition_get_flags(par)); | |
636 | break; | |
637 | case COL_SCHEME: | |
638 | { | |
639 | blkid_parttable tab = blkid_partition_get_table(par); | |
640 | if (tab) | |
641 | cstr = blkid_parttable_get_type(tab); | |
642 | break; | |
643 | } | |
644 | default: | |
645 | break; | |
646 | } | |
647 | ||
648 | if (cstr) | |
649 | rc = scols_line_set_data(line, i, cstr); | |
650 | else if (str) | |
651 | rc = scols_line_refer_data(line, i, str); | |
652 | if (rc) { | |
653 | warn(_("failed to add output data")); | |
654 | break; | |
655 | } | |
656 | } | |
657 | ||
658 | return rc; | |
659 | } | |
660 | ||
661 | static int show_parts(blkid_partlist ls, int scols_flags, int lower, int upper) | |
662 | { | |
663 | int i, rc = -1; | |
664 | struct libscols_table *table; | |
665 | int nparts; | |
666 | ||
667 | assert(ls); | |
668 | ||
669 | nparts = blkid_partlist_numof_partitions(ls); | |
670 | if (!nparts) | |
671 | return 0; | |
672 | ||
673 | scols_init_debug(0); | |
674 | table = scols_new_table(); | |
675 | if (!table) { | |
676 | warn(_("failed to allocate output table")); | |
677 | return -1; | |
678 | } | |
679 | scols_table_enable_raw(table, !!(scols_flags & PARTX_RAW)); | |
680 | scols_table_enable_export(table, !!(scols_flags & PARTX_EXPORT)); | |
681 | scols_table_enable_noheadings(table, !!(scols_flags & PARTX_NOHEADINGS)); | |
682 | ||
683 | for (i = 0; (size_t)i < ncolumns; i++) { | |
684 | const struct colinfo *col = get_column_info(i); | |
685 | ||
686 | if (!scols_table_new_column(table, col->name, col->whint, col->flags)) { | |
687 | warnx(_("failed to allocate output column")); | |
688 | goto done; | |
689 | } | |
690 | } | |
691 | ||
692 | rc = recount_range_by_pt(ls, &lower, &upper); | |
693 | if (rc) | |
694 | goto done; | |
695 | ||
696 | for (i = 0; i < nparts; i++) { | |
697 | blkid_partition par = blkid_partlist_get_partition(ls, i); | |
698 | int n = blkid_partition_get_partno(par); | |
699 | ||
700 | if (lower && n < lower) | |
701 | continue; | |
702 | if (upper && n > upper) | |
703 | continue; | |
704 | ||
705 | rc = add_scols_line(table, par); | |
706 | if (rc) | |
707 | break; | |
708 | } | |
709 | ||
710 | rc = 0; | |
711 | scols_print_table(table); | |
712 | done: | |
713 | scols_unref_table(table); | |
714 | return rc; | |
715 | } | |
716 | ||
717 | static blkid_partlist get_partlist(blkid_probe pr, | |
718 | const char *device, char *type) | |
719 | { | |
720 | blkid_partlist ls; | |
721 | blkid_parttable tab; | |
722 | ||
723 | assert(pr); | |
724 | assert(device); | |
725 | ||
726 | if (type) { | |
727 | char *name[] = { type, NULL }; | |
728 | ||
729 | if (blkid_probe_filter_partitions_type(pr, | |
730 | BLKID_FLTR_ONLYIN, name)) { | |
731 | warnx(_("failed to initialize blkid " | |
732 | "filter for '%s'"), type); | |
733 | return NULL; | |
734 | } | |
735 | } | |
736 | ||
737 | ls = blkid_probe_get_partitions(pr); | |
738 | if (!ls) { | |
739 | warnx(_("%s: failed to read partition table"), device); | |
740 | return NULL; | |
741 | } | |
742 | ||
743 | tab = blkid_partlist_get_table(ls); | |
744 | if (verbose && tab) { | |
745 | printf(_("%s: partition table type '%s' detected\n"), | |
746 | device, blkid_parttable_get_type(tab)); | |
747 | ||
748 | if (!blkid_partlist_numof_partitions(ls)) | |
749 | printf(_("%s: partition table with no partitions"), device); | |
750 | } | |
751 | ||
752 | return ls; | |
753 | } | |
754 | ||
755 | static void __attribute__((__noreturn__)) usage(void) | |
756 | { | |
757 | FILE *out = stdout; | |
758 | size_t i; | |
759 | ||
760 | fputs(USAGE_HEADER, out); | |
761 | fprintf(out, | |
762 | _(" %s [-a|-d|-s|-u] [--nr <n:m> | <partition>] <disk>\n"), | |
763 | program_invocation_short_name); | |
764 | ||
765 | fputs(USAGE_SEPARATOR, out); | |
766 | fputs(_("Tell the kernel about the presence and numbering of partitions.\n"), out); | |
767 | ||
768 | fputs(USAGE_OPTIONS, out); | |
769 | fputs(_(" -a, --add add specified partitions or all of them\n"), out); | |
770 | fputs(_(" -d, --delete delete specified partitions or all of them\n"), out); | |
771 | fputs(_(" -u, --update update specified partitions or all of them\n"), out); | |
772 | fputs(_(" -s, --show list partitions\n\n"), out); | |
773 | fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out); | |
774 | fputs(_(" -g, --noheadings don't print headings for --show\n"), out); | |
775 | fputs(_(" -n, --nr <n:m> specify the range of partitions (e.g. --nr 2:4)\n"), out); | |
776 | fputs(_(" -o, --output <list> define which output columns to use\n"), out); | |
777 | fputs(_(" --output-all output all columns\n"), out); | |
778 | fputs(_(" -P, --pairs use key=\"value\" output format\n"), out); | |
779 | fputs(_(" -r, --raw use raw output format\n"), out); | |
780 | fputs(_(" -S, --sector-size <num> overwrite sector size\n"), out); | |
781 | fputs(_(" -t, --type <type> specify the partition type\n"), out); | |
782 | fputs(_(" --list-types list supported partition types and exit\n"), out); | |
783 | fputs(_(" -v, --verbose verbose mode\n"), out); | |
784 | ||
785 | fputs(USAGE_SEPARATOR, out); | |
786 | fprintf(out, USAGE_HELP_OPTIONS(22)); | |
787 | ||
788 | fputs(USAGE_COLUMNS, out); | |
789 | for (i = 0; i < NCOLS; i++) | |
790 | fprintf(out, " %10s %s\n", infos[i].name, _(infos[i].help)); | |
791 | ||
792 | fprintf(out, USAGE_MAN_TAIL("partx(8)")); | |
793 | ||
794 | exit(EXIT_SUCCESS); | |
795 | } | |
796 | ||
797 | int main(int argc, char **argv) | |
798 | { | |
799 | int fd, c, what = ACT_NONE, lower = 0, upper = 0, rc = 0; | |
800 | int scols_flags = 0; | |
801 | char *type = NULL; | |
802 | char *device = NULL; /* pointer to argv[], ie: /dev/sda1 */ | |
803 | char *wholedisk = NULL; /* allocated, ie: /dev/sda */ | |
804 | char *outarg = NULL; | |
805 | dev_t disk_devno = 0, part_devno = 0; | |
806 | unsigned int sector_size = 0; | |
807 | ||
808 | enum { | |
809 | OPT_LIST_TYPES = CHAR_MAX + 1, | |
810 | OPT_OUTPUT_ALL | |
811 | }; | |
812 | static const struct option long_opts[] = { | |
813 | { "bytes", no_argument, NULL, 'b' }, | |
814 | { "noheadings", no_argument, NULL, 'g' }, | |
815 | { "raw", no_argument, NULL, 'r' }, | |
816 | { "list", no_argument, NULL, 'l' }, | |
817 | { "show", no_argument, NULL, 's' }, | |
818 | { "add", no_argument, NULL, 'a' }, | |
819 | { "delete", no_argument, NULL, 'd' }, | |
820 | { "update", no_argument, NULL, 'u' }, | |
821 | { "type", required_argument, NULL, 't' }, | |
822 | { "list-types", no_argument, NULL, OPT_LIST_TYPES }, | |
823 | { "nr", required_argument, NULL, 'n' }, | |
824 | { "output", required_argument, NULL, 'o' }, | |
825 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, | |
826 | { "pairs", no_argument, NULL, 'P' }, | |
827 | { "sector-size",required_argument, NULL, 'S' }, | |
828 | { "help", no_argument, NULL, 'h' }, | |
829 | { "version", no_argument, NULL, 'V' }, | |
830 | { "verbose", no_argument, NULL, 'v' }, | |
831 | { NULL, 0, NULL, 0 } | |
832 | }; | |
833 | ||
834 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ | |
835 | { 'P','a','d','l','r','s','u' }, | |
836 | { 0 } | |
837 | }; | |
838 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
839 | ||
840 | setlocale(LC_ALL, ""); | |
841 | bindtextdomain(PACKAGE, LOCALEDIR); | |
842 | textdomain(PACKAGE); | |
843 | close_stdout_atexit(); | |
844 | ||
845 | while ((c = getopt_long(argc, argv, | |
846 | "abdglrsuvn:t:o:PS:hV", long_opts, NULL)) != -1) { | |
847 | ||
848 | err_exclusive_options(c, long_opts, excl, excl_st); | |
849 | ||
850 | switch(c) { | |
851 | case 'a': | |
852 | what = ACT_ADD; | |
853 | break; | |
854 | case 'b': | |
855 | partx_flags |= FL_BYTES; | |
856 | break; | |
857 | case 'd': | |
858 | what = ACT_DELETE; | |
859 | break; | |
860 | case 'g': | |
861 | scols_flags |= PARTX_NOHEADINGS; | |
862 | break; | |
863 | case 'l': | |
864 | what = ACT_LIST; | |
865 | break; | |
866 | case 'n': | |
867 | if (ul_parse_range(optarg, &lower, &upper, 0)) | |
868 | errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range")); | |
869 | break; | |
870 | case 'o': | |
871 | outarg = optarg; | |
872 | break; | |
873 | case OPT_OUTPUT_ALL: | |
874 | for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++) | |
875 | columns[ncolumns] = ncolumns; | |
876 | break; | |
877 | case 'P': | |
878 | scols_flags |= PARTX_EXPORT; | |
879 | what = ACT_SHOW; | |
880 | break; | |
881 | case 'r': | |
882 | scols_flags |= PARTX_RAW; | |
883 | what = ACT_SHOW; | |
884 | break; | |
885 | case 's': | |
886 | what = ACT_SHOW; | |
887 | break; | |
888 | case 'S': | |
889 | sector_size = strtou32_or_err(optarg, _("invalid sector size argument")); | |
890 | break; | |
891 | case 't': | |
892 | type = optarg; | |
893 | break; | |
894 | case 'u': | |
895 | what = ACT_UPD; | |
896 | break; | |
897 | case 'v': | |
898 | verbose = 1; | |
899 | break; | |
900 | case OPT_LIST_TYPES: | |
901 | { | |
902 | size_t idx = 0; | |
903 | const char *name = NULL; | |
904 | ||
905 | while (blkid_partitions_get_name(idx++, &name) == 0) | |
906 | puts(name); | |
907 | return EXIT_SUCCESS; | |
908 | } | |
909 | case 'h': | |
910 | usage(); | |
911 | case 'V': | |
912 | print_version(EXIT_SUCCESS); | |
913 | default: | |
914 | errtryhelp(EXIT_FAILURE); | |
915 | } | |
916 | } | |
917 | ||
918 | if (what == ACT_NONE) | |
919 | what = ACT_SHOW; | |
920 | ||
921 | /* --show default, could by modified by -o */ | |
922 | if (what == ACT_SHOW && !ncolumns) { | |
923 | columns[ncolumns++] = COL_PARTNO; | |
924 | columns[ncolumns++] = COL_START; | |
925 | columns[ncolumns++] = COL_END; | |
926 | columns[ncolumns++] = COL_SECTORS; | |
927 | columns[ncolumns++] = COL_SIZE; | |
928 | columns[ncolumns++] = COL_NAME; | |
929 | columns[ncolumns++] = COL_UUID; | |
930 | } | |
931 | ||
932 | if (what == ACT_SHOW && outarg && | |
933 | string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), | |
934 | &ncolumns, column_name_to_id) < 0) | |
935 | return EXIT_FAILURE; | |
936 | ||
937 | /* | |
938 | * Note that 'partx /dev/sda1' == 'partx /dev/sda1 /dev/sda' | |
939 | * so assume that the device and/or disk are always the last | |
940 | * arguments to be passed to partx. | |
941 | */ | |
942 | if (optind == argc - 2) { | |
943 | /* passed 2 arguments: | |
944 | * /dev/sda1 /dev/sda : partition + whole-disk | |
945 | * -- /dev/sda1 : partition that should be used as a whole-disk | |
946 | */ | |
947 | device = argv[optind]; | |
948 | ||
949 | if (strcmp(device, "-") == 0) { | |
950 | device = NULL; | |
951 | wholedisk = xstrdup(argv[optind + 1]); | |
952 | } else { | |
953 | device = argv[optind]; | |
954 | wholedisk = xstrdup(argv[optind + 1]); | |
955 | ||
956 | if (device && wholedisk && !ul_startswith(device, wholedisk)) | |
957 | errx(EXIT_FAILURE, _("partition and disk name do not match")); | |
958 | } | |
959 | } else if (optind == argc - 1) { | |
960 | /* passed only one arg (ie: /dev/sda3 or /dev/sda) */ | |
961 | struct stat sb; | |
962 | ||
963 | device = argv[optind]; | |
964 | ||
965 | if (stat(device, &sb)) | |
966 | err(EXIT_FAILURE, _("stat of %s failed"), device); | |
967 | ||
968 | part_devno = sb.st_rdev; | |
969 | ||
970 | if (blkid_devno_to_wholedisk(part_devno, | |
971 | NULL, 0, &disk_devno) == 0 && | |
972 | part_devno != disk_devno) | |
973 | wholedisk = blkid_devno_to_devname(disk_devno); | |
974 | ||
975 | if (!wholedisk) { | |
976 | wholedisk = xstrdup(device); | |
977 | disk_devno = part_devno; | |
978 | device = NULL; | |
979 | part_devno = 0; | |
980 | } | |
981 | } else { | |
982 | warnx(_("bad usage")); | |
983 | errtryhelp(EXIT_FAILURE); | |
984 | } | |
985 | if (device && (upper || lower)) | |
986 | errx(EXIT_FAILURE, _("--nr and <partition> are mutually exclusive")); | |
987 | ||
988 | assert(wholedisk); | |
989 | ||
990 | if (device) { | |
991 | /* use partno from given partition instead of --nr range, e.g: | |
992 | * partx -d /dev/sda3 | |
993 | * is the same like: | |
994 | * partx -d --nr 3 /dev/sda | |
995 | */ | |
996 | struct stat sb; | |
997 | ||
998 | if (!part_devno && !stat(device, &sb)) | |
999 | part_devno = sb.st_rdev; | |
1000 | ||
1001 | lower = upper = get_partno_from_device(device, part_devno); | |
1002 | } | |
1003 | ||
1004 | if (verbose) | |
1005 | printf(_("partition: %s, disk: %s, lower: %d, upper: %d\n"), | |
1006 | device ? device : "none", wholedisk, lower, upper); | |
1007 | ||
1008 | if (what == ACT_ADD || what == ACT_DELETE) { | |
1009 | struct stat x; | |
1010 | ||
1011 | if (stat(wholedisk, &x)) | |
1012 | errx(EXIT_FAILURE, "%s", wholedisk); | |
1013 | ||
1014 | if (S_ISREG(x.st_mode)) { | |
1015 | /* not a blkdev, try to associate it to a loop device */ | |
1016 | if (what == ACT_DELETE) | |
1017 | errx(EXIT_FAILURE, _("%s: cannot delete partitions"), | |
1018 | wholedisk); | |
1019 | if (!loopmod_supports_partscan()) | |
1020 | errx(EXIT_FAILURE, _("%s: partitioned loop devices unsupported"), | |
1021 | wholedisk); | |
1022 | assoc_loopdev(wholedisk); | |
1023 | free(wholedisk); | |
1024 | wholedisk = xstrdup(lc.device); | |
1025 | } else if (!S_ISBLK(x.st_mode)) | |
1026 | errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk); | |
1027 | } | |
1028 | if ((fd = open(wholedisk, O_RDONLY)) == -1) | |
1029 | err(EXIT_FAILURE, _("cannot open %s"), wholedisk); | |
1030 | ||
1031 | if (what == ACT_DELETE) | |
1032 | rc = del_parts(fd, wholedisk, disk_devno, lower, upper); | |
1033 | else { | |
1034 | blkid_probe pr = blkid_new_probe(); | |
1035 | blkid_partlist ls = NULL; | |
1036 | ||
1037 | if (!pr || blkid_probe_set_device(pr, fd, 0, 0)) | |
1038 | warnx(_("%s: failed to initialize blkid prober"), | |
1039 | wholedisk); | |
1040 | else { | |
1041 | if (sector_size) | |
1042 | blkid_probe_set_sectorsize(pr, sector_size); | |
1043 | ||
1044 | ls = get_partlist(pr, wholedisk, type); | |
1045 | } | |
1046 | ||
1047 | if (ls) { | |
1048 | switch (what) { | |
1049 | case ACT_SHOW: | |
1050 | rc = show_parts(ls, scols_flags, lower, upper); | |
1051 | break; | |
1052 | case ACT_LIST: | |
1053 | rc = list_parts(ls, lower, upper); | |
1054 | break; | |
1055 | case ACT_ADD: | |
1056 | rc = add_parts(fd, wholedisk, ls, lower, upper); | |
1057 | break; | |
1058 | case ACT_UPD: | |
1059 | rc = upd_parts(fd, wholedisk, disk_devno, ls, lower, upper); | |
1060 | break; | |
1061 | case ACT_NONE: | |
1062 | break; | |
1063 | default: | |
1064 | abort(); | |
1065 | } | |
1066 | } else | |
1067 | rc = 1; | |
1068 | ||
1069 | blkid_free_probe(pr); | |
1070 | } | |
1071 | ||
1072 | free(wholedisk); | |
1073 | ||
1074 | if (loopdev) | |
1075 | loopcxt_deinit(&lc); | |
1076 | ||
1077 | if (close_fd(fd) != 0) | |
1078 | err(EXIT_FAILURE, _("write failed")); | |
1079 | ||
1080 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; | |
1081 | } |