11f1e2261SMartin Matuska#!/bin/ksh -p
21f1e2261SMartin Matuska#
31f1e2261SMartin Matuska# CDDL HEADER START
41f1e2261SMartin Matuska#
51f1e2261SMartin Matuska# The contents of this file are subject to the terms of the
61f1e2261SMartin Matuska# Common Development and Distribution License (the "License").
71f1e2261SMartin Matuska# You may not use this file except in compliance with the License.
81f1e2261SMartin Matuska#
91f1e2261SMartin Matuska# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska# or https://opensource.org/licenses/CDDL-1.0.
111f1e2261SMartin Matuska# See the License for the specific language governing permissions
121f1e2261SMartin Matuska# and limitations under the License.
131f1e2261SMartin Matuska#
141f1e2261SMartin Matuska# When distributing Covered Code, include this CDDL HEADER in each
151f1e2261SMartin Matuska# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
161f1e2261SMartin Matuska# If applicable, add the following below this CDDL HEADER, with the
171f1e2261SMartin Matuska# fields enclosed by brackets "[]" replaced with your own identifying
181f1e2261SMartin Matuska# information: Portions Copyright [yyyy] [name of copyright owner]
191f1e2261SMartin Matuska#
201f1e2261SMartin Matuska# CDDL HEADER END
211f1e2261SMartin Matuska#
221f1e2261SMartin Matuska
231f1e2261SMartin Matuska. $STF_SUITE/tests/functional/user_namespace/user_namespace_common.kshlib
241f1e2261SMartin Matuska
251f1e2261SMartin Matuska#
261f1e2261SMartin Matuska# DESCRIPTION:
271f1e2261SMartin Matuska#	Regression test for delegation of datasets to user namespaces.
281f1e2261SMartin Matuska#
291f1e2261SMartin Matuska# STRATEGY:
301f1e2261SMartin Matuska#       1. Delegate two datasets with distinctive names to a user namespace.
311f1e2261SMartin Matuska#	2. Check that 'zfs list' is not able to see datasets outside of the
321f1e2261SMartin Matuska#	   delegation, which have a prefix matching one of the delegated sets.
331f1e2261SMartin Matuska#	   Also, check that all the delegated sets are visible.
341f1e2261SMartin Matuska#
351f1e2261SMartin Matuska
361f1e2261SMartin Matuskaverify_runnable "both"
371f1e2261SMartin Matuska
381f1e2261SMartin Matuskauser_ns_cleanup() {
391f1e2261SMartin Matuska	if [ -n "$proc_ns_added" ]; then
401f1e2261SMartin Matuska		log_must zfs unzone $proc_ns_added $TESTPOOL/userns
411f1e2261SMartin Matuska		log_must zfs unzone $proc_ns_added $TESTPOOL/otheruserns
421f1e2261SMartin Matuska	fi
431f1e2261SMartin Matuska	if [ -n "$unshared_pid" ]; then
441f1e2261SMartin Matuska		kill -9 $unshared_pid
451f1e2261SMartin Matuska		# Give it a sec to make the global cleanup more reliable.
461f1e2261SMartin Matuska		sleep 1
471f1e2261SMartin Matuska	fi
481f1e2261SMartin Matuska	log_must zfs destroy -r $TESTPOOL/userns
491f1e2261SMartin Matuska	log_must zfs destroy -r $TESTPOOL/usernsisitnot
501f1e2261SMartin Matuska	log_must zfs destroy -r $TESTPOOL/otheruserns
511f1e2261SMartin Matuska}
521f1e2261SMartin Matuska
531f1e2261SMartin Matuskalog_onexit user_ns_cleanup
541f1e2261SMartin Matuska
551f1e2261SMartin Matuskalog_assert "Check zfs list command handling of dataset visibility in user namespaces"
561f1e2261SMartin Matuska
571f1e2261SMartin Matuska# Create the baseline dataset.
581f1e2261SMartin Matuskalog_must zfs create -o zoned=on $TESTPOOL/userns
591f1e2261SMartin Matuska# Datasets with a prefix matching the delegated dataset should not be
601f1e2261SMartin Matuska# automatically considered visible.
611f1e2261SMartin Matuskalog_must zfs create -o zoned=on $TESTPOOL/usernsisitnot
621f1e2261SMartin Matuska# All delegated datasets should be visible.
631f1e2261SMartin Matuskalog_must zfs create -o zoned=on $TESTPOOL/otheruserns
641f1e2261SMartin Matuska
651f1e2261SMartin Matuska# 1. Create a user namespace with a cloned mount namespace, then delegate.
661f1e2261SMartin Matuskaunshare -Urm echo test
671f1e2261SMartin Matuskaif [ "$?" -ne "0" ]; then
681f1e2261SMartin Matuska	log_unsupported "Failed to create user namespace"
691f1e2261SMartin Matuskafi
701f1e2261SMartin Matuskaunshare -Urm /usr/bin/sleep 1h &
711f1e2261SMartin Matuskaunshared_pid=$!
721f1e2261SMartin Matuskaif [ "$?" -ne "0" ]; then
731f1e2261SMartin Matuska	log_unsupported "Failed to create user namespace"
741f1e2261SMartin Matuskafi
751f1e2261SMartin Matuskaproc_ns=/proc/$unshared_pid/ns/user
761f1e2261SMartin Matuskasleep 2 # Wait for unshare to acquire user namespace
771f1e2261SMartin Matuskalog_note "unshare: child=${unshared_pid} proc_ns=${proc_ns}"
781f1e2261SMartin Matuska
791f1e2261SMartin MatuskaNSENTER="nsenter -t $unshared_pid --all"
801f1e2261SMartin Matuska
811f1e2261SMartin Matuska$NSENTER echo test
821f1e2261SMartin Matuskaif [ "$?" -ne "0" ]; then
831f1e2261SMartin Matuska	log_unsupported "Failed to enter user namespace"
841f1e2261SMartin Matuskafi
851f1e2261SMartin Matuska
861f1e2261SMartin Matuska# 1b. Pre-test by checking that 'zone' does something new.
871f1e2261SMartin Matuskalist="$($NSENTER zfs list -r -H -o name | tr '\n' ' ')"
881f1e2261SMartin Matuskalog_must test -z "$list"
891f1e2261SMartin Matuskalog_must zfs zone $proc_ns $TESTPOOL/userns
901f1e2261SMartin Matuskalog_must zfs zone $proc_ns $TESTPOOL/otheruserns
91*c7046f76SMartin Matuskaproc_ns_added="$proc_ns"
921f1e2261SMartin Matuska
931f1e2261SMartin Matuska# 2. 'zfs list'
941f1e2261SMartin Matuskalist="$($NSENTER zfs list -r -H -o name $TESTPOOL | tr '\n' ' ')"
951f1e2261SMartin Matuskalog_must test "$list" = "$TESTPOOL $TESTPOOL/otheruserns $TESTPOOL/userns "
961f1e2261SMartin Matuska
971f1e2261SMartin Matuskalog_pass "Check zfs list command handling of dataset visibility in user namespaces"
98