1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 Red Hat Inc.
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 * Parted 1.9 sends error messages to stdout, hence use of the
34 * COMMAND_FLAG_FOLD_STDOUT_ON_STDERR flag.
36 * parted occasionally fails to do ioctl(BLKRRPART) on the device,
37 * apparently because of some internal race in the code. We attempt
38 * to detect and recover from this error if we can.
41 recover_blkrrpart (const char *device, const char *err)
46 "Error informing the kernel about modifications to partition"))
49 r = command (NULL, NULL, "blockdev", "--rereadpt", device, NULL);
58 #define RUN_PARTED(error,device,...) \
63 r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR, \
64 "parted", "-s", "--", (device), __VA_ARGS__); \
66 if (recover_blkrrpart ((device), err) == -1) { \
67 reply_with_error ("%s: parted: %s: %s", __func__, (device), err); \
77 check_parttype (const char *parttype)
79 /* Check and translate parttype. */
80 if (STREQ (parttype, "aix") ||
81 STREQ (parttype, "amiga") ||
82 STREQ (parttype, "bsd") ||
83 STREQ (parttype, "dasd") ||
84 STREQ (parttype, "dvh") ||
85 STREQ (parttype, "gpt") ||
86 STREQ (parttype, "mac") ||
87 STREQ (parttype, "msdos") ||
88 STREQ (parttype, "pc98") ||
89 STREQ (parttype, "sun"))
91 else if (STREQ (parttype, "rdb"))
93 else if (STREQ (parttype, "efi"))
95 else if (STREQ (parttype, "mbr"))
102 do_part_init (const char *device, const char *parttype)
104 parttype = check_parttype (parttype);
106 reply_with_error ("unknown partition type: common choices are \"gpt\" and \"msdos\"");
110 RUN_PARTED (return -1, device, "mklabel", parttype, NULL);
118 do_part_add (const char *device, const char *prlogex,
119 int64_t startsect, int64_t endsect)
124 /* Check and translate prlogex. */
125 if (STREQ (prlogex, "primary") ||
126 STREQ (prlogex, "logical") ||
127 STREQ (prlogex, "extended"))
129 else if (STREQ (prlogex, "p"))
131 else if (STREQ (prlogex, "l"))
133 else if (STREQ (prlogex, "e"))
134 prlogex = "extended";
136 reply_with_error ("unknown partition type: %s: this should be \"primary\", \"logical\" or \"extended\"", prlogex);
141 reply_with_error ("startsect cannot be negative");
144 /* but endsect can be negative */
146 snprintf (startstr, sizeof startstr, "%" PRIi64 "s", startsect);
147 snprintf (endstr, sizeof endstr, "%" PRIi64 "s", endsect);
149 /* XXX Bug: If the partition table type (which we don't know in this
150 * function) is GPT, then this parted command sets the _partition
151 * name_ to prlogex, eg. "primary". I would essentially describe
152 * this as a bug in the parted mkpart command.
154 RUN_PARTED (return -1, device, "mkpart", prlogex, startstr, endstr, NULL);
162 do_part_del (const char *device, int partnum)
165 reply_with_error ("partition number must be >= 1");
169 char partnum_str[16];
170 snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
172 RUN_PARTED (return -1, device, "rm", partnum_str, NULL);
179 do_part_disk (const char *device, const char *parttype)
181 parttype = check_parttype (parttype);
183 reply_with_error ("unknown partition type: common choices are \"gpt\" and \"msdos\"");
187 /* Align all partitions created this way to 128 sectors, and leave
188 * the last 128 sectors at the end of the disk free. This wastes
189 * 64K+64K = 128K on 512-byte sector disks. The rationale is:
191 * - aligned operations are faster
192 * - absolute minimum recommended alignment is 64K (1M would be better)
193 * - GPT requires at least 34 sectors at the end of the disk.
195 const char *startstr = "128s";
196 const char *endstr = "-128s";
198 RUN_PARTED (return -1,
201 /* See comment about about the parted mkpart command. */
202 "mkpart", STREQ (parttype, "gpt") ? "p1" : "primary",
203 startstr, endstr, NULL);
211 do_part_set_bootable (const char *device, int partnum, int bootable)
214 reply_with_error ("partition number must be >= 1");
220 snprintf (partstr, sizeof partstr, "%d", partnum);
222 RUN_PARTED (return -1,
223 device, "set", partstr, "boot", bootable ? "on" : "off", NULL);
231 do_part_set_name (const char *device, int partnum, const char *name)
234 reply_with_error ("partition number must be >= 1");
240 snprintf (partstr, sizeof partstr, "%d", partnum);
242 RUN_PARTED (return -1, device, "name", partstr, name, NULL);
249 /* Return the nth field from a string of ':'/';'-delimited strings.
250 * Useful for parsing the return value from 'parted -m'.
253 get_table_field (const char *line, int n)
255 const char *p = line;
257 while (*p && n > 0) {
258 p += strcspn (p, ":;") + 1;
263 reply_with_error ("not enough fields in output of parted print command: %s",
268 size_t len = strcspn (p, ":;");
269 char *q = strndup (p, len);
271 reply_with_perror ("strndup");
278 /* RHEL 5 parted doesn't have the -m (machine readable) option so we
279 * must do a lot more work to parse the output in
280 * print_partition_table below. Test for this option the first time
281 * this function is called.
284 test_parted_m_opt (void)
286 static int result = -1;
292 int r = commandr (NULL, &err, "parted", "-s", "-m", "/dev/null", NULL);
294 /* Test failed, eg. missing or completely unusable parted binary. */
295 reply_with_error ("could not run 'parted' command");
300 if (err && strstr (err, "invalid option -- m"))
309 print_partition_table (const char *device, int parted_has_m_opt)
314 if (parted_has_m_opt)
315 r = command (&out, &err, "parted", "-m", "--", device,
319 r = command (&out, &err, "parted", "-s", "--", device,
323 reply_with_error ("parted print: %s: %s", device,
324 /* Hack for parted 1.x which sends errors to stdout. */
336 do_part_get_parttype (const char *device)
338 int parted_has_m_opt = test_parted_m_opt ();
339 if (parted_has_m_opt == -1)
342 char *out = print_partition_table (device, parted_has_m_opt);
346 if (parted_has_m_opt) {
347 /* New-style parsing using the "machine-readable" format from
350 char **lines = split_lines (out);
356 if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
357 reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
358 lines[0] ? lines[0] : "(signature was null)");
359 free_strings (lines);
363 if (lines[1] == NULL) {
364 reply_with_error ("parted didn't return a line describing the device");
365 free_strings (lines);
369 /* lines[1] is something like:
370 * "/dev/sda:1953525168s:scsi:512:512:msdos:ATA Hitachi HDT72101;"
372 char *r = get_table_field (lines[1], 5);
374 free_strings (lines);
378 free_strings (lines);
380 /* If "loop" return an error (RHBZ#634246). */
381 if (STREQ (r, "loop")) {
383 reply_with_error ("not a partitioned device");
390 /* Old-style. Look for "\nPartition Table: <str>\n". */
391 char *p = strstr (out, "\nPartition Table: ");
393 reply_with_error ("parted didn't return Partition Table line");
399 char *q = strchr (p, '\n');
401 reply_with_error ("parted Partition Table has no end of line char");
411 reply_with_perror ("strdup");
415 /* If "loop" return an error (RHBZ#634246). */
416 if (STREQ (p, "loop")) {
418 reply_with_error ("not a partitioned device");
422 return p; /* caller frees */
426 guestfs_int_partition_list *
427 do_part_list (const char *device)
429 int parted_has_m_opt = test_parted_m_opt ();
430 if (parted_has_m_opt == -1)
433 char *out = print_partition_table (device, parted_has_m_opt);
437 char **lines = split_lines (out);
443 guestfs_int_partition_list *r;
445 if (parted_has_m_opt) {
446 /* New-style parsing using the "machine-readable" format from
449 * lines[0] is "BYT;", lines[1] is the device line which we ignore,
450 * lines[2..] are the partitions themselves. Count how many.
452 size_t nr_rows = 0, row;
453 for (row = 2; lines[row] != NULL; ++row)
456 r = malloc (sizeof *r);
458 reply_with_perror ("malloc");
461 r->guestfs_int_partition_list_len = nr_rows;
462 r->guestfs_int_partition_list_val =
463 malloc (nr_rows * sizeof (guestfs_int_partition));
464 if (r->guestfs_int_partition_list_val == NULL) {
465 reply_with_perror ("malloc");
469 /* Now parse the lines. */
471 for (i = 0, row = 2; lines[row] != NULL; ++i, ++row) {
472 if (sscanf (lines[row], "%d:%" SCNi64 "B:%" SCNi64 "B:%" SCNi64 "B",
473 &r->guestfs_int_partition_list_val[i].part_num,
474 &r->guestfs_int_partition_list_val[i].part_start,
475 &r->guestfs_int_partition_list_val[i].part_end,
476 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
477 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
483 /* Old-style. Start at the line following "^Number", up to the
486 size_t start = 0, end = 0, row;
488 for (row = 0; lines[row] != NULL; ++row)
489 if (STRPREFIX (lines[row], "Number")) {
495 reply_with_error ("parted output has no \"Number\" line");
499 for (row = start; lines[row] != NULL; ++row)
500 if (STREQ (lines[row], "")) {
506 reply_with_error ("parted output has no blank after end of table");
510 size_t nr_rows = end - start;
512 r = malloc (sizeof *r);
514 reply_with_perror ("malloc");
517 r->guestfs_int_partition_list_len = nr_rows;
518 r->guestfs_int_partition_list_val =
519 malloc (nr_rows * sizeof (guestfs_int_partition));
520 if (r->guestfs_int_partition_list_val == NULL) {
521 reply_with_perror ("malloc");
525 /* Now parse the lines. */
527 for (i = 0, row = start; row < end; ++i, ++row) {
528 if (sscanf (lines[row], " %d %" SCNi64 "B %" SCNi64 "B %" SCNi64 "B",
529 &r->guestfs_int_partition_list_val[i].part_num,
530 &r->guestfs_int_partition_list_val[i].part_start,
531 &r->guestfs_int_partition_list_val[i].part_end,
532 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
533 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
539 free_strings (lines);
543 free (r->guestfs_int_partition_list_val);
547 free_strings (lines);
552 do_part_get_bootable (const char *device, int partnum)
555 reply_with_error ("partition number must be >= 1");
559 int parted_has_m_opt = test_parted_m_opt ();
560 if (parted_has_m_opt == -1)
563 char *out = print_partition_table (device, parted_has_m_opt);
567 char **lines = split_lines (out);
573 if (parted_has_m_opt) {
574 /* New-style parsing using the "machine-readable" format from
577 * Partitions may not be in any order, so we have to look for
578 * the matching partition number (RHBZ#602997).
580 if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
581 reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
582 lines[0] ? lines[0] : "(signature was null)");
583 free_strings (lines);
587 if (lines[1] == NULL) {
588 reply_with_error ("parted didn't return a line describing the device");
589 free_strings (lines);
595 for (row = 2; lines[row] != NULL; ++row) {
596 if (sscanf (lines[row], "%d:", &pnum) != 1) {
597 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
598 free_strings (lines);
605 if (lines[row] == NULL) {
606 reply_with_error ("partition number %d not found", partnum);
607 free_strings (lines);
611 char *boot = get_table_field (lines[row], 6);
613 free_strings (lines);
617 int r = STREQ (boot, "boot");
620 free_strings (lines);
625 /* Old-style: First look for the line matching "^Number". */
626 size_t start = 0, header, row;
628 for (row = 0; lines[row] != NULL; ++row)
629 if (STRPREFIX (lines[row], "Number")) {
636 reply_with_error ("parted output has no \"Number\" line");
637 free_strings (lines);
641 /* Now we have to look at the column number of the "Flags" field.
642 * This is because parted's output has no way to represent a
643 * missing field except as whitespace, so we cannot just count
644 * fields from the left. eg. The "File system" field is often
645 * missing in the output.
647 char *p = strstr (lines[header], "Flags");
649 reply_with_error ("parted output has no \"Flags\" field");
650 free_strings (lines);
653 size_t col = p - lines[header];
655 /* Look for the line corresponding to this partition number. */
656 row = start + partnum - 1;
657 if (row >= count_strings (lines) || !STRPREFIX (lines[row], " ")) {
658 reply_with_error ("partition number out of range: %d", partnum);
659 free_strings (lines);
663 int r = STRPREFIX (&lines[row][col], "boot");
664 free_strings (lines);
669 /* Currently we use sfdisk for getting and setting the ID byte. In
670 * future, extend parted to provide this functionality. As a result
671 * of using sfdisk, this won't work for non-MBR-style partitions, but
672 * that limitation is noted in the documentation and we can extend it
673 * later without breaking the ABI.
676 do_part_get_mbr_id (const char *device, int partnum)
679 reply_with_error ("partition number must be >= 1");
683 char partnum_str[16];
684 snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
689 r = command (&out, &err, "sfdisk", "--print-id", device, partnum_str, NULL);
691 reply_with_error ("sfdisk --print-id: %s", err);
699 /* It's printed in hex ... */
701 if (sscanf (out, "%x", &id) != 1) {
702 reply_with_error ("sfdisk --print-id: cannot parse output: %s", out);
712 do_part_set_mbr_id (const char *device, int partnum, int idbyte)
715 reply_with_error ("partition number must be >= 1");
719 char partnum_str[16];
720 snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
723 snprintf (idbyte_str, sizeof partnum_str, "%x", idbyte); /* NB: hex */
728 r = command (NULL, &err, "sfdisk",
729 "--change-id", device, partnum_str, idbyte_str, NULL);
731 reply_with_error ("sfdisk --change-id: %s", err);