2 # Copyright (C) 2016 Ian Kelling
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 script=$
(readlink
-f -- "$BASH_SOURCE")
18 [[ $EUID == 0 ]] ||
exec sudo
-E "$script" "$@"
20 source /usr
/local
/lib
/err
24 Usage: ${0##*/} [OPTIONS]
26 -h|--help Print help and exit.
27 -f|--force Use kill -9 to try fixing unmount errors
28 -v|--verbose Be more verbose
31 Note: In git this is not not executable because it's meant to be installed
32 using ./install-my-scripts
34 Note: Uses util-linux getopt option parsing: spaces between args and
35 options, short options can be combined, options before args.
40 all_vols
=(q a o i
ar qr
)
44 while read -r line
; do
46 grep -xFq "$line" "$file" ||
tee -a "$file"<<<"$line"
67 if ! mountpoint
-q $dir; then
73 while read -r start mpoint end
; do
74 l
="$start $mpoint $end"
75 # kill off any lines that duplicate the mount point.
76 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc
/fstab
82 for m
in ${my_pids[@]}; do
84 echo "$0: error: pids to kill includes our pid or a parent. ps output:" >&2
95 if pids
=$
(timeout
4 lsof
-t $dir); then
97 timeout
4 lsof
-w $dir
101 # fuser will find open sockets that lsof won't, for example from gpg-agent.
102 # note: -v shows kernel processes, which then doesn't return true when we want
103 if pids
=$
(timeout
4 fuser
-m $dir 2>/dev
/null
); then
106 fuser
-$sig -mvk $dir
109 if ! $found_pids; then
117 if mountpoint
-q $dir; then
118 if m umount
-R $dir; then
121 if ! kill-dir TERM TERM TERM INT INT HUP HUP TERM TERM TERM INT INT HUP HUP
; then
122 if $force; then kill-dir KILL
; fi
125 if m umount
-R $dir; then
128 echo "$0: failed to umount $dir"
136 # duplicated in check-subvol
137 # Reassign $1 var from /dev/dm- to corresponding /dev/mapper/
141 if [[ $devref == /dev
/dm-
* ]]; then
142 for mapdev
in /dev
/mapper
/*; do
143 if [[ $
(readlink
-f $mapdev) == "$devref" ]]; then
152 ##### begin command line parsing ########
154 # you can remove this if you do not have options which can have args with spaces or empty.
158 temp
=$
(getopt
-l help,force
,verbose hfv
"$@") || usage
1
162 -f|
--force) force
=true
;;
163 -v|
--verbose) verbose
=true
;;
166 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
171 ##### end command line parsing ########
175 ##### begin setup fstab for subvols we care about ######
177 if [[ -e /mnt
/root
/root2-crypttab
]]; then
178 tu
/etc
/crypttab
</mnt
/root
/root2-crypttab
179 while read -r mapper_dev _
; do
180 if [[ ! -e /dev
/mapper
/$mapper_dev ]]; then
181 m cryptdisks_start
$mapper_dev
183 done < <(cat /mnt
/root
/root2-crypttab
)
185 if [[ -e /mnt
/root
/root2-fstab
]]; then
186 tu
/etc
/fstab
</mnt
/root
/root2-fstab
192 root_dev
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
194 o_dev
=$
(awk '$2 == "/mnt/o" {print $1}' /etc
/mtab
)
197 if [[ $o_dev == "$root_dev" ]]; then
201 # root2_dev=$(awk '$2 == "/mnt/root2" {print $1}' /etc/mtab)
202 # mapper-dev root2_dev
203 # # dont bother with the above for crypt2_dev
204 # crypt2_dev=$root2_dev
207 if cryptsetup status
$root_dev &>/dev
/null
; then
209 else # if we are in a recovery boot, find the next best crypt device
212 for dev
in $
(dmsetup
ls --target crypt |
awk '{print $1}'); do
214 if awk '{print $1}' /etc
/mtab |
grep -Fx $dev &>/dev
/null
; then
223 # dont tax the cpus of old laptops
224 if ((`nproc` > 2)); then
225 mopts
+=,compress=zstd
229 $crypt_dev /a btrfs noatime,subvol=a$mopts 0 0
234 # ssh and probably some other things care about parent directory
235 # ownership, and ssh doesn\'t allow any group writable parent
236 # directories, so we are forced to use a directory structure similar
237 # to home directories
238 f
=(/mnt
/root
/btrbk
/q.
*); f
=${f[0]}
241 $crypt_dev /q btrfs noatime,subvol=q,gid=1000$mopts 0 0
242 /q/p /p none bind$mopts 0 0
246 f
=(/mnt
/root
/btrbk
/qr.
*); f
=${f[0]}
249 $crypt_dev /qr btrfs noatime,subvol=qr$mopts 0 0
253 f
=(/mnt
/root
/btrbk
/ar.
*); f
=${f[0]}
256 $crypt_dev /ar btrfs noatime,subvol=ar,uid=1000,gid=1000$mopts 0 0
261 f
=(/mnt
/o
/btrbk
/o.
*); f
=${f[0]}
264 $o_dev /o btrfs noatime,subvol=o$mopts 0 0
265 /o/m /m none bind$mopts 0 0
272 ##### end setup fstab for subvols we care about ######
274 ### begin get pids that this program depends on so we dont kill them
278 while [[ ${my_pids[-1]} != 1 && ${my_pids[-1]} != ${my_pids[-2]} && $count -lt $loop_limit ]]; do
280 p
=$
(ps
-p ${my_pids[-1]} -o ppid
=)
281 if [[ $p == 0 ||
! $p ]]; then
286 ### end get pids that this program depends on so we dont kill them
288 for vol
in ${all_vols[@]}; do
290 if ! awk '{print $2}' /etc
/fstab |
grep -xF $d &>/dev
/null
; then
295 ##### begin building up list of bind mounts ######
296 binds
=() # list of bind mounts
300 for r
in ${roots[@]}; do
301 # eg. when r=/q/p, for lines like
302 # /q/p /p none bind 0 0
304 new_roots
+=($
(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+(\S+,|)bind[[:space:],].*#\1#p" /etc
/fstab
))
306 (( ${#new_roots} )) ||
break
307 binds
+=(${new_roots[@]})
308 # roots is used to recursively find binds of binds if they exist.
309 roots
=( ${new_roots[@]} )
311 ##### end building up list of bind mounts ######
314 # if latest is already mounted, make sure binds are mounted and move on
315 m check-subvol-stale
$d
316 # populated by check-subvol-stale if stale
317 if ! fresh_snap
=$
(cat /nocow
/btrfs-stale
/$vol 2>/dev
/null
); then
320 for b
in ${binds[@]}; do
321 if mountpoint
-q $b; then
323 if [[ $did != $bid ]]; then
332 ## not using arbtt at the moment
333 # if [[ $vol == q ]]; then
334 # ## allow to fail, user might not be logged in
335 # x sudo -u $(id -nu 1000) XDG_RUNTIME_DIR=/run/user/1000 systemctl --user stop arbtt ||:
339 for dir
in $
(echo $d ${binds[*]}\ |
tac -s\
); do
343 # if we unmounted some but not all, restore them and move on
344 if ! $umount_ret; then
345 for dir
in ${unmounted[@]}; do
351 #### begin dealing with leaf vols ####
353 ### begin getting root_dir
354 ### this is duplicated in check-subvol-stale
356 dev
=$
(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc
/fstab
/etc
/mtab|
head -n1)
358 # note, we need $dev because $d might not be mounted, and we do this loop
359 # because the device in fstab for the rootfs can be different.
360 for devx
in $
(btrfs fil show
$dev|
sed -rn 's#.*path (\S+)$#\1#p'); do
361 if [[ $devx == dm-
* ]]; then
366 root_dir
=$
(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc
/mtab
/etc
/fstab|
head -n1)
367 if [[ $root_dir ]]; then
372 if [[ ! $root_dir ]]; then
373 echo "$0: error could not find root subvol mount for $dev" >&2
376 ### end getting root_dir
379 if [[ -e $vol ]]; then
380 leaf
=$vol.leaf.$
(date +%Y-
%m-
%dT
%H
:%M
:%S
%z
)
382 m btrfs property
set -ts $leaf ro true
384 ### begin check if leaf is different, delete it if not ###
385 parentid
=$
(btrfs sub show
$leaf |
awk '$1 == "Parent" && $2 == "UUID:" {print $3}')
387 bsub
= # base subvolume
388 # go in reverse order as its more likely to be at the end
389 for ((i
=${#bsubs[@]}-1; i
>=0; i--
)); do
390 if [[ $parentid == $
(btrfs sub show
${bsubs[i]} |
awk '$1 == "UUID:" {print $2}') ]]; then
397 # in testing, same subvol is 136 bytes. allow some overhead. 32 happens sometimes under systemd.
399 # EPIPE 32 Broken pipe
400 lines
=$
(btrfs send
--no-data -p $bsub $leaf | btrfs receive
--dump |
head -n 100 |
wc -l ||
[[ $?
== 141 ||
${PIPESTATUS[0]} == 32 ]])
401 if [[ $lines == 0 ]]; then
402 # example output of no differences:
403 # snapshot ./qrtest uuid=c41ff6b7-0527-f34d-95ac-190eecf54ff5 transid=2239 parent_uuid=64949e1b-4a3e-3945-9a8e-cd7b7c15d7d6 parent_transid=2239
404 echo suspected identical
: $bsub $leaf
405 x btrfs sub del
$leaf
408 ### end check if leaf is different, delete it if not ###
410 ## begin expire leaf vols ##
411 leaf_vols
=($vol.leaf.
*)
412 count
=${#leaf_vols[@]}
413 leaf_limit_time
=$
(( EPOCHSECONDS
- 60*60*24*60 )) # 60 days
414 leaf_new_limit_time
=$
(( EPOCHSECONDS
- 60*60*24 )) # 1 day
415 # this goes backwards from oldest. leaf_new_limit_time is just in case
416 # the order gets screwed up or something.
417 for leaf
in ${leaf_vols[@]}; do
418 leaf_time
=$
(date -d ${leaf#$vol.leaf.} +%s
)
419 if (( leaf_limit_time
> leaf_time ||
( leaf_new_limit_time
> leaf_time
&& count
> 15 ) )); then
420 x btrfs sub del
$leaf
424 ## end expire leaf vols ##
426 #### end dealing with leaf vols ####
428 # Note, we make a few assumptions in this script, like
429 # $d was not a different subvol id than $vol, and
430 # things otherwise didn't get mounted very strangely.
431 m btrfs sub snapshot
$fresh_snap $vol
432 for dir
in $d ${binds[@]}; do
436 ## arbtt disabled for now
437 # if [[ $vol == q ]]; then
438 # # maybe this will fail if X is not running
439 # x sudo -u $(id -nu 1000) XDG_RUNTIME_DIR=/run/user/1000 systemctl --user start arbtt ||:
442 stale_dir
=/nocow
/btrfs-stale
448 for dir
in /mnt
/r
7/amy
/{root
/root
,boot
/boot
}_ubuntubionic
/mnt
/{root
2/root
,boot
2/boot
}_ubuntubionic
; do
451 if [[ ! -d $root_dir ]]; then
452 # this only exists on host kd currently
455 # if latest is already mounted, make sure binds are mounted and move on
456 m check-subvol-stale
-p $dir
457 # populated by check-subvol-stale if stale
458 if ! fresh_snap
=$
(cat /nocow
/btrfs-stale
/$vol 2>/dev
/null
); then
461 if [[ -d $dir ]]; then
462 if ! kill-dir TERM TERM TERM INT INT HUP HUP TERM TERM TERM INT INT HUP HUP
; then
463 if $force; then kill-dir KILL
; fi
467 m btrfs sub snapshot
$fresh_snap $dir
468 rm -f /nocow
/btrfs-stale
/$vol