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