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>
32 /* You must mount something on "/" first before many operations.
33 * Hence we have an internal function which can test if something is
34 * mounted on *or under* the sysroot directory. (It has to be *or
35 * under* because of mkmountpoint and friends).
38 is_root_mounted (void)
43 fp = setmntent ("/etc/mtab", "r");
49 while ((m = getmntent (fp)) != NULL) {
50 /* Allow a mount directory like "/sysroot". */
51 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
56 /* Or allow a mount directory like "/sysroot/...". */
57 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/')
65 /* The "simple mount" call offers no complex options, you can just
66 * mount a device on a mountpoint. The variations like mount_ro,
67 * mount_options and mount_vfs let you set progressively more things.
69 * It's tempting to try a direct mount(2) syscall, but that doesn't
70 * do any autodetection, so we are better off calling out to
75 do_mount_vfs (const char *options, const char *vfstype,
76 const char *device, const char *mountpoint)
83 ABS_PATH (mountpoint, 0, return -1);
85 mp = sysroot_path (mountpoint);
87 reply_with_perror ("malloc");
91 /* Check the mountpoint exists and is a directory. */
92 if (stat (mp, &statbuf) == -1) {
93 reply_with_perror ("mount: %s", mountpoint);
97 if (!S_ISDIR (statbuf.st_mode)) {
98 reply_with_perror ("mount: %s: mount point is not a directory", mountpoint);
104 r = command (NULL, &error,
105 "mount", "-o", options, "-t", vfstype, device, mp, NULL);
107 r = command (NULL, &error,
108 "mount", "-o", options, device, mp, NULL);
111 reply_with_error ("%s on %s: %s", device, mountpoint, error);
120 do_mount (const char *device, const char *mountpoint)
122 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
126 do_mount_ro (const char *device, const char *mountpoint)
128 return do_mount_vfs ("ro", NULL, device, mountpoint);
132 do_mount_options (const char *options, const char *device,
133 const char *mountpoint)
135 return do_mount_vfs (options, NULL, device, mountpoint);
138 /* Again, use the external /bin/umount program, so that /etc/mtab
142 do_umount (const char *pathordevice)
149 is_dev = STREQLEN (pathordevice, "/dev/", 5);
150 buf = is_dev ? strdup (pathordevice)
151 : sysroot_path (pathordevice);
153 reply_with_perror ("malloc");
158 RESOLVE_DEVICE (buf, 0, { free (buf); return -1; });
160 r = command (NULL, &err, "umount", buf, NULL);
164 reply_with_error ("%s: %s", pathordevice, err);
175 mounts_or_mountpoints (int mp)
180 int size = 0, alloc = 0;
183 char matching[5 + sysroot_len];
186 r = command (&out, &err, "mount", NULL);
188 reply_with_error ("mount: %s", err);
196 /* Lines have the format:
197 * /dev/foo on /mountpoint type ...
199 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
203 pend = strchr (p, '\n');
209 p2 = strstr (p, matching);
212 if (add_string (&ret, &size, &alloc, p) == -1) {
217 p2 += 4 + sysroot_len; /* skip " on /sysroot" */
218 len = strcspn (p2, " ");
220 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
225 if (add_string (&ret, &size, &alloc, p2) == -1) {
237 if (add_string (&ret, &size, &alloc, NULL) == -1)
240 /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
241 for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
242 if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
244 r = lv_canonical (ret[i], &canonical);
253 /* Ignore the case where r == 0. This might happen where
254 * eg. a LUKS /dev/mapper device is mounted, but that won't
255 * correspond to any LV.
266 return mounts_or_mountpoints (0);
270 do_mountpoints (void)
272 return mounts_or_mountpoints (1);
275 /* Unmount everything mounted under /sysroot.
277 * We have to unmount in the correct order, so we sort the paths by
278 * longest first to ensure that child paths are unmounted by parent
281 * This call is more important than it appears at first, because it
282 * is widely used by both test and production code in order to
283 * get back to a known state (nothing mounted, everything synchronized).
286 compare_longest_first (const void *vp1, const void *vp2)
288 char * const *p1 = (char * const *) vp1;
289 char * const *p2 = (char * const *) vp2;
290 int n1 = strlen (*p1);
291 int n2 = strlen (*p2);
300 char **mounts = NULL;
301 int size = 0, alloc = 0;
302 char *p, *p2, *p3, *pend;
303 char matching[5 + sysroot_len];
305 r = command (&out, &err, "mount", NULL);
307 reply_with_error ("mount: %s", err);
315 /* Lines have the format:
316 * /dev/foo on /mountpoint type ...
318 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
322 pend = strchr (p, '\n');
328 p2 = strstr (p, matching);
331 p3 = p2 + strcspn (p2, " ");
333 if (add_string (&mounts, &size, &alloc, p2) == -1) {
343 qsort (mounts, size, sizeof (char *), compare_longest_first);
346 for (i = 0; i < size; ++i) {
347 r = command (NULL, &err, "umount", mounts[i], NULL);
349 reply_with_error ("umount: %s: %s", mounts[i], err);
351 free_stringslen (mounts, size);
357 free_stringslen (mounts, size);
362 /* Mount using the loopback device. You can't use the generic
363 * do_mount call for this because the first parameter isn't a
367 do_mount_loop (const char *file, const char *mountpoint)
373 /* We have to prefix /sysroot on both the filename and the mountpoint. */
374 mp = sysroot_path (mountpoint);
376 reply_with_perror ("malloc");
380 buf = sysroot_path (file);
382 reply_with_perror ("malloc");
387 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
391 reply_with_error ("%s on %s: %s", file, mountpoint, error);
399 /* Specialized calls mkmountpoint and rmmountpoint are really
400 * variations on mkdir and rmdir which do no checking of the
401 * is_root_mounted() flag.
404 do_mkmountpoint (const char *path)
408 /* NEED_ROOT (return -1); - we don't want this test for this call. */
409 ABS_PATH (path, 0, return -1);
412 r = mkdir (path, 0777);
416 reply_with_perror ("%s", path);
424 do_rmmountpoint (const char *path)
428 /* NEED_ROOT (return -1); - we don't want this test for this call. */
429 ABS_PATH (path, 0, return -1);
436 reply_with_perror ("%s", path);