Call blockdev --rereadpt after sfdisk commands.
[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 (const char *device, int n, int cyls, int heads, int sectors,
34         const char *extra_flag,
35         char *const *lines)
36 {
37   FILE *fp;
38   char buf[256];
39   int i;
40
41   strcpy (buf, "sfdisk");
42
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
52   /* The above are all guaranteed to fit in the fixed-size buffer.
53      However, extra_flag and device have no restrictions,
54      so we must check.  */
55
56   if (extra_flag) {
57     if (strlen (buf) + 1 + strlen (extra_flag) >= sizeof buf) {
58       reply_with_error ("internal buffer overflow: sfdisk extra_flag too long");
59       return -1;
60     }
61     sprintf (buf + strlen (buf), " %s", extra_flag);
62   }
63
64   if (strlen (buf) + 1 + strlen (device) >= sizeof buf) {
65     reply_with_error ("internal buffer overflow: sfdisk device name too long");
66     return -1;
67   }
68   sprintf (buf + strlen (buf), " %s", device);
69
70   if (verbose)
71     printf ("%s\n", buf);
72
73   fp = popen (buf, "w");
74   if (fp == NULL) {
75     reply_with_perror ("failed to open pipe: %s", buf);
76     return -1;
77   }
78
79   for (i = 0; lines[i] != NULL; ++i) {
80     if (fprintf (fp, "%s\n", lines[i]) < 0) {
81       reply_with_perror ("failed to write to pipe: %s", buf);
82       pclose (fp);
83       return -1;
84     }
85   }
86
87   if (pclose (fp) != 0) {
88     reply_with_error ("%s: external command failed", buf);
89     return -1;
90   }
91
92   udev_settle ();
93
94   /* sfdisk sometimes fails on fast machines with:
95    *
96    * Re-reading the partition table ...
97    * BLKRRPART: Device or resource busy
98    * The command to re-read the partition table failed.
99    * Run partprobe(8), kpartx(8) or reboot your system now,
100    * before using mkfs
101    *
102    * Unclear if this is a bug in sfdisk or the kernel or some
103    * other component.  In any case, reread the partition table
104    * unconditionally here.
105    */
106   return do_blockdev_rereadpt (device);
107 }
108
109 int
110 do_sfdisk (const char *device, int cyls, int heads, int sectors,
111            char *const *lines)
112 {
113   return sfdisk (device, 0, cyls, heads, sectors, NULL, lines);
114 }
115
116 int
117 do_sfdisk_N (const char *device, int n, int cyls, int heads, int sectors,
118              const char *line)
119 {
120   char const *const lines[2] = { line, NULL };
121
122   return sfdisk (device, n, cyls, heads, sectors, NULL, (void *) lines);
123 }
124
125 int
126 do_sfdiskM (const char *device, char *const *lines)
127 {
128   return sfdisk (device, 0, 0, 0, 0, "-uM", lines);
129 }
130
131 static char *
132 sfdisk_flag (const char *device, const char *flag)
133 {
134   char *out, *err;
135   int r;
136
137   r = command (&out, &err, "sfdisk", flag, device, NULL);
138   if (r == -1) {
139     reply_with_error ("%s: %s", device, err);
140     free (out);
141     free (err);
142     return NULL;
143   }
144
145   free (err);
146
147   udev_settle ();
148
149   return out;                   /* caller frees */
150 }
151
152 char *
153 do_sfdisk_l (const char *device)
154 {
155   return sfdisk_flag (device, "-l");
156 }
157
158 char *
159 do_sfdisk_kernel_geometry (const char *device)
160 {
161   return sfdisk_flag (device, "-g");
162 }
163
164 char *
165 do_sfdisk_disk_geometry (const char *device)
166 {
167   return sfdisk_flag (device, "-G");
168 }