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 ABS_PATH (mountpoint, return -1);
53 is_root = strcmp (mountpoint, "/") == 0;
55 if (!root_mounted && !is_root) {
56 reply_with_error ("mount: you must mount something on / first");
60 mp = sysroot_path (mountpoint);
62 reply_with_perror ("malloc");
67 r = command (NULL, &error,
68 "mount", "-o", options, "-t", vfstype, device, mp, NULL);
70 r = command (NULL, &error,
71 "mount", "-o", options, device, mp, NULL);
74 reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
86 do_mount (const char *device, const char *mountpoint)
88 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
92 do_mount_ro (const char *device, const char *mountpoint)
94 return do_mount_vfs ("ro", NULL, device, mountpoint);
98 do_mount_options (const char *options, const char *device,
99 const char *mountpoint)
101 return do_mount_vfs (options, NULL, device, mountpoint);
104 /* Again, use the external /bin/umount program, so that /etc/mtab
108 do_umount (const char *pathordevice)
113 char *buf = (strncmp (pathordevice, "/dev/", 5) == 0
114 ? strdup (pathordevice)
115 : sysroot_path (pathordevice));
117 reply_with_perror ("malloc");
121 r = command (NULL, &err, "umount", buf, NULL);
125 reply_with_error ("umount: %s: %s", pathordevice, err);
132 /* update root_mounted? */
138 mounts_or_mountpoints (int mp)
143 int size = 0, alloc = 0;
146 char matching[5 + sysroot_len];
148 r = command (&out, &err, "mount", NULL);
150 reply_with_error ("mount: %s", err);
158 /* Lines have the format:
159 * /dev/foo on /mountpoint type ...
161 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
165 pend = strchr (p, '\n');
171 p2 = strstr (p, matching);
174 if (add_string (&ret, &size, &alloc, p) == -1) {
179 p2 += 4 + sysroot_len; /* skip " on /sysroot" */
180 len = strcspn (p2, " ");
182 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
187 if (add_string (&ret, &size, &alloc, p2) == -1) {
199 if (add_string (&ret, &size, &alloc, NULL) == -1)
208 return mounts_or_mountpoints (0);
212 do_mountpoints (void)
214 return mounts_or_mountpoints (1);
217 /* Unmount everything mounted under /sysroot.
219 * We have to unmount in the correct order, so we sort the paths by
220 * longest first to ensure that child paths are unmounted by parent
223 * This call is more important than it appears at first, because it
224 * is widely used by both test and production code in order to
225 * get back to a known state (nothing mounted, everything synchronized).
228 compare_longest_first (const void *vp1, const void *vp2)
230 char * const *p1 = (char * const *) vp1;
231 char * const *p2 = (char * const *) vp2;
232 int n1 = strlen (*p1);
233 int n2 = strlen (*p2);
242 char **mounts = NULL;
243 int size = 0, alloc = 0;
244 char *p, *p2, *p3, *pend;
245 char matching[5 + sysroot_len];
247 r = command (&out, &err, "mount", NULL);
249 reply_with_error ("mount: %s", err);
257 /* Lines have the format:
258 * /dev/foo on /mountpoint type ...
260 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
264 pend = strchr (p, '\n');
270 p2 = strstr (p, matching);
273 p3 = p2 + strcspn (p2, " ");
275 if (add_string (&mounts, &size, &alloc, p2) == -1) {
285 qsort (mounts, size, sizeof (char *), compare_longest_first);
288 for (i = 0; i < size; ++i) {
289 r = command (NULL, &err, "umount", mounts[i], NULL);
291 reply_with_error ("umount: %s: %s", mounts[i], err);
293 free_stringslen (mounts, size);
299 free_stringslen (mounts, size);
301 /* We've unmounted root now, so ... */
307 /* Mount using the loopback device. You can't use the generic
308 * do_mount call for this because the first parameter isn't a
312 do_mount_loop (const char *file, const char *mountpoint)
318 /* We have to prefix /sysroot on both the filename and the mountpoint. */
319 mp = sysroot_path (mountpoint);
321 reply_with_perror ("malloc");
325 buf = sysroot_path (file);
327 reply_with_perror ("malloc");
332 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
336 reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
344 /* Specialized calls mkmountpoint and rmmountpoint are really
345 * variations on mkdir and rmdir which do no checking and (in the
346 * mkmountpoint case) set the root_mounted flag.
349 do_mkmountpoint (const char *path)
353 /* NEED_ROOT (return -1); - we don't want this test for this call. */
354 ABS_PATH (path, return -1);
357 r = mkdir (path, 0777);
361 reply_with_perror ("mkmountpoint: %s", path);
365 /* Set the flag so that filesystems can be mounted here,
366 * not just on /sysroot.
374 do_rmmountpoint (const char *path)
378 /* NEED_ROOT (return -1); - we don't want this test for this call. */
379 ABS_PATH (path, return -1);
386 reply_with_perror ("rmmountpoint: %s", path);