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