3abdabddaf53af11d64751880b69492b3eceaaaa
[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 (const char *options, const char *vfstype,
43               const char *device, const char *mountpoint)
44 {
45   int len, r, is_root;
46   char *mp;
47   char *error;
48
49   is_root = strcmp (mountpoint, "/") == 0;
50
51   if (!root_mounted && !is_root) {
52     reply_with_error ("mount: you must mount something on / first");
53     return -1;
54   }
55
56   len = strlen (mountpoint) + 9;
57
58   mp = malloc (len);
59   if (!mp) {
60     reply_with_perror ("malloc");
61     return -1;
62   }
63
64   snprintf (mp, len, "/sysroot%s", mountpoint);
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   if (r == -1) {
73     reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
74     free (error);
75     return -1;
76   }
77
78   if (is_root)
79     root_mounted = 1;
80
81   return 0;
82 }
83
84 int
85 do_mount (const char *device, const char *mountpoint)
86 {
87   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
88 }
89
90 int
91 do_mount_ro (const char *device, const char *mountpoint)
92 {
93   return do_mount_vfs ("ro", NULL, device, mountpoint);
94 }
95
96 int
97 do_mount_options (const char *options, const char *device,
98                   const char *mountpoint)
99 {
100   return do_mount_vfs (options, NULL, device, mountpoint);
101 }
102
103 /* Again, use the external /bin/umount program, so that /etc/mtab
104  * is kept updated.
105  */
106 int
107 do_umount (const char *pathordevice)
108 {
109   int len, freeit = 0, r;
110   char *buf;
111   char *err;
112
113   if (strncmp (pathordevice, "/dev/", 5) == 0)
114     buf = (char *) pathordevice;
115   else {
116     len = strlen (pathordevice) + 9;
117     freeit = 1;
118     buf = malloc (len);
119     if (buf == NULL) {
120       reply_with_perror ("malloc");
121       return -1;
122     }
123     snprintf (buf, len, "/sysroot%s", pathordevice);
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 char **
142 do_mounts (void)
143 {
144   char *out, *err;
145   int r;
146   char **ret = NULL;
147   int size = 0, alloc = 0;
148   char *p, *pend, *p2;
149
150   r = command (&out, &err, "mount", NULL);
151   if (r == -1) {
152     reply_with_error ("mount: %s", err);
153     free (out);
154     free (err);
155     return NULL;
156   }
157
158   free (err);
159
160   p = out;
161   while (p) {
162     pend = strchr (p, '\n');
163     if (pend) {
164       *pend = '\0';
165       pend++;
166     }
167
168     /* Lines have the format:
169      *   /dev/foo on /mountpoint type ...
170      */
171     p2 = strstr (p, " on /sysroot");
172     if (p2 != NULL) {
173       *p2 = '\0';
174       if (add_string (&ret, &size, &alloc, p) == -1) {
175         free (out);
176         return NULL;
177       }
178     }
179
180     p = pend;
181   }
182
183   free (out);
184
185   if (add_string (&ret, &size, &alloc, NULL) == -1)
186     return NULL;
187
188   return ret;
189 }
190
191 /* Only unmount stuff under /sysroot */
192 int
193 do_umount_all (void)
194 {
195   char **mounts;
196   int i, r;
197   char *err;
198
199   mounts = do_mounts ();
200   if (mounts == NULL)           /* do_mounts has already replied */
201     return -1;
202
203   for (i = 0; mounts[i] != NULL; ++i) {
204     r = command (NULL, &err, "umount", mounts[i], NULL);
205     if (r == -1) {
206       reply_with_error ("umount: %s: %s", mounts[i], err);
207       free (err);
208       free_strings (mounts);
209       return -1;
210     }
211     free (err);
212   }
213
214   free_strings (mounts);
215   return 0;
216 }