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., 675 Mass Ave, Cambridge, MA 02139, 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 64 sectors, and leave
188 * the last 64 sectors at the end of the disk free. This wastes
189 * 32K+32K = 64K on 512-byte sector disks. The rationale is:
191 * - aligned operations are faster
192 * - GPT requires at least 34 sectors at the end of the disk.
194 const char *startstr = "64s";
195 const char *endstr = "-64s";
197 RUN_PARTED (return -1,
200 /* See comment about about the parted mkpart command. */
201 "mkpart", STREQ (parttype, "gpt") ? "p1" : "primary",
202 startstr, endstr, NULL);
210 do_part_set_bootable (const char *device, int partnum, int bootable)
213 reply_with_error ("partition number must be >= 1");
219 snprintf (partstr, sizeof partstr, "%d", partnum);
221 RUN_PARTED (return -1,
222 device, "set", partstr, "boot", bootable ? "on" : "off", NULL);
230 do_part_set_name (const char *device, int partnum, const char *name)
233 reply_with_error ("partition number must be >= 1");
239 snprintf (partstr, sizeof partstr, "%d", partnum);
241 RUN_PARTED (return -1, device, "name", partstr, name, NULL);
248 /* Return the nth field from a string of ':'/';'-delimited strings.
249 * Useful for parsing the return value from 'parted -m'.
252 get_table_field (const char *line, int n)
254 const char *p = line;
256 while (*p && n > 0) {
257 p += strcspn (p, ":;") + 1;
262 reply_with_error ("not enough fields in output of parted print command: %s",
267 size_t len = strcspn (p, ":;");
268 char *q = strndup (p, len);
270 reply_with_perror ("strndup");
277 /* RHEL 5 parted doesn't have the -m (machine readable) option so we
278 * must do a lot more work to parse the output in
279 * print_partition_table below. Test for this option the first time
280 * this function is called.
283 test_parted_m_opt (void)
285 static int result = -1;
291 int r = commandr (NULL, &err, "parted", "-s", "-m", "/dev/null", NULL);
293 /* Test failed, eg. missing or completely unusable parted binary. */
294 reply_with_error ("could not run 'parted' command");
299 if (err && strstr (err, "invalid option -- m"))
308 print_partition_table (const char *device, int parted_has_m_opt)
313 if (parted_has_m_opt)
314 r = command (&out, &err, "parted", "-m", "--", device,
318 r = command (&out, &err, "parted", "-s", "--", device,
322 reply_with_error ("parted print: %s: %s", device,
323 /* Hack for parted 1.x which sends errors to stdout. */
335 do_part_get_parttype (const char *device)
337 int parted_has_m_opt = test_parted_m_opt ();
338 if (parted_has_m_opt == -1)
341 char *out = print_partition_table (device, parted_has_m_opt);
345 if (parted_has_m_opt) {
346 /* New-style parsing using the "machine-readable" format from
349 char **lines = split_lines (out);
355 if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
356 reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
357 lines[0] ? lines[0] : "(signature was null)");
358 free_strings (lines);
362 if (lines[1] == NULL) {
363 reply_with_error ("parted didn't return a line describing the device");
364 free_strings (lines);
368 /* lines[1] is something like:
369 * "/dev/sda:1953525168s:scsi:512:512:msdos:ATA Hitachi HDT72101;"
371 char *r = get_table_field (lines[1], 5);
373 free_strings (lines);
377 free_strings (lines);
379 /* If "loop" return an error (RHBZ#634246). */
380 if (STREQ (r, "loop")) {
382 reply_with_error ("not a partitioned device");
389 /* Old-style. Look for "\nPartition Table: <str>\n". */
390 char *p = strstr (out, "\nPartition Table: ");
392 reply_with_error ("parted didn't return Partition Table line");
398 char *q = strchr (p, '\n');
400 reply_with_error ("parted Partition Table has no end of line char");
410 reply_with_perror ("strdup");
414 /* If "loop" return an error (RHBZ#634246). */
415 if (STREQ (p, "loop")) {
417 reply_with_error ("not a partitioned device");
421 return p; /* caller frees */
425 guestfs_int_partition_list *
426 do_part_list (const char *device)
428 int parted_has_m_opt = test_parted_m_opt ();
429 if (parted_has_m_opt == -1)
432 char *out = print_partition_table (device, parted_has_m_opt);
436 char **lines = split_lines (out);
442 guestfs_int_partition_list *r;
444 if (parted_has_m_opt) {
445 /* New-style parsing using the "machine-readable" format from
448 * lines[0] is "BYT;", lines[1] is the device line which we ignore,
449 * lines[2..] are the partitions themselves. Count how many.
451 size_t nr_rows = 0, row;
452 for (row = 2; lines[row] != NULL; ++row)
455 r = malloc (sizeof *r);
457 reply_with_perror ("malloc");
460 r->guestfs_int_partition_list_len = nr_rows;
461 r->guestfs_int_partition_list_val =
462 malloc (nr_rows * sizeof (guestfs_int_partition));
463 if (r->guestfs_int_partition_list_val == NULL) {
464 reply_with_perror ("malloc");
468 /* Now parse the lines. */
470 for (i = 0, row = 2; lines[row] != NULL; ++i, ++row) {
471 if (sscanf (lines[row], "%d:%" SCNi64 "B:%" SCNi64 "B:%" SCNi64 "B",
472 &r->guestfs_int_partition_list_val[i].part_num,
473 &r->guestfs_int_partition_list_val[i].part_start,
474 &r->guestfs_int_partition_list_val[i].part_end,
475 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
476 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
482 /* Old-style. Start at the line following "^Number", up to the
485 size_t start = 0, end = 0, row;
487 for (row = 0; lines[row] != NULL; ++row)
488 if (STRPREFIX (lines[row], "Number")) {
494 reply_with_error ("parted output has no \"Number\" line");
498 for (row = start; lines[row] != NULL; ++row)
499 if (STREQ (lines[row], "")) {
505 reply_with_error ("parted output has no blank after end of table");
509 size_t nr_rows = end - start;
511 r = malloc (sizeof *r);
513 reply_with_perror ("malloc");
516 r->guestfs_int_partition_list_len = nr_rows;
517 r->guestfs_int_partition_list_val =
518 malloc (nr_rows * sizeof (guestfs_int_partition));
519 if (r->guestfs_int_partition_list_val == NULL) {
520 reply_with_perror ("malloc");
524 /* Now parse the lines. */
526 for (i = 0, row = start; row < end; ++i, ++row) {
527 if (sscanf (lines[row], " %d %" SCNi64 "B %" SCNi64 "B %" SCNi64 "B",
528 &r->guestfs_int_partition_list_val[i].part_num,
529 &r->guestfs_int_partition_list_val[i].part_start,
530 &r->guestfs_int_partition_list_val[i].part_end,
531 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
532 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
538 free_strings (lines);
542 free (r->guestfs_int_partition_list_val);
546 free_strings (lines);
551 do_part_get_bootable (const char *device, int partnum)
554 reply_with_error ("partition number must be >= 1");
558 int parted_has_m_opt = test_parted_m_opt ();
559 if (parted_has_m_opt == -1)
562 char *out = print_partition_table (device, parted_has_m_opt);
566 char **lines = split_lines (out);
572 if (parted_has_m_opt) {
573 /* New-style parsing using the "machine-readable" format from
576 * Partitions may not be in any order, so we have to look for
577 * the matching partition number (RHBZ#602997).
579 if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
580 reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
581 lines[0] ? lines[0] : "(signature was null)");
582 free_strings (lines);
586 if (lines[1] == NULL) {
587 reply_with_error ("parted didn't return a line describing the device");
588 free_strings (lines);
594 for (row = 2; lines[row] != NULL; ++row) {
595 if (sscanf (lines[row], "%d:", &pnum) != 1) {
596 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
597 free_strings (lines);
604 if (lines[row] == NULL) {
605 reply_with_error ("partition number %d not found", partnum);
606 free_strings (lines);
610 char *boot = get_table_field (lines[row], 6);
612 free_strings (lines);
616 int r = STREQ (boot, "boot");
619 free_strings (lines);
624 /* Old-style: First look for the line matching "^Number". */
625 size_t start = 0, header, row;
627 for (row = 0; lines[row] != NULL; ++row)
628 if (STRPREFIX (lines[row], "Number")) {
635 reply_with_error ("parted output has no \"Number\" line");
636 free_strings (lines);
640 /* Now we have to look at the column number of the "Flags" field.
641 * This is because parted's output has no way to represent a
642 * missing field except as whitespace, so we cannot just count
643 * fields from the left. eg. The "File system" field is often
644 * missing in the output.
646 char *p = strstr (lines[header], "Flags");
648 reply_with_error ("parted output has no \"Flags\" field");
649 free_strings (lines);
652 size_t col = p - lines[header];
654 /* Look for the line corresponding to this partition number. */
655 row = start + partnum - 1;
656 if (row >= count_strings (lines) || !STRPREFIX (lines[row], " ")) {
657 reply_with_error ("partition number out of range: %d", partnum);
658 free_strings (lines);
662 int r = STRPREFIX (&lines[row][col], "boot");
663 free_strings (lines);
668 /* Currently we use sfdisk for getting and setting the ID byte. In
669 * future, extend parted to provide this functionality. As a result
670 * of using sfdisk, this won't work for non-MBR-style partitions, but
671 * that limitation is noted in the documentation and we can extend it
672 * later without breaking the ABI.
675 do_part_get_mbr_id (const char *device, int partnum)
678 reply_with_error ("partition number must be >= 1");
682 char partnum_str[16];
683 snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
688 r = command (&out, &err, "sfdisk", "--print-id", device, partnum_str, NULL);
690 reply_with_error ("sfdisk --print-id: %s", err);
698 /* It's printed in hex ... */
700 if (sscanf (out, "%x", &id) != 1) {
701 reply_with_error ("sfdisk --print-id: cannot parse output: %s", out);
711 do_part_set_mbr_id (const char *device, int partnum, int idbyte)
714 reply_with_error ("partition number must be >= 1");
718 char partnum_str[16];
719 snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
722 snprintf (idbyte_str, sizeof partnum_str, "%x", idbyte); /* NB: hex */
727 r = command (NULL, &err, "sfdisk",
728 "--change-id", device, partnum_str, idbyte_str, NULL);
730 reply_with_error ("sfdisk --change-id: %s", err);