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 # usage: mount-latest-subvol
19 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
22 set -E; shopt -s extdebug
27 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
29 echo "$0: exiting with code $err"
36 local -i argc_index
=0 frame i start
=${1:-1} max_indent
=8 indent
39 if [[ $
(shopt -p extdebug
) == *-s* ]]; then
43 for ((frame
=0; frame
< ${#FUNCNAME[@]}-1; frame
++)); do
44 argc
=${BASH_ARGC[frame]}
46 ((frame
< start
)) && continue
47 if (( ${#BASH_SOURCE[@]} > 1 )); then
48 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
50 indent
=$
((frame-start
+1))
51 indent
=$
((indent
< max_indent ? indent
: max_indent
))
52 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
54 for ((i
=argc_index-1
; i
>= argc_index-argc
; i--
)); do
55 printf " %s" "${BASH_ARGV[i]}"
64 while read -r line
; do
66 grep -xFq "$line" "$file" ||
tee -a "$file"<<<"$line"
69 e
() { printf "%s\n" "$*"; "$@"; }
72 if ! mountpoint
$dir &>/dev
/null
; then
78 while read -r start mpoint end
; do
79 l
="$start $mpoint $end"
80 # kill off any lines that duplicate the mount point.
81 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc
/fstab
87 for m
in ${my_pids[@]}; do
89 echo "$0: error: pids to kill includes our pid or a parent" >&2
100 if pids
=$
(timeout
4 lsof
-t $dir); then
102 timeout
4 lsof
-w $dir
106 # fuser will find open sockets that lsof won't, for example from gpg-agent.
107 # note: -v shows kernel processes, which then doesn't return true when we want
108 if pids
=$
(timeout
4 fuser
-m $dir 2>/dev
/null
); then
111 fuser
-$sig -mvk $dir
114 if ! $found_pids; then
122 if [[ $1 == -f ]]; then
128 ##### begin setup fstab for subvols we care about ######
129 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
131 $first_root_crypt /a btrfs noatime,subvol=a 0 0
136 # ssh and probably some other things care about parent directory
137 # ownership, and ssh doesn\'t allow any group writable parent
138 # directories, so we are forced to use a directory structure similar
139 # to home directories
140 f
=(/mnt
/root
/btrbk
/q.
*)
143 $first_root_crypt /q btrfs noatime,subvol=q,gid=1000 0 0
144 /q/p /p none bind 0 0
148 f
=(/mnt
/root
/btrbk
/o.
*)
151 $first_root_crypt /o btrfs noatime,subvol=o 0 0
152 /o/m /m none bind 0 0
156 if [[ $HOSTNAME == frodo
]]; then
158 $first_root_crypt /i btrfs noatime,subvol=i 0 0
161 ##### end setup fstab for subvols we care about ######
163 # get pids that this program depends on so we dont kill them
167 while [[ ${my_pids[-1]} != 1 && ${my_pids[-1]} != ${my_pids[-2]} && $count -lt $loop_limit ]]; do
169 p
=$
(ps
-p ${my_pids[-1]} -o ppid
=)
170 if [[ $p == 0 ||
! $p ]]; then
177 for vol
in q a o i
; do
179 if ! awk '{print $2}' /etc
/fstab |
grep -xF $d &>/dev
/null
; then
184 ##### begin building up list of bind mounts ######
185 binds
=() # list of bind mounts
186 roots
=($d) # list of bind mounts, plus the original mount
189 for r
in ${roots[@]}; do
190 # eg. when r=/q/p, for lines like
191 # /q/p /p none bind 0 0
193 new_roots
+=($
(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc
/fstab
))
195 (( ${#new_roots} )) ||
break
196 binds
+=(${new_roots[@]})
197 roots
=( ${new_roots[@]} )
199 ##### end building up list of bind mounts ######
202 # if latest is already mounted, make sure binds are mounted and move on
203 if e check-subvol-stale
$d; then
205 for b
in ${binds[@]}; do
211 fresh_snap
=$
(</nocow
/btrfs-stale
/$vol)
212 if [[ ! $fresh_snap ]]; then
213 echo "$0: error. empty fresh_snap var"
220 for dir
in $
(echo $d ${binds[*]}\ |
tac -s\
); do
221 if mountpoint
$dir; then
222 if e umount
-R $dir; then
225 if ! kill-dir TERM TERM TERM INT INT HUP HUP
; then
226 if $force; then kill-dir KILL
; fi
229 if e umount
-R $dir; then
232 echo "$0: failed to umount $dir"
241 if ! $umount_ret; then
242 for dir
in ${unmounted[@]}; do
248 # todo: decipher /mnt/root, like we do in check-subvol-stale
250 if [[ -e $vol ]]; then
251 e
mv $vol $vol.leaf.$
(date +%Y-
%m-
%dT
%H
:%M
:%S
%z
)
252 leaf_vols
=($vol.leaf.
*)
253 for leaf
in ${leaf_vols[@]}; do
254 leaf_secs
=$
(date -d ${leaf#$vol.leaf.} +%s
)
255 if (( $
(date +%s
) - 60*60*24*60 > leaf_secs
)); then # 60 days
256 e btrfs sub del
$leaf
260 # Note, we make a few assumptions in this script, like
261 # $d was not a different subvol id than $vol, and
262 # things otherwise didn't get mounted very strangely.
263 e btrfs sub snapshot
$fresh_snap $vol
264 for dir
in $d ${binds[@]}; do
267 stale_dir
=/nocow
/btrfs-stale
272 if [[ $HOSTNAME == kdxxxxxxxxx
]]; then
273 # partitioned it with fai partitioner outside of fai,
274 # because it\'s worth it to have 1% space reserved for boot and
275 # swap partitions in case I ever want to boot off those drives.
277 # . /a/bin/fai/fai-wrapper
278 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
279 # fai-setclass ROTATIONAL
280 # export LUKS_DIR=/q/root/luks/
281 # # because the partition nums existed already
282 # fai-setclass REPARTITION
283 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
286 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
287 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
288 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
289 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
292 for dev
in ${devs[@]}; do
296 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
297 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
300 tu
/etc
/crypttab
<<EOF
301 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-kd discard,luks
303 if [[ ! -e /dev
/mapper
/crypt_dev_
$dev ]]; then
304 cryptdisks_start crypt_dev_
$dev
307 # note, could do an else here and have some kind of mount for /i