New command: 'mountpoints' which returns a hash of device -> mountpoint.
[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
26 #include "daemon.h"
27 #include "actions.h"
28
29 /* You must mount something on "/" first, hence: */
30 int root_mounted = 0;
31
32 /* The "simple mount" call offers no complex options, you can just
33  * mount a device on a mountpoint.  The variations like mount_ro,
34  * mount_options and mount_vfs let you set progressively more things.
35  *
36  * It's tempting to try a direct mount(2) syscall, but that doesn't
37  * do any autodetection, so we are better off calling out to
38  * /bin/mount.
39  */
40
41 int
42 do_mount_vfs (char *options, char *vfstype,
43               char *device, char *mountpoint)
44 {
45   int len, r, is_root;
46   char *mp;
47   char *error;
48
49   IS_DEVICE (device, -1);
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   len = strlen (mountpoint) + 9;
59
60   mp = malloc (len);
61   if (!mp) {
62     reply_with_perror ("malloc");
63     return -1;
64   }
65
66   snprintf (mp, len, "/sysroot%s", mountpoint);
67
68   if (vfstype)
69     r = command (NULL, &error,
70                  "mount", "-o", options, "-t", vfstype, device, mp, NULL);
71   else
72     r = command (NULL, &error,
73                  "mount", "-o", options, device, mp, NULL);
74   free (mp);
75   if (r == -1) {
76     reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
77     free (error);
78     return -1;
79   }
80
81   if (is_root)
82     root_mounted = 1;
83
84   return 0;
85 }
86
87 int
88 do_mount (char *device, char *mountpoint)
89 {
90   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
91 }
92
93 int
94 do_mount_ro (char *device, char *mountpoint)
95 {
96   return do_mount_vfs ("ro", NULL, device, mountpoint);
97 }
98
99 int
100 do_mount_options (char *options, char *device,
101                   char *mountpoint)
102 {
103   return do_mount_vfs (options, NULL, device, mountpoint);
104 }
105
106 /* Again, use the external /bin/umount program, so that /etc/mtab
107  * is kept updated.
108  */
109 int
110 do_umount (char *pathordevice)
111 {
112   int len, freeit = 0, r;
113   char *buf;
114   char *err;
115
116   if (strncmp (pathordevice, "/dev/", 5) == 0) {
117     buf = pathordevice;
118     IS_DEVICE (buf, -1);
119   } else {
120     len = strlen (pathordevice) + 9;
121     freeit = 1;
122     buf = malloc (len);
123     if (buf == NULL) {
124       reply_with_perror ("malloc");
125       return -1;
126     }
127     snprintf (buf, len, "/sysroot%s", pathordevice);
128   }
129
130   r = command (NULL, &err, "umount", buf, NULL);
131   if (freeit) free (buf);
132   if (r == -1) {
133     reply_with_error ("umount: %s: %s", pathordevice, err);
134     free (err);
135     return -1;
136   }
137
138   free (err);
139
140   /* update root_mounted? */
141
142   return 0;
143 }
144
145 static char **
146 mounts_or_mountpoints (int mp)
147 {
148   char *out, *err;
149   int r;
150   char **ret = NULL;
151   int size = 0, alloc = 0;
152   char *p, *pend, *p2;
153   int len;
154
155   r = command (&out, &err, "mount", NULL);
156   if (r == -1) {
157     reply_with_error ("mount: %s", err);
158     free (out);
159     free (err);
160     return NULL;
161   }
162
163   free (err);
164
165   p = out;
166   while (p) {
167     pend = strchr (p, '\n');
168     if (pend) {
169       *pend = '\0';
170       pend++;
171     }
172
173     /* Lines have the format:
174      *   /dev/foo on /mountpoint type ...
175      */
176     p2 = strstr (p, " on /sysroot");
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 += 12;               /* 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
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   p = out;
262   while (p) {
263     pend = strchr (p, '\n');
264     if (pend) {
265       *pend = '\0';
266       pend++;
267     }
268
269     /* Lines have the format:
270      *   /dev/foo on /mountpoint type ...
271      */
272     p2 = strstr (p, " on /sysroot");
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 len, r;
317   char *buf, *mp;
318   char *error;
319
320   NEED_ROOT (-1);
321   ABS_PATH (file, -1);
322
323   /* We have to prefix /sysroot on both the filename and the mountpoint. */
324   len = strlen (mountpoint) + 9;
325   mp = malloc (len);
326   if (!mp) {
327     reply_with_perror ("malloc");
328     return -1;
329   }
330   snprintf (mp, len, "/sysroot%s", mountpoint);
331
332   len = strlen (file) + 9;
333   buf = malloc (len);
334   if (!file) {
335     reply_with_perror ("malloc");
336     free (mp);
337     return -1;
338   }
339   snprintf (buf, len, "/sysroot%s", file);
340
341   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
342   free (mp);
343   free (buf);
344   if (r == -1) {
345     reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
346     free (error);
347     return -1;
348   }
349
350   return 0;
351 }