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