]>
git.proxmox.com Git - mirror_zfs.git/blob - cmd/mount_zfs/mount_zfs.c
0e7810703e26d22de9a7a745adbbec30b32c29ef
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 * 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
)
53 /* Assume pool/dataset is more likely */
54 strlcpy(*dataset
, target
, PATH_MAX
);
56 int fd
= open(target
, O_RDONLY
| O_CLOEXEC
);
61 if (zpool_read_label(fd
, &cfg
, NULL
) == 0) {
63 if (!nvlist_lookup_string(cfg
, ZPOOL_CONFIG_POOL_NAME
, &nm
))
64 strlcpy(*dataset
, nm
, PATH_MAX
);
73 * Update the mtab_* code to use the libmount library when it is commonly
74 * available otherwise fallback to legacy mode. The mount(8) utility will
75 * manage the lock file for us to prevent racing updates to /etc/mtab.
78 mtab_is_writeable(void)
83 error
= lstat("/etc/mtab", &st
);
84 if (error
|| S_ISLNK(st
.st_mode
))
87 fd
= open("/etc/mtab", O_RDWR
| O_CREAT
, 0644);
96 mtab_update(char *dataset
, char *mntpoint
, char *type
, char *mntopts
)
102 mnt
.mnt_fsname
= dataset
;
103 mnt
.mnt_dir
= mntpoint
;
105 mnt
.mnt_opts
= mntopts
? mntopts
: "";
109 fp
= setmntent("/etc/mtab", "a+");
111 (void) fprintf(stderr
, gettext(
112 "filesystem '%s' was mounted, but /etc/mtab "
113 "could not be opened due to error %d\n"),
115 return (MOUNT_FILEIO
);
118 error
= addmntent(fp
, &mnt
);
120 (void) fprintf(stderr
, gettext(
121 "filesystem '%s' was mounted, but /etc/mtab "
122 "could not be updated due to error %d\n"),
124 return (MOUNT_FILEIO
);
127 (void) endmntent(fp
);
129 return (MOUNT_SUCCESS
);
133 main(int argc
, char **argv
)
136 char prop
[ZFS_MAXPROPLEN
];
137 uint64_t zfs_version
= 0;
138 char mntopts
[MNT_LINE_MAX
] = { '\0' };
139 char badopt
[MNT_LINE_MAX
] = { '\0' };
140 char mtabopt
[MNT_LINE_MAX
] = { '\0' };
141 char mntpoint
[PATH_MAX
];
142 char dataset
[PATH_MAX
], *pdataset
= dataset
;
143 unsigned long mntflags
= 0, zfsflags
= 0, remount
= 0;
144 int sloppy
= 0, fake
= 0, verbose
= 0, nomtab
= 0, zfsutil
= 0;
147 (void) setlocale(LC_ALL
, "");
148 (void) setlocale(LC_NUMERIC
, "C");
149 (void) textdomain(TEXT_DOMAIN
);
154 while ((c
= getopt_long(argc
, argv
, "sfnvo:h?", 0, 0)) != -1) {
169 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
173 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
175 (void) fprintf(stderr
, gettext("Usage: mount.zfs "
176 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
177 return (MOUNT_USAGE
);
184 /* check that we only have two arguments */
187 (void) fprintf(stderr
, gettext("missing dataset "
190 (void) fprintf(stderr
,
191 gettext("missing mountpoint argument\n"));
193 (void) fprintf(stderr
, gettext("too many arguments\n"));
194 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
195 return (MOUNT_USAGE
);
198 parse_dataset(argv
[0], &pdataset
);
200 /* canonicalize the mount point */
201 if (realpath(argv
[1], mntpoint
) == NULL
) {
202 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
203 "mounted at '%s' due to canonicalization error %d.\n"),
204 dataset
, argv
[1], errno
);
205 return (MOUNT_SYSERR
);
208 /* validate mount options and set mntflags */
209 error
= zfs_parse_mount_options(mntopts
, &mntflags
, &zfsflags
, sloppy
,
214 (void) fprintf(stderr
, gettext("filesystem '%s' "
215 "cannot be mounted due to a memory allocation "
216 "failure.\n"), dataset
);
217 return (MOUNT_SYSERR
);
219 (void) fprintf(stderr
, gettext("filesystem '%s' "
220 "cannot be mounted due to invalid option "
221 "'%s'.\n"), dataset
, badopt
);
222 (void) fprintf(stderr
, gettext("Use the '-s' option "
223 "to ignore the bad mount option.\n"));
224 return (MOUNT_USAGE
);
226 (void) fprintf(stderr
, gettext("filesystem '%s' "
227 "cannot be mounted due to internal error %d.\n"),
229 return (MOUNT_SOFTWARE
);
234 (void) fprintf(stdout
, gettext("mount.zfs:\n"
235 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
236 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
237 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
238 dataset
, mntpoint
, mntflags
, zfsflags
, mntopts
, mtabopt
);
240 if (mntflags
& MS_REMOUNT
) {
245 if (zfsflags
& ZS_ZFSUTIL
)
248 if ((g_zfs
= libzfs_init()) == NULL
) {
249 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
250 return (MOUNT_SYSERR
);
253 /* try to open the dataset to access the mount point */
254 if ((zhp
= zfs_open(g_zfs
, dataset
,
255 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_SNAPSHOT
)) == NULL
) {
256 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
257 "mounted, unable to open the dataset\n"), dataset
);
259 return (MOUNT_USAGE
);
262 zfs_adjust_mount_options(zhp
, mntpoint
, mntopts
, mtabopt
);
264 /* treat all snapshots as legacy mount points */
265 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
)
266 (void) strlcpy(prop
, ZFS_MOUNTPOINT_LEGACY
, ZFS_MAXPROPLEN
);
268 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, prop
,
269 sizeof (prop
), NULL
, NULL
, 0, B_FALSE
);
272 * Fetch the max supported zfs version in case we get ENOTSUP
273 * back from the mount command, since we need the zfs handle
276 zfs_version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
277 if (zfs_version
== 0) {
278 fprintf(stderr
, gettext("unable to fetch "
279 "ZFS version for filesystem '%s'\n"), dataset
);
280 return (MOUNT_SYSERR
);
287 * Legacy mount points may only be mounted using 'mount', never using
288 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
289 * we differentiate the two cases using the 'zfsutil' mount option.
290 * This mount option should only be supplied by the 'zfs mount' util.
292 * The only exception to the above rule is '-o remount' which is
293 * always allowed for non-legacy datasets. This is done because when
294 * using zfs as your root file system both rc.sysinit/umountroot and
295 * systemd depend on 'mount -o remount <mountpoint>' to work.
297 if (zfsutil
&& (strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
) == 0)) {
298 (void) fprintf(stderr
, gettext(
299 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
300 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
301 "See zfs(8) for more information.\n"),
302 dataset
, mntpoint
, dataset
, mntpoint
);
303 return (MOUNT_USAGE
);
306 if (!zfsutil
&& !(remount
|| fake
) &&
307 strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
)) {
308 (void) fprintf(stderr
, gettext(
309 "filesystem '%s' cannot be mounted using 'mount'.\n"
310 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
311 "See zfs(8) for more information.\n"),
312 dataset
, "legacy", dataset
);
313 return (MOUNT_USAGE
);
317 error
= mount(dataset
, mntpoint
, MNTTYPE_ZFS
,
324 (void) fprintf(stderr
, gettext("mount point "
325 "'%s' does not exist\n"), mntpoint
);
326 return (MOUNT_SYSERR
);
328 (void) fprintf(stderr
, gettext("filesystem "
329 "'%s' is already mounted\n"), dataset
);
332 if (zfs_version
> ZPL_VERSION
) {
333 (void) fprintf(stderr
,
334 gettext("filesystem '%s' (v%d) is not "
335 "supported by this implementation of "
336 "ZFS (max v%d).\n"), dataset
,
337 (int)zfs_version
, (int)ZPL_VERSION
);
339 (void) fprintf(stderr
,
340 gettext("filesystem '%s' mount "
341 "failed for unknown reason.\n"), dataset
);
343 return (MOUNT_SYSERR
);
346 if (mntflags
& MS_MANDLOCK
) {
347 (void) fprintf(stderr
, gettext("filesystem "
348 "'%s' has the 'nbmand=on' property set, "
349 "this mount\noption may be disabled in "
350 "your kernel. Use 'zfs set nbmand=off'\n"
351 "to disable this option and try to "
352 "mount the filesystem again.\n"), dataset
);
353 return (MOUNT_SYSERR
);
358 (void) fprintf(stderr
, gettext("filesystem "
359 "'%s' can not be mounted: %s\n"), dataset
,
361 return (MOUNT_USAGE
);
365 if (!nomtab
&& mtab_is_writeable()) {
366 error
= mtab_update(dataset
, mntpoint
, MNTTYPE_ZFS
, mtabopt
);
371 return (MOUNT_SUCCESS
);