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 http://www.opensolaris.org/os/licensing.
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 * Return the pool/dataset to mount given the name passed to mount. This
47 * is expected to be of the form pool/dataset, however may also refer to
48 * a block device if that device contains a valid zfs label.
51 parse_dataset(char *dataset)
54 struct stat64 statbuf;
59 * We expect a pool/dataset to be provided, however if we're
60 * given a device which is a member of a zpool we attempt to
61 * extract the pool name stored in the label. Given the pool
62 * name we can mount the root dataset.
64 error = stat64(dataset, &statbuf);
70 fd = open(dataset, O_RDONLY);
74 error = zpool_read_label(fd, &config, NULL);
79 error = nvlist_lookup_string(config,
80 ZPOOL_CONFIG_POOL_NAME, &name);
84 dataset = strdup(name);
91 * If a file or directory in your current working directory is
92 * named 'dataset' then mount(8) will prepend your current working
93 * directory to the dataset. There is no way to prevent this
94 * behavior so we simply check for it and strip the prepended
95 * patch when it is added.
97 if (getcwd(cwd, PATH_MAX) == NULL)
102 /* Do not add one when cwd already ends in a trailing '/' */
103 if (strncmp(cwd, dataset, len) == 0)
104 return (dataset + len + (cwd[len-1] != '/'));
110 * Update the mtab_* code to use the libmount library when it is commonly
111 * available otherwise fallback to legacy mode. The mount(8) utility will
112 * manage the lock file for us to prevent racing updates to /etc/mtab.
115 mtab_is_writeable(void)
120 error = lstat("/etc/mtab", &st);
121 if (error || S_ISLNK(st.st_mode))
124 fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
133 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
139 mnt.mnt_fsname = dataset;
140 mnt.mnt_dir = mntpoint;
142 mnt.mnt_opts = mntopts ? mntopts : "";
146 fp = setmntent("/etc/mtab", "a+");
148 (void) fprintf(stderr, gettext(
149 "filesystem '%s' was mounted, but /etc/mtab "
150 "could not be opened due to error %d\n"),
152 return (MOUNT_FILEIO);
155 error = addmntent(fp, &mnt);
157 (void) fprintf(stderr, gettext(
158 "filesystem '%s' was mounted, but /etc/mtab "
159 "could not be updated due to error %d\n"),
161 return (MOUNT_FILEIO);
164 (void) endmntent(fp);
166 return (MOUNT_SUCCESS);
170 main(int argc, char **argv)
173 char prop[ZFS_MAXPROPLEN];
174 uint64_t zfs_version = 0;
175 char mntopts[MNT_LINE_MAX] = { '\0' };
176 char badopt[MNT_LINE_MAX] = { '\0' };
177 char mtabopt[MNT_LINE_MAX] = { '\0' };
178 char mntpoint[PATH_MAX];
180 unsigned long mntflags = 0, zfsflags = 0, remount = 0;
181 int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
184 (void) setlocale(LC_ALL, "");
185 (void) setlocale(LC_NUMERIC, "C");
186 (void) textdomain(TEXT_DOMAIN);
191 while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
206 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
210 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
212 (void) fprintf(stderr, gettext("Usage: mount.zfs "
213 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
214 return (MOUNT_USAGE);
221 /* check that we only have two arguments */
224 (void) fprintf(stderr, gettext("missing dataset "
227 (void) fprintf(stderr,
228 gettext("missing mountpoint argument\n"));
230 (void) fprintf(stderr, gettext("too many arguments\n"));
231 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
232 return (MOUNT_USAGE);
235 dataset = parse_dataset(argv[0]);
237 /* canonicalize the mount point */
238 if (realpath(argv[1], mntpoint) == NULL) {
239 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
240 "mounted at '%s' due to canonicalization error %d.\n"),
241 dataset, argv[1], errno);
242 return (MOUNT_SYSERR);
245 /* validate mount options and set mntflags */
246 error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
251 (void) fprintf(stderr, gettext("filesystem '%s' "
252 "cannot be mounted due to a memory allocation "
253 "failure.\n"), dataset);
254 return (MOUNT_SYSERR);
256 (void) fprintf(stderr, gettext("filesystem '%s' "
257 "cannot be mounted due to invalid option "
258 "'%s'.\n"), dataset, badopt);
259 (void) fprintf(stderr, gettext("Use the '-s' option "
260 "to ignore the bad mount option.\n"));
261 return (MOUNT_USAGE);
263 (void) fprintf(stderr, gettext("filesystem '%s' "
264 "cannot be mounted due to internal error %d.\n"),
266 return (MOUNT_SOFTWARE);
271 (void) fprintf(stdout, gettext("mount.zfs:\n"
272 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
273 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
274 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
275 dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
277 if (mntflags & MS_REMOUNT) {
282 if (zfsflags & ZS_ZFSUTIL)
285 if ((g_zfs = libzfs_init()) == NULL) {
286 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
287 return (MOUNT_SYSERR);
290 /* try to open the dataset to access the mount point */
291 if ((zhp = zfs_open(g_zfs, dataset,
292 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
293 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
294 "mounted, unable to open the dataset\n"), dataset);
296 return (MOUNT_USAGE);
299 zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
301 /* treat all snapshots as legacy mount points */
302 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
303 (void) strlcpy(prop, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
305 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
306 sizeof (prop), NULL, NULL, 0, B_FALSE);
309 * Fetch the max supported zfs version in case we get ENOTSUP
310 * back from the mount command, since we need the zfs handle
313 zfs_version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
314 if (zfs_version == 0) {
315 fprintf(stderr, gettext("unable to fetch "
316 "ZFS version for filesystem '%s'\n"), dataset);
317 return (MOUNT_SYSERR);
324 * Legacy mount points may only be mounted using 'mount', never using
325 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
326 * we differentiate the two cases using the 'zfsutil' mount option.
327 * This mount option should only be supplied by the 'zfs mount' util.
329 * The only exception to the above rule is '-o remount' which is
330 * always allowed for non-legacy datasets. This is done because when
331 * using zfs as your root file system both rc.sysinit/umountroot and
332 * systemd depend on 'mount -o remount <mountpoint>' to work.
334 if (zfsutil && (strcmp(prop, ZFS_MOUNTPOINT_LEGACY) == 0)) {
335 (void) fprintf(stderr, gettext(
336 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
337 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
338 "See zfs(8) for more information.\n"),
339 dataset, mntpoint, dataset, mntpoint);
340 return (MOUNT_USAGE);
343 if (!zfsutil && !(remount || fake) &&
344 strcmp(prop, ZFS_MOUNTPOINT_LEGACY)) {
345 (void) fprintf(stderr, gettext(
346 "filesystem '%s' cannot be mounted using 'mount'.\n"
347 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
348 "See zfs(8) for more information.\n"),
349 dataset, "legacy", dataset);
350 return (MOUNT_USAGE);
354 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
361 (void) fprintf(stderr, gettext("mount point "
362 "'%s' does not exist\n"), mntpoint);
363 return (MOUNT_SYSERR);
365 (void) fprintf(stderr, gettext("filesystem "
366 "'%s' is already mounted\n"), dataset);
369 if (zfs_version > ZPL_VERSION) {
370 (void) fprintf(stderr,
371 gettext("filesystem '%s' (v%d) is not "
372 "supported by this implementation of "
373 "ZFS (max v%d).\n"), dataset,
374 (int)zfs_version, (int)ZPL_VERSION);
376 (void) fprintf(stderr,
377 gettext("filesystem '%s' mount "
378 "failed for unknown reason.\n"), dataset);
380 return (MOUNT_SYSERR);
383 if (mntflags & MS_MANDLOCK) {
384 (void) fprintf(stderr, gettext("filesystem "
385 "'%s' has the 'nbmand=on' property set, "
386 "this mount\noption may be disabled in "
387 "your kernel. Use 'zfs set nbmand=off'\n"
388 "to disable this option and try to "
389 "mount the filesystem again.\n"), dataset);
390 return (MOUNT_SYSERR);
395 (void) fprintf(stderr, gettext("filesystem "
396 "'%s' can not be mounted: %s\n"), dataset,
398 return (MOUNT_USAGE);
402 if (!nomtab && mtab_is_writeable()) {
403 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
408 return (MOUNT_SUCCESS);