Generated code for 'checksum' command.
[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.
34  *
35  * It's tempting to try a direct mount(2) syscall, but that doesn't
36  * do any autodetection, so we are better off calling out to
37  * /bin/mount.
38  */
39
40 int
41 do_mount (const char *device, const char *mountpoint)
42 {
43   int len, r, is_root;
44   char *mp;
45   char *error;
46
47   is_root = strcmp (mountpoint, "/") == 0;
48
49   if (!root_mounted && !is_root) {
50     reply_with_error ("mount: you must mount something on / first");
51     return -1;
52   }
53
54   len = strlen (mountpoint) + 9;
55
56   mp = malloc (len);
57   if (!mp) {
58     reply_with_perror ("malloc");
59     return -1;
60   }
61
62   snprintf (mp, len, "/sysroot%s", mountpoint);
63
64   r = command (NULL, &error,
65                "mount", "-o", "sync,noatime", device, mp, NULL);
66   if (r == -1) {
67     reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
68     free (error);
69     return -1;
70   }
71
72   if (is_root)
73     root_mounted = 1;
74
75   return 0;
76 }
77
78 /* Again, use the external /bin/umount program, so that /etc/mtab
79  * is kept updated.
80  */
81 int
82 do_umount (const char *pathordevice)
83 {
84   int len, freeit = 0, r;
85   char *buf;
86   char *err;
87
88   if (strncmp (pathordevice, "/dev/", 5) == 0)
89     buf = (char *) pathordevice;
90   else {
91     len = strlen (pathordevice) + 9;
92     freeit = 1;
93     buf = malloc (len);
94     if (buf == NULL) {
95       reply_with_perror ("malloc");
96       return -1;
97     }
98     snprintf (buf, len, "/sysroot%s", pathordevice);
99   }
100
101   r = command (NULL, &err, "umount", buf, NULL);
102   if (freeit) free (buf);
103   if (r == -1) {
104     reply_with_error ("umount: %s: %s", pathordevice, err);
105     free (err);
106     return -1;
107   }
108
109   free (err);
110
111   /* update root_mounted? */
112
113   return 0;
114 }
115
116 char **
117 do_mounts (void)
118 {
119   char *out, *err;
120   int r;
121   char **ret = NULL;
122   int size = 0, alloc = 0;
123   char *p, *pend, *p2;
124
125   r = command (&out, &err, "mount", NULL);
126   if (r == -1) {
127     reply_with_error ("mount: %s", err);
128     free (out);
129     free (err);
130     return NULL;
131   }
132
133   free (err);
134
135   p = out;
136   while (p) {
137     pend = strchr (p, '\n');
138     if (pend) {
139       *pend = '\0';
140       pend++;
141     }
142
143     /* Lines have the format:
144      *   /dev/foo on /mountpoint type ...
145      */
146     p2 = strstr (p, " on /sysroot");
147     if (p2 != NULL) {
148       *p2 = '\0';
149       if (add_string (&ret, &size, &alloc, p) == -1) {
150         free (out);
151         return NULL;
152       }
153     }
154
155     p = pend;
156   }
157
158   free (out);
159
160   if (add_string (&ret, &size, &alloc, NULL) == -1)
161     return NULL;
162
163   return ret;
164 }
165
166 /* Only unmount stuff under /sysroot */
167 int
168 do_umount_all (void)
169 {
170   char **mounts;
171   int i, r;
172   char *err;
173
174   mounts = do_mounts ();
175   if (mounts == NULL)           /* do_mounts has already replied */
176     return -1;
177
178   for (i = 0; mounts[i] != NULL; ++i) {
179     r = command (NULL, &err, "umount", mounts[i], NULL);
180     if (r == -1) {
181       reply_with_error ("umount: %s: %s", mounts[i], err);
182       free (err);
183       free_strings (mounts);
184       return -1;
185     }
186     free (err);
187   }
188
189   free_strings (mounts);
190   return 0;
191 }