2 * mount_libmount.c -- Linux NFS [u]mount based on libmount
4 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
33 #include <libmount/libmount.h>
36 #include "mount_config.h"
38 #include "nfs_mount.h"
39 #include "nfs4_mount.h"
48 int nfs_mount_data_version;
54 #define FOREGROUND (0)
55 #define BACKGROUND (1)
58 * Store mount options to mtab (or /dev/.mount/utab), called from mount.nfs.
60 * Note that on systems without /etc/mtab the fs-specific options are not
61 * managed by libmount at all. We have to use "mount attributes" that are
62 * private for mount.<type> helpers.
64 static void store_mount_options(struct libmnt_fs *fs, const char *opts)
66 mnt_fs_set_fs_options(fs, opts); /* for mtab */
67 mnt_fs_set_attributes(fs, opts); /* for non-mtab systems */
71 * Retrieve mount options from mtab (or /dev/.mount/utab) called from umount.nfs.
73 * The result can passed to free().
75 char *retrieve_mount_options(struct libmnt_fs *fs)
82 opts = mnt_fs_get_attributes(fs); /* /dev/.mount/utab */
86 return mnt_fs_strdup_options(fs); /* /etc/mtab */
89 static int try_mount(struct libmnt_context *cxt, int bg)
93 char *src = NULL, *tgt = NULL, *type = NULL, *opts = NULL;
94 unsigned long flags = 0;
97 fs = mnt_context_get_fs(cxt);
99 /* libmount returns read-only pointers (const char)
100 * so, reallocate for nfsmount() functions.
102 if ((p = mnt_fs_get_source(fs))) /* spec */
104 if ((p = mnt_fs_get_target(fs))) /* mountpoint */
106 if ((p = mnt_fs_get_fstype(fs))) /* FS type */
108 if ((p = mnt_fs_get_fs_options(fs))) /* mount options */
111 mnt_context_get_mflags(cxt, &flags); /* mount(2) flags */
112 fake = mnt_context_is_fake(cxt);
115 ret = nfsmount_string(src, tgt, type, flags, &opts, fake, bg);
117 else if (strcmp(type, "nfs4") == 0)
118 ret = nfs4mount(src, tgt, flags, &opts, fake, bg);
120 ret = nfsmount(src, tgt, flags, &opts, fake, bg);
122 /* Store mount options if not called with mount --no-mtab */
123 if (!ret && !mnt_context_is_nomtab(cxt))
124 store_mount_options(fs, opts);
134 /* returns: error = -1, success = 0 , unknown = 1 */
135 static int is_vers4(struct libmnt_context *cxt)
137 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
138 struct libmnt_table *tb = NULL;
139 const char *src = mnt_context_get_source(cxt),
140 *tgt = mnt_context_get_target(cxt);
146 if (!mnt_fs_is_kernel(fs)) {
147 struct libmnt_table *tb = mnt_new_table_from_file("/proc/mounts");
151 fs = mnt_table_find_pair(tb, src, tgt, MNT_ITER_BACKWARD);
155 const char *type = mnt_fs_get_fstype(fs);
156 if (type && strcmp(type, "nfs4") == 0)
163 static int umount_main(struct libmnt_context *cxt, int argc, char **argv)
166 char *spec = NULL, *opts = NULL;
168 static const struct option longopts[] = {
169 { "force", 0, 0, 'f' },
170 { "help", 0, 0, 'h' },
171 { "no-mtab", 0, 0, 'n' },
172 { "verbose", 0, 0, 'v' },
173 { "read-only", 0, 0, 'r' },
174 { "lazy", 0, 0, 'l' },
175 { "types", 1, 0, 't' },
179 mnt_context_init_helper(cxt, MNT_ACT_UMOUNT, 0);
181 while ((c = getopt_long (argc, argv, "fvnrlh", longopts, NULL)) != -1) {
183 rc = mnt_context_helper_setopt(cxt, c, optarg);
184 if (rc == 0) /* valid option */
186 if (rc < 0) /* error (probably ENOMEM) */
188 /* rc==1 means unknow option */
194 spec = argv[optind++];
196 if (!spec || (*spec != '/' && strchr(spec,':') == NULL)) {
197 nfs_error(_("%s: no mount point provided"), progname);
201 if (mnt_context_set_target(cxt, spec))
203 if (mnt_context_set_fstype_pattern(cxt, "nfs,nfs4")) /* restrict filesystems */
206 /* read mtab/fstab, evaluate permissions, etc. */
207 rc = mnt_context_prepare_umount(cxt);
209 nfs_error(_("%s: failed to prepare umount: %s\n"),
210 progname, strerror(-rc));
214 opts = retrieve_mount_options(mnt_context_get_fs(cxt));
216 if (!mnt_context_is_lazy(cxt)) {
218 /* we have full FS description (e.g. from mtab or /proc) */
219 switch (is_vers4(cxt)) {
221 /* We ignore the error from nfs_umount23.
222 * If the actual umount succeeds (in del_mtab),
223 * we don't want to signal an error, as that
224 * could cause /sbin/mount to retry!
226 nfs_umount23(mnt_context_get_source(cxt), opts);
228 case 1: /* unknown */
234 /* strange, no entry in mtab or /proc not mounted */
235 nfs_umount23(spec, "tcp,v3");
238 rc = mnt_context_do_umount(cxt); /* call umount(2) syscall */
239 mnt_context_finalize_mount(cxt); /* mtab update */
241 if (rc && !mnt_context_get_status(cxt)) {
242 /* mnt_context_do_umount() returns errno if umount(2) failed */
243 umount_error(rc, spec);
254 static int mount_main(struct libmnt_context *cxt, int argc, char **argv)
257 struct libmnt_fs *fs;
258 char *spec = NULL, *mount_point = NULL, *opts = NULL;
260 static const struct option longopts[] = {
261 { "fake", 0, 0, 'f' },
262 { "help", 0, 0, 'h' },
263 { "no-mtab", 0, 0, 'n' },
264 { "read-only", 0, 0, 'r' },
266 { "verbose", 0, 0, 'v' },
267 { "version", 0, 0, 'V' },
268 { "read-write", 0, 0, 'w' },
270 { "options", 1, 0, 'o' },
271 { "sloppy", 0, 0, 's' },
275 mount_config_init(progname);
276 mnt_context_init_helper(cxt, MNT_ACT_MOUNT, 0);
278 while ((c = getopt_long(argc, argv, "fhnrVvwo:s", longopts, NULL)) != -1) {
280 rc = mnt_context_helper_setopt(cxt, c, optarg);
281 if (rc == 0) /* valid option */
283 if (rc < 0) /* error (probably ENOMEM) */
285 /* rc==1 means unknow option */
288 printf("%s: ("PACKAGE_STRING")\n", progname);
298 spec = argv[optind++];
300 mount_point = argv[optind++];
303 nfs_error(_("%s: no mount point provided"), progname);
307 nfs_error(_("%s: no mount spec provided"), progname);
311 if (geteuid() != 0) {
312 nfs_error(_("%s: not installed setuid - "
313 "\"user\" NFS mounts not supported."), progname);
317 verbose = mnt_context_is_verbose(cxt);
318 sloppy = mnt_context_is_sloppy(cxt);
319 nomtab = mnt_context_is_nomtab(cxt);
321 if (strcmp(progname, "mount.nfs4") == 0)
322 mnt_context_set_fstype(cxt, "nfs4");
324 mnt_context_set_fstype(cxt, "nfs"); /* default */
326 rc = mnt_context_set_source(cxt, spec);
328 mnt_context_set_target(cxt, mount_point);
330 nfs_error(_("%s: failed to set spec or mountpoint: %s"),
331 progname, strerror(errno));
335 mount_point = mnt_resolve_path(mount_point,
336 mnt_context_get_cache(cxt));
338 if (chk_mountpoint(mount_point))
341 * Concatenate mount options from the configuration file
343 fs = mnt_context_get_fs(cxt);
345 opts = mnt_fs_strdup_options(fs);
347 opts = mount_config_opts(spec, mount_point, opts);
348 mnt_fs_set_options(fs, opts);
351 rc = mnt_context_prepare_mount(cxt);
353 nfs_error(_("%s: failed to prepare mount: %s\n"),
354 progname, strerror(-rc));
358 rc = try_mount(cxt, FOREGROUND);
361 printf(_("%s: backgrounding \"%s\"\n"),
362 progname, mnt_context_get_source(cxt));
363 printf(_("%s: mount options: \"%s\"\n"),
369 nfs_error(_("%s: failed to start "
370 "background process: %s\n"),
371 progname, strerror(errno));
375 rc = try_mount(cxt, BACKGROUND);
378 printf(_("%s: giving up \"%s\"\n"),
379 progname, mnt_context_get_source(cxt));
382 mnt_context_set_syscall_status(cxt, rc == EX_SUCCESS ? 0 : -1);
383 mnt_context_finalize_mount(cxt); /* mtab update */
389 int main(int argc, char *argv[])
391 struct libmnt_context *cxt;
395 cxt = mnt_new_context();
397 nfs_error(_("Can't initilize libmount: %s"),
403 progname = basename(argv[0]);
404 nfs_mount_data_version = discover_nfs_mount_data_version(&string);
406 if(strncmp(progname, "umount", 6) == 0)
407 rc = umount_main(cxt, argc, argv);
409 rc = mount_main(cxt, argc, argv);
411 mnt_free_context(cxt);