forked from bolthole/zrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzrep_vars
490 lines (403 loc) · 14.3 KB
/
zrep_vars
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
########### File: zrep_vars
# This should basically be included as common vars before all zrep stuff.
# It contains all 'constant' definitions, as well as a few crucial
# shared routines, such as lock handling ones.
########################################################################
# User tunable section. These may be overridden in user environment vars
SSH=${SSH:-ssh}
ZREP_PATH=${ZREP_PATH:-zrep} #Set to /full/path/to/zrep, if needed, for remote
# Set this if you want to use a different zfs property to store zrep info.
# default has things like zrep:dest-host and zrep:dest-fs
# Change this to non-default, if you want to have multiple destinations.
# You then need to run a separate zrep for each dest.
# In this case, I suggest all runs use non-default value.
# eg: ZREPTAG=zrep-1, ZREPTAG=zrep-2.
# or, ZREPTAG=zrep-uk, ZREPTAG=zrep-us
# ** make sure value can be used in a snapshot name as well, because it
# ** will be !
# !! also, make sure variable is EXPORTED !!
ZREPTAG=${ZREPTAG:-zrep}
#ZREP_CREATE_FLAGS="-o whatever" #Use for extra options on remote zfs create
# If you'd like a little extra debug output define this env var to something
#DEBUG=1
# The fastest alternative transport, IF you have multicore/thread CPUs,
# would seem to be bbcp. If you have both, then you probably want to
# define something like this in your environment:
#BBCP="bbcp -s 8"
# You can apply filters to tweak throughput in various ways
# Sometimes it helps receive performance to use mbuffer
# Other times, you may have highly compressible data, and custom
# compression routines such as lzop or lz4 may show significant
# gains over ssh builtin compression
#
# lz4 example:
# ZREP_OUTFILTER="lz4 -c"
# ZREP_INFILTER="lz4 -d"
#
# mbuffer example:
# ZREP_OUTFILTER="mbuffer -q -m 1G -s 128k"
# ZREP_INFILTER="mbuffer -q -m 1G -s 128k"
# If you want to recursively create/send snapshots, set this to -R
# (or use the -R option to BOTH zrep init and zrep sync
#ZREP_R=-R
# The default incremental flag is -I.
# UNLESS you set ZREPTAG to something other than zrep, in which case
# you will have multiple zrep snapshot names probably going to different
# places, and expiration wont work properly on the remote sides
# So we will autochange incremental type to -i. .. unless you explicitly
# set an override value for INC_FLAG in either case.
#ZREP_INC_FLAG=-I
# If you want to override uname -n, with an official
# canonical name for zrep:src-host, use this environment variable when you
# run "zrep init"
#ZREP_SRC_HOST=somehost.your.dom
# Solaris hack, to use native perl, which isnt always in $PATH,but should
# always be there. It's also simple, straightforward, and non-extended.
# On other OSs this path will not exist, so it will just fall back to
# use the system default perl. Unless user wants to specify perl path.
# If you dont have /usr/perl5, this wont hurt you so just ignore it.
PERL_BIN=${PERL_BIN:-/usr/perl5/bin}
# Hidden var, that isnt really meant to be used directly.
# It gets set if you use "zrep sync -c".
# But you could theoretically set this directly instead if you prefer
#ZREP_CHANGEDONLY=yes
#########################################################################
#########################################################################
# Everything else below here, should not need to be touched.
# First we have some "special" internal vars.
# Then autodetect routines,
# and then internal utilities such as locking functions.
# zfs get syntax is so long and ugly, this is just an internal convenience
# Get a zfs property on fs or snap. Get JUST the value, and only
# a "locally set" value rather than an inherited one
ZFSGETLVAL="zfs get -H -o value -s local"
# But.. sometimes you want to allow propagated values. like
# the ones sent via the zrep_init setup
ZFSGETVAL="zfs get -H -o value"
# **warning** !!
# $ZFSGETLVAL returns "" on value not set. However,
# $ZFSGETVAL returns "-" on value not set. Grrr @zfs writers.
# Work around a bug in gentoo ksh that breaks "ls -l" builtin.
# Would prefer to just override with _AST_FEATURES, but apparently,
# that only gets checked when ksh first starts or something
# Maybe use getconf itself somehow, if safe?
if getconf PATH_RESOLVE > /dev/null 2>&1 ; then
LS=/bin/ls
else
LS=ls
fi
# -n enforces "no dereference" of existing symlink, which is default
# behaviour on some, but not all systems, apparently
LN_S="ln -n -s"
if [[ "$ZREP_SRC_HOST" != "" ]] ; then
Z_LOCAL_HOST=${ZREP_SRC_HOST}
else
Z_LOCAL_HOST=`uname -n`
Z_LOCAL_HOST=${Z_LOCAL_HOST%%.*}
fi
if [[ "$ZREP_R" == "-R" ]] ; then
Z_SNAP_R="-r"
fi
if [[ "$MBUFFER" != "" ]] ; then
_errprint WARNING: MBUFER variable deprecated in zrep
_errprint WARNING: use ZREP_OUTFILTER and ZREP_INFILTER instead
ZREP_OUTFILTER="$ZREP_OUTFILTER $MBUFFER"
ZREP_INFILTER="$MBUFFER $ZREP_INFILTER"
fi
# I HATE having to use a global for this.
# However, there is apparently a bug in typeset behaviour
# introduced in ksh93. UGHHH.
#ZREP_FORCE="-f"
# Sneaky vars to avoid having to use if clauses in the core code
# HOWEVER! Note that ksh doesnt seem to evaluate pipe symbols when normaly
# expanded in a command line. So, have to use 'eval' to get them to register
if [[ "$ZREP_OUTFILTER" != "" ]] ; then
Z_F_OUT="| $ZREP_OUTFILTER"
fi
if [[ "$ZREP_INFILTER" != "" ]] ; then
Z_F_IN="$ZREP_INFILTER |"
fi
# used to have polymorphic assign of ZREP_INC_FLAG here, but had
# to move it to AFTER checking if -t option used
# full name for this should probably be something like,
# PROPTYPES_THAT_ZREP_STATUS_AND_LIST_CAN_USE. But that's too long :)
# Not easy to check if property types allow type "received".
# Ancient systems do not allow it
# So, just tie this to MU6 related check,like HAS_SNAPPROPS, lower down
PROPTYPES="local,received"
# dump the usage message, and check for capabilities
# make sure we dont spew for non-root, so that "zrep status" works
case `id` in
*'(root)'*)
ZREP_RUNDIR=${ZREP_RUNDIR:-/var/run}
;;
*)
ZREP_RUNDIR=${ZREP_RUNDIR:-/tmp}
;;
esac
zrep_checkfile=$ZREP_RUNDIR/zrep.check.$$
zfs >$zrep_checkfile 2>&1
# Previously did a bit of a hack job for feature detection.
# Now attempting to make it smarter,
# at the expense of some startup speed :(
Z_HAS_X=0 # can use recv -x
Z_HAS_REC_U=0 # can use recv -u
Z_HAS_REC_O=0 # can use recv -o (note: SmartOS -o is WRONG)
Z_HAS_SNAPPROPS=0
if grep 'help' $zrep_checkfile >/dev/null ;then
# Presume Solaris 11, which has all features, but
# does not allow line-by-line feature detection easily
Z_HAS_X=1 # can use recv -x
Z_HAS_REC_U=1 # can use recv -u
Z_HAS_REC_O=1 # can use recv -o
Z_HAS_SNAPPROPS=1 # can set properties on snapshots
# This also lets me set "last synced" timestamps
# otherwise cant use zrep:sent sanely.
# Would lose information on rollbacks
DEPTHCAP="-d 1" # limits "list -r"
else
if grep 'receive[ |].*-[a-zA-Z]*x' $zrep_checkfile >/dev/null ;then
Z_HAS_X=1 # can use recv -x
fi
if grep 'receive .*-[a-zA-Z]*u' $zrep_checkfile >/dev/null ;then
Z_HAS_REC_U=1 # can use recv -u
fi
# This bit is unfortunately ugly. Two problems:
# SmartOS and FreeBSD implemented recv -o WRONG! So no Z_HAS_REC_O for it!
# But also, -o doesnt even show in the output of solaris zfs usage. sigh.
# So have to be creative
if grep 'create .* -o prop' $zrep_checkfile >/dev/null ;then
grep 'rec.* -o origin' $zrep_checkfile >/dev/null ||
Z_HAS_REC_O=1 # can use recv -o
fi
if grep 'set .*snapshot' $zrep_checkfile >/dev/null ;then
Z_HAS_SNAPPROPS=1 # can set properties on snapshots
fi
if grep 'list.*-d' $zrep_checkfile >/dev/null ;then
DEPTHCAP="-d 1" # limits "list -r"
else
DEPTHCAP=""
print WARNING: old ZFS version detected with no depth protection
print WARNING: You may not nest zrep managed filesystems
fi
fi
if ((!Z_HAS_SNAPPROPS)) ; then
PROPTYPES="local"
fi
rm $zrep_checkfile
Z_LOCK_RETRY=${Z_LOCK_RETRY:-10} # default 10 second retry, 1 per sec
Z_SAVE_COUNT=${Z_SAVE_COUNT:-5}
Z_GLOBAL_LOCKFILE=$ZREP_RUNDIR/zrep.lock
if [[ "$Z_GLOBAL_PID" == "" ]] ; then
export Z_GLOBAL_PID=$$
fi
Z_SETHOLD=${Z_SETHOLD:-"zfs hold"}
# if your zfs isnt new enough, and you like to live dangerously,
# you can skip setting holds by using this instead.
# Although I may not have gotten around to using this in the code either!
#Z_SETHOLD="echo skipping zfs hold on"
# return PID of proc holding global lock, or nothing
zrep_global_lock_pid(){
$LS -l $Z_GLOBAL_LOCKFILE 2>/dev/null |awk -F/ '{print $NF}'
}
# return 0 if "we" are holding lock, 1 otherwise
# Note that we check for "us, OR our global parent", if different
#
zrep_has_global_lock(){
lockpid=`zrep_global_lock_pid`
if [[ "$lockpid" == "" ]] ; then return 1 ; fi
if [[ "$lockpid" != "$Z_GLOBAL_PID" ]] ; then
if [[ "$lockpid" != "$$" ]] ; then
_debugprint 'has_global_lock? no. lock held by PID' $lockpid
return 1
fi
fi
return 0
}
#Note: it is an ERROR to call this if you already have lock
#It is binary, not recursive ownership.
zrep_get_global_lock(){
typeset retry_count=$Z_LOCK_RETRY
typeset lockpid
[[ -d /proc/$$ ]] || zrep_errquit "/proc fs must be functional to use zrep"
$LN_S /proc/$Z_GLOBAL_PID $Z_GLOBAL_LOCKFILE && return 0
# otherwise, deal with fail
# Careful of race conditions on stale CLEAN UP!
# How to resolve problem where
# * multiple instances running
# * one instance detects stale
# * multiple instances decide to remove it
# * ONE removes it and creates new symlink
# * SECOND one was paused between detection and removal.. so removes
# * VALID lockfile?!?!
# For now, must request manual cleanup
while (( retry_count > 0 )); do
sleep 1
$LN_S /proc/$Z_GLOBAL_PID $Z_GLOBAL_LOCKFILE && return 0
retry_count=$((retry_count-1))
lockpid=`zrep_global_lock_pid`
if [[ ! -d /proc/$lockpid ]] ; then
_errprint ERROR: stale global lock file
_errprint ERROR: shut down ALL zrep instances, then manually remove
_errprint $Z_GLOBAL_LOCKFILE
fi
done
print Failed to acquire global lock
return 1
}
zrep_release_global_lock(){
if zrep_has_global_lock ; then
rm $Z_GLOBAL_LOCKFILE
return $?
else
print ERROR: zrep_release_global_lock called, but do not own lock
return 1
fi
}
# returns PID of zrep process holding a lock on filesystem, if there is one.
# NOTE: If "-s local" used, prints "" if lock unheld
# If no -s specified, prints "-" if lock unheld
zrep_fs_lock_pid(){
$ZFSGETLVAL ${ZREPTAG}:lock-pid $1
}
zrep_has_fs_lock(){
typeset check=`$ZFSGETLVAL ${ZREPTAG}:lock-pid $1`
if ((check == $$)) ; then
return 0
else
return 1
fi
}
# use global lock first (if not already), then
# grab lock on individual fs
# return 1 on fail, 0 on lock acquired
# Note that it is an ERROR to call this, if you already have lock
# Note2: if a dead process has lock, it will forcibly override and
# acqure lock
zrep_lock_fs(){
# global lock is slow. so do quickcheck first.
typeset check=`zrep_fs_lock_pid $1` newcheck
if [[ "$check" != "" ]] ; then
# See if owning process still exists.
ls -d /proc/$check >/dev/null 2>&1 && return 1
fi
zrep_get_global_lock
if [[ $? -ne 0 ]] ; then
if [[ "$DEBUG" != "" ]] ; then
_errprint zrep_lock_fs: failed to get global lock. PID=$$ fs=$1
fi
return 1
fi
# Double-check if needed, now that we have global lock
if [[ "$check" != "" ]] ; then
newcheck=`zrep_fs_lock_pid $1`
if [[ "$newcheck" != "$check" ]] && [[ "$newcheck" != "" ]]
then
# oops. someone else must have dealt with it.
# If they havent reset it then give up
zrep_release_global_lock
return 1
fi
# Keep in mind that stdin/out could be busy
# Cant use regular debugprint
if [[ "$DEBUG" != "" ]] ; then
_errprint overiding stale lock on $1 from pid $check
fi
fi
zfs set ${ZREPTAG}:lock-pid=$$ $1
zfs set ${ZREPTAG}:lock-time=`date +%Y%m%d%H%M%S` $1
if [[ "$DEBUG" != "" ]] ; then
_errprint DEBUG: zrep_lock_fs: set lock on $1
fi
zrep_release_global_lock
}
# release lock, if we have it.
# Since this could be called by an exit cleanup routine blindly,
# dont exit program if we dont have lock. But do return error
zrep_unlock_fs(){
typeset lockpid=`zrep_fs_lock_pid $1`
if ((lockpid != $$)) ; then return 1; fi
#since "we" already have it locked, no need to get global lock first
zfs inherit ${ZREPTAG}:lock-time $1
zfs inherit ${ZREPTAG}:lock-pid $1
if [[ "$DEBUG" != "" ]] ; then
_errprint zrep_unlock_fs: unset lock on $1
fi
return 0
}
# Quit whole program with error status, outputting args to stderr
# Release global lock if we are holding it
# Unless we're running in parallel batch mode
# I'll need to plan that out more carefully!
#
zrep_errquit(){
_errprint Error: "$@"
if zrep_has_global_lock ; then
if [[ "$$" -ne "$Z_GLOBAL_PID" ]] ; then
print EXTRA-ERROR: Running in child proc.
print 'Not sure whether to release global lock. NOT releasing!'
exit 1
else
zrep_release_global_lock
fi
fi
exit 1
}
# Optimization wrapper for ssh: if destination host is ourself, dont use ssh.
# Just run the local command mentioned
# Be careful about quotes here. In fact, try not to use any.
# Usage: zrep_ssh desthost commands_for_ssh go_here
zrep_ssh(){
typeset ssh_cmd
case "$1" in
localhost|$Z_LOCAL_HOST)
ssh_cmd="eval"
shift
$ssh_cmd "$@"
return
;;
esac
if [[ "$2" == "$ZREP_PATH "* ]] && [[ "$DEBUG" != "" ]]
then
#okay yes this is horrible. sigh.
#we normally go to great lengths to preserve ssh arg as single quoted string,
# to identically match passed in arg quoting.
#but this next line undoes that
set -- $*
ssh_cmd="$SSH $1 $ZREP_PATH -D"
shift
shift
$ssh_cmd "$@"
return
fi
ssh_cmd="$SSH $1"
shift
$ssh_cmd "$@"
return
}
zrep_gettimeinseconds(){
# unfortunately, solaris date doesnt do '%s', so need to use perl
typeset seconds
typeset PATH=$PERL_BIN:$PATH
seconds=`perl -e 'print int(time);' 2>/dev/null`
if [[ -z "$seconds" ]] ; then
# attempt fallback if no perl present (eg: stock FreeBSD)
seconds=`date +%s`
fi
if [[ -z "$seconds" ]] ; then
zrep_errquit zrep_gettimeinseconds doesnt know what to do
fi
print $seconds
}
_debugprint(){
if [[ "$DEBUG" != "" ]] ; then
print DEBUG: $@
fi
}
# This consolidated function is both for prettiness, and also
# to make dealing with github issue #22 easier, about redirecting stderr
_errprint(){
# I thought /dev/fd was ksh builtin, so safest. But it glitches on some linuxen.
# print $@ >/dev/fd/2
print $@ >&2
}