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.
29 /* You must mount something on "/" first, hence: */
32 /* The "simple mount" call offers no complex options, you can just
33 * mount a device on a mountpoint. The variations like mount_ro,
34 * mount_options and mount_vfs let you set progressively more things.
36 * It's tempting to try a direct mount(2) syscall, but that doesn't
37 * do any autodetection, so we are better off calling out to
42 do_mount_vfs (const char *options, const char *vfstype,
43 const char *device, const char *mountpoint)
49 is_root = strcmp (mountpoint, "/") == 0;
51 if (!root_mounted && !is_root) {
52 reply_with_error ("mount: you must mount something on / first");
56 len = strlen (mountpoint) + 9;
60 reply_with_perror ("malloc");
64 snprintf (mp, len, "/sysroot%s", mountpoint);
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);
73 reply_with_error ("mount: %s on %s: %s", device, mountpoint, error);
85 do_mount (const char *device, const char *mountpoint)
87 return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
91 do_mount_ro (const char *device, const char *mountpoint)
93 return do_mount_vfs ("ro", NULL, device, mountpoint);
97 do_mount_options (const char *options, const char *device,
98 const char *mountpoint)
100 return do_mount_vfs (options, NULL, device, mountpoint);
103 /* Again, use the external /bin/umount program, so that /etc/mtab
107 do_umount (const char *pathordevice)
109 int len, freeit = 0, r;
113 if (strncmp (pathordevice, "/dev/", 5) == 0)
114 buf = (char *) pathordevice;
116 len = strlen (pathordevice) + 9;
120 reply_with_perror ("malloc");
123 snprintf (buf, len, "/sysroot%s", pathordevice);
126 r = command (NULL, &err, "umount", buf, NULL);
127 if (freeit) free (buf);
129 reply_with_error ("umount: %s: %s", pathordevice, err);
136 /* update root_mounted? */
147 int size = 0, alloc = 0;
150 r = command (&out, &err, "mount", NULL);
152 reply_with_error ("mount: %s", err);
162 pend = strchr (p, '\n');
168 /* Lines have the format:
169 * /dev/foo on /mountpoint type ...
171 p2 = strstr (p, " on /sysroot");
174 if (add_string (&ret, &size, &alloc, p) == -1) {
185 if (add_string (&ret, &size, &alloc, NULL) == -1)
191 /* Unmount everything mounted under /sysroot.
193 * We have to unmount in the correct order, so we sort the paths by
194 * longest first to ensure that child paths are unmounted by parent
197 * This call is more important than it appears at first, because it
198 * is widely used by both test and production code in order to
199 * get back to a known state (nothing mounted, everything synchronized).
202 compare_longest_first (const void *vp1, const void *vp2)
204 char * const *p1 = (char * const *) vp1;
205 char * const *p2 = (char * const *) vp2;
206 int n1 = strlen (*p1);
207 int n2 = strlen (*p2);
216 char **mounts = NULL;
217 int size = 0, alloc = 0;
218 char *p, *p2, *p3, *pend;
220 r = command (&out, &err, "mount", NULL);
222 reply_with_error ("mount: %s", err);
232 pend = strchr (p, '\n');
238 /* Lines have the format:
239 * /dev/foo on /mountpoint type ...
241 p2 = strstr (p, " on /sysroot");
244 p3 = p2 + strcspn (p2, " ");
246 if (add_string (&mounts, &size, &alloc, p2) == -1) {
256 qsort (mounts, size, sizeof (char *), compare_longest_first);
259 for (i = 0; i < size; ++i) {
260 r = command (NULL, &err, "umount", mounts[i], NULL);
262 reply_with_error ("umount: %s: %s", mounts[i], err);
264 free_stringslen (mounts, size);
270 free_stringslen (mounts, size);
272 /* We've unmounted root now, so ... */