1#!/bin/bash
2#
3# Copyright (c) International Business Machines  Corp., 2005
4# Author: Avantika Mathur (mathurav@us.ibm.com)
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19#
20
21mflags=""
22#mflags="-n" # Don't futz with mtab
23
24flag=y
25while getopts "n" arg "$@"
26do
27        case "$arg" in
28        n)      flag=n
29                shift
30                ;;
31        esac
32done
33
34bind_type="$1"
35dir="$2"
36
37
38if [ ! -d "$dir" ]
39then
40	if [ -e "$dir" ]; then
41		echo "ERROR: a file by the name \"$dir\" exists"
42		exit 1
43	fi
44	mkdir -p "$dir"
45	echo "mkdir -p \"$dir\""
46fi
47
48
49if [ "$flag" = "y" ] && [ "$bind_type" != slave ]
50then
51	mount $mflags --bind "$dir" "$dir" || exit $?
52	echo "mount $mflags --bind \"$dir\" \"$dir\""
53fi
54
55# Try to use native mount, else fallback to included smount binary
56case "$bind_type" in
57   share)
58   	echo "mount $mflags --make-rshared \"$dir\""
59	mount $mflags --make-rshared "$dir" 2> /dev/null || \
60	smount "$dir" rshared || exit $?
61      	;;
62   priv)
63   	echo "mount $mflags --make-rprivate \"$dir\""
64	mount $mflags --make-rprivate "$dir" 2> /dev/null || \
65	smount "$dir" rprivate || exit $?
66      	;;
67   slave)
68   	echo "mount $mflags --make-rslave \"$dir\""
69	mount $mflags --make-rslave "$dir" 2> /dev/null || \
70	smount "$dir" rslave || exit $?
71      	;;
72   unclone)
73   	echo "mount $mflags --make-runbindable \"$dir\""
74	mount $mflags --make-runbindable "$dir" 2> /dev/null || \
75	smount "$dir" runclone || exit $?
76      	;;
77   nshare)
78   	echo "mount $mflags --make-shared \"$dir\""
79	mount $mflags --make-shared "$dir" 2> /dev/null || \
80	smount "$dir" shared || exit $?
81      	;;
82   npriv)
83   	echo "mount $mflags --make-private \"$dir\""
84	mount $mflags --make-private "$dir" 2> /dev/null || \
85	smount "$dir" private || exit $?
86      	;;
87   nslave)
88   	echo "mount $mflags --make-slave \"$dir\""
89	mount $mflags --make-slave "$dir" 2> /dev/null || \
90	smount "$dir" slave || exit $?
91      	;;
92   nunclone)
93   	echo "mount $mflags --make-unbindable \"$dir\""
94	mount $mflags --make-unbindable "$dir" 2> /dev/null || \
95	smount "$dir" unclone || exit $?
96      	;;
97   *)
98   	echo "$0: unrecognized bind type (1st arg): $bind_type" 1>&2
99	exit 1
100	;;
101esac
102