change almost all uses: s/IS_DEVICE/RESOLVE_DEVICE/
[libguestfs.git] / daemon / ext2.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 <unistd.h>
24 #include <ctype.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 char **
31 do_tune2fs_l (char *device)
32 {
33   int r;
34   char *out, *err;
35   char *p, *pend, *colon;
36   char **ret = NULL;
37   int size = 0, alloc = 0;
38
39   RESOLVE_DEVICE (device, return NULL);
40
41   r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
42   if (r == -1) {
43     reply_with_error ("tune2fs: %s", err);
44     free (err);
45     free (out);
46     return NULL;
47   }
48   free (err);
49
50   p = out;
51
52   /* Discard the first line if it contains "tune2fs ...". */
53   if (strncmp (p, "tune2fs ", 8) == 0) {
54     p = strchr (p, '\n');
55     if (p) p++;
56     else {
57       reply_with_error ("tune2fs: truncated output");
58       free (out);
59       return NULL;
60     }
61   }
62
63   /* Read the lines and split into "key: value". */
64   while (*p) {
65     pend = strchrnul (p, '\n');
66     if (*pend == '\n') {
67       *pend = '\0';
68       pend++;
69     }
70
71     if (!*p) continue;
72
73     colon = strchr (p, ':');
74     if (colon) {
75       *colon = '\0';
76
77       do { colon++; } while (*colon && isspace (*colon));
78
79       if (add_string (&ret, &size, &alloc, p) == -1) {
80         free (out);
81         return NULL;
82       }
83       if (strcmp (colon, "<none>") == 0 ||
84           strcmp (colon, "<not available>") == 0 ||
85           strcmp (colon, "(none)") == 0) {
86         if (add_string (&ret, &size, &alloc, "") == -1) {
87           free (out);
88           return NULL;
89         }
90       } else {
91         if (add_string (&ret, &size, &alloc, colon) == -1) {
92           free (out);
93           return NULL;
94         }
95       }
96     }
97     else {
98       if (add_string (&ret, &size, &alloc, p) == -1) {
99         free (out);
100         return NULL;
101       }
102       if (add_string (&ret, &size, &alloc, "") == -1) {
103         free (out);
104         return NULL;
105       }
106     }
107
108     p = pend;
109   }
110
111   free (out);
112
113   if (add_string (&ret, &size, &alloc, NULL) == -1)
114     return NULL;
115
116   return ret;
117 }
118
119 int
120 do_set_e2label (char *device, char *label)
121 {
122   int r;
123   char *err;
124
125   RESOLVE_DEVICE (device, return -1);
126
127   r = command (NULL, &err, "/sbin/e2label", device, label, NULL);
128   if (r == -1) {
129     reply_with_error ("e2label: %s", err);
130     free (err);
131     return -1;
132   }
133
134   free (err);
135   return 0;
136 }
137
138 char *
139 do_get_e2label (char *device)
140 {
141   int r, len;
142   char *out, *err;
143
144   RESOLVE_DEVICE (device, return NULL);
145
146   r = command (&out, &err, "/sbin/e2label", device, NULL);
147   if (r == -1) {
148     reply_with_error ("e2label: %s", err);
149     free (out);
150     free (err);
151     return NULL;
152   }
153
154   free (err);
155
156   /* Remove any trailing \n from the label. */
157   len = strlen (out);
158   if (len > 0 && out[len-1] == '\n')
159     out[len-1] = '\0';
160
161   return out;                   /* caller frees */
162 }
163
164 int
165 do_set_e2uuid (char *device, char *uuid)
166 {
167   int r;
168   char *err;
169
170   RESOLVE_DEVICE (device, return -1);
171
172   r = command (NULL, &err, "/sbin/tune2fs", "-U", uuid, device, NULL);
173   if (r == -1) {
174     reply_with_error ("tune2fs -U: %s", err);
175     free (err);
176     return -1;
177   }
178
179   free (err);
180   return 0;
181 }
182
183 char *
184 do_get_e2uuid (char *device)
185 {
186   int r;
187   char *out, *err, *p, *q;
188
189   RESOLVE_DEVICE (device, return NULL);
190
191   /* It's not so straightforward to get the volume UUID.  We have
192    * to use tune2fs -l and then look for a particular string in
193    * the output.
194    */
195
196   r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
197   if (r == -1) {
198     reply_with_error ("tune2fs -l: %s", err);
199     free (out);
200     free (err);
201     return NULL;
202   }
203
204   free (err);
205
206   /* Look for /\nFilesystem UUID:\s+/ in the output. */
207   p = strstr (out, "\nFilesystem UUID:");
208   if (p == NULL) {
209     reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
210     free (out);
211     return NULL;
212   }
213
214   p += 17;
215   while (*p && isspace (*p))
216     p++;
217   if (!*p) {
218     reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
219     free (out);
220     return NULL;
221   }
222
223   /* Now 'p' hopefully points to the start of the UUID. */
224   q = p;
225   while (*q && (isxdigit (*q) || *q == '-'))
226     q++;
227   if (!*q) {
228     reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
229     free (out);
230     return NULL;
231   }
232
233   *q = '\0';
234
235   p = strdup (p);
236   if (!p) {
237     reply_with_perror ("strdup");
238     free (out);
239     return NULL;
240   }
241
242   free (out);
243   return p;                     /* caller frees */
244 }
245
246 int
247 do_resize2fs (char *device)
248 {
249   char *err;
250   int r;
251
252   RESOLVE_DEVICE (device, return -1);
253
254   r = command (NULL, &err, "/sbin/resize2fs", device, NULL);
255   if (r == -1) {
256     reply_with_error ("resize2fs: %s", err);
257     free (err);
258     return -1;
259   }
260
261   free (err);
262   return 0;
263 }
264
265 int
266 do_e2fsck_f (char *device)
267 {
268   char *err;
269   int r;
270
271   RESOLVE_DEVICE (device, return -1);
272
273   r = command (NULL, &err, "/sbin/e2fsck", "-p", "-f", device, NULL);
274   if (r == -1) {
275     reply_with_error ("e2fsck: %s", err);
276     free (err);
277     return -1;
278   }
279
280   free (err);
281   return 0;
282 }