4f6068232b093ba6aa438ab1cba81f4520a989fa
[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 (char *options, char *vfstype,
45               char *device, 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 (char *device, char *mountpoint)
85 {
86   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
87 }
88
89 int
90 do_mount_ro (char *device, char *mountpoint)
91 {
92   return do_mount_vfs ("ro", NULL, device, mountpoint);
93 }
94
95 int
96 do_mount_options (char *options, char *device,
97                   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 (char *pathordevice)
107 {
108   int freeit = 0, r;
109   char *buf;
110   char *err;
111
112   if (strncmp (pathordevice, "/dev/", 5) == 0) {
113     buf = pathordevice;
114     RESOLVE_DEVICE (buf, return -1);
115   } else {
116     buf = sysroot_path (pathordevice);
117     if (buf == NULL) {
118       reply_with_perror ("malloc");
119       return -1;
120     }
121     freeit = 1;
122   }
123
124   r = command (NULL, &err, "umount", buf, NULL);
125   if (freeit) free (buf);
126   if (r == -1) {
127     reply_with_error ("umount: %s: %s", pathordevice, err);
128     free (err);
129     return -1;
130   }
131
132   free (err);
133
134   /* update root_mounted? */
135
136   return 0;
137 }
138
139 static char **
140 mounts_or_mountpoints (int mp)
141 {
142   char *out, *err;
143   int r;
144   char **ret = NULL;
145   int size = 0, alloc = 0;
146   char *p, *pend, *p2;
147   int len;
148   char matching[5 + sysroot_len];
149
150   r = command (&out, &err, "mount", NULL);
151   if (r == -1) {
152     reply_with_error ("mount: %s", err);
153     free (out);
154     free (err);
155     return NULL;
156   }
157
158   free (err);
159
160   /* Lines have the format:
161    *   /dev/foo on /mountpoint type ...
162    */
163   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
164
165   p = out;
166   while (p) {
167     pend = strchr (p, '\n');
168     if (pend) {
169       *pend = '\0';
170       pend++;
171     }
172
173     p2 = strstr (p, matching);
174     if (p2 != NULL) {
175       *p2 = '\0';
176       if (add_string (&ret, &size, &alloc, p) == -1) {
177         free (out);
178         return NULL;
179       }
180       if (mp) {
181         p2 += 4 + sysroot_len;  /* skip " on /sysroot" */
182         len = strcspn (p2, " ");
183
184         if (len == 0)           /* .. just /sysroot, so we turn it into "/" */
185           p2 = (char *) "/";
186         else
187           p2[len] = '\0';
188
189         if (add_string (&ret, &size, &alloc, p2) == -1) {
190           free (out);
191           return NULL;
192         }
193       }
194     }
195
196     p = pend;
197   }
198
199   free (out);
200
201   if (add_string (&ret, &size, &alloc, NULL) == -1)
202     return NULL;
203
204   return ret;
205 }
206
207 char **
208 do_mounts (void)
209 {
210   return mounts_or_mountpoints (0);
211 }
212
213 char **
214 do_mountpoints (void)
215 {
216   return mounts_or_mountpoints (1);
217 }
218
219 /* Unmount everything mounted under /sysroot.
220  *
221  * We have to unmount in the correct order, so we sort the paths by
222  * longest first to ensure that child paths are unmounted by parent
223  * paths.
224  *
225  * This call is more important than it appears at first, because it
226  * is widely used by both test and production code in order to
227  * get back to a known state (nothing mounted, everything synchronized).
228  */
229 static int
230 compare_longest_first (const void *vp1, const void *vp2)
231 {
232   char * const *p1 = (char * const *) vp1;
233   char * const *p2 = (char * const *) vp2;
234   int n1 = strlen (*p1);
235   int n2 = strlen (*p2);
236   return n2 - n1;
237 }
238
239 int
240 do_umount_all (void)
241 {
242   char *out, *err;
243   int i, r;
244   char **mounts = NULL;
245   int size = 0, alloc = 0;
246   char *p, *p2, *p3, *pend;
247   char matching[5 + sysroot_len];
248
249   r = command (&out, &err, "mount", NULL);
250   if (r == -1) {
251     reply_with_error ("mount: %s", err);
252     free (out);
253     free (err);
254     return -1;
255   }
256
257   free (err);
258
259   /* Lines have the format:
260    *   /dev/foo on /mountpoint type ...
261    */
262   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
263
264   p = out;
265   while (p) {
266     pend = strchr (p, '\n');
267     if (pend) {
268       *pend = '\0';
269       pend++;
270     }
271
272     p2 = strstr (p, matching);
273     if (p2 != NULL) {
274       p2 += 4;
275       p3 = p2 + strcspn (p2, " ");
276       *p3 = '\0';
277       if (add_string (&mounts, &size, &alloc, p2) == -1) {
278         free (out);
279         return -1;
280       }
281     }
282
283     p = pend;
284   }
285   free (out);
286
287   qsort (mounts, size, sizeof (char *), compare_longest_first);
288
289   /* Unmount them. */
290   for (i = 0; i < size; ++i) {
291     r = command (NULL, &err, "umount", mounts[i], NULL);
292     if (r == -1) {
293       reply_with_error ("umount: %s: %s", mounts[i], err);
294       free (err);
295       free_stringslen (mounts, size);
296       return -1;
297     }
298     free (err);
299   }
300
301   free_stringslen (mounts, size);
302
303   /* We've unmounted root now, so ... */
304   root_mounted = 0;
305
306   return 0;
307 }
308
309 /* Mount using the loopback device.  You can't use the generic
310  * do_mount call for this because the first parameter isn't a
311  * device.
312  */
313 int
314 do_mount_loop (char *file, char *mountpoint)
315 {
316   int r;
317   char *buf, *mp;
318   char *error;
319
320   NEED_ROOT (return -1);
321   ABS_PATH (file, return -1);
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 ("mount: %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 (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, return -1);
360
361   CHROOT_IN;
362   r = mkdir (path, 0777);
363   CHROOT_OUT;
364
365   if (r == -1) {
366     reply_with_perror ("mkmountpoint: %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 (char *path)
380 {
381   int r;
382
383   NEED_ROOT (return -1);
384   ABS_PATH (path, return -1);
385
386   CHROOT_IN;
387   r = rmdir (path);
388   CHROOT_OUT;
389
390   if (r == -1) {
391     reply_with_perror ("rmmountpoint: %s", path);
392     return -1;
393   }
394
395   return 0;
396 }