1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
26 #include <sys/types.h>
31 /* You must mount something on "/" first, hence: */
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.
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
44 do_mount_vfs (const char *options, const char *vfstype,
45 const char *device, const char *mountpoint)
51 is_root = strcmp (mountpoint, "/") == 0;
53 if (!root_mounted && !is_root) {
54 reply_with_error ("mount: you must mount something on / first");
58 mp = sysroot_path (mountpoint);
60 reply_with_perror ("malloc");
65 r = command (NULL, &error,
66 "mount", "-o", options, "-t", vfstype, device, mp, NULL);
68 r = command (NULL, &error,
69 "mount", "-o", options, device, mp, NULL);
72 reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
84 do_mount (const char *device, const char *mountpoint)
86 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
90 do_mount_ro (const char *device, const char *mountpoint)
92 return do_mount_vfs ("ro", NULL, device, mountpoint);
96 do_mount_options (const char *options, const char *device,
97 const char *mountpoint)
99 return do_mount_vfs (options, NULL, device, mountpoint);
102 /* Again, use the external /bin/umount program, so that /etc/mtab
106 do_umount (const char *pathordevice)
112 if (strncmp (pathordevice, "/dev/", 5) == 0) {
114 RESOLVE_DEVICE (buf, return -1);
116 buf = sysroot_path (pathordevice);
118 reply_with_perror ("malloc");
124 r = command (NULL, &err, "umount", buf, NULL);
125 if (freeit) free (buf);
127 reply_with_error ("umount: %s: %s", pathordevice, err);
134 /* update root_mounted? */
140 mounts_or_mountpoints (int mp)
145 int size = 0, alloc = 0;
148 char matching[5 + sysroot_len];
150 r = command (&out, &err, "mount", NULL);
152 reply_with_error ("mount: %s", err);
160 /* Lines have the format:
161 * /dev/foo on /mountpoint type ...
163 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
167 pend = strchr (p, '\n');
173 p2 = strstr (p, matching);
176 if (add_string (&ret, &size, &alloc, p) == -1) {
181 p2 += 4 + sysroot_len; /* skip " on /sysroot" */
182 len = strcspn (p2, " ");
184 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
189 if (add_string (&ret, &size, &alloc, p2) == -1) {
201 if (add_string (&ret, &size, &alloc, NULL) == -1)
210 return mounts_or_mountpoints (0);
214 do_mountpoints (void)
216 return mounts_or_mountpoints (1);
219 /* Unmount everything mounted under /sysroot.
221 * We have to unmount in the correct order, so we sort the paths by
222 * longest first to ensure that child paths are unmounted by parent
225 * This call is more important than it appears at first, because it
226 * is widely used by both test and production code in order to
227 * get back to a known state (nothing mounted, everything synchronized).
230 compare_longest_first (const void *vp1, const void *vp2)
232 char * const *p1 = (char * const *) vp1;
233 char * const *p2 = (char * const *) vp2;
234 int n1 = strlen (*p1);
235 int n2 = strlen (*p2);
244 char **mounts = NULL;
245 int size = 0, alloc = 0;
246 char *p, *p2, *p3, *pend;
247 char matching[5 + sysroot_len];
249 r = command (&out, &err, "mount", NULL);
251 reply_with_error ("mount: %s", err);
259 /* Lines have the format:
260 * /dev/foo on /mountpoint type ...
262 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
266 pend = strchr (p, '\n');
272 p2 = strstr (p, matching);
275 p3 = p2 + strcspn (p2, " ");
277 if (add_string (&mounts, &size, &alloc, p2) == -1) {
287 qsort (mounts, size, sizeof (char *), compare_longest_first);
290 for (i = 0; i < size; ++i) {
291 r = command (NULL, &err, "umount", mounts[i], NULL);
293 reply_with_error ("umount: %s: %s", mounts[i], err);
295 free_stringslen (mounts, size);
301 free_stringslen (mounts, size);
303 /* We've unmounted root now, so ... */
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
314 do_mount_loop (const char *file, const char *mountpoint)
320 /* We have to prefix /sysroot on both the filename and the mountpoint. */
321 mp = sysroot_path (mountpoint);
323 reply_with_perror ("malloc");
327 buf = sysroot_path (file);
329 reply_with_perror ("malloc");
334 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
338 reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
346 /* Specialized calls mkmountpoint and rmmountpoint are really
347 * variations on mkdir and rmdir which do no checking and (in the
348 * mkmountpoint case) set the root_mounted flag.
351 do_mkmountpoint (const char *path)
355 /* NEED_ROOT (return -1); - we don't want this test for this call. */
356 ABS_PATH (path, return -1);
359 r = mkdir (path, 0777);
363 reply_with_perror ("mkmountpoint: %s", path);
367 /* Set the flag so that filesystems can be mounted here,
368 * not just on /sysroot.
376 do_rmmountpoint (const char *path)
385 reply_with_perror ("rmmountpoint: %s", path);