Fix detection of optional libvirt support in virt-inspector.
[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   if (r == -1) {
75     reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
76     free (error);
77     return -1;
78   }
79
80   if (is_root)
81     root_mounted = 1;
82
83   return 0;
84 }
85
86 int
87 do_mount (char *device, char *mountpoint)
88 {
89   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
90 }
91
92 int
93 do_mount_ro (char *device, char *mountpoint)
94 {
95   return do_mount_vfs ("ro", NULL, device, mountpoint);
96 }
97
98 int
99 do_mount_options (char *options, char *device,
100                   char *mountpoint)
101 {
102   return do_mount_vfs (options, NULL, device, mountpoint);
103 }
104
105 /* Again, use the external /bin/umount program, so that /etc/mtab
106  * is kept updated.
107  */
108 int
109 do_umount (char *pathordevice)
110 {
111   int len, freeit = 0, r;
112   char *buf;
113   char *err;
114
115   if (strncmp (pathordevice, "/dev/", 5) == 0) {
116     buf = pathordevice;
117     IS_DEVICE (buf, -1);
118   } else {
119     len = strlen (pathordevice) + 9;
120     freeit = 1;
121     buf = malloc (len);
122     if (buf == NULL) {
123       reply_with_perror ("malloc");
124       return -1;
125     }
126     snprintf (buf, len, "/sysroot%s", pathordevice);
127   }
128
129   r = command (NULL, &err, "umount", buf, NULL);
130   if (freeit) free (buf);
131   if (r == -1) {
132     reply_with_error ("umount: %s: %s", pathordevice, err);
133     free (err);
134     return -1;
135   }
136
137   free (err);
138
139   /* update root_mounted? */
140
141   return 0;
142 }
143
144 char **
145 do_mounts (void)
146 {
147   char *out, *err;
148   int r;
149   char **ret = NULL;
150   int size = 0, alloc = 0;
151   char *p, *pend, *p2;
152
153   r = command (&out, &err, "mount", NULL);
154   if (r == -1) {
155     reply_with_error ("mount: %s", err);
156     free (out);
157     free (err);
158     return NULL;
159   }
160
161   free (err);
162
163   p = out;
164   while (p) {
165     pend = strchr (p, '\n');
166     if (pend) {
167       *pend = '\0';
168       pend++;
169     }
170
171     /* Lines have the format:
172      *   /dev/foo on /mountpoint type ...
173      */
174     p2 = strstr (p, " on /sysroot");
175     if (p2 != NULL) {
176       *p2 = '\0';
177       if (add_string (&ret, &size, &alloc, p) == -1) {
178         free (out);
179         return NULL;
180       }
181     }
182
183     p = pend;
184   }
185
186   free (out);
187
188   if (add_string (&ret, &size, &alloc, NULL) == -1)
189     return NULL;
190
191   return ret;
192 }
193
194 /* Unmount everything mounted under /sysroot.
195  *
196  * We have to unmount in the correct order, so we sort the paths by
197  * longest first to ensure that child paths are unmounted by parent
198  * paths.
199  *
200  * This call is more important than it appears at first, because it
201  * is widely used by both test and production code in order to
202  * get back to a known state (nothing mounted, everything synchronized).
203  */
204 static int
205 compare_longest_first (const void *vp1, const void *vp2)
206 {
207   char * const *p1 = (char * const *) vp1;
208   char * const *p2 = (char * const *) vp2;
209   int n1 = strlen (*p1);
210   int n2 = strlen (*p2);
211   return n2 - n1;
212 }
213
214 int
215 do_umount_all (void)
216 {
217   char *out, *err;
218   int i, r;
219   char **mounts = NULL;
220   int size = 0, alloc = 0;
221   char *p, *p2, *p3, *pend;
222
223   r = command (&out, &err, "mount", NULL);
224   if (r == -1) {
225     reply_with_error ("mount: %s", err);
226     free (out);
227     free (err);
228     return -1;
229   }
230
231   free (err);
232
233   p = out;
234   while (p) {
235     pend = strchr (p, '\n');
236     if (pend) {
237       *pend = '\0';
238       pend++;
239     }
240
241     /* Lines have the format:
242      *   /dev/foo on /mountpoint type ...
243      */
244     p2 = strstr (p, " on /sysroot");
245     if (p2 != NULL) {
246       p2 += 4;
247       p3 = p2 + strcspn (p2, " ");
248       *p3 = '\0';
249       if (add_string (&mounts, &size, &alloc, p2) == -1) {
250         free (out);
251         return -1;
252       }
253     }
254
255     p = pend;
256   }
257   free (out);
258
259   qsort (mounts, size, sizeof (char *), compare_longest_first);
260
261   /* Unmount them. */
262   for (i = 0; i < size; ++i) {
263     r = command (NULL, &err, "umount", mounts[i], NULL);
264     if (r == -1) {
265       reply_with_error ("umount: %s: %s", mounts[i], err);
266       free (err);
267       free_stringslen (mounts, size);
268       return -1;
269     }
270     free (err);
271   }
272
273   free_stringslen (mounts, size);
274
275   /* We've unmounted root now, so ... */
276   root_mounted = 0;
277
278   return 0;
279 }