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