X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=cmd%2Fmount_zfs%2Fmount_zfs.c;h=27a9014b3953894afd6e632892641d8ba09ac38b;hb=0c1171dcb5c50f3a03c60f4346209c23a2b5ef86;hp=6a2e9ff3647c856869f5bd0d7d4ad28c09253e33;hpb=2cf7f52bc42f215d4ef27d0fd75fc1b1417cb841;p=zfs.git diff --git a/cmd/mount_zfs/mount_zfs.c b/cmd/mount_zfs/mount_zfs.c index 6a2e9ff..27a9014 100644 --- a/cmd/mount_zfs/mount_zfs.c +++ b/cmd/mount_zfs/mount_zfs.c @@ -30,6 +30,7 @@ #include #include #include +#include #ifdef HAVE_LIBSELINUX #include #endif /* HAVE_LIBSELINUX */ @@ -130,15 +131,6 @@ parse_option(char *mntopt, unsigned long *mntflags, if (strncmp(name, opt->name, strlen(name)) == 0) { *mntflags |= opt->mntmask; *zfsflags |= opt->zfsmask; - - /* MS_USERS implies default user options */ - if (opt->mntmask & (MS_USERS)) - *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV); - - /* MS_OWNER|MS_GROUP imply default owner options */ - if (opt->mntmask & (MS_OWNER | MS_GROUP)) - *mntflags |= (MS_NOSUID|MS_NODEV); - error = 0; goto out; } @@ -219,17 +211,55 @@ out: } /* - * If a file or directory in your current working directory is named - * 'dataset' then mount(8) will prepend your current working directory - * to dataset. The is no way to prevent this behavior so we simply - * check for it and strip the prepended patch when it is added. + * Return the pool/dataset to mount given the name passed to mount. This + * is expected to be of the form pool/dataset, however may also refer to + * a block device if that device contains a valid zfs label. */ static char * parse_dataset(char *dataset) { char cwd[PATH_MAX]; + struct stat64 statbuf; + int error; int len; + /* + * We expect a pool/dataset to be provided, however if we're + * given a device which is a member of a zpool we attempt to + * extract the pool name stored in the label. Given the pool + * name we can mount the root dataset. + */ + error = stat64(dataset, &statbuf); + if (error == 0) { + nvlist_t *config; + char *name; + int fd; + + fd = open(dataset, O_RDONLY); + if (fd < 0) + goto out; + + error = zpool_read_label(fd, &config); + (void) close(fd); + if (error) + goto out; + + error = nvlist_lookup_string(config, + ZPOOL_CONFIG_POOL_NAME, &name); + if (error == 0) + dataset = strdup(name); + + nvlist_free(config); + return (dataset); + } +out: + /* + * If a file or directory in your current working directory is + * named 'dataset' then mount(8) will prepend your current working + * directory to the dataset. There is no way to prevent this + * behavior so we simply check for it and strip the prepended + * patch when it is added. + */ if (getcwd(cwd, PATH_MAX) == NULL) return (dataset); @@ -310,8 +340,9 @@ main(int argc, char **argv) char mntopts[MNT_LINE_MAX] = { '\0' }; char badopt[MNT_LINE_MAX] = { '\0' }; char mtabopt[MNT_LINE_MAX] = { '\0' }; - char *dataset, *mntpoint; - unsigned long mntflags = 0, zfsflags = 0, remount_ro = 0; + char mntpoint[PATH_MAX]; + char *dataset; + unsigned long mntflags = 0, zfsflags = 0, remount = 0; int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0; int error, c; @@ -366,7 +397,14 @@ main(int argc, char **argv) } dataset = parse_dataset(argv[0]); - mntpoint = argv[1]; + + /* canonicalize the mount point */ + if (realpath(argv[1], mntpoint) == NULL) { + (void) fprintf(stderr, gettext("filesystem '%s' cannot be " + "mounted at '%s' due to canonicalization error %d.\n"), + dataset, argv[1], errno); + return (MOUNT_SYSERR); + } /* validate mount options and set mntflags */ error = parse_options(mntopts, &mntflags, &zfsflags, sloppy, @@ -416,11 +454,10 @@ main(int argc, char **argv) " mountopts: \"%s\"\n mtabopts: \"%s\"\n"), dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt); - if (mntflags & MS_REMOUNT) + if (mntflags & MS_REMOUNT) { nomtab = 1; - - if ((mntflags & MS_REMOUNT) && (mntflags & MS_RDONLY)) - remount_ro = 1; + remount = 1; + } if (zfsflags & ZS_ZFSUTIL) zfsutil = 1; @@ -453,9 +490,10 @@ main(int argc, char **argv) * we differentiate the two cases using the 'zfsutil' mount option. * This mount option should only be supplied by the 'zfs mount' util. * - * The only exception to the above rule is '-o remount,ro'. This is - * always allowed for non-legacy datasets for rc.sysinit/umountroot - * to safely remount the root filesystem and flush its cache. + * 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 ' to work. */ if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) { (void) fprintf(stderr, gettext( @@ -466,7 +504,8 @@ main(int argc, char **argv) return (MOUNT_USAGE); } - if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY) && !remount_ro) { + if (!zfsutil && !(remount || fake) && + strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) { (void) fprintf(stderr, gettext( "filesystem '%s' cannot be mounted using 'mount'.\n" "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n" @@ -480,6 +519,10 @@ main(int argc, char **argv) mntflags, mntopts); 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);