4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 Lawrence Livermore National Security, LLC.
30 #include <sys/mount.h>
31 #include <sys/mntent.h>
40 #define ZS_COMMENT 0x00000000 /* comment */
41 #define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */
43 libzfs_handle_t *g_zfs;
46 * Opportunistically convert a target string into a pool name. If the
47 * string does not represent a block device with a valid zfs label
48 * then it is passed through without modification.
51 parse_dataset(const char *target, char **dataset)
54 * Prior to util-linux 2.36.2, if a file or directory in the
55 * current working directory was named 'dataset' then mount(8)
56 * would prepend the current working directory to the dataset.
57 * Check for it and strip the prepended path when it is added.
60 if (getcwd(cwd, PATH_MAX) == NULL) {
64 int len = strlen(cwd);
65 if (strncmp(cwd, target, len) == 0)
68 /* Assume pool/dataset is more likely */
69 strlcpy(*dataset, target, PATH_MAX);
71 int fd = open(target, O_RDONLY | O_CLOEXEC);
76 if (zpool_read_label(fd, &cfg, NULL) == 0) {
77 const char *nm = NULL;
78 if (!nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &nm))
79 strlcpy(*dataset, nm, PATH_MAX);
88 * Update the mtab_* code to use the libmount library when it is commonly
89 * available otherwise fallback to legacy mode. The mount(8) utility will
90 * manage the lock file for us to prevent racing updates to /etc/mtab.
93 mtab_is_writeable(void)
98 error = lstat("/etc/mtab", &st);
99 if (error || S_ISLNK(st.st_mode))
102 fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
111 mtab_update(const char *dataset, const char *mntpoint, const char *type,
118 mnt.mnt_fsname = (char *)dataset;
119 mnt.mnt_dir = (char *)mntpoint;
120 mnt.mnt_type = (char *)type;
121 mnt.mnt_opts = (char *)(mntopts ?: "");
125 fp = setmntent("/etc/mtab", "a+e");
127 (void) fprintf(stderr, gettext(
128 "filesystem '%s' was mounted, but /etc/mtab "
129 "could not be opened due to error: %s\n"),
130 dataset, strerror(errno));
131 return (MOUNT_FILEIO);
134 error = addmntent(fp, &mnt);
136 (void) fprintf(stderr, gettext(
137 "filesystem '%s' was mounted, but /etc/mtab "
138 "could not be updated due to error: %s\n"),
139 dataset, strerror(errno));
140 return (MOUNT_FILEIO);
143 (void) endmntent(fp);
145 return (MOUNT_SUCCESS);
149 main(int argc, char **argv)
152 char prop[ZFS_MAXPROPLEN];
153 uint64_t zfs_version = 0;
154 char mntopts[MNT_LINE_MAX] = { '\0' };
155 char badopt[MNT_LINE_MAX] = { '\0' };
156 char mtabopt[MNT_LINE_MAX] = { '\0' };
157 char mntpoint[PATH_MAX];
158 char dataset[PATH_MAX], *pdataset = dataset;
159 unsigned long mntflags = 0, zfsflags = 0, remount = 0;
160 int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
163 (void) setlocale(LC_ALL, "");
164 (void) setlocale(LC_NUMERIC, "C");
165 (void) textdomain(TEXT_DOMAIN);
170 while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
185 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
190 (void) fprintf(stderr,
191 gettext("Invalid option '%c'\n"), optopt);
192 (void) fprintf(stderr, gettext("Usage: mount.zfs "
193 "[-sfnvh] [-o options] <dataset> <mountpoint>\n"));
194 return (MOUNT_USAGE);
201 /* check that we only have two arguments */
204 (void) fprintf(stderr, gettext("missing dataset "
207 (void) fprintf(stderr,
208 gettext("missing mountpoint argument\n"));
210 (void) fprintf(stderr, gettext("too many arguments\n"));
211 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
212 return (MOUNT_USAGE);
215 parse_dataset(argv[0], &pdataset);
217 /* canonicalize the mount point */
218 if (realpath(argv[1], mntpoint) == NULL) {
219 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
220 "mounted at '%s' due to canonicalization error: %s\n"),
221 dataset, argv[1], strerror(errno));
222 return (MOUNT_SYSERR);
225 /* validate mount options and set mntflags */
226 error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
231 (void) fprintf(stderr, gettext("filesystem '%s' "
232 "cannot be mounted due to a memory allocation "
233 "failure.\n"), dataset);
234 return (MOUNT_SYSERR);
236 (void) fprintf(stderr, gettext("filesystem '%s' "
237 "cannot be mounted due to invalid option "
238 "'%s'.\n"), dataset, badopt);
239 (void) fprintf(stderr, gettext("Use the '-s' option "
240 "to ignore the bad mount option.\n"));
241 return (MOUNT_USAGE);
243 (void) fprintf(stderr, gettext("filesystem '%s' "
244 "cannot be mounted due to internal error %d.\n"),
246 return (MOUNT_SOFTWARE);
250 if (mntflags & MS_REMOUNT) {
255 if (zfsflags & ZS_ZFSUTIL)
258 if ((g_zfs = libzfs_init()) == NULL) {
259 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
260 return (MOUNT_SYSERR);
263 /* try to open the dataset to access the mount point */
264 if ((zhp = zfs_open(g_zfs, dataset,
265 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
266 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
267 "mounted, unable to open the dataset\n"), dataset);
269 return (MOUNT_USAGE);
272 if (!zfsutil || sloppy ||
273 libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
274 zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
277 /* treat all snapshots as legacy mount points */
278 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
279 (void) strlcpy(prop, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
281 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
282 sizeof (prop), NULL, NULL, 0, B_FALSE);
285 * Fetch the max supported zfs version in case we get ENOTSUP
286 * back from the mount command, since we need the zfs handle
289 zfs_version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
290 if (zfs_version == 0) {
291 fprintf(stderr, gettext("unable to fetch "
292 "ZFS version for filesystem '%s'\n"), dataset);
295 return (MOUNT_SYSERR);
299 * Legacy mount points may only be mounted using 'mount', never using
300 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
301 * we differentiate the two cases using the 'zfsutil' mount option.
302 * This mount option should only be supplied by the 'zfs mount' util.
304 * The only exception to the above rule is '-o remount' which is
305 * always allowed for non-legacy datasets. This is done because when
306 * using zfs as your root file system both rc.sysinit/umountroot and
307 * systemd depend on 'mount -o remount <mountpoint>' to work.
309 if (zfsutil && (strcmp(prop, ZFS_MOUNTPOINT_LEGACY) == 0)) {
310 (void) fprintf(stderr, gettext(
311 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
312 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
313 "See zfs(8) for more information.\n"),
314 dataset, mntpoint, dataset, mntpoint);
317 return (MOUNT_USAGE);
320 if (!zfsutil && !(remount || fake) &&
321 strcmp(prop, ZFS_MOUNTPOINT_LEGACY)) {
322 (void) fprintf(stderr, gettext(
323 "filesystem '%s' cannot be mounted using 'mount'.\n"
324 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
325 "See zfs(8) for more information.\n"),
326 dataset, "legacy", dataset);
329 return (MOUNT_USAGE);
333 (void) fprintf(stdout, gettext("mount.zfs:\n"
334 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
335 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
336 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
337 dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
340 if (zfsutil && !sloppy &&
341 !libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
342 error = zfs_mount_at(zhp, mntopts, mntflags, mntpoint);
344 (void) fprintf(stderr, "zfs_mount_at() failed: "
345 "%s", libzfs_error_description(g_zfs));
348 return (MOUNT_SYSERR);
351 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
362 (void) fprintf(stderr, gettext("mount point "
363 "'%s' does not exist\n"), mntpoint);
364 return (MOUNT_SYSERR);
366 (void) fprintf(stderr, gettext("filesystem "
367 "'%s' is already mounted\n"), dataset);
370 if (zfs_version > ZPL_VERSION) {
371 (void) fprintf(stderr,
372 gettext("filesystem '%s' (v%d) is not "
373 "supported by this implementation of "
374 "ZFS (max v%d).\n"), dataset,
375 (int)zfs_version, (int)ZPL_VERSION);
377 (void) fprintf(stderr,
378 gettext("filesystem '%s' mount "
379 "failed for unknown reason.\n"), dataset);
381 return (MOUNT_SYSERR);
384 if (mntflags & MS_MANDLOCK) {
385 (void) fprintf(stderr, gettext("filesystem "
386 "'%s' has the 'nbmand=on' property set, "
387 "this mount\noption may be disabled in "
388 "your kernel. Use 'zfs set nbmand=off'\n"
389 "to disable this option and try to "
390 "mount the filesystem again.\n"), dataset);
391 return (MOUNT_SYSERR);
396 (void) fprintf(stderr, gettext("filesystem "
397 "'%s' can not be mounted: %s\n"), dataset,
399 return (MOUNT_USAGE);
403 if (!nomtab && mtab_is_writeable()) {
404 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
409 return (MOUNT_SUCCESS);