-
Notifications
You must be signed in to change notification settings - Fork 57
/
zrep_vars
622 lines (513 loc) · 19.2 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
########### 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
#
# Additionally, we check /etc/default/zrep first.
# If you want to allow users to override the options set there, then be sure
# to use similar syntax to below:
# VAR=${VAR:-yourvalue}
#
if [[ -f /etc/default/zrep ]] ; then
. /etc/default/zrep
fi
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}
# If you'd like a little extra debug output define this env var to something
#DEBUG=1
#If set to yes(default), renames failed sync snapshots to "zrep_#####_unsent"
ZREP_RENAME_UNSENT=${ZREP_RENAME_UNSENT:-yes}
# The default behavior for up-to-date ZFS filesystems, is that zrep will call
# a zfs send/receieve that creates the remote filesystem with pretty much the same options.
# However, sometimes, it is important to have the remote side have a diferent set of
# options, BEFORE data has been transferred. ie: with remote using compression.
# Note1: If set, this will stop the typical inheritance of src filesystem properties
# Note2: It will force the "old zfs creation style" codepath for init. But not
# for the rest of zrep
#ZREP_CREATE_FLAGS="-o compression=on,xx=yy"
#Convenience hook.
#By default, the remote fs should start with the same properties that the master has.
#However, sometimes people want different ones.
#If this var is set, a post-init ssh will be made, to set the requested properties.
#While some zfs implementations support multiple values in a single set command,
#Others do not. So if multiple are set here, multiple ssh calls will be made.
# syntax: "prop1=value [prop2=value prop3=value ... ]"
#ZREP_INIT_REMOTE_PROPERTIES="compression=on"
# 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
# Sometimes, people may want to add extra flags to "zfs send".
# If your system supports it, then adding -c means that
# compressed filesystems will be sent in compressed form, rather
# then autouncompressing
# --raw is sometime used for encrypted filesystems
#ZREP_SEND_FLAGS="-c --raw"
# Theoretically identical in purpose to ZREP_SEND_FLAGS, but it turns out that when using
# zfs send -t (resume-token)
# you are only allowed to use a restricted set of flags. It normally uses what was
# set the first time.
# So, for resume sends, we clear send flags, and only use what is set here below
#ZREP_RESUME_FLAGS=-v
# There are two types of zfs incremental snapshots.
# 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.
# Probably should have named this INCR_FLAG, but it's in use now
#ZREP_INC_FLAG=-I
# This currently doesnt do much, and is probably best not user-set.
# I should probably make use of this more standardized.
# But you can set it if you want
#ZREP_VERBOSE=yes
# Some odd people like to configure a non-root user for zrep,
# and not give it expire permissions.
# Well.. okay then...
#ZREP_SKIP_EXPIRE=1
# 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
# This only gets used at init time
ZREP_SAVE_COUNT=${ZREP_SAVE_COUNT:-5}
# This currently is only used in zrep status
# If you change it to %s then zrep will display
# seconds since the most resent snapshot was synced.
ZREP_DATEFORMAT=${ZREP_DATEFORMAT:-%Y/%m/%d-%H:%M:%S}
#########################################################################
#########################################################################
# Everything else below here, should not need to be touched.
#########################################################################
#########################################################################
_debugprint(){
if [[ "$DEBUG" != "" ]] ; then
echo 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.
# echo $@ >/dev/fd/2
echo $@ >&2
}
# 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.
# It follows symlinks.
# 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?
# Trick would be to do
# export _AST_FEATURES="PATH_RESOLVE = physical"
# but cant.
#### This is not even used any more!! But I'm keeping it in as
#### documentation for historical knowlege
#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"
# I dont use ln any more, but leaving this for useful historical info.
# side note: ksh built-in ln, DOES NOT SUPPORT -n !
if [[ "$ZREP_SRC_HOST" != "" ]] ; then
Z_LOCAL_HOST=${ZREP_SRC_HOST}
else
Z_LOCAL_HOST=`uname -n`
Z_LOCAL_HOST=${Z_LOCAL_HOST%%.*}
fi
# Slightly ugly to implement this as a global flag. But... it makes
# a lot of things simpler, like "ssh zrep xyhz" for multiple things.
if [[ "$1" == "-R" ]] ; then
ZREP_R="-R"
shift
fi
if [[ "$ZREP_R" == "-R" ]] ; then
# ZREP_R is a user-settable env var. It also gets used in
# "zfs send" commandlines. However.. we also need to call
# "zfs snap".. which requires LOWERCASE R. So this is an automatically set
# mirror of that.
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
# allow override, for code test utility
zrep_checkfile=${_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 NOT WHAT WE WANT)
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!
# They use -o to set "origin", not to set options
# 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.
# Note that some systems have '-o' directly after create, and some do not.
if grep 'create .*-o prop' $zrep_checkfile >/dev/null ;then
# This is probably nested under the create check, because we
# only use recv -o, right after using create -o.
# However, we now ALWAYS use create -o, so.. may be unneccesary
# to nest
# Skip zfs that uses recv -o origin
if ! grep 'rec[ev].*-o origin' $zrep_checkfile >/dev/null &&
grep 'rec[ev].*-o ' $zrep_checkfile >/dev/null ; then
Z_HAS_REC_O=1 # can use recv -o
fi
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=""
echo WARNING: old ZFS version detected with no depth protection
echo 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
# This is named like a global override. and CAN be overridden by user.
# But should only be used in zrep_vars module
# Note: This path is why you should only give zfs privileges to a SINGLE USER.
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(){
cat $Z_GLOBAL_LOCKFILE 2>/dev/null
}
# 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.
#We do NOT try to clean up stale global lock.
#This is a shortterm lock. It should never be stale. If it is,
#it could indicate a more serious system/zrep problem happening.
zrep_get_global_lock(){
typeset retry_count=$Z_LOCK_RETRY
typeset lockpid
set -C #noclobber
# ignore error this time, because we retry anyway.
echo $Z_GLOBAL_PID > $Z_GLOBAL_LOCKFILE 2>/dev/null && return 0
# Otherwise, deal with fail/retry.
# 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
errmsg=`echo $Z_GLOBAL_PID 2>&1 > $Z_GLOBAL_LOCKFILE `
if [[ $? -eq 0 ]] ; then return 0 ; fi
retry_count=$((retry_count-1))
lockpid=`zrep_global_lock_pid`
if [[ "$lockpid" -le 0 ]] ; then
zrep_errquit ERROR: invalid contents for global lock file $Z_GLOBAL_LOCKFILE
fi
# Does the process holding the lock actually still exist?
# In theory, teenietiny chance of race condition for false stale. That's okay.
kill -0 $lockpid 2>/dev/null
if [[ $? -ne 0 ]] ; then
_errprint ERROR: stale global lock file
_errprint ERROR: shut down ALL zrep instances, then manually remove
_errprint $Z_GLOBAL_LOCKFILE
fi
done
echo Failed to acquire global lock
echo Error message was: $errmsg
return 1
}
zrep_release_global_lock(){
if zrep_has_global_lock ; then
rm $Z_GLOBAL_LOCKFILE
return $?
else
echo 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.
kill -0 $check 2>/dev/null
if [[ $? -eq 0 ]] ; then
_debugprint lock is still held by $check
return 1
else
_debugprint lock is no longer held by $check
fi
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
# Yes we already checked this, but we didnt have global lock.
# Avoid race condition and doublecheck now that we have global lock.
if [[ "$check" != "" ]] ; then
newcheck=`zrep_fs_lock_pid $1`
if [[ "$newcheck" != "$check" ]] && [[ "$newcheck" != "" ]]
then
# oops. someone else beat us to it.
# Better luck next time.
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
echo EXTRA-ERROR: Running in child proc.
echo '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(){
typeset seconds
typeset PATH=$PERL_BIN:$PATH
seconds=`printf '%(%s)T'`
if [[ -z "$seconds" ]] ; then
# Unfortunately, solaris date doesnt do '%s', so try perl first.
# It's more consistent.
seconds=`perl -e 'print int(time);' 2>/dev/null`
fi
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
echo $seconds
}