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)
111 char *buf = (strncmp (pathordevice, "/dev/", 5) == 0
112 ? strdup (pathordevice)
113 : sysroot_path (pathordevice));
115 reply_with_perror ("malloc");
119 r = command (NULL, &err, "umount", buf, NULL);
123 reply_with_error ("umount: %s: %s", pathordevice, err);
130 /* update root_mounted? */
136 mounts_or_mountpoints (int mp)
141 int size = 0, alloc = 0;
144 char matching[5 + sysroot_len];
146 r = command (&out, &err, "mount", NULL);
148 reply_with_error ("mount: %s", err);
156 /* Lines have the format:
157 * /dev/foo on /mountpoint type ...
159 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
163 pend = strchr (p, '\n');
169 p2 = strstr (p, matching);
172 if (add_string (&ret, &size, &alloc, p) == -1) {
177 p2 += 4 + sysroot_len; /* skip " on /sysroot" */
178 len = strcspn (p2, " ");
180 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
185 if (add_string (&ret, &size, &alloc, p2) == -1) {
197 if (add_string (&ret, &size, &alloc, NULL) == -1)
206 return mounts_or_mountpoints (0);
210 do_mountpoints (void)
212 return mounts_or_mountpoints (1);
215 /* Unmount everything mounted under /sysroot.
217 * We have to unmount in the correct order, so we sort the paths by
218 * longest first to ensure that child paths are unmounted by parent
221 * This call is more important than it appears at first, because it
222 * is widely used by both test and production code in order to
223 * get back to a known state (nothing mounted, everything synchronized).
226 compare_longest_first (const void *vp1, const void *vp2)
228 char * const *p1 = (char * const *) vp1;
229 char * const *p2 = (char * const *) vp2;
230 int n1 = strlen (*p1);
231 int n2 = strlen (*p2);
240 char **mounts = NULL;
241 int size = 0, alloc = 0;
242 char *p, *p2, *p3, *pend;
243 char matching[5 + sysroot_len];
245 r = command (&out, &err, "mount", NULL);
247 reply_with_error ("mount: %s", err);
255 /* Lines have the format:
256 * /dev/foo on /mountpoint type ...
258 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
262 pend = strchr (p, '\n');
268 p2 = strstr (p, matching);
271 p3 = p2 + strcspn (p2, " ");
273 if (add_string (&mounts, &size, &alloc, p2) == -1) {
283 qsort (mounts, size, sizeof (char *), compare_longest_first);
286 for (i = 0; i < size; ++i) {
287 r = command (NULL, &err, "umount", mounts[i], NULL);
289 reply_with_error ("umount: %s: %s", mounts[i], err);
291 free_stringslen (mounts, size);
297 free_stringslen (mounts, size);
299 /* We've unmounted root now, so ... */
305 /* Mount using the loopback device. You can't use the generic
306 * do_mount call for this because the first parameter isn't a
310 do_mount_loop (const char *file, const char *mountpoint)
316 /* We have to prefix /sysroot on both the filename and the mountpoint. */
317 mp = sysroot_path (mountpoint);
319 reply_with_perror ("malloc");
323 buf = sysroot_path (file);
325 reply_with_perror ("malloc");
330 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
334 reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
342 /* Specialized calls mkmountpoint and rmmountpoint are really
343 * variations on mkdir and rmdir which do no checking and (in the
344 * mkmountpoint case) set the root_mounted flag.
347 do_mkmountpoint (const char *path)
351 /* NEED_ROOT (return -1); - we don't want this test for this call. */
352 ABS_PATH (path, return -1);
355 r = mkdir (path, 0777);
359 reply_with_perror ("mkmountpoint: %s", path);
363 /* Set the flag so that filesystems can be mounted here,
364 * not just on /sysroot.
372 do_rmmountpoint (const char *path)
381 reply_with_perror ("rmmountpoint: %s", path);