e0183fb4dcd47be14f4a737129a396342c1e1fad
[libguestfs.git] / daemon / parted.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
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  * 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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "daemon.h"
29 #include "actions.h"
30
31 /* Notes:
32  *
33  * Parted 1.9 sends error messages to stdout, hence use of the
34  * COMMAND_FLAG_FOLD_STDOUT_ON_STDERR flag.
35  *
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.
39  */
40 static int
41 recover_blkrrpart (const char *device, const char *err)
42 {
43   int r;
44
45   if (!strstr (err,
46                "Error informing the kernel about modifications to partition"))
47     return -1;
48
49   r = command (NULL, NULL, "/sbin/blockdev", "--rereadpt", device, NULL);
50   if (r == -1)
51     return -1;
52
53   udev_settle ();
54
55   return 0;
56 }
57
58 #define RUN_PARTED(device,...)                                          \
59   do {                                                                  \
60     int r;                                                              \
61     char *err;                                                          \
62                                                                         \
63     r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,       \
64                   "/sbin/parted", "-s", "--", (device), __VA_ARGS__);   \
65     if (r == -1) {                                                      \
66       if (recover_blkrrpart ((device), err) == -1) {                    \
67         reply_with_error ("%s: parted: %s: %s", __func__, (device), err); \
68         free (err);                                                     \
69         return -1;                                                      \
70       }                                                                 \
71     }                                                                   \
72                                                                         \
73     free (err);                                                         \
74   } while (0)
75
76 static const char *
77 check_parttype (const char *parttype)
78 {
79   /* Check and translate parttype. */
80   if (strcmp (parttype, "aix") == 0 ||
81       strcmp (parttype, "amiga") == 0 ||
82       strcmp (parttype, "bsd") == 0 ||
83       strcmp (parttype, "dasd") == 0 ||
84       strcmp (parttype, "dvh") == 0 ||
85       strcmp (parttype, "gpt") == 0 ||
86       strcmp (parttype, "mac") == 0 ||
87       strcmp (parttype, "msdos") == 0 ||
88       strcmp (parttype, "pc98") == 0 ||
89       strcmp (parttype, "sun") == 0)
90     return parttype;
91   else if (strcmp (parttype, "rdb") == 0)
92     return "amiga";
93   else if (strcmp (parttype, "efi") == 0)
94     return "gpt";
95   else if (strcmp (parttype, "mbr") == 0)
96     return "msdos";
97   else
98     return NULL;
99 }
100
101 int
102 do_part_init (const char *device, const char *parttype)
103 {
104   parttype = check_parttype (parttype);
105   if (!parttype) {
106     reply_with_error ("part-init: unknown partition type: common choices are \"gpt\" and \"msdos\"");
107     return -1;
108   }
109
110   RUN_PARTED (device, "mklabel", parttype, NULL);
111
112   udev_settle ();
113
114   return 0;
115 }
116
117 int
118 do_part_add (const char *device, const char *prlogex,
119              int64_t startsect, int64_t endsect)
120 {
121   char startstr[32];
122   char endstr[32];
123
124   /* Check and translate prlogex. */
125   if (strcmp (prlogex, "primary") == 0 ||
126       strcmp (prlogex, "logical") == 0 ||
127       strcmp (prlogex, "extended") == 0)
128     ;
129   else if (strcmp (prlogex, "p") == 0)
130     prlogex = "primary";
131   else if (strcmp (prlogex, "l") == 0)
132     prlogex = "logical";
133   else if (strcmp (prlogex, "e") == 0)
134     prlogex = "extended";
135   else {
136     reply_with_error ("part-add: unknown partition type: %s: this should be \"primary\", \"logical\" or \"extended\"", prlogex);
137     return -1;
138   }
139
140   if (startsect < 0) {
141     reply_with_error ("part-add: startsect cannot be negative");
142     return -1;
143   }
144   /* but endsect can be negative */
145
146   snprintf (startstr, sizeof startstr, "%" PRIi64 "s", startsect);
147   snprintf (endstr, sizeof endstr, "%" PRIi64 "s", endsect);
148
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.
153    */
154   RUN_PARTED (device, "mkpart", prlogex, startstr, endstr, NULL);
155
156   udev_settle ();
157
158   return 0;
159 }
160
161 int
162 do_part_disk (const char *device, const char *parttype)
163 {
164   const char *startstr;
165   const char *endstr;
166
167   parttype = check_parttype (parttype);
168   if (!parttype) {
169     reply_with_error ("part-disk: unknown partition type: common choices are \"gpt\" and \"msdos\"");
170     return -1;
171   }
172
173   /* Voooooodooooooooo (thanks Jim Meyering for working this out). */
174   if (strcmp (parttype, "msdos") == 0) {
175     startstr = "1s";
176     endstr = "-1s";
177   } else if (strcmp (parttype, "gpt") == 0) {
178     startstr = "34s";
179     endstr = "-34s";
180   } else {
181     /* untested */
182     startstr = "1s";
183     endstr = "-1s";
184   }
185
186   RUN_PARTED (device,
187               "mklabel", parttype,
188               /* See comment about about the parted mkpart command. */
189               "mkpart", strcmp (parttype, "gpt") == 0 ? "p1" : "primary",
190               startstr, endstr, NULL);
191
192   udev_settle ();
193
194   return 0;
195 }
196
197 int
198 do_part_set_bootable (const char *device, int partnum, int bootable)
199 {
200   char partstr[16];
201
202   snprintf (partstr, sizeof partstr, "%d", partnum);
203
204   RUN_PARTED (device, "set", partstr, "boot", bootable ? "on" : "off", NULL);
205
206   udev_settle ();
207
208   return 0;
209 }
210
211 int
212 do_part_set_name (const char *device, int partnum, const char *name)
213 {
214   char partstr[16];
215
216   snprintf (partstr, sizeof partstr, "%d", partnum);
217
218   RUN_PARTED (device, "name", partstr, name, NULL);
219
220   udev_settle ();
221
222   return 0;
223 }
224
225 static char **
226 print_partition_table (const char *device)
227 {
228   char *out, *err;
229   int r;
230   char **lines;
231
232   r = command (&out, &err, "/sbin/parted", "-m", "--", device,
233                "unit", "b",
234                "print", NULL);
235   if (r == -1) {
236     reply_with_error ("parted print: %s: %s", device,
237                       /* Hack for parted 1.x which sends errors to stdout. */
238                       *err ? err : out);
239     free (out);
240     free (err);
241     return NULL;
242   }
243   free (err);
244
245   lines = split_lines (out);
246   free (out);
247
248   if (!lines)
249     return NULL;
250
251   if (lines[0] == NULL || strcmp (lines[0], "BYT;") != 0) {
252     reply_with_error ("parted print: unknown signature, expected \"BYT;\" as first line of the output: %s",
253                       lines[0] ? lines[0] : "(signature was null)");
254     free_strings (lines);
255     return NULL;
256   }
257
258   if (lines[1] == NULL) {
259     reply_with_error ("parted print: parted didn't return a line describing the device");
260     free_strings (lines);
261     return NULL;
262   }
263
264   return lines;
265 }
266
267 char *
268 do_part_get_parttype (const char *device)
269 {
270   char **lines;
271   char *r;
272
273   lines = print_partition_table (device);
274   if (!lines)
275     return NULL;
276
277   /* lines[1] is something like:
278    * "/dev/sda:1953525168s:scsi:512:512:msdos:ATA Hitachi HDT72101;"
279    */
280   if (strtok (lines[1], ":") == NULL /* device */
281       || strtok (NULL, ":") == NULL  /* size */
282       || strtok (NULL, ":") == NULL  /* transport */
283       || strtok (NULL, ":") == NULL  /* sector size */
284       || strtok (NULL, ":") == NULL  /* physical sector size */
285       || (r = strtok (NULL, ":")) == NULL /* return value */
286       ) {
287     reply_with_error ("part_get_parttype: too few fields in output from parted print command: %s", lines[1]);
288     free_strings (lines);
289     return NULL;
290   }
291
292   r = strdup (r);
293   if (!r) {
294     reply_with_perror ("strdup");
295     free_strings (lines);
296     return NULL;
297   }
298
299   free_strings (lines);
300
301   return r;
302 }
303
304 guestfs_int_partition_list *
305 do_part_list (const char *device)
306 {
307   char **lines;
308   size_t row, nr_rows, i;
309   guestfs_int_partition_list *r;
310
311   lines = print_partition_table (device);
312   if (!lines)
313     return NULL;
314
315   /* lines[0] is "BYT;", lines[1] is the device line which we ignore,
316    * lines[2..] are the partitions themselves.  Count how many.
317    */
318   nr_rows = 0;
319   for (row = 2; lines[row] != NULL; ++row)
320     ++nr_rows;
321
322   r = malloc (sizeof *r);
323   if (r == NULL) {
324     reply_with_perror ("malloc");
325     goto error1;
326   }
327   r->guestfs_int_partition_list_len = nr_rows;
328   r->guestfs_int_partition_list_val =
329     malloc (nr_rows * sizeof (guestfs_int_partition));
330   if (r->guestfs_int_partition_list_val == NULL) {
331     reply_with_perror ("malloc");
332     goto error2;
333   }
334
335   /* Now parse the lines. */
336   for (i = 0, row = 2; lines[row] != NULL; ++i, ++row) {
337     if (sscanf (lines[row], "%d:%" SCNi64 "B:%" SCNi64 "B:%" SCNi64 "B",
338                 &r->guestfs_int_partition_list_val[i].part_num,
339                 &r->guestfs_int_partition_list_val[i].part_start,
340                 &r->guestfs_int_partition_list_val[i].part_end,
341                 &r->guestfs_int_partition_list_val[i].part_size) != 4) {
342       reply_with_error ("part_list: could not parse row from output of parted print command: %s", lines[row]);
343       goto error3;
344     }
345   }
346
347   free_strings (lines);
348   return r;
349
350  error3:
351   free (r->guestfs_int_partition_list_val);
352  error2:
353   free (r);
354  error1:
355   free_strings (lines);
356   return NULL;
357 }