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);
124 do_mount (const char *device, const char *mountpoint)
126 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
130 do_mount_ro (const char *device, const char *mountpoint)
132 return do_mount_vfs ("ro", NULL, device, mountpoint);
136 do_mount_options (const char *options, const char *device,
137 const char *mountpoint)
139 return do_mount_vfs (options, NULL, device, mountpoint);
142 /* Again, use the external /bin/umount program, so that /etc/mtab
146 do_umount (const char *pathordevice)
153 is_dev = STREQLEN (pathordevice, "/dev/", 5);
154 buf = is_dev ? strdup (pathordevice)
155 : sysroot_path (pathordevice);
157 reply_with_perror ("malloc");
162 RESOLVE_DEVICE (buf, , { free (buf); return -1; });
164 r = command (NULL, &err, "umount", buf, NULL);
168 reply_with_error ("%s: %s", pathordevice, err);
178 /* Implement 'mounts' (mp==0) and 'mountpoints' (mp==1) calls. */
180 mounts_or_mountpoints (int mp)
185 int size = 0, alloc = 0;
189 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
190 * that requires custom parsing code.
192 fp = setmntent ("/proc/mounts", "r");
194 perror ("/proc/mounts");
198 while ((m = getmntent (fp)) != NULL) {
199 /* Allow a mount directory like "/sysroot". */
200 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
201 if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) {
207 add_string (&ret, &size, &alloc, "/") == -1)
210 /* Or allow a mount directory like "/sysroot/...". */
211 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
212 if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1)
215 add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1)
222 if (add_string (&ret, &size, &alloc, NULL) == -1)
225 /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
226 for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
227 if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
229 r = lv_canonical (ret[i], &canonical);
238 /* Ignore the case where r == 0. This might happen where
239 * eg. a LUKS /dev/mapper device is mounted, but that won't
240 * correspond to any LV.
251 return mounts_or_mountpoints (0);
255 do_mountpoints (void)
257 return mounts_or_mountpoints (1);
260 /* Unmount everything mounted under /sysroot.
262 * We have to unmount in the correct order, so we sort the paths by
263 * longest first to ensure that child paths are unmounted by parent
266 * This call is more important than it appears at first, because it
267 * is widely used by both test and production code in order to
268 * get back to a known state (nothing mounted, everything synchronized).
271 compare_longest_first (const void *vp1, const void *vp2)
273 char * const *p1 = (char * const *) vp1;
274 char * const *p2 = (char * const *) vp2;
275 int n1 = strlen (*p1);
276 int n2 = strlen (*p2);
285 char **mounts = NULL;
286 int size = 0, alloc = 0;
290 /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
291 * that requires custom parsing code.
293 fp = setmntent ("/proc/mounts", "r");
295 perror ("/proc/mounts");
299 while ((m = getmntent (fp)) != NULL) {
300 /* Allow a mount directory like "/sysroot". */
301 if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
302 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
307 /* Or allow a mount directory like "/sysroot/...". */
308 if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
309 if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
318 qsort (mounts, size, sizeof (char *), compare_longest_first);
321 for (i = 0; i < size; ++i) {
322 r = command (NULL, &err, "umount", mounts[i], NULL);
324 reply_with_error ("umount: %s: %s", mounts[i], err);
326 free_stringslen (mounts, size);
332 free_stringslen (mounts, size);
337 /* Mount using the loopback device. You can't use the generic
338 * do_mount call for this because the first parameter isn't a
342 do_mount_loop (const char *file, const char *mountpoint)
348 /* We have to prefix /sysroot on both the filename and the mountpoint. */
349 mp = sysroot_path (mountpoint);
351 reply_with_perror ("malloc");
355 buf = sysroot_path (file);
357 reply_with_perror ("malloc");
362 r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
366 reply_with_error ("%s on %s: %s", file, mountpoint, error);
375 /* Specialized calls mkmountpoint and rmmountpoint are really
376 * variations on mkdir and rmdir which do no checking of the
377 * is_root_mounted() flag.
380 do_mkmountpoint (const char *path)
384 /* NEED_ROOT (return -1); - we don't want this test for this call. */
385 ABS_PATH (path, , return -1);
388 r = mkdir (path, 0777);
392 reply_with_perror ("%s", path);
400 do_rmmountpoint (const char *path)
404 /* NEED_ROOT (return -1); - we don't want this test for this call. */
405 ABS_PATH (path, , return -1);
412 reply_with_perror ("%s", path);