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 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
44 * that requires custom parsing code.
46 fp = setmntent ("/proc/mounts", "r");
48 perror ("/proc/mounts");
52 while ((m = getmntent (fp)) != NULL) {
53 /* Allow a mount directory like "/sysroot". */
54 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
59 /* Or allow a mount directory like "/sysroot/...". */
60 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/')
68 /* The "simple mount" call offers no complex options, you can just
69 * mount a device on a mountpoint. The variations like mount_ro,
70 * mount_options and mount_vfs let you set progressively more things.
72 * It's tempting to try a direct mount(2) syscall, but that doesn't
73 * do any autodetection, so we are better off calling out to
78 do_mount_vfs (const char *options, const char *vfstype,
79 const char *device, const char *mountpoint)
86 ABS_PATH (mountpoint, , return -1);
88 mp = sysroot_path (mountpoint);
90 reply_with_perror ("malloc");
94 /* Check the mountpoint exists and is a directory. */
95 if (stat (mp, &statbuf) == -1) {
96 reply_with_perror ("mount: %s", mountpoint);
100 if (!S_ISDIR (statbuf.st_mode)) {
101 reply_with_perror ("mount: %s: mount point is not a directory", mountpoint);
107 r = command (NULL, &error,
108 "mount", "-o", options, "-t", vfstype, device, mp, NULL);
110 r = command (NULL, &error,
111 "mount", "-o", options, device, mp, NULL);
114 reply_with_error ("%s on %s: %s", device, mountpoint, error);
123 do_mount (const char *device, const char *mountpoint)
125 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
129 do_mount_ro (const char *device, const char *mountpoint)
131 return do_mount_vfs ("ro", NULL, device, mountpoint);
135 do_mount_options (const char *options, const char *device,
136 const char *mountpoint)
138 return do_mount_vfs (options, NULL, device, mountpoint);
141 /* Again, use the external /bin/umount program, so that /etc/mtab
145 do_umount (const char *pathordevice)
152 is_dev = STREQLEN (pathordevice, "/dev/", 5);
153 buf = is_dev ? strdup (pathordevice)
154 : sysroot_path (pathordevice);
156 reply_with_perror ("malloc");
161 RESOLVE_DEVICE (buf, , { free (buf); return -1; });
163 r = command (NULL, &err, "umount", buf, NULL);
167 reply_with_error ("%s: %s", pathordevice, err);
178 mounts_or_mountpoints (int mp)
183 int size = 0, alloc = 0;
186 char matching[5 + sysroot_len];
189 r = command (&out, &err, "mount", NULL);
191 reply_with_error ("mount: %s", err);
199 /* Lines have the format:
200 * /dev/foo on /mountpoint type ...
202 snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
206 pend = strchr (p, '\n');
212 p2 = strstr (p, matching);
215 if (add_string (&ret, &size, &alloc, p) == -1) {
220 p2 += 4 + sysroot_len; /* skip " on /sysroot" */
221 len = strcspn (p2, " ");
223 if (len == 0) /* .. just /sysroot, so we turn it into "/" */
228 if (add_string (&ret, &size, &alloc, p2) == -1) {
240 if (add_string (&ret, &size, &alloc, NULL) == -1)
243 /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
244 for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
245 if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
247 r = lv_canonical (ret[i], &canonical);
256 /* Ignore the case where r == 0. This might happen where
257 * eg. a LUKS /dev/mapper device is mounted, but that won't
258 * correspond to any LV.
269 return mounts_or_mountpoints (0);
273 do_mountpoints (void)
275 return mounts_or_mountpoints (1);
278 /* Unmount everything mounted under /sysroot.
280 * We have to unmount in the correct order, so we sort the paths by
281 * longest first to ensure that child paths are unmounted by parent
284 * This call is more important than it appears at first, because it
285 * is widely used by both test and production code in order to
286 * get back to a known state (nothing mounted, everything synchronized).
289 compare_longest_first (const void *vp1, const void *vp2)
291 char * const *p1 = (char * const *) vp1;
292 char * const *p2 = (char * const *) vp2;
293 int n1 = strlen (*p1);
294 int n2 = strlen (*p2);
303 char **mounts = NULL;
304 int size = 0, alloc = 0;
308 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
309 * that requires custom parsing code.
311 fp = setmntent ("/proc/mounts", "r");
313 perror ("/proc/mounts");
317 while ((m = getmntent (fp)) != NULL) {
318 /* Allow a mount directory like "/sysroot". */
319 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
320 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
325 /* Or allow a mount directory like "/sysroot/...". */
326 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
327 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
336 qsort (mounts, size, sizeof (char *), compare_longest_first);
339 for (i = 0; i < size; ++i) {
340 r = command (NULL, &err, "umount", mounts[i], NULL);
342 reply_with_error ("umount: %s: %s", mounts[i], err);
344 free_stringslen (mounts, size);
350 free_stringslen (mounts, size);
355 /* Mount using the loopback device. You can't use the generic
356 * do_mount call for this because the first parameter isn't a
360 do_mount_loop (const char *file, const char *mountpoint)
366 /* We have to prefix /sysroot on both the filename and the mountpoint. */
367 mp = sysroot_path (mountpoint);
369 reply_with_perror ("malloc");
373 buf = sysroot_path (file);
375 reply_with_perror ("malloc");
380 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
384 reply_with_error ("%s on %s: %s", file, mountpoint, error);
392 /* Specialized calls mkmountpoint and rmmountpoint are really
393 * variations on mkdir and rmdir which do no checking of the
394 * is_root_mounted() flag.
397 do_mkmountpoint (const char *path)
401 /* NEED_ROOT (return -1); - we don't want this test for this call. */
402 ABS_PATH (path, , return -1);
405 r = mkdir (path, 0777);
409 reply_with_perror ("%s", path);
417 do_rmmountpoint (const char *path)
421 /* NEED_ROOT (return -1); - we don't want this test for this call. */
422 ABS_PATH (path, , return -1);
429 reply_with_perror ("%s", path);