Use a squashfs attached as /dev/sdd during the C API tests.
[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");
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       fclose (fp);
67       return -1;
68     }
69   }
70
71   if (fclose (fp) == EOF) {
72     reply_with_perror (buf);
73     fclose (fp);
74     return -1;
75   }
76
77   return 0;
78 }
79
80 int
81 do_sfdisk (const char *device, int cyls, int heads, int sectors,
82            char * const* const lines)
83 {
84   return sfdisk (device, 0, cyls, heads, sectors, lines);
85 }
86
87 int
88 do_sfdisk_N (const char *device, int n, int cyls, int heads, int sectors,
89              const char *line)
90 {
91   const char *lines[2] = { line, NULL };
92
93   return sfdisk (device, n, cyls, heads, sectors, lines);
94 }
95
96 static char *
97 sfdisk_flag (const char *device, const char *flag)
98 {
99   char *out, *err;
100   int r;
101
102   IS_DEVICE (device, NULL);
103
104   r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
105   if (r == -1) {
106     reply_with_error ("sfdisk: %s: %s", device, err);
107     free (out);
108     free (err);
109     return NULL;
110   }
111
112   free (err);
113
114   return out;                   /* caller frees */
115 }
116
117 char *
118 do_sfdisk_l (const char *device)
119 {
120   return sfdisk_flag (device, "-l");
121 }
122
123 char *
124 do_sfdisk_kernel_geometry (const char *device)
125 {
126   return sfdisk_flag (device, "-g");
127 }
128
129 char *
130 do_sfdisk_disk_geometry (const char *device)
131 {
132   return sfdisk_flag (device, "-G");
133 }