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