#!/bin/sh # # Pawel Jakub Dawidek # # This script is supposed to remount file systems with org.freebsd:owner # property set as regular user from the property. This way file system owner # will be able to perform further operations on the file system. # zfs list -H -t filesystem -o name,canmount,mountpoint,org.freebsd:owner | while read dataset canmount mountpoint owner; do # Skip datasets that are not suppose to be mounted. [ "${canmount}" != "on" ] && continue # Skip datasets with non-path mountpoints. [ "${mountpoint}" = "${mountpoint#/}" ] && continue # Skip datasets without org.freebsd:owner property. [ "${owner}" = "-" ] && continue # Get owner's UID needed for chown. # This way we also check if the owner from the property is an existing system user. uid=`id -u ${owner} 2>/dev/null` if [ $? -ne 0 ]; then echo "Invalid org.freebsd:owner property set for ${dataset}, skipping." >/dev/stderr continue fi # Skip if already mounted by the given user. mount -t zfs | egrep -q '^'${dataset}' on '${mountpoint}' \(zfs, .*, mounted by '${owner}'\)$' [ $? -eq 0 ] && continue # Owner's gid for chown. gid=`id -g ${owner}` [ $? -ne 0 ] && continue # Should never fail. # Unmount file system if it is actually mounted. if [ "`zfs get -H -o value mounted ${dataset}`" = "yes" ]; then out=`zfs umount ${dataset} 2>&1` if [ $? -ne 0 ]; then echo "zfs umount ${dataset} failed (${out}), skipping." >/dev/stderr continue fi fi # Create mountpoint directory if it doesn't exist. if [ ! -d ${mountpoint} ]; then out=`mkdir ${mountpoint} 2>&1` if [ $? -ne 0 ]; then echo "mkdir ${mountpoint} failed (${out}), skipping." >/dev/stderr continue fi fi # Set mountpoint directory owner to the file system owner. out=`chown ${uid}:${gid} ${mountpoint} 2>&1` if [ $? -ne 0 ]; then echo "chown ${uid}:${gid} ${mountpoint} failed (${out}), skipping." >/dev/stderr continue fi # Mount the file system with owner's credentials. out=`su -m ${owner} -c "zfs mount ${dataset}" 2>&1` if [ $? -ne 0 ]; then echo "zfs mount ${dataset} failed (${out}), skipping." >/dev/stderr continue fi done zfs share -a