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 = STREQ (mountpoint, "/");
55 if (!root_mounted && !is_root) {
56 reply_with_error ("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 ("%s on %s: %s", device, mountpoint, error);
87 do_mount (const char *device, const char *mountpoint)
89 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
93 do_mount_ro (const char *device, const char *mountpoint)
95 return do_mount_vfs ("ro", NULL, device, mountpoint);
99 do_mount_options (const char *options, const char *device,
100 const char *mountpoint)
102 return do_mount_vfs (options, NULL, device, mountpoint);
105 /* Again, use the external /bin/umount program, so that /etc/mtab
109 do_umount (const char *pathordevice)
116 is_dev = STREQLEN (pathordevice, "/dev/", 5);
117 buf = is_dev ? strdup (pathordevice)
118 : sysroot_path (pathordevice);
120 reply_with_perror ("malloc");
125 RESOLVE_DEVICE (buf, , { free (buf); return -1; });
127 r = command (NULL, &err, "umount", buf, NULL);
131 reply_with_error ("%s: %s", pathordevice, err);
138 /* update root_mounted? */
144 mounts_or_mountpoints (int mp)
149 int size = 0, alloc = 0;
152 char matching[5 + sysroot_len];
155 r = command (&out, &err, "mount", NULL);
157 reply_with_error ("mount: %s", err);
165 /* Lines have the format:
166 * /dev/foo on /mountpoint type ...
168 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
172 pend = strchr (p, '\n');
178 p2 = strstr (p, matching);
181 if (add_string (&ret, &size, &alloc, p) == -1) {
186 p2 += 4 + sysroot_len; /* 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)
209 /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
210 for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
211 if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
213 r = lv_canonical (ret[i], &canonical);
222 /* Ignore the case where r == 0. This might happen where
223 * eg. a LUKS /dev/mapper device is mounted, but that won't
224 * correspond to any LV.
235 return mounts_or_mountpoints (0);
239 do_mountpoints (void)
241 return mounts_or_mountpoints (1);
244 /* Unmount everything mounted under /sysroot.
246 * We have to unmount in the correct order, so we sort the paths by
247 * longest first to ensure that child paths are unmounted by parent
250 * This call is more important than it appears at first, because it
251 * is widely used by both test and production code in order to
252 * get back to a known state (nothing mounted, everything synchronized).
255 compare_longest_first (const void *vp1, const void *vp2)
257 char * const *p1 = (char * const *) vp1;
258 char * const *p2 = (char * const *) vp2;
259 int n1 = strlen (*p1);
260 int n2 = strlen (*p2);
269 char **mounts = NULL;
270 int size = 0, alloc = 0;
271 char *p, *p2, *p3, *pend;
272 char matching[5 + sysroot_len];
274 r = command (&out, &err, "mount", NULL);
276 reply_with_error ("mount: %s", err);
284 /* Lines have the format:
285 * /dev/foo on /mountpoint type ...
287 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
291 pend = strchr (p, '\n');
297 p2 = strstr (p, matching);
300 p3 = p2 + strcspn (p2, " ");
302 if (add_string (&mounts, &size, &alloc, p2) == -1) {
312 qsort (mounts, size, sizeof (char *), compare_longest_first);
315 for (i = 0; i < size; ++i) {
316 r = command (NULL, &err, "umount", mounts[i], NULL);
318 reply_with_error ("umount: %s: %s", mounts[i], err);
320 free_stringslen (mounts, size);
326 free_stringslen (mounts, size);
328 /* We've unmounted root now, so ... */
334 /* Mount using the loopback device. You can't use the generic
335 * do_mount call for this because the first parameter isn't a
339 do_mount_loop (const char *file, const char *mountpoint)
345 /* We have to prefix /sysroot on both the filename and the mountpoint. */
346 mp = sysroot_path (mountpoint);
348 reply_with_perror ("malloc");
352 buf = sysroot_path (file);
354 reply_with_perror ("malloc");
359 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
363 reply_with_error ("%s on %s: %s", file, mountpoint, error);
372 /* Specialized calls mkmountpoint and rmmountpoint are really
373 * variations on mkdir and rmdir which do no checking and (in the
374 * mkmountpoint case) set the root_mounted flag.
377 do_mkmountpoint (const char *path)
381 /* NEED_ROOT (return -1); - we don't want this test for this call. */
382 ABS_PATH (path, , return -1);
385 r = mkdir (path, 0777);
389 reply_with_perror ("%s", path);
393 /* Set the flag so that filesystems can be mounted here,
394 * not just on /sysroot.
402 do_rmmountpoint (const char *path)
406 /* NEED_ROOT (return -1); - we don't want this test for this call. */
407 ABS_PATH (path, , return -1);
414 reply_with_perror ("%s", path);