1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 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_disk (const char *device, const char *parttype)
164 const char *startstr;
167 parttype = check_parttype (parttype);
169 reply_with_error ("unknown partition type: common choices are \"gpt\" and \"msdos\"");
173 /* Voooooodooooooooo (thanks Jim Meyering for working this out). */
174 if (STREQ (parttype, "msdos")) {
177 } else if (STREQ (parttype, "gpt")) {
186 RUN_PARTED (return -1,
189 /* See comment about about the parted mkpart command. */
190 "mkpart", STREQ (parttype, "gpt") ? "p1" : "primary",
191 startstr, endstr, NULL);
199 do_part_set_bootable (const char *device, int partnum, int bootable)
203 snprintf (partstr, sizeof partstr, "%d", partnum);
205 RUN_PARTED (return -1,
206 device, "set", partstr, "boot", bootable ? "on" : "off", NULL);
214 do_part_set_name (const char *device, int partnum, const char *name)
218 snprintf (partstr, sizeof partstr, "%d", partnum);
220 RUN_PARTED (return -1, device, "name", partstr, name, NULL);
227 /* Return the nth field from a string of ':'/';'-delimited strings.
228 * Useful for parsing the return value from print_partition_table
232 get_table_field (const char *line, int n)
234 const char *p = line;
236 while (*p && n > 0) {
237 p += strcspn (p, ":;") + 1;
242 reply_with_error ("not enough fields in output of parted print command: %s",
247 size_t len = strcspn (p, ":;");
248 char *q = strndup (p, len);
250 reply_with_perror ("strndup");
258 print_partition_table (const char *device)
264 r = command (&out, &err, "parted", "-m", "--", device,
268 reply_with_error ("parted print: %s: %s", device,
269 /* Hack for parted 1.x which sends errors to stdout. */
277 lines = split_lines (out);
283 if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
284 reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
285 lines[0] ? lines[0] : "(signature was null)");
286 free_strings (lines);
290 if (lines[1] == NULL) {
291 reply_with_error ("parted didn't return a line describing the device");
292 free_strings (lines);
300 do_part_get_parttype (const char *device)
302 char **lines = print_partition_table (device);
306 /* lines[1] is something like:
307 * "/dev/sda:1953525168s:scsi:512:512:msdos:ATA Hitachi HDT72101;"
309 char *r = get_table_field (lines[1], 5);
311 free_strings (lines);
315 free_strings (lines);
320 guestfs_int_partition_list *
321 do_part_list (const char *device)
324 size_t row, nr_rows, i;
325 guestfs_int_partition_list *r;
327 lines = print_partition_table (device);
331 /* lines[0] is "BYT;", lines[1] is the device line which we ignore,
332 * lines[2..] are the partitions themselves. Count how many.
335 for (row = 2; lines[row] != NULL; ++row)
338 r = malloc (sizeof *r);
340 reply_with_perror ("malloc");
343 r->guestfs_int_partition_list_len = nr_rows;
344 r->guestfs_int_partition_list_val =
345 malloc (nr_rows * sizeof (guestfs_int_partition));
346 if (r->guestfs_int_partition_list_val == NULL) {
347 reply_with_perror ("malloc");
351 /* Now parse the lines. */
352 for (i = 0, row = 2; lines[row] != NULL; ++i, ++row) {
353 if (sscanf (lines[row], "%d:%" SCNi64 "B:%" SCNi64 "B:%" SCNi64 "B",
354 &r->guestfs_int_partition_list_val[i].part_num,
355 &r->guestfs_int_partition_list_val[i].part_start,
356 &r->guestfs_int_partition_list_val[i].part_end,
357 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
358 reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
363 free_strings (lines);
367 free (r->guestfs_int_partition_list_val);
371 free_strings (lines);