2011-03-04 23:14:46 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* Copyright (c) 2011 Lawrence Livermore National Security, LLC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libintl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/mount.h>
|
2015-08-31 23:46:01 +00:00
|
|
|
#include <sys/mntent.h>
|
2011-03-04 23:14:46 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <libzfs.h>
|
2018-11-05 19:22:33 +00:00
|
|
|
#include <libzutil.h>
|
2012-06-08 20:30:39 +00:00
|
|
|
#include <locale.h>
|
2016-01-23 20:37:35 +00:00
|
|
|
#include <getopt.h>
|
2016-03-25 19:47:03 +00:00
|
|
|
#include <fcntl.h>
|
2018-02-16 01:53:18 +00:00
|
|
|
#include <errno.h>
|
2011-03-04 23:14:46 +00:00
|
|
|
|
2015-08-31 23:46:01 +00:00
|
|
|
#define ZS_COMMENT 0x00000000 /* comment */
|
|
|
|
#define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */
|
|
|
|
|
2011-03-04 23:14:46 +00:00
|
|
|
libzfs_handle_t *g_zfs;
|
|
|
|
|
|
|
|
/*
|
2020-12-10 05:24:26 +00:00
|
|
|
* Opportunistically convert a target string into a pool name. If the
|
|
|
|
* string does not represent a block device with a valid zfs label
|
|
|
|
* then it is passed through without modification.
|
2011-03-04 23:14:46 +00:00
|
|
|
*/
|
2020-11-10 23:50:44 +00:00
|
|
|
static void
|
|
|
|
parse_dataset(const char *target, char **dataset)
|
2011-03-04 23:14:46 +00:00
|
|
|
{
|
2021-01-19 19:57:31 +00:00
|
|
|
/*
|
|
|
|
* Prior to util-linux 2.36.2, if a file or directory in the
|
|
|
|
* current working directory was named 'dataset' then mount(8)
|
|
|
|
* would prepend the current working directory to the dataset.
|
|
|
|
* Check for it and strip the prepended path when it is added.
|
|
|
|
*/
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
if (getcwd(cwd, PATH_MAX) == NULL) {
|
|
|
|
perror("getcwd");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int len = strlen(cwd);
|
|
|
|
if (strncmp(cwd, target, len) == 0)
|
|
|
|
target += len;
|
|
|
|
|
2020-12-10 05:24:26 +00:00
|
|
|
/* Assume pool/dataset is more likely */
|
|
|
|
strlcpy(*dataset, target, PATH_MAX);
|
2020-11-10 23:50:44 +00:00
|
|
|
|
2020-12-10 05:24:26 +00:00
|
|
|
int fd = open(target, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2020-11-10 23:50:44 +00:00
|
|
|
|
2020-12-10 05:24:26 +00:00
|
|
|
nvlist_t *cfg = NULL;
|
|
|
|
if (zpool_read_label(fd, &cfg, NULL) == 0) {
|
|
|
|
char *nm = NULL;
|
|
|
|
if (!nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &nm))
|
|
|
|
strlcpy(*dataset, nm, PATH_MAX);
|
|
|
|
nvlist_free(cfg);
|
2020-11-10 23:50:44 +00:00
|
|
|
}
|
2020-12-10 05:24:26 +00:00
|
|
|
|
|
|
|
if (close(fd))
|
|
|
|
perror("close");
|
2011-03-04 23:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the mtab_* code to use the libmount library when it is commonly
|
|
|
|
* available otherwise fallback to legacy mode. The mount(8) utility will
|
|
|
|
* manage the lock file for us to prevent racing updates to /etc/mtab.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
mtab_is_writeable(void)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int error, fd;
|
|
|
|
|
2016-09-20 17:07:58 +00:00
|
|
|
error = lstat("/etc/mtab", &st);
|
2011-03-04 23:14:46 +00:00
|
|
|
if (error || S_ISLNK(st.st_mode))
|
|
|
|
return (0);
|
|
|
|
|
2016-09-20 17:07:58 +00:00
|
|
|
fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
|
2011-03-04 23:14:46 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
|
|
|
|
{
|
|
|
|
struct mntent mnt;
|
|
|
|
FILE *fp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mnt.mnt_fsname = dataset;
|
|
|
|
mnt.mnt_dir = mntpoint;
|
|
|
|
mnt.mnt_type = type;
|
|
|
|
mnt.mnt_opts = mntopts ? mntopts : "";
|
|
|
|
mnt.mnt_freq = 0;
|
|
|
|
mnt.mnt_passno = 0;
|
|
|
|
|
2016-09-20 17:07:58 +00:00
|
|
|
fp = setmntent("/etc/mtab", "a+");
|
2011-03-04 23:14:46 +00:00
|
|
|
if (!fp) {
|
|
|
|
(void) fprintf(stderr, gettext(
|
2016-09-20 17:07:58 +00:00
|
|
|
"filesystem '%s' was mounted, but /etc/mtab "
|
2020-12-10 05:24:59 +00:00
|
|
|
"could not be opened due to error: %s\n"),
|
|
|
|
dataset, strerror(errno));
|
2011-03-04 23:14:46 +00:00
|
|
|
return (MOUNT_FILEIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = addmntent(fp, &mnt);
|
|
|
|
if (error) {
|
|
|
|
(void) fprintf(stderr, gettext(
|
2016-09-20 17:07:58 +00:00
|
|
|
"filesystem '%s' was mounted, but /etc/mtab "
|
2020-12-10 05:24:59 +00:00
|
|
|
"could not be updated due to error: %s\n"),
|
|
|
|
dataset, strerror(errno));
|
2011-03-04 23:14:46 +00:00
|
|
|
return (MOUNT_FILEIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) endmntent(fp);
|
|
|
|
|
|
|
|
return (MOUNT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
zfs_handle_t *zhp;
|
2013-12-19 06:24:14 +00:00
|
|
|
char prop[ZFS_MAXPROPLEN];
|
2014-08-29 19:12:21 +00:00
|
|
|
uint64_t zfs_version = 0;
|
2011-03-04 23:14:46 +00:00
|
|
|
char mntopts[MNT_LINE_MAX] = { '\0' };
|
|
|
|
char badopt[MNT_LINE_MAX] = { '\0' };
|
2011-03-15 18:17:33 +00:00
|
|
|
char mtabopt[MNT_LINE_MAX] = { '\0' };
|
2012-02-13 03:44:20 +00:00
|
|
|
char mntpoint[PATH_MAX];
|
2020-11-10 23:50:44 +00:00
|
|
|
char dataset[PATH_MAX], *pdataset = dataset;
|
2012-07-30 22:38:02 +00:00
|
|
|
unsigned long mntflags = 0, zfsflags = 0, remount = 0;
|
2011-03-04 23:14:46 +00:00
|
|
|
int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
|
|
|
|
int error, c;
|
|
|
|
|
|
|
|
(void) setlocale(LC_ALL, "");
|
2020-09-09 17:14:04 +00:00
|
|
|
(void) setlocale(LC_NUMERIC, "C");
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) textdomain(TEXT_DOMAIN);
|
|
|
|
|
|
|
|
opterr = 0;
|
|
|
|
|
|
|
|
/* check options */
|
2016-01-23 20:37:35 +00:00
|
|
|
while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
|
2011-03-04 23:14:46 +00:00
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
sloppy = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fake = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nomtab = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
(void) strlcpy(mntopts, optarg, sizeof (mntopts));
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
(void) fprintf(stderr, gettext("Invalid option '%c'\n"),
|
|
|
|
optopt);
|
|
|
|
(void) fprintf(stderr, gettext("Usage: mount.zfs "
|
|
|
|
"[-sfnv] [-o options] <dataset> <mountpoint>\n"));
|
|
|
|
return (MOUNT_USAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* check that we only have two arguments */
|
|
|
|
if (argc != 2) {
|
|
|
|
if (argc == 0)
|
|
|
|
(void) fprintf(stderr, gettext("missing dataset "
|
|
|
|
"argument\n"));
|
|
|
|
else if (argc == 1)
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
gettext("missing mountpoint argument\n"));
|
|
|
|
else
|
|
|
|
(void) fprintf(stderr, gettext("too many arguments\n"));
|
|
|
|
(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
|
|
|
|
return (MOUNT_USAGE);
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:50:44 +00:00
|
|
|
parse_dataset(argv[0], &pdataset);
|
2012-02-13 03:44:20 +00:00
|
|
|
|
|
|
|
/* canonicalize the mount point */
|
|
|
|
if (realpath(argv[1], mntpoint) == NULL) {
|
|
|
|
(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
|
2020-12-10 05:24:59 +00:00
|
|
|
"mounted at '%s' due to canonicalization error: %s\n"),
|
|
|
|
dataset, argv[1], strerror(errno));
|
2012-02-13 03:44:20 +00:00
|
|
|
return (MOUNT_SYSERR);
|
|
|
|
}
|
2011-03-04 23:14:46 +00:00
|
|
|
|
|
|
|
/* validate mount options and set mntflags */
|
2020-05-21 01:02:41 +00:00
|
|
|
error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
|
2011-03-15 18:17:33 +00:00
|
|
|
badopt, mtabopt);
|
2011-03-04 23:14:46 +00:00
|
|
|
if (error) {
|
|
|
|
switch (error) {
|
|
|
|
case ENOMEM:
|
|
|
|
(void) fprintf(stderr, gettext("filesystem '%s' "
|
|
|
|
"cannot be mounted due to a memory allocation "
|
2011-03-15 18:17:33 +00:00
|
|
|
"failure.\n"), dataset);
|
2011-03-04 23:14:46 +00:00
|
|
|
return (MOUNT_SYSERR);
|
2011-03-15 18:17:33 +00:00
|
|
|
case ENOENT:
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) fprintf(stderr, gettext("filesystem '%s' "
|
2013-12-17 18:38:28 +00:00
|
|
|
"cannot be mounted due to invalid option "
|
2011-03-15 18:17:33 +00:00
|
|
|
"'%s'.\n"), dataset, badopt);
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) fprintf(stderr, gettext("Use the '-s' option "
|
|
|
|
"to ignore the bad mount option.\n"));
|
|
|
|
return (MOUNT_USAGE);
|
|
|
|
default:
|
|
|
|
(void) fprintf(stderr, gettext("filesystem '%s' "
|
2011-03-15 18:17:33 +00:00
|
|
|
"cannot be mounted due to internal error %d.\n"),
|
2011-03-04 23:14:46 +00:00
|
|
|
dataset, error);
|
|
|
|
return (MOUNT_SOFTWARE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
(void) fprintf(stdout, gettext("mount.zfs:\n"
|
|
|
|
" dataset: \"%s\"\n mountpoint: \"%s\"\n"
|
|
|
|
" mountflags: 0x%lx\n zfsflags: 0x%lx\n"
|
2011-03-15 18:17:33 +00:00
|
|
|
" mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
|
|
|
|
dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
|
2011-03-04 23:14:46 +00:00
|
|
|
|
2012-07-30 22:38:02 +00:00
|
|
|
if (mntflags & MS_REMOUNT) {
|
2011-03-04 23:14:46 +00:00
|
|
|
nomtab = 1;
|
2012-07-30 22:38:02 +00:00
|
|
|
remount = 1;
|
|
|
|
}
|
2011-03-15 16:34:56 +00:00
|
|
|
|
|
|
|
if (zfsflags & ZS_ZFSUTIL)
|
2011-03-04 23:14:46 +00:00
|
|
|
zfsutil = 1;
|
|
|
|
|
2015-05-20 21:39:52 +00:00
|
|
|
if ((g_zfs = libzfs_init()) == NULL) {
|
2019-09-18 16:05:57 +00:00
|
|
|
(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
|
2011-03-04 23:14:46 +00:00
|
|
|
return (MOUNT_SYSERR);
|
2015-05-20 21:39:52 +00:00
|
|
|
}
|
2011-03-04 23:14:46 +00:00
|
|
|
|
|
|
|
/* try to open the dataset to access the mount point */
|
2011-05-05 16:40:57 +00:00
|
|
|
if ((zhp = zfs_open(g_zfs, dataset,
|
|
|
|
ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
|
|
|
|
"mounted, unable to open the dataset\n"), dataset);
|
|
|
|
libzfs_fini(g_zfs);
|
|
|
|
return (MOUNT_USAGE);
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:02:41 +00:00
|
|
|
zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
|
2015-08-31 23:46:01 +00:00
|
|
|
|
2011-05-05 16:40:57 +00:00
|
|
|
/* treat all snapshots as legacy mount points */
|
|
|
|
if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
|
2013-12-19 06:24:14 +00:00
|
|
|
(void) strlcpy(prop, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
|
2011-05-05 16:40:57 +00:00
|
|
|
else
|
2013-12-19 06:24:14 +00:00
|
|
|
(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
|
|
|
|
sizeof (prop), NULL, NULL, 0, B_FALSE);
|
2011-03-04 23:14:46 +00:00
|
|
|
|
2014-08-29 19:12:21 +00:00
|
|
|
/*
|
|
|
|
* Fetch the max supported zfs version in case we get ENOTSUP
|
|
|
|
* back from the mount command, since we need the zfs handle
|
|
|
|
* to do so.
|
|
|
|
*/
|
|
|
|
zfs_version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
|
|
|
|
if (zfs_version == 0) {
|
|
|
|
fprintf(stderr, gettext("unable to fetch "
|
|
|
|
"ZFS version for filesystem '%s'\n"), dataset);
|
|
|
|
return (MOUNT_SYSERR);
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:14:46 +00:00
|
|
|
zfs_close(zhp);
|
|
|
|
libzfs_fini(g_zfs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Legacy mount points may only be mounted using 'mount', never using
|
|
|
|
* 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
|
|
|
|
* we differentiate the two cases using the 'zfsutil' mount option.
|
|
|
|
* This mount option should only be supplied by the 'zfs mount' util.
|
2011-03-15 16:34:56 +00:00
|
|
|
*
|
2012-07-30 22:38:02 +00:00
|
|
|
* The only exception to the above rule is '-o remount' which is
|
|
|
|
* always allowed for non-legacy datasets. This is done because when
|
|
|
|
* using zfs as your root file system both rc.sysinit/umountroot and
|
|
|
|
* systemd depend on 'mount -o remount <mountpoint>' to work.
|
2011-03-04 23:14:46 +00:00
|
|
|
*/
|
2013-12-19 06:24:14 +00:00
|
|
|
if (zfsutil && (strcmp(prop, ZFS_MOUNTPOINT_LEGACY) == 0)) {
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) fprintf(stderr, gettext(
|
|
|
|
"filesystem '%s' cannot be mounted using 'zfs mount'.\n"
|
|
|
|
"Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
|
|
|
|
"See zfs(8) for more information.\n"),
|
2013-11-01 19:26:11 +00:00
|
|
|
dataset, mntpoint, dataset, mntpoint);
|
2011-03-04 23:14:46 +00:00
|
|
|
return (MOUNT_USAGE);
|
|
|
|
}
|
|
|
|
|
2012-12-27 14:44:16 +00:00
|
|
|
if (!zfsutil && !(remount || fake) &&
|
2013-12-19 06:24:14 +00:00
|
|
|
strcmp(prop, ZFS_MOUNTPOINT_LEGACY)) {
|
2011-03-04 23:14:46 +00:00
|
|
|
(void) fprintf(stderr, gettext(
|
|
|
|
"filesystem '%s' cannot be mounted using 'mount'.\n"
|
|
|
|
"Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
|
|
|
|
"See zfs(8) for more information.\n"),
|
|
|
|
dataset, "legacy", dataset);
|
|
|
|
return (MOUNT_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fake) {
|
|
|
|
error = mount(dataset, mntpoint, MNTTYPE_ZFS,
|
|
|
|
mntflags, mntopts);
|
2014-08-29 19:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
(void) fprintf(stderr, gettext("mount point "
|
|
|
|
"'%s' does not exist\n"), mntpoint);
|
|
|
|
return (MOUNT_SYSERR);
|
|
|
|
case EBUSY:
|
|
|
|
(void) fprintf(stderr, gettext("filesystem "
|
|
|
|
"'%s' is already mounted\n"), dataset);
|
|
|
|
return (MOUNT_BUSY);
|
|
|
|
case ENOTSUP:
|
|
|
|
if (zfs_version > ZPL_VERSION) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
gettext("filesystem '%s' (v%d) is not "
|
|
|
|
"supported by this implementation of "
|
|
|
|
"ZFS (max v%d).\n"), dataset,
|
2016-12-12 18:46:26 +00:00
|
|
|
(int)zfs_version, (int)ZPL_VERSION);
|
2014-08-29 19:12:21 +00:00
|
|
|
} else {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
gettext("filesystem '%s' mount "
|
|
|
|
"failed for unknown reason.\n"), dataset);
|
2011-03-04 23:14:46 +00:00
|
|
|
}
|
2014-08-29 19:12:21 +00:00
|
|
|
return (MOUNT_SYSERR);
|
2017-06-07 17:59:44 +00:00
|
|
|
#ifdef MS_MANDLOCK
|
|
|
|
case EPERM:
|
|
|
|
if (mntflags & MS_MANDLOCK) {
|
|
|
|
(void) fprintf(stderr, gettext("filesystem "
|
|
|
|
"'%s' has the 'nbmand=on' property set, "
|
|
|
|
"this mount\noption may be disabled in "
|
|
|
|
"your kernel. Use 'zfs set nbmand=off'\n"
|
|
|
|
"to disable this option and try to "
|
|
|
|
"mount the filesystem again.\n"), dataset);
|
|
|
|
return (MOUNT_SYSERR);
|
|
|
|
}
|
|
|
|
/* fallthru */
|
|
|
|
#endif
|
2014-08-29 19:12:21 +00:00
|
|
|
default:
|
|
|
|
(void) fprintf(stderr, gettext("filesystem "
|
2017-06-07 17:59:44 +00:00
|
|
|
"'%s' can not be mounted: %s\n"), dataset,
|
|
|
|
strerror(errno));
|
2014-08-29 19:12:21 +00:00
|
|
|
return (MOUNT_USAGE);
|
2011-03-04 23:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nomtab && mtab_is_writeable()) {
|
2011-03-15 18:17:33 +00:00
|
|
|
error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
|
2011-03-04 23:14:46 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (MOUNT_SUCCESS);
|
|
|
|
}
|