Call 'udevadm settle' after operations which add/remove device nodes.
[libguestfs.git] / daemon / sfdisk.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 <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 static int
33 sfdisk (char *device, int n, int cyls, int heads, int sectors,
34         char * const* const lines)
35 {
36   FILE *fp;
37   char buf[256];
38   int i;
39
40   IS_DEVICE (device, -1);
41
42   strcpy (buf, "/sbin/sfdisk");
43   if (n > 0)
44     sprintf (buf + strlen (buf), " -N %d", n);
45   if (cyls)
46     sprintf (buf + strlen (buf), " -C %d", cyls);
47   if (heads)
48     sprintf (buf + strlen (buf), " -H %d", heads);
49   if (sectors)
50     sprintf (buf + strlen (buf), " -S %d", sectors);
51   /* Safe because of IS_DEVICE above: */
52   sprintf (buf + strlen (buf), " %s", device);
53
54   if (verbose)
55     printf ("%s\n", buf);
56
57   fp = popen (buf, "w");
58   if (fp == NULL) {
59     reply_with_perror (buf);
60     return -1;
61   }
62
63   for (i = 0; lines[i] != NULL; ++i) {
64     if (fprintf (fp, "%s\n", lines[i]) < 0) {
65       reply_with_perror (buf);
66       pclose (fp);
67       return -1;
68     }
69   }
70
71   if (pclose (fp) != 0) {
72     reply_with_error ("%s: external command failed", buf);
73     return -1;
74   }
75
76   udev_settle ();
77
78   return 0;
79 }
80
81 int
82 do_sfdisk (char *device, int cyls, int heads, int sectors,
83            char **lines)
84 {
85   return sfdisk (device, 0, cyls, heads, sectors, lines);
86 }
87
88 int
89 do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors,
90              char *line)
91 {
92   const char *lines[2] = { line, NULL };
93
94   return sfdisk (device, n, cyls, heads, sectors, lines);
95 }
96
97 static char *
98 sfdisk_flag (char *device, const char *flag)
99 {
100   char *out, *err;
101   int r;
102
103   IS_DEVICE (device, NULL);
104
105   r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
106   if (r == -1) {
107     reply_with_error ("sfdisk: %s: %s", device, err);
108     free (out);
109     free (err);
110     return NULL;
111   }
112
113   free (err);
114
115   udev_settle ();
116
117   return out;                   /* caller frees */
118 }
119
120 char *
121 do_sfdisk_l (char *device)
122 {
123   return sfdisk_flag (device, "-l");
124 }
125
126 char *
127 do_sfdisk_kernel_geometry (char *device)
128 {
129   return sfdisk_flag (device, "-g");
130 }
131
132 char *
133 do_sfdisk_disk_geometry (char *device)
134 {
135   return sfdisk_flag (device, "-G");
136 }