Docs: Add "API Overview" section to guestfs(3) manpage.
[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         const char *extra_flag,
35         char * const* const lines)
36 {
37   FILE *fp;
38   char buf[256];
39   int i;
40
41   IS_DEVICE (device, -1);
42
43   strcpy (buf, "/sbin/sfdisk");
44
45   if (n > 0)
46     sprintf (buf + strlen (buf), " -N %d", n);
47   if (cyls)
48     sprintf (buf + strlen (buf), " -C %d", cyls);
49   if (heads)
50     sprintf (buf + strlen (buf), " -H %d", heads);
51   if (sectors)
52     sprintf (buf + strlen (buf), " -S %d", sectors);
53   if (extra_flag)
54     sprintf (buf + strlen (buf), " %s", extra_flag);
55
56   /* Safe because of IS_DEVICE above: */
57   sprintf (buf + strlen (buf), " %s", device);
58
59   if (verbose)
60     printf ("%s\n", buf);
61
62   fp = popen (buf, "w");
63   if (fp == NULL) {
64     reply_with_perror (buf);
65     return -1;
66   }
67
68   for (i = 0; lines[i] != NULL; ++i) {
69     if (fprintf (fp, "%s\n", lines[i]) < 0) {
70       reply_with_perror (buf);
71       pclose (fp);
72       return -1;
73     }
74   }
75
76   if (pclose (fp) != 0) {
77     reply_with_error ("%s: external command failed", buf);
78     return -1;
79   }
80
81   udev_settle ();
82
83   return 0;
84 }
85
86 int
87 do_sfdisk (char *device, int cyls, int heads, int sectors,
88            char **lines)
89 {
90   return sfdisk (device, 0, cyls, heads, sectors, NULL, lines);
91 }
92
93 int
94 do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors,
95              char *line)
96 {
97   const char *lines[2] = { line, NULL };
98
99   return sfdisk (device, n, cyls, heads, sectors, NULL, lines);
100 }
101
102 int
103 do_sfdiskM (char *device, char **lines)
104 {
105   return sfdisk (device, 0, 0, 0, 0, "-uM", lines);
106 }
107
108 static char *
109 sfdisk_flag (char *device, const char *flag)
110 {
111   char *out, *err;
112   int r;
113
114   IS_DEVICE (device, NULL);
115
116   r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
117   if (r == -1) {
118     reply_with_error ("sfdisk: %s: %s", device, err);
119     free (out);
120     free (err);
121     return NULL;
122   }
123
124   free (err);
125
126   udev_settle ();
127
128   return out;                   /* caller frees */
129 }
130
131 char *
132 do_sfdisk_l (char *device)
133 {
134   return sfdisk_flag (device, "-l");
135 }
136
137 char *
138 do_sfdisk_kernel_geometry (char *device)
139 {
140   return sfdisk_flag (device, "-g");
141 }
142
143 char *
144 do_sfdisk_disk_geometry (char *device)
145 {
146   return sfdisk_flag (device, "-G");
147 }