Subversion Repositories livecd

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 beyerle@PS 1
#!/bin/sh
2
 
3
# Copyright (C) 2005, 2006, 2007 Junjiro Okajima
4
#
5
# This program, aufs is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
19
# $Id: unionctl,v 1.16 2007/02/05 01:44:49 sfjro Exp $
20
 
21
# simple script for controling aufs.
22
# compatible with unionctl(8) in unionfs(5).
23
# if you use "mount -o remount" to add/del/mod branches,
24
# this scirpt is unnecessary.
25
 
26
PATH=/usr/bin:/usr/sbin:/bin:/sbin
27
export PATH
28
eecho() { echo "$@" 1>&2; }
29
 
30
me=`basename $0`
31
err=0
32
usage()
33
{
34
	eecho "${me}: $@"
35
	eecho usage:
36
	eecho "${me}" union_dir --add \
37
		'[ --before | --after bindex | branch ]' '[ --mode perm ]' branch
38
	eecho "${me}" union_dir --remove branch
39
	eecho "${me}" union_dir --mode branch perm
40
	eecho "${me}" union_dir --list
41
	eecho "${me}" union_dir '--whereis | --query1 | --q1' path_under_union '[ ,,, ]'
42
	err=1
43
}
44
 
45
conv() # [escape]
46
{
47
	sed -r -e '
48
	s/\\/\\134/g
49
	s/$/\\012/
50
	' |
51
	tr -d '\n' |
52
	sed -r -e '
53
	s/ /\\040/g
54
	s/\t/\\011/g
55
	s/\r/\\015/g
56
	s/\\012$//
57
	' |
58
	{ test "$1" = "escape" && sed -r -e 's/\\/\\\\/g' || cat; }
59
	echo
60
}
61
 
62
tgt=
63
branches()
64
{
65
	echo "$tgt" |
66
	tr ',' '\n' |
67
	egrep '^(dirs|br)[=:]' |
68
	sed -r -e 's/^(dirs|br)[=:]//' |
69
	tr ':=' '\n '
70
}
71
 
72
########################################
73
 
74
test $# -lt 2 && eecho "${me}: bad arg $@" && exit 1
75
f=/proc/mounts
76
test ! -f $f && eecho "${me}: $f is necessary" && exit 1
77
 
78
set -e
79
#set -x; echo $@
80
tmp=/tmp/$$
81
cd "$1"
82
mntpnt="$PWD"
83
e_mntpnt=`echo "$mntpnt" | conv escape`
84
cd "$OLDPWD"
85
action="$2"
86
shift 2
87
 
88
tgt=`grep "aufs $e_mntpnt" $f | tail -1 | rev | cut -f3- -d' ' | rev`
89
test "$tgt" = "" && eecho "${me}: no such mntpnt $mntpnt" && exit 1
90
dev=`echo "$tgt" | sed -e 's: '"$mntpnt aufs"' .*$::`
91
branches > $tmp
92
#cat $tmp | while read i; do echo $i; done; exit
93
 
94
find_bindex() # dir
95
{
96
	echo "$1" | grep -q '^[0-9]*$' && echo "$1" && return 0
97
	local i n err
98
	n=0
99
	cat $tmp | rev | cut -f2- -d' ' | rev |
100
	while read i
101
	do
102
		test "$i" = "$1" && echo $n && return 0
103
		n=`expr $n + 1`
104
	done
105
	err=$?
106
	test $err -eq 0 && return 0
107
	rm -f $tmp
108
	eecho "${me}: no such branch $1"
109
	echo -2
110
	return 1
111
}
112
 
113
Remount() #options...
114
{
115
	rm -f $tmp
116
	local mount212p=0
117
	{ mount --version 2> /dev/null || :; } | fgrep -q mount-2.12p && mount212p=1
118
	test $mount212p -eq 1 && exec mount -o remount,"$@" "$mntpnt"
119
	exec mount -o remount,"$@" "$dev" "$mntpnt"
120
}
121
 
122
do_add()
123
{
124
	local bindex perm br
125
	bindex=0
126
	perm=rw # unionfs default
127
	br=
128
	while [ "$br" = "" ]
129
	do
130
		case "$1" in
131
		--before)	bindex=`find_bindex "$2"`
132
				bindex=`expr $bindex - 1` || :
133
				;;
134
		--after)	bindex=`find_bindex "$2"`
135
				bindex=`expr $bindex + 1` || :
136
				;;
137
		--mode)		perm="$2";;
138
		--*)		eecho "${me}: unkown option $1"; return 1;;
139
		*)		br="$1"
140
		esac
141
		test "$br" = "" && shift 2 || :
142
	done
143
 
144
	test $# -ne 1 && eecho "${me}: bad arg $@" && return 1
145
	br="$1"
146
	test $bindex -lt 0 && bindex=0 || :
147
	Remount add:${bindex}:"${br}"="$perm"
148
}
149
 
150
do_del()
151
{
152
	test $# -ne 1 && eecho "${me}: bad arg $@" && return 1
153
	Remount del:"$1"
154
}
155
 
156
do_mod()
157
{
158
	test $# -ne 2 && eecho "${me}: bad arg $@" && return 1
159
	Remount mod:"$1"="$2"
160
}
161
 
162
do_list()
163
{
164
	local path mode
165
	cat $tmp | rev |
166
	while read mode path
167
	do
168
		mode=`echo $mode | rev`
169
		path=`echo "$path" | rev`
170
		case $mode in
171
		ro)	mode="r--";;
172
		rw)	mode="rw-";;
173
		ro+wh)	mode="R--";;
174
		*)	mode="---";;
175
		esac
176
		/bin/echo -e \\t"$path" \(${mode}\)
177
	done
178
}
179
 
180
do_query1()
181
{
182
	local path i f
183
	cat $tmp | rev | cut -f2- -d' ' | rev |
184
	while read path
185
	do
186
		for i
187
		do
188
			i=`echo "$i" | sed -e 's:^/::'`
189
			test -e "$mntpnt/$i" || continue
190
			f="$path/$i"
191
			test -e "$f" && if [ $# -eq 1 ]
192
			then echo "$path"
193
			else echo "$f"
194
			fi || :
195
		done
196
	done
197
}
198
 
199
case "$action" in
200
--add)		do_add "$@";;
201
--remove)	do_del "$@";;
202
--mode)		do_mod "$@";;
203
--list)		do_list;;
204
--q1|--query1|--whereis)
205
		do_query1 "$@";;
206
--query)	usage "unsupported action $action"; err=1;;
207
*)		usage "unkown action $action"; err=1;;
208
esac
209
 
210
rm -f $tmp $tmp.*
211
exit $err