base64-in: Ignore garbage characters in input.
[libguestfs.git] / daemon / mount.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 <sys/stat.h>
26 #include <sys/types.h>
27
28 #include "daemon.h"
29 #include "actions.h"
30
31 /* You must mount something on "/" first, hence: */
32 int root_mounted = 0;
33
34 /* The "simple mount" call offers no complex options, you can just
35  * mount a device on a mountpoint.  The variations like mount_ro,
36  * mount_options and mount_vfs let you set progressively more things.
37  *
38  * It's tempting to try a direct mount(2) syscall, but that doesn't
39  * do any autodetection, so we are better off calling out to
40  * /bin/mount.
41  */
42
43 int
44 do_mount_vfs (const char *options, const char *vfstype,
45               const char *device, const char *mountpoint)
46 {
47   int r, is_root;
48   char *mp;
49   char *error;
50
51   ABS_PATH (mountpoint, 0, return -1);
52
53   is_root = STREQ (mountpoint, "/");
54
55   if (!root_mounted && !is_root) {
56     reply_with_error ("you must mount something on / first");
57     return -1;
58   }
59
60   mp = sysroot_path (mountpoint);
61   if (!mp) {
62     reply_with_perror ("malloc");
63     return -1;
64   }
65
66   if (vfstype)
67     r = command (NULL, &error,
68                  "mount", "-o", options, "-t", vfstype, device, mp, NULL);
69   else
70     r = command (NULL, &error,
71                  "mount", "-o", options, device, mp, NULL);
72   free (mp);
73   if (r == -1) {
74     reply_with_error ("%s on %s: %s", device, mountpoint, error);
75     free (error);
76     return -1;
77   }
78
79   if (is_root)
80     root_mounted = 1;
81
82   return 0;
83 }
84
85 int
86 do_mount (const char *device, const char *mountpoint)
87 {
88   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
89 }
90
91 int
92 do_mount_ro (const char *device, const char *mountpoint)
93 {
94   return do_mount_vfs ("ro", NULL, device, mountpoint);
95 }
96
97 int
98 do_mount_options (const char *options, const char *device,
99                   const char *mountpoint)
100 {
101   return do_mount_vfs (options, NULL, device, mountpoint);
102 }
103
104 /* Again, use the external /bin/umount program, so that /etc/mtab
105  * is kept updated.
106  */
107 int
108 do_umount (const char *pathordevice)
109 {
110   int r;
111   char *err;
112   char *buf;
113   int is_dev;
114
115   is_dev = STREQLEN (pathordevice, "/dev/", 5);
116   buf = is_dev ? strdup (pathordevice)
117                : sysroot_path (pathordevice);
118   if (buf == NULL) {
119     reply_with_perror ("malloc");
120     return -1;
121   }
122
123   if (is_dev)
124     RESOLVE_DEVICE (buf, 0, { free (buf); return -1; });
125
126   r = command (NULL, &err, "umount", buf, NULL);
127   free (buf);
128
129   if (r == -1) {
130     reply_with_error ("%s: %s", pathordevice, err);
131     free (err);
132     return -1;
133   }
134
135   free (err);
136
137   /* update root_mounted? */
138
139   return 0;
140 }
141
142 static char **
143 mounts_or_mountpoints (int mp)
144 {
145   char *out, *err;
146   int r;
147   char **ret = NULL;
148   int size = 0, alloc = 0;
149   char *p, *pend, *p2;
150   int len;
151   char matching[5 + sysroot_len];
152
153   r = command (&out, &err, "mount", NULL);
154   if (r == -1) {
155     reply_with_error ("mount: %s", err);
156     free (out);
157     free (err);
158     return NULL;
159   }
160
161   free (err);
162
163   /* Lines have the format:
164    *   /dev/foo on /mountpoint type ...
165    */
166   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
167
168   p = out;
169   while (p) {
170     pend = strchr (p, '\n');
171     if (pend) {
172       *pend = '\0';
173       pend++;
174     }
175
176     p2 = strstr (p, matching);
177     if (p2 != NULL) {
178       *p2 = '\0';
179       if (add_string (&ret, &size, &alloc, p) == -1) {
180         free (out);
181         return NULL;
182       }
183       if (mp) {
184         p2 += 4 + sysroot_len;  /* skip " on /sysroot" */
185         len = strcspn (p2, " ");
186
187         if (len == 0)           /* .. just /sysroot, so we turn it into "/" */
188           p2 = (char *) "/";
189         else
190           p2[len] = '\0';
191
192         if (add_string (&ret, &size, &alloc, p2) == -1) {
193           free (out);
194           return NULL;
195         }
196       }
197     }
198
199     p = pend;
200   }
201
202   free (out);
203
204   if (add_string (&ret, &size, &alloc, NULL) == -1)
205     return NULL;
206
207   return ret;
208 }
209
210 char **
211 do_mounts (void)
212 {
213   return mounts_or_mountpoints (0);
214 }
215
216 char **
217 do_mountpoints (void)
218 {
219   return mounts_or_mountpoints (1);
220 }
221
222 /* Unmount everything mounted under /sysroot.
223  *
224  * We have to unmount in the correct order, so we sort the paths by
225  * longest first to ensure that child paths are unmounted by parent
226  * paths.
227  *
228  * This call is more important than it appears at first, because it
229  * is widely used by both test and production code in order to
230  * get back to a known state (nothing mounted, everything synchronized).
231  */
232 static int
233 compare_longest_first (const void *vp1, const void *vp2)
234 {
235   char * const *p1 = (char * const *) vp1;
236   char * const *p2 = (char * const *) vp2;
237   int n1 = strlen (*p1);
238   int n2 = strlen (*p2);
239   return n2 - n1;
240 }
241
242 int
243 do_umount_all (void)
244 {
245   char *out, *err;
246   int i, r;
247   char **mounts = NULL;
248   int size = 0, alloc = 0;
249   char *p, *p2, *p3, *pend;
250   char matching[5 + sysroot_len];
251
252   r = command (&out, &err, "mount", NULL);
253   if (r == -1) {
254     reply_with_error ("mount: %s", err);
255     free (out);
256     free (err);
257     return -1;
258   }
259
260   free (err);
261
262   /* Lines have the format:
263    *   /dev/foo on /mountpoint type ...
264    */
265   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
266
267   p = out;
268   while (p) {
269     pend = strchr (p, '\n');
270     if (pend) {
271       *pend = '\0';
272       pend++;
273     }
274
275     p2 = strstr (p, matching);
276     if (p2 != NULL) {
277       p2 += 4;
278       p3 = p2 + strcspn (p2, " ");
279       *p3 = '\0';
280       if (add_string (&mounts, &size, &alloc, p2) == -1) {
281         free (out);
282         return -1;
283       }
284     }
285
286     p = pend;
287   }
288   free (out);
289
290   qsort (mounts, size, sizeof (char *), compare_longest_first);
291
292   /* Unmount them. */
293   for (i = 0; i < size; ++i) {
294     r = command (NULL, &err, "umount", mounts[i], NULL);
295     if (r == -1) {
296       reply_with_error ("umount: %s: %s", mounts[i], err);
297       free (err);
298       free_stringslen (mounts, size);
299       return -1;
300     }
301     free (err);
302   }
303
304   free_stringslen (mounts, size);
305
306   /* We've unmounted root now, so ... */
307   root_mounted = 0;
308
309   return 0;
310 }
311
312 /* Mount using the loopback device.  You can't use the generic
313  * do_mount call for this because the first parameter isn't a
314  * device.
315  */
316 int
317 do_mount_loop (const char *file, const char *mountpoint)
318 {
319   int r;
320   char *buf, *mp;
321   char *error;
322
323   /* We have to prefix /sysroot on both the filename and the mountpoint. */
324   mp = sysroot_path (mountpoint);
325   if (!mp) {
326     reply_with_perror ("malloc");
327     return -1;
328   }
329
330   buf = sysroot_path (file);
331   if (!file) {
332     reply_with_perror ("malloc");
333     free (mp);
334     return -1;
335   }
336
337   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
338   free (mp);
339   free (buf);
340   if (r == -1) {
341     reply_with_error ("%s on %s: %s", file, mountpoint, error);
342     free (error);
343     return -1;
344   }
345
346   return 0;
347 }
348
349 /* Specialized calls mkmountpoint and rmmountpoint are really
350  * variations on mkdir and rmdir which do no checking and (in the
351  * mkmountpoint case) set the root_mounted flag.
352  */
353 int
354 do_mkmountpoint (const char *path)
355 {
356   int r;
357
358   /* NEED_ROOT (return -1); - we don't want this test for this call. */
359   ABS_PATH (path, 0, return -1);
360
361   CHROOT_IN;
362   r = mkdir (path, 0777);
363   CHROOT_OUT;
364
365   if (r == -1) {
366     reply_with_perror ("%s", path);
367     return -1;
368   }
369
370   /* Set the flag so that filesystems can be mounted here,
371    * not just on /sysroot.
372    */
373   root_mounted = 1;
374
375   return 0;
376 }
377
378 int
379 do_rmmountpoint (const char *path)
380 {
381   int r;
382
383   /* NEED_ROOT (return -1); - we don't want this test for this call. */
384   ABS_PATH (path, 0, return -1);
385
386   CHROOT_IN;
387   r = rmdir (path);
388   CHROOT_OUT;
389
390   if (r == -1) {
391     reply_with_perror ("%s", path);
392     return -1;
393   }
394
395   return 0;
396 }