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 (char *options, char *vfstype,
45 char *device, char *mountpoint)
51 IS_DEVICE (device, -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 len = strlen (mountpoint) + 9;
64 reply_with_perror ("malloc");
68 snprintf (mp, len, "/sysroot%s", mountpoint);
71 r = command (NULL, &error,
72 "mount", "-o", options, "-t", vfstype, device, mp, NULL);
74 r = command (NULL, &error,
75 "mount", "-o", options, device, mp, NULL);
78 reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
90 do_mount (char *device, char *mountpoint)
92 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
96 do_mount_ro (char *device, char *mountpoint)
98 return do_mount_vfs ("ro", NULL, device, mountpoint);
102 do_mount_options (char *options, char *device,
105 return do_mount_vfs (options, NULL, device, mountpoint);
108 /* Again, use the external /bin/umount program, so that /etc/mtab
112 do_umount (char *pathordevice)
114 int len, freeit = 0, r;
118 if (strncmp (pathordevice, "/dev/", 5) == 0) {
122 len = strlen (pathordevice) + 9;
126 reply_with_perror ("malloc");
129 snprintf (buf, len, "/sysroot%s", pathordevice);
132 r = command (NULL, &err, "umount", buf, NULL);
133 if (freeit) free (buf);
135 reply_with_error ("umount: %s: %s", pathordevice, err);
142 /* update root_mounted? */
148 mounts_or_mountpoints (int mp)
153 int size = 0, alloc = 0;
157 r = command (&out, &err, "mount", NULL);
159 reply_with_error ("mount: %s", err);
169 pend = strchr (p, '\n');
175 /* Lines have the format:
176 * /dev/foo on /mountpoint type ...
178 p2 = strstr (p, " on /sysroot");
181 if (add_string (&ret, &size, &alloc, p) == -1) {
186 p2 += 12; /* skip " on /sysroot" */
187 len = strcspn (p2, " ");
189 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
194 if (add_string (&ret, &size, &alloc, p2) == -1) {
206 if (add_string (&ret, &size, &alloc, NULL) == -1)
215 return mounts_or_mountpoints (0);
219 do_mountpoints (void)
221 return mounts_or_mountpoints (1);
224 /* Unmount everything mounted under /sysroot.
226 * We have to unmount in the correct order, so we sort the paths by
227 * longest first to ensure that child paths are unmounted by parent
230 * This call is more important than it appears at first, because it
231 * is widely used by both test and production code in order to
232 * get back to a known state (nothing mounted, everything synchronized).
235 compare_longest_first (const void *vp1, const void *vp2)
237 char * const *p1 = (char * const *) vp1;
238 char * const *p2 = (char * const *) vp2;
239 int n1 = strlen (*p1);
240 int n2 = strlen (*p2);
249 char **mounts = NULL;
250 int size = 0, alloc = 0;
251 char *p, *p2, *p3, *pend;
253 r = command (&out, &err, "mount", NULL);
255 reply_with_error ("mount: %s", err);
265 pend = strchr (p, '\n');
271 /* Lines have the format:
272 * /dev/foo on /mountpoint type ...
274 p2 = strstr (p, " on /sysroot");
277 p3 = p2 + strcspn (p2, " ");
279 if (add_string (&mounts, &size, &alloc, p2) == -1) {
289 qsort (mounts, size, sizeof (char *), compare_longest_first);
292 for (i = 0; i < size; ++i) {
293 r = command (NULL, &err, "umount", mounts[i], NULL);
295 reply_with_error ("umount: %s: %s", mounts[i], err);
297 free_stringslen (mounts, size);
303 free_stringslen (mounts, size);
305 /* We've unmounted root now, so ... */
311 /* Mount using the loopback device. You can't use the generic
312 * do_mount call for this because the first parameter isn't a
316 do_mount_loop (char *file, char *mountpoint)
325 /* We have to prefix /sysroot on both the filename and the mountpoint. */
326 len = strlen (mountpoint) + 9;
329 reply_with_perror ("malloc");
332 snprintf (mp, len, "/sysroot%s", mountpoint);
334 len = strlen (file) + 9;
337 reply_with_perror ("malloc");
341 snprintf (buf, len, "/sysroot%s", file);
343 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
347 reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
355 /* Specialized calls mkmountpoint and rmmountpoint are really
356 * variations on mkdir and rmdir which do no checking and (in the
357 * mkmountpoint case) set the root_mounted flag.
360 do_mkmountpoint (char *path)
364 /* NEED_ROOT (-1); - we don't want this test for this call. */
368 r = mkdir (path, 0777);
372 reply_with_perror ("mkmountpoint: %s", path);
376 /* Set the flag so that filesystems can be mounted here,
377 * not just on /sysroot.
385 do_rmmountpoint (char *path)
397 reply_with_perror ("rmmountpoint: %s", path);