umount-all: Add udev_settle after unmounting disks.
[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   size_t i;
153
154   r = command (&out, &err, "mount", NULL);
155   if (r == -1) {
156     reply_with_error ("mount: %s", err);
157     free (out);
158     free (err);
159     return NULL;
160   }
161
162   free (err);
163
164   /* Lines have the format:
165    *   /dev/foo on /mountpoint type ...
166    */
167   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
168
169   p = out;
170   while (p) {
171     pend = strchr (p, '\n');
172     if (pend) {
173       *pend = '\0';
174       pend++;
175     }
176
177     p2 = strstr (p, matching);
178     if (p2 != NULL) {
179       *p2 = '\0';
180       if (add_string (&ret, &size, &alloc, p) == -1) {
181         free (out);
182         return NULL;
183       }
184       if (mp) {
185         p2 += 4 + sysroot_len;  /* skip " on /sysroot" */
186         len = strcspn (p2, " ");
187
188         if (len == 0)           /* .. just /sysroot, so we turn it into "/" */
189           p2 = (char *) "/";
190         else
191           p2[len] = '\0';
192
193         if (add_string (&ret, &size, &alloc, p2) == -1) {
194           free (out);
195           return NULL;
196         }
197       }
198     }
199
200     p = pend;
201   }
202
203   free (out);
204
205   if (add_string (&ret, &size, &alloc, NULL) == -1)
206     return NULL;
207
208   /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
209   for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
210     if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
211       char *canonical;
212       r = lv_canonical (ret[i], &canonical);
213       if (r == -1) {
214         free_strings (ret);
215         return NULL;
216       }
217       if (r == 1) {
218         free (ret[i]);
219         ret[i] = canonical;
220       }
221       /* Ignore the case where r == 0.  This might happen where
222        * eg. a LUKS /dev/mapper device is mounted, but that won't
223        * correspond to any LV.
224        */
225     }
226   }
227
228   return ret;
229 }
230
231 char **
232 do_mounts (void)
233 {
234   return mounts_or_mountpoints (0);
235 }
236
237 char **
238 do_mountpoints (void)
239 {
240   return mounts_or_mountpoints (1);
241 }
242
243 /* Unmount everything mounted under /sysroot.
244  *
245  * We have to unmount in the correct order, so we sort the paths by
246  * longest first to ensure that child paths are unmounted by parent
247  * paths.
248  *
249  * This call is more important than it appears at first, because it
250  * is widely used by both test and production code in order to
251  * get back to a known state (nothing mounted, everything synchronized).
252  */
253 static int
254 compare_longest_first (const void *vp1, const void *vp2)
255 {
256   char * const *p1 = (char * const *) vp1;
257   char * const *p2 = (char * const *) vp2;
258   int n1 = strlen (*p1);
259   int n2 = strlen (*p2);
260   return n2 - n1;
261 }
262
263 int
264 do_umount_all (void)
265 {
266   char *out, *err;
267   int i, r;
268   char **mounts = NULL;
269   int size = 0, alloc = 0;
270   char *p, *p2, *p3, *pend;
271   char matching[5 + sysroot_len];
272
273   r = command (&out, &err, "mount", NULL);
274   if (r == -1) {
275     reply_with_error ("mount: %s", err);
276     free (out);
277     free (err);
278     return -1;
279   }
280
281   free (err);
282
283   /* Lines have the format:
284    *   /dev/foo on /mountpoint type ...
285    */
286   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
287
288   p = out;
289   while (p) {
290     pend = strchr (p, '\n');
291     if (pend) {
292       *pend = '\0';
293       pend++;
294     }
295
296     p2 = strstr (p, matching);
297     if (p2 != NULL) {
298       p2 += 4;
299       p3 = p2 + strcspn (p2, " ");
300       *p3 = '\0';
301       if (add_string (&mounts, &size, &alloc, p2) == -1) {
302         free (out);
303         return -1;
304       }
305     }
306
307     p = pend;
308   }
309   free (out);
310
311   qsort (mounts, size, sizeof (char *), compare_longest_first);
312
313   /* Unmount them. */
314   for (i = 0; i < size; ++i) {
315     r = command (NULL, &err, "umount", mounts[i], NULL);
316     if (r == -1) {
317       reply_with_error ("umount: %s: %s", mounts[i], err);
318       free (err);
319       free_stringslen (mounts, size);
320       return -1;
321     }
322     free (err);
323   }
324
325   free_stringslen (mounts, size);
326
327   /* Without this, lvm_remove_all was failing on Ubuntu. */
328   udev_settle ();
329
330   /* We've unmounted root now, so ... */
331   root_mounted = 0;
332
333   return 0;
334 }
335
336 /* Mount using the loopback device.  You can't use the generic
337  * do_mount call for this because the first parameter isn't a
338  * device.
339  */
340 int
341 do_mount_loop (const char *file, const char *mountpoint)
342 {
343   int r;
344   char *buf, *mp;
345   char *error;
346
347   /* We have to prefix /sysroot on both the filename and the mountpoint. */
348   mp = sysroot_path (mountpoint);
349   if (!mp) {
350     reply_with_perror ("malloc");
351     return -1;
352   }
353
354   buf = sysroot_path (file);
355   if (!file) {
356     reply_with_perror ("malloc");
357     free (mp);
358     return -1;
359   }
360
361   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
362   free (mp);
363   free (buf);
364   if (r == -1) {
365     reply_with_error ("%s on %s: %s", file, mountpoint, error);
366     free (error);
367     return -1;
368   }
369
370   return 0;
371 }
372
373 /* Specialized calls mkmountpoint and rmmountpoint are really
374  * variations on mkdir and rmdir which do no checking and (in the
375  * mkmountpoint case) set the root_mounted flag.
376  */
377 int
378 do_mkmountpoint (const char *path)
379 {
380   int r;
381
382   /* NEED_ROOT (return -1); - we don't want this test for this call. */
383   ABS_PATH (path, 0, return -1);
384
385   CHROOT_IN;
386   r = mkdir (path, 0777);
387   CHROOT_OUT;
388
389   if (r == -1) {
390     reply_with_perror ("%s", path);
391     return -1;
392   }
393
394   /* Set the flag so that filesystems can be mounted here,
395    * not just on /sysroot.
396    */
397   root_mounted = 1;
398
399   return 0;
400 }
401
402 int
403 do_rmmountpoint (const char *path)
404 {
405   int r;
406
407   /* NEED_ROOT (return -1); - we don't want this test for this call. */
408   ABS_PATH (path, 0, return -1);
409
410   CHROOT_IN;
411   r = rmdir (path);
412   CHROOT_OUT;
413
414   if (r == -1) {
415     reply_with_perror ("%s", path);
416     return -1;
417   }
418
419   return 0;
420 }