9d7a220f8a15802350295e0a511d74de57cb241d
[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         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 --no-reread");
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   fp = popen (buf, "w");
55   if (fp == NULL) {
56     reply_with_perror (buf);
57     return -1;
58   }
59
60   for (i = 0; lines[i] != NULL; ++i) {
61     if (fprintf (fp, "%s\n", lines[i]) < 0) {
62       reply_with_perror (buf);
63       fclose (fp);
64       return -1;
65     }
66   }
67
68   if (fclose (fp) == EOF) {
69     reply_with_perror (buf);
70     fclose (fp);
71     return -1;
72   }
73
74   return 0;
75 }
76
77 int
78 do_sfdisk (const char *device, int cyls, int heads, int sectors,
79            char * const* const lines)
80 {
81   return sfdisk (device, 0, cyls, heads, sectors, lines);
82 }
83
84 int
85 do_sfdisk_N (const char *device, int n, int cyls, int heads, int sectors,
86              const char *line)
87 {
88   const char *lines[2] = { line, NULL };
89
90   return sfdisk (device, n, cyls, heads, sectors, lines);
91 }
92
93 static char *
94 sfdisk_flag (const char *device, const char *flag)
95 {
96   char *out, *err;
97   int r;
98
99   IS_DEVICE (device, NULL);
100
101   r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
102   if (r == -1) {
103     reply_with_error ("sfdisk: %s: %s", device, err);
104     free (out);
105     free (err);
106     return NULL;
107   }
108
109   free (err);
110
111   return out;                   /* caller frees */
112 }
113
114 char *
115 do_sfdisk_l (const char *device)
116 {
117   return sfdisk_flag (device, "-l");
118 }
119
120 char *
121 do_sfdisk_kernel_geometry (const char *device)
122 {
123   return sfdisk_flag (device, "-g");
124 }
125
126 char *
127 do_sfdisk_disk_geometry (const char *device)
128 {
129   return sfdisk_flag (device, "-G");
130 }