]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/mount_zfs/mount_zfs.c
0e7810703e26d22de9a7a745adbbec30b32c29ef
[mirror_zfs.git] / cmd / mount_zfs / mount_zfs.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 Lawrence Livermore National Security, LLC.
25 */
26
27 #include <libintl.h>
28 #include <unistd.h>
29 #include <sys/file.h>
30 #include <sys/mount.h>
31 #include <sys/mntent.h>
32 #include <sys/stat.h>
33 #include <libzfs.h>
34 #include <libzutil.h>
35 #include <locale.h>
36 #include <getopt.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #define ZS_COMMENT 0x00000000 /* comment */
41 #define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */
42
43 libzfs_handle_t *g_zfs;
44
45 /*
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.
49 */
50 static void
51 parse_dataset(const char *target, char **dataset)
52 {
53 /* Assume pool/dataset is more likely */
54 strlcpy(*dataset, target, PATH_MAX);
55
56 int fd = open(target, O_RDONLY | O_CLOEXEC);
57 if (fd < 0)
58 return;
59
60 nvlist_t *cfg = NULL;
61 if (zpool_read_label(fd, &cfg, NULL) == 0) {
62 char *nm = NULL;
63 if (!nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &nm))
64 strlcpy(*dataset, nm, PATH_MAX);
65 nvlist_free(cfg);
66 }
67
68 if (close(fd))
69 perror("close");
70 }
71
72 /*
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.
76 */
77 static int
78 mtab_is_writeable(void)
79 {
80 struct stat st;
81 int error, fd;
82
83 error = lstat("/etc/mtab", &st);
84 if (error || S_ISLNK(st.st_mode))
85 return (0);
86
87 fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
88 if (fd < 0)
89 return (0);
90
91 close(fd);
92 return (1);
93 }
94
95 static int
96 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
97 {
98 struct mntent mnt;
99 FILE *fp;
100 int error;
101
102 mnt.mnt_fsname = dataset;
103 mnt.mnt_dir = mntpoint;
104 mnt.mnt_type = type;
105 mnt.mnt_opts = mntopts ? mntopts : "";
106 mnt.mnt_freq = 0;
107 mnt.mnt_passno = 0;
108
109 fp = setmntent("/etc/mtab", "a+");
110 if (!fp) {
111 (void) fprintf(stderr, gettext(
112 "filesystem '%s' was mounted, but /etc/mtab "
113 "could not be opened due to error %d\n"),
114 dataset, errno);
115 return (MOUNT_FILEIO);
116 }
117
118 error = addmntent(fp, &mnt);
119 if (error) {
120 (void) fprintf(stderr, gettext(
121 "filesystem '%s' was mounted, but /etc/mtab "
122 "could not be updated due to error %d\n"),
123 dataset, errno);
124 return (MOUNT_FILEIO);
125 }
126
127 (void) endmntent(fp);
128
129 return (MOUNT_SUCCESS);
130 }
131
132 int
133 main(int argc, char **argv)
134 {
135 zfs_handle_t *zhp;
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;
145 int error, c;
146
147 (void) setlocale(LC_ALL, "");
148 (void) setlocale(LC_NUMERIC, "C");
149 (void) textdomain(TEXT_DOMAIN);
150
151 opterr = 0;
152
153 /* check options */
154 while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
155 switch (c) {
156 case 's':
157 sloppy = 1;
158 break;
159 case 'f':
160 fake = 1;
161 break;
162 case 'n':
163 nomtab = 1;
164 break;
165 case 'v':
166 verbose++;
167 break;
168 case 'o':
169 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
170 break;
171 case 'h':
172 case '?':
173 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
174 optopt);
175 (void) fprintf(stderr, gettext("Usage: mount.zfs "
176 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
177 return (MOUNT_USAGE);
178 }
179 }
180
181 argc -= optind;
182 argv += optind;
183
184 /* check that we only have two arguments */
185 if (argc != 2) {
186 if (argc == 0)
187 (void) fprintf(stderr, gettext("missing dataset "
188 "argument\n"));
189 else if (argc == 1)
190 (void) fprintf(stderr,
191 gettext("missing mountpoint argument\n"));
192 else
193 (void) fprintf(stderr, gettext("too many arguments\n"));
194 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
195 return (MOUNT_USAGE);
196 }
197
198 parse_dataset(argv[0], &pdataset);
199
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);
206 }
207
208 /* validate mount options and set mntflags */
209 error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
210 badopt, mtabopt);
211 if (error) {
212 switch (error) {
213 case ENOMEM:
214 (void) fprintf(stderr, gettext("filesystem '%s' "
215 "cannot be mounted due to a memory allocation "
216 "failure.\n"), dataset);
217 return (MOUNT_SYSERR);
218 case ENOENT:
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);
225 default:
226 (void) fprintf(stderr, gettext("filesystem '%s' "
227 "cannot be mounted due to internal error %d.\n"),
228 dataset, error);
229 return (MOUNT_SOFTWARE);
230 }
231 }
232
233 if (verbose)
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);
239
240 if (mntflags & MS_REMOUNT) {
241 nomtab = 1;
242 remount = 1;
243 }
244
245 if (zfsflags & ZS_ZFSUTIL)
246 zfsutil = 1;
247
248 if ((g_zfs = libzfs_init()) == NULL) {
249 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
250 return (MOUNT_SYSERR);
251 }
252
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);
258 libzfs_fini(g_zfs);
259 return (MOUNT_USAGE);
260 }
261
262 zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
263
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);
267 else
268 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
269 sizeof (prop), NULL, NULL, 0, B_FALSE);
270
271 /*
272 * Fetch the max supported zfs version in case we get ENOTSUP
273 * back from the mount command, since we need the zfs handle
274 * to do so.
275 */
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);
281 }
282
283 zfs_close(zhp);
284 libzfs_fini(g_zfs);
285
286 /*
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.
291 *
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.
296 */
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);
304 }
305
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);
314 }
315
316 if (!fake) {
317 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
318 mntflags, mntopts);
319 }
320
321 if (error) {
322 switch (errno) {
323 case ENOENT:
324 (void) fprintf(stderr, gettext("mount point "
325 "'%s' does not exist\n"), mntpoint);
326 return (MOUNT_SYSERR);
327 case EBUSY:
328 (void) fprintf(stderr, gettext("filesystem "
329 "'%s' is already mounted\n"), dataset);
330 return (MOUNT_BUSY);
331 case ENOTSUP:
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);
338 } else {
339 (void) fprintf(stderr,
340 gettext("filesystem '%s' mount "
341 "failed for unknown reason.\n"), dataset);
342 }
343 return (MOUNT_SYSERR);
344 #ifdef MS_MANDLOCK
345 case EPERM:
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);
354 }
355 /* fallthru */
356 #endif
357 default:
358 (void) fprintf(stderr, gettext("filesystem "
359 "'%s' can not be mounted: %s\n"), dataset,
360 strerror(errno));
361 return (MOUNT_USAGE);
362 }
363 }
364
365 if (!nomtab && mtab_is_writeable()) {
366 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
367 if (error)
368 return (error);
369 }
370
371 return (MOUNT_SUCCESS);
372 }