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);
177 /* Implement 'mounts' (mp==0) and 'mountpoints' (mp==1) calls. */
179 mounts_or_mountpoints (int mp)
184 int size = 0, alloc = 0;
188 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
189 * that requires custom parsing code.
191 fp = setmntent ("/proc/mounts", "r");
193 perror ("/proc/mounts");
197 while ((m = getmntent (fp)) != NULL) {
198 /* Allow a mount directory like "/sysroot". */
199 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
200 if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) {
206 add_string (&ret, &size, &alloc, "/") == -1)
209 /* Or allow a mount directory like "/sysroot/...". */
210 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
211 if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1)
214 add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1)
221 if (add_string (&ret, &size, &alloc, NULL) == -1)
224 /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
225 for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
226 if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
228 r = lv_canonical (ret[i], &canonical);
237 /* Ignore the case where r == 0. This might happen where
238 * eg. a LUKS /dev/mapper device is mounted, but that won't
239 * correspond to any LV.
250 return mounts_or_mountpoints (0);
254 do_mountpoints (void)
256 return mounts_or_mountpoints (1);
259 /* Unmount everything mounted under /sysroot.
261 * We have to unmount in the correct order, so we sort the paths by
262 * longest first to ensure that child paths are unmounted by parent
265 * This call is more important than it appears at first, because it
266 * is widely used by both test and production code in order to
267 * get back to a known state (nothing mounted, everything synchronized).
270 compare_longest_first (const void *vp1, const void *vp2)
272 char * const *p1 = (char * const *) vp1;
273 char * const *p2 = (char * const *) vp2;
274 int n1 = strlen (*p1);
275 int n2 = strlen (*p2);
284 char **mounts = NULL;
285 int size = 0, alloc = 0;
289 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
290 * that requires custom parsing code.
292 fp = setmntent ("/proc/mounts", "r");
294 perror ("/proc/mounts");
298 while ((m = getmntent (fp)) != NULL) {
299 /* Allow a mount directory like "/sysroot". */
300 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
301 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
306 /* Or allow a mount directory like "/sysroot/...". */
307 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
308 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
317 qsort (mounts, size, sizeof (char *), compare_longest_first);
320 for (i = 0; i < size; ++i) {
321 r = command (NULL, &err, "umount", mounts[i], NULL);
323 reply_with_error ("umount: %s: %s", mounts[i], err);
325 free_stringslen (mounts, size);
331 free_stringslen (mounts, size);
336 /* Mount using the loopback device. You can't use the generic
337 * do_mount call for this because the first parameter isn't a
341 do_mount_loop (const char *file, const char *mountpoint)
347 /* We have to prefix /sysroot on both the filename and the mountpoint. */
348 mp = sysroot_path (mountpoint);
350 reply_with_perror ("malloc");
354 buf = sysroot_path (file);
356 reply_with_perror ("malloc");
361 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
365 reply_with_error ("%s on %s: %s", file, mountpoint, error);
373 /* Specialized calls mkmountpoint and rmmountpoint are really
374 * variations on mkdir and rmdir which do no checking of the
375 * is_root_mounted() flag.
378 do_mkmountpoint (const char *path)
382 /* NEED_ROOT (return -1); - we don't want this test for this call. */
383 ABS_PATH (path, , return -1);
386 r = mkdir (path, 0777);
390 reply_with_perror ("%s", path);
398 do_rmmountpoint (const char *path)
402 /* NEED_ROOT (return -1); - we don't want this test for this call. */
403 ABS_PATH (path, , return -1);
410 reply_with_perror ("%s", path);