]>
git.proxmox.com Git - mirror_zfs.git/blob - cmd/mount_zfs/mount_zfs.c
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>
34 #ifdef HAVE_LIBSELINUX
35 #include <selinux/selinux.h>
36 #endif /* HAVE_LIBSELINUX */
38 libzfs_handle_t
*g_zfs
;
40 typedef struct option_map
{
42 unsigned long mntmask
;
43 unsigned long zfsmask
;
46 static const option_map_t option_map
[] = {
47 /* Canonicalized filesystem independent options from mount(8) */
48 { MNTOPT_NOAUTO
, MS_COMMENT
, ZS_COMMENT
},
49 { MNTOPT_DEFAULTS
, MS_COMMENT
, ZS_COMMENT
},
50 { MNTOPT_NODEVICES
, MS_NODEV
, ZS_COMMENT
},
51 { MNTOPT_DIRSYNC
, MS_DIRSYNC
, ZS_COMMENT
},
52 { MNTOPT_NOEXEC
, MS_NOEXEC
, ZS_COMMENT
},
53 { MNTOPT_GROUP
, MS_GROUP
, ZS_COMMENT
},
54 { MNTOPT_NETDEV
, MS_COMMENT
, ZS_COMMENT
},
55 { MNTOPT_NOFAIL
, MS_COMMENT
, ZS_COMMENT
},
56 { MNTOPT_NOSUID
, MS_NOSUID
, ZS_COMMENT
},
57 { MNTOPT_OWNER
, MS_OWNER
, ZS_COMMENT
},
58 { MNTOPT_REMOUNT
, MS_REMOUNT
, ZS_COMMENT
},
59 { MNTOPT_RO
, MS_RDONLY
, ZS_COMMENT
},
60 { MNTOPT_RW
, MS_COMMENT
, ZS_COMMENT
},
61 { MNTOPT_SYNC
, MS_SYNCHRONOUS
, ZS_COMMENT
},
62 { MNTOPT_USER
, MS_USERS
, ZS_COMMENT
},
63 { MNTOPT_USERS
, MS_USERS
, ZS_COMMENT
},
64 /* acl flags passed with util-linux-2.24 mount command */
65 { MNTOPT_ACL
, MS_POSIXACL
, ZS_COMMENT
},
66 { MNTOPT_NOACL
, MS_COMMENT
, ZS_COMMENT
},
67 { MNTOPT_POSIXACL
, MS_POSIXACL
, ZS_COMMENT
},
69 { MNTOPT_NOATIME
, MS_NOATIME
, ZS_COMMENT
},
72 { MNTOPT_NODIRATIME
, MS_NODIRATIME
, ZS_COMMENT
},
75 { MNTOPT_RELATIME
, MS_RELATIME
, ZS_COMMENT
},
78 { MNTOPT_DFRATIME
, MS_STRICTATIME
, ZS_COMMENT
},
80 { MNTOPT_CONTEXT
, MS_COMMENT
, ZS_NOCONTEXT
},
81 { MNTOPT_NOCONTEXT
, MS_COMMENT
, ZS_NOCONTEXT
},
82 { MNTOPT_FSCONTEXT
, MS_COMMENT
, ZS_NOCONTEXT
},
83 { MNTOPT_DEFCONTEXT
, MS_COMMENT
, ZS_NOCONTEXT
},
84 { MNTOPT_ROOTCONTEXT
, MS_COMMENT
, ZS_NOCONTEXT
},
86 { MNTOPT_IVERSION
, MS_I_VERSION
, ZS_COMMENT
},
89 { MNTOPT_NBMAND
, MS_MANDLOCK
, ZS_COMMENT
},
91 /* Valid options not found in mount(8) */
92 { MNTOPT_BIND
, MS_BIND
, ZS_COMMENT
},
94 { MNTOPT_RBIND
, MS_BIND
|MS_REC
, ZS_COMMENT
},
96 { MNTOPT_COMMENT
, MS_COMMENT
, ZS_COMMENT
},
98 { MNTOPT_NOSUB
, MS_NOSUB
, ZS_COMMENT
},
101 { MNTOPT_QUIET
, MS_SILENT
, ZS_COMMENT
},
103 /* Custom zfs options */
104 { MNTOPT_XATTR
, MS_COMMENT
, ZS_COMMENT
},
105 { MNTOPT_NOXATTR
, MS_COMMENT
, ZS_COMMENT
},
106 { MNTOPT_ZFSUTIL
, MS_COMMENT
, ZS_ZFSUTIL
},
110 * Break the mount option in to a name/value pair. The name is
111 * validated against the option map and mount flags set accordingly.
114 parse_option(char *mntopt
, unsigned long *mntflags
,
115 unsigned long *zfsflags
, int sloppy
)
117 const option_map_t
*opt
;
118 char *ptr
, *name
, *value
= NULL
;
121 name
= strdup(mntopt
);
125 for (ptr
= name
; ptr
&& *ptr
; ptr
++) {
129 VERIFY3P(value
, !=, NULL
);
134 for (opt
= option_map
; opt
->name
!= NULL
; opt
++) {
135 if (strncmp(name
, opt
->name
, strlen(name
)) == 0) {
136 *mntflags
|= opt
->mntmask
;
137 *zfsflags
|= opt
->zfsmask
;
146 /* If required further process on the value may be done here */
152 * Translate the mount option string in to MS_* mount flags for the
153 * kernel vfs. When sloppy is non-zero unknown options will be ignored
154 * otherwise they are considered fatal are copied in to badopt.
157 parse_options(char *mntopts
, unsigned long *mntflags
, unsigned long *zfsflags
,
158 int sloppy
, char *badopt
, char *mtabopt
)
160 int error
= 0, quote
= 0, flag
= 0, count
= 0;
161 char *ptr
, *opt
, *opts
;
163 opts
= strdup(mntopts
);
171 * Scan through all mount options which must be comma delimited.
172 * We must be careful to notice regions which are double quoted
173 * and skip commas in these regions. Each option is then checked
174 * to determine if it is a known option.
176 for (ptr
= opts
; ptr
&& !flag
; ptr
++) {
189 if ((*ptr
== ',') || (*ptr
== '\0')) {
192 error
= parse_option(opt
, mntflags
, zfsflags
, sloppy
);
199 if (!(*mntflags
& MS_REMOUNT
) &&
200 !(*zfsflags
& ZS_ZFSUTIL
)) {
202 strlcat(mtabopt
, ",", MNT_LINE_MAX
);
204 strlcat(mtabopt
, opt
, MNT_LINE_MAX
);
218 * Return the pool/dataset to mount given the name passed to mount. This
219 * is expected to be of the form pool/dataset, however may also refer to
220 * a block device if that device contains a valid zfs label.
223 parse_dataset(char *dataset
)
226 struct stat64 statbuf
;
231 * We expect a pool/dataset to be provided, however if we're
232 * given a device which is a member of a zpool we attempt to
233 * extract the pool name stored in the label. Given the pool
234 * name we can mount the root dataset.
236 error
= stat64(dataset
, &statbuf
);
242 fd
= open(dataset
, O_RDONLY
);
246 error
= zpool_read_label(fd
, &config
);
251 error
= nvlist_lookup_string(config
,
252 ZPOOL_CONFIG_POOL_NAME
, &name
);
256 dataset
= strdup(name
);
263 * If a file or directory in your current working directory is
264 * named 'dataset' then mount(8) will prepend your current working
265 * directory to the dataset. There is no way to prevent this
266 * behavior so we simply check for it and strip the prepended
267 * patch when it is added.
269 if (getcwd(cwd
, PATH_MAX
) == NULL
)
274 /* Do not add one when cwd already ends in a trailing '/' */
275 if (strncmp(cwd
, dataset
, len
) == 0)
276 return (dataset
+ len
+ (cwd
[len
-1] != '/'));
282 * Update the mtab_* code to use the libmount library when it is commonly
283 * available otherwise fallback to legacy mode. The mount(8) utility will
284 * manage the lock file for us to prevent racing updates to /etc/mtab.
287 mtab_is_writeable(void)
292 error
= lstat(MNTTAB
, &st
);
293 if (error
|| S_ISLNK(st
.st_mode
))
296 fd
= open(MNTTAB
, O_RDWR
| O_CREAT
, 0644);
305 mtab_update(char *dataset
, char *mntpoint
, char *type
, char *mntopts
)
311 mnt
.mnt_fsname
= dataset
;
312 mnt
.mnt_dir
= mntpoint
;
314 mnt
.mnt_opts
= mntopts
? mntopts
: "";
318 fp
= setmntent(MNTTAB
, "a+");
320 (void) fprintf(stderr
, gettext(
321 "filesystem '%s' was mounted, but %s "
322 "could not be opened due to error %d\n"),
323 dataset
, MNTTAB
, errno
);
324 return (MOUNT_FILEIO
);
327 error
= addmntent(fp
, &mnt
);
329 (void) fprintf(stderr
, gettext(
330 "filesystem '%s' was mounted, but %s "
331 "could not be updated due to error %d\n"),
332 dataset
, MNTTAB
, errno
);
333 return (MOUNT_FILEIO
);
336 (void) endmntent(fp
);
338 return (MOUNT_SUCCESS
);
342 main(int argc
, char **argv
)
345 char legacy
[ZFS_MAXPROPLEN
];
346 char mntopts
[MNT_LINE_MAX
] = { '\0' };
347 char badopt
[MNT_LINE_MAX
] = { '\0' };
348 char mtabopt
[MNT_LINE_MAX
] = { '\0' };
349 char mntpoint
[PATH_MAX
];
351 unsigned long mntflags
= 0, zfsflags
= 0, remount
= 0;
352 int sloppy
= 0, fake
= 0, verbose
= 0, nomtab
= 0, zfsutil
= 0;
355 (void) setlocale(LC_ALL
, "");
356 (void) textdomain(TEXT_DOMAIN
);
361 while ((c
= getopt(argc
, argv
, "sfnvo:h?")) != -1) {
376 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
380 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
382 (void) fprintf(stderr
, gettext("Usage: mount.zfs "
383 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
384 return (MOUNT_USAGE
);
391 /* check that we only have two arguments */
394 (void) fprintf(stderr
, gettext("missing dataset "
397 (void) fprintf(stderr
,
398 gettext("missing mountpoint argument\n"));
400 (void) fprintf(stderr
, gettext("too many arguments\n"));
401 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
402 return (MOUNT_USAGE
);
405 dataset
= parse_dataset(argv
[0]);
407 /* canonicalize the mount point */
408 if (realpath(argv
[1], mntpoint
) == NULL
) {
409 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
410 "mounted at '%s' due to canonicalization error %d.\n"),
411 dataset
, argv
[1], errno
);
412 return (MOUNT_SYSERR
);
415 /* validate mount options and set mntflags */
416 error
= parse_options(mntopts
, &mntflags
, &zfsflags
, sloppy
,
421 (void) fprintf(stderr
, gettext("filesystem '%s' "
422 "cannot be mounted due to a memory allocation "
423 "failure.\n"), dataset
);
424 return (MOUNT_SYSERR
);
426 (void) fprintf(stderr
, gettext("filesystem '%s' "
427 "cannot be mounted due to invalid option "
428 "'%s'.\n"), dataset
, badopt
);
429 (void) fprintf(stderr
, gettext("Use the '-s' option "
430 "to ignore the bad mount option.\n"));
431 return (MOUNT_USAGE
);
433 (void) fprintf(stderr
, gettext("filesystem '%s' "
434 "cannot be mounted due to internal error %d.\n"),
436 return (MOUNT_SOFTWARE
);
440 #ifdef HAVE_LIBSELINUX
442 * Automatically add the default zfs context when selinux is enabled
443 * and the caller has not specified their own context. This must be
444 * done until zfs is added to the default selinux policy configuration
445 * as a known filesystem type which supports xattrs.
447 if (is_selinux_enabled() && !(zfsflags
& ZS_NOCONTEXT
)) {
448 (void) strlcat(mntopts
, ",context=\"system_u:"
449 "object_r:file_t:s0\"", sizeof (mntopts
));
450 (void) strlcat(mtabopt
, ",context=\"system_u:"
451 "object_r:file_t:s0\"", sizeof (mtabopt
));
453 #endif /* HAVE_LIBSELINUX */
457 (void) fprintf(stdout
, gettext("mount.zfs:\n"
458 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
459 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
460 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
461 dataset
, mntpoint
, mntflags
, zfsflags
, mntopts
, mtabopt
);
463 if (mntflags
& MS_REMOUNT
) {
468 if (zfsflags
& ZS_ZFSUTIL
)
471 if ((g_zfs
= libzfs_init()) == NULL
)
472 return (MOUNT_SYSERR
);
474 /* try to open the dataset to access the mount point */
475 if ((zhp
= zfs_open(g_zfs
, dataset
,
476 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_SNAPSHOT
)) == NULL
) {
477 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
478 "mounted, unable to open the dataset\n"), dataset
);
480 return (MOUNT_USAGE
);
483 /* treat all snapshots as legacy mount points */
484 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
)
485 (void) strlcpy(legacy
, ZFS_MOUNTPOINT_LEGACY
, ZFS_MAXPROPLEN
);
487 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, legacy
,
488 sizeof (legacy
), NULL
, NULL
, 0, B_FALSE
);
494 * Legacy mount points may only be mounted using 'mount', never using
495 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
496 * we differentiate the two cases using the 'zfsutil' mount option.
497 * This mount option should only be supplied by the 'zfs mount' util.
499 * The only exception to the above rule is '-o remount' which is
500 * always allowed for non-legacy datasets. This is done because when
501 * using zfs as your root file system both rc.sysinit/umountroot and
502 * systemd depend on 'mount -o remount <mountpoint>' to work.
504 if (zfsutil
&& (strcmp(legacy
, ZFS_MOUNTPOINT_LEGACY
) == 0)) {
505 (void) fprintf(stderr
, gettext(
506 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
507 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
508 "See zfs(8) for more information.\n"),
509 dataset
, mntpoint
, dataset
, mntpoint
);
510 return (MOUNT_USAGE
);
513 if (!zfsutil
&& !(remount
|| fake
) &&
514 strcmp(legacy
, ZFS_MOUNTPOINT_LEGACY
)) {
515 (void) fprintf(stderr
, gettext(
516 "filesystem '%s' cannot be mounted using 'mount'.\n"
517 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
518 "See zfs(8) for more information.\n"),
519 dataset
, "legacy", dataset
);
520 return (MOUNT_USAGE
);
524 error
= mount(dataset
, mntpoint
, MNTTYPE_ZFS
,
529 (void) fprintf(stderr
, gettext("mount point "
530 "'%s' does not exist\n"), mntpoint
);
531 return (MOUNT_SYSERR
);
533 (void) fprintf(stderr
, gettext("filesystem "
534 "'%s' is already mounted\n"), dataset
);
537 (void) fprintf(stderr
, gettext("filesystem "
538 "'%s' can not be mounted due to error "
539 "%d\n"), dataset
, errno
);
540 return (MOUNT_USAGE
);
545 if (!nomtab
&& mtab_is_writeable()) {
546 error
= mtab_update(dataset
, mntpoint
, MNTTYPE_ZFS
, mtabopt
);
551 return (MOUNT_SUCCESS
);