summaryrefslogtreecommitdiff
path: root/package/aufs2-util/src/aubrsync
blob: 54adac9cb27edc87bbc45ce3a61192dfb1d5d5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/sh

# Copyright (C) 2005-2009 Junjiro Okajima
#
# This program, aufs is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#
# The development of this script is sponcered by ASUSTek Computer Inc.
# Kindly they agreed that I keep my aufs work as free software as it has
# been.
#

set -eu
#set -x

me=$(basename $0)
EEcho() # str
{
	echo ${me}: $@ 1>&2
}

f=/sbin/mount.aufs
test ! -x $f && EEcho $f is not installed && exit 1

# special handling for backward compatibility.
#
# aufs in the donated eeepc is aufs1 20080211 without CONFIG_AUFS_COMPAT.
# /etc/default/aufs was introduced in aufs1 20080922.
# shwh/noshwh was introduced in aufs1 20080310 with CONFIG_AUFS_SHWH.
# noshwh became always available regardless CONFIG_AUFS_SHWH in aufs1 20081117.

noshwh=1
AUFS_VERSION=20080211
f=/etc/default/aufs
if [ -s $f ]
then
	. $f
else
	echo ${me}: warning, broken $f, assuming aufs is $AUFS_VERSION
	f=/usr/lib/aufs.shlib
	test ! -s $f && EEcho $f is not installed && exit 1
	. $f

	case $AUFS_VERSION in
	200*) # aufs1
		test $AUFS_VERSION -lt 20081117 && noshwh=0
		;;
	esac
	AUFS_SUPER_MAGIC=1635083891
	AUFS_SUPER_MAGIC_HEX=0x61756673
	AUFS_WH_PFX=.wh.
	AUFS_WH_PFX2=.wh..wh.
	AUFS_WH_DIROPQ=.wh..wh..opq
fi

########################################

_Rsync="rsync --exclude=lost+found"
Rsync="$_Rsync -aHSx --devices --specials --delete-before"
Copy="$Rsync"
Move="$Copy"
RsyncWh="$_Rsync -ptgoHx"

FindForRm() # rw
{
	echo "find \"$1\" -xdev -depth \(
		\( ! -type d
			\( -name $AUFS_WH_DIROPQ
			-o ! -name ${AUFS_WH_PFX2}\* \) \)
		-o \( -type d
			! -name ${AUFS_WH_PFX2}\*
			! -wholename \"$1\"
			! -wholename \"$1/lost+found\" \)
		\) -print0"
}

MoveWh() # rw ro+wh
{
	cd "$1"
	find . -xdev -name ${AUFS_WH_PFX}\* ! -name ${AUFS_WH_PFX2}\* \
		-printf '%P\n' |
	while read wh
	do
		f=$(echo "$wh" | sed -e '
			s/^'${AUFS_WH_PFX}'//
			t
			s:/'${AUFS_WH_PFX}':/:
			')
		test -e "$dst/$f" || echo "$wh"
	done |
	# -v
	$RsyncWh --files-from=- ./ "$2"
	cd "$OLDPWD"
}

copy()
{
	$Copy $@ "$mntpnt"/ "$dst"
}

_move()
{
	set +x
	test $hinotify -ne 1 && echo ${me}: warning, -i is not specified
	src_is_nfs=0
	test $(stat -f -c %T "$src") = nfs && src_is_nfs=1
	set $quiet

	$Move $@ &&
	eval $(FindForRm "$src") |
	{
		if [ $src_is_nfs -eq 1 ]
		then
			mount -o remount "$mntpnt"
			mount -o remount "$src"
		fi
		xargs -r0 rm -fr #-v
	}
}

move()
{
	_move $@ "$mntpnt"/ "$dst"
}

move_with_wh()
{
	{
	set +x
	MoveWh "$src" "$dst"
	set $quiet
	} &&
	move --exclude=${AUFS_WH_PFX}\*
}

# backward compatibility
move_w()
{
	move_with_wh $@
}

Usage()
{
	t=$(FindForRm src_branch | sed -e '
		s/"//g
		$b
		s/$/ \\/')

	cat <<- EOF
	$me Options move | move_with_wh | copy \\
	        mntpnt src_branch dst_branch [ options for rsync ]

	generic form:
	$me [ -w | --wh ] [ -i | --inotify ] Options \\
	        mntpnt cmd [ parameters for cmd ]

	Options:
	        [ -n | --dry_run ]
	        [ -q | --quiet ]

	The dst_branch must be mounted as writable.
	During the operation, the mntpnt is set readonly.
	If you are opening a file for writing on the writable branch,
	you need to close the file before invoking this script.
	The -w or --wh option requires CONFIG_AUFS_SHWH enabled.
	The -i or --inotify option requires CONFIG_AUFS_HINOTIFY enabled.

	'copy' is a shortcut for
	        $me mntpnt \\
	        $Copy mntpnt/ dst_branch
	'move' is a shortcut for
	        $me mntpnt \\
	        "$Move \\
	        mntpnt/ dst_branch && \\
	        $t |\\
	        xargs -r0 rm -fr"
	        Note: in most cases, you will need '-i' option, and
	              find(1) is invoked by $me only when rsync(1)
	              succeded.
	'move_with_wh' is a simple variation of 'move' which moves
	whiteouts separately before the actual 'move'.

	If you execute this script under linux-2.6.24 or earlier, the
	kernel may produce a harmless warning "inotify.c:NNN
	set_dentry_child_flags()". The message was already removed in
	linux-2.6.25.

	examples:
	- Copy and reflect all the modification (modifed files, newly
	  created and removed ones) in the upper branch to the lower
	  branch. This operation is for aufs which has only 2 branches,
	  and mainly for a system shutdown script.
	  All files on the upper branch remain.

	  $ sudo $me copy /your/aufs /your/upper_branch /your/lower_branch

	- Like above (2 branches), move and reflect all modifications
	  from upper to lower. Almost all files on the upper branch will
	  be removed. You can still use this aufs after the
	  operation. But the inode number may be changed. If your
	  application which depends upon the inode number was running at
	  that time, it may not work correctly.

	  $ sudo $me move /your/aufs /your/upper_branch /your/lower_branch
	EOF

# 	- Like above (2 branches), generate a new middle layer like a
# 	  snapshot including whiteouts and make the upper_branch almost
# 	  empty, but untouch the lower_branch.
# 
# 	  $ img=/hda1/a.ext2
# 	  $ dd if=/dev/zero of=\$img bs=4k count=1k
# 	  $ mkfs -t ext2 -F \$img
# 	  $ sudo mount -o rw,loop \$img /your/new_branch
# 	  $ sudo mount -o remount,ins:1:/your/new_branch=ro+wh /your/aufs
# 	  $ sudo $me _move /your/aufs /your/upper_branch /your/lower_branch \\
# 	        "--remove-source-files \\
# 	        --exclude=$AUFS_WH_BASE \\
# 	        --exclude=$AUFS_WH_PLINKDIR \\
# 	        --exclude=$AUFS_WH_TMPDIR \\
# 	        /your/upper_branch/ /your/new_branch; \\
# 	        mount -o remount,ro /your/new_branch"
# 	EOF
}

########################################

wh=0
hinotify=0
quiet=-x
dry_run=
cmd=
cmd_opts=
for i
do
	case $i in
	-w|--wh)	wh=1;;
	-i|--inotify)	hinotify=1;;
	-n|--dry_run)	dry_run=echo;;
	-q|--quiet)	quiet=+x;;
	-h|--help)	Usage; exit 0;;
	--)		shift; break;;
	*)		break;;
	esac
	shift
done

test $# -lt 2 && Usage 1>&2 && exit 1
case "$1" in
_move|move|copy|move_w|move_with_wh)
	test $# -lt 4 && Usage 1>&2 && exit 1
	cmd=$1
	SetDir mntpnt "$2"
	SetDir src "$3"
	SetDir dst "$4"
	shift 4
	wh=0
	;;
*)
	SetDir mntpnt "$1"
	cmd="$2"
	shift 2
	;;
esac
cmd_opts="$@"

case $(stat -f -c %T "$mntpnt") in
aufs|UNKNOWN*${AUFS_SUPER_MAGIC_HEX}*)	;;
*)
	EEcho "$mntpnt" is not aufs
	exit 1
	;;
esac

cur_opts=$(MntOpts "$mntpnt")
test ! "$cur_opts" &&
EEcho bad /proc/mounts or "$mntpnt" is not mounted &&
exit 1
cur_opts="udba=reval,noshwh,$cur_opts"
test $noshwh -eq 0 && cur_opts=$(echo $cur_opts | sed -e 's/,noshwh//')

# force flushing the pusedo-links
tmp_opts="remount,ro,udba=reval,noshwh"
test $noshwh -eq 0 && tmp_opts=$(echo $tmp_opts | sed -e 's/,noshwh//')
test $wh -eq 1 && tmp_opts="$tmp_opts,shwh"
test $hinotify -eq 1 && tmp_opts="$tmp_opts,udba=inotify"

# here we go
trap "$dry_run mount -o remount,$cur_opts \"$mntpnt\"" EXIT
set $quiet
$dry_run mount -o $tmp_opts "$mntpnt"
eval "$dry_run $cmd $cmd_opts"