lots of fixes
[distro-setup] / mount-latest-subvol
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 # usage: mount-latest-subvol
17 #
18 # Note, at source location, intentionally not executable, run and read
19 # install-my-scripts.
20
21 cd /
22 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
23
24 errcatch() {
25 set -E; shopt -s extdebug
26 _err-trap() {
27 err=$?
28 exec >&2
29 set +x
30 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
31 bash-trace 2
32 echo "$0: exiting with code $err"
33 exit $err
34 }
35 trap _err-trap ERR
36 set -o pipefail
37 }
38 bash-trace() {
39 local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
40 local source
41 local extdebug=false
42 if [[ $(shopt -p extdebug) == *-s* ]]; then
43 extdebug=true
44 fi
45
46 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
47 argc=${BASH_ARGC[frame]}
48 argc_index+=$argc
49 ((frame < start)) && continue
50 if (( ${#BASH_SOURCE[@]} > 1 )); then
51 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
52 fi
53 indent=$((frame-start+1))
54 indent=$((indent < max_indent ? indent : max_indent))
55 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
56 if $extdebug; then
57 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
58 printf " %s" "${BASH_ARGV[i]}"
59 done
60 fi
61 echo \'
62 done
63 }
64 errcatch
65
66 tu() {
67 while read -r line; do
68 file="$1"
69 grep -xFq "$line" "$file" || tee -a "$file"<<<"$line"
70 done
71 }
72 e() { printf "%s\n" "$*"; "$@"; }
73 mnt() {
74 dir=$1
75 if ! mountpoint $dir &>/dev/null; then
76 mkdir -p $dir
77 e mount $dir
78 fi
79 }
80 fstab() {
81 while read -r start mpoint end; do
82 l="$start $mpoint $end"
83 # kill off any lines that duplicate the mount point.
84 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc/fstab
85 tu /etc/fstab <<<"$l"
86 done
87 }
88 pid-check() {
89 for p in ${pids}; do
90 for m in ${my_pids[@]}; do
91 if (( p == m )); then
92 echo "$0: error: pids to kill includes our pid or a parent. ps output:" >&2
93 ps -f -p $p
94 exit 1
95 fi
96 done
97 done
98 }
99 kill-dir() {
100 for sig; do
101 echo kill-dir $sig
102 found_pids=false
103 if pids=$(timeout 4 lsof -t $dir); then
104 found_pids=true
105 timeout 4 lsof -w $dir
106 pid-check
107 kill -$sig $pids
108 fi
109 # fuser will find open sockets that lsof won't, for example from gpg-agent.
110 # note: -v shows kernel processes, which then doesn't return true when we want
111 if pids=$(timeout 4 fuser -m $dir 2>/dev/null); then
112 pid-check
113 found_pids=true
114 fuser -$sig -mvk $dir
115 fi
116 sleep .5
117 if ! $found_pids; then
118 return 0
119 fi
120 done
121 return 1
122 }
123
124 force=false
125 if [[ $1 == -f ]]; then
126 force=true
127 fi
128
129 ret=0
130
131 ##### begin setup fstab for subvols we care about ######
132 root_dev=$(awk '$2 == "/" {print $1}' /etc/mtab)
133 if [[ $root_dev == /dev/dm-* ]]; then
134 for d in /dev/mapper/*; do
135 if [[ $(readlink -f $d) == $root_dev ]]; then
136 root_dev=$d
137 break
138 fi
139 done
140 fi
141
142 if cryptsetup status $root_dev &>/dev/null; then
143 crypt_dev=$root_dev
144 else # if we are in a recovery boot, find the next best crypt device
145 noauto=,noauto
146 for dev in $(dmsetup ls --target crypt | awk '{print $1}'); do
147 dev=/dev/mapper/$dev
148 if awk '{print $1}' /etc/mtab | grep -Fx $dev &>/dev/null; then
149 crypt_dev=$dev
150 break
151 fi
152 done
153 fi
154
155
156 fstab <<EOF
157 $crypt_dev /a btrfs noatime,subvol=a$noauto 0 0
158 EOF
159
160 shopt -s nullglob
161
162 # ssh and probably some other things care about parent directory
163 # ownership, and ssh doesn\'t allow any group writable parent
164 # directories, so we are forced to use a directory structure similar
165 # to home directories
166 f=(/mnt/root/btrbk/q.*)
167 if [[ -e $f ]]; then
168 fstab <<EOF
169 $crypt_dev /q btrfs noatime,subvol=q,gid=1000$noauto 0 0
170 /q/p /p none bind$noauto 0 0
171 EOF
172 fi
173
174 f=(/mnt/root/btrbk/o.*)
175 if [[ -e $f ]]; then
176 fstab <<EOF
177 $crypt_dev /o btrfs noatime,subvol=o$noauto 0 0
178 /o/m /m none bind$noauto 0 0
179 EOF
180 fi
181
182 if [[ $HOSTNAME == frodo ]]; then
183 fstab <<EOF
184 $crypt_dev /i btrfs noatime,subvol=i$noauto 0 0
185 EOF
186 fi
187 ##### end setup fstab for subvols we care about ######
188
189 # get pids that this program depends on so we dont kill them
190 my_pids=($$ $PPID)
191 loop_limit=30
192 count=0
193 while [[ ${my_pids[-1]} != 1 && ${my_pids[-1]} != ${my_pids[-2]} && $count -lt $loop_limit ]]; do
194 count=$((count + 1))
195 p=$(ps -p ${my_pids[-1]} -o ppid=)
196 if [[ $p == 0 || ! $p ]]; then
197 break
198 fi
199 my_pids+=($p)
200 done
201
202
203 for vol in q a o i; do
204 d=/$vol
205 if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
206 continue
207 fi
208
209
210 ##### begin building up list of bind mounts ######
211 binds=() # list of bind mounts
212 roots=($d) # list of bind mounts, plus the original mount
213 while true; do
214 new_roots=()
215 for r in ${roots[@]}; do
216 # eg. when r=/q/p, for lines like
217 # /q/p /p none bind 0 0
218 # output /p
219 new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc/fstab))
220 done
221 (( ${#new_roots} )) || break
222 binds+=(${new_roots[@]})
223 roots=( ${new_roots[@]} )
224 done
225 ##### end building up list of bind mounts ######
226
227
228 # if latest is already mounted, make sure binds are mounted and move on
229 e check-subvol-stale $d
230 # populated by check-subvol-stale if stale
231 if ! fresh_snap=$(cat /nocow/btrfs-stale/$vol 2>/dev/null); then
232 mnt $d
233 for b in ${binds[@]}; do
234 mnt $b
235 done
236 continue
237 fi
238
239 umount_ret=true
240 unmounted=()
241 for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
242 if mountpoint $dir; then
243 if e umount -R $dir; then
244 unmounted+=($dir)
245 else
246 if ! kill-dir TERM TERM TERM INT INT HUP HUP; then
247 if $force; then kill-dir KILL; fi
248 fi
249
250 if e umount -R $dir; then
251 unmounted+=($dir)
252 else
253 echo "$0: failed to umount $dir"
254 umount_ret=false
255 ret=1
256 continue
257 fi
258 fi
259 fi
260 done
261
262 if ! $umount_ret; then
263 for dir in ${unmounted[@]}; do
264 mnt $dir
265 done
266 continue
267 fi
268
269 # todo: decipher /mnt/root, like we do in check-subvol-stale
270 cd /mnt/root
271 if [[ -e $vol ]]; then
272 e mv $vol $vol.leaf.$(date +%Y-%m-%dT%H:%M:%S%z)
273 leaf_vols=($vol.leaf.*)
274 for leaf in ${leaf_vols[@]}; do
275 leaf_secs=$(date -d ${leaf#$vol.leaf.} +%s)
276 if (( $(date +%s) - 60*60*24*60 > leaf_secs )); then # 60 days
277 e btrfs sub del $leaf
278 fi
279 done
280 fi
281 # Note, we make a few assumptions in this script, like
282 # $d was not a different subvol id than $vol, and
283 # things otherwise didn't get mounted very strangely.
284 e btrfs sub snapshot $fresh_snap $vol
285 for dir in $d ${binds[@]}; do
286 e mnt $dir
287 done
288 stale_dir=/nocow/btrfs-stale
289 rm -f $stale_dir/$d
290 done
291
292 ### disabled
293 if [[ $HOSTNAME == kdxxxxxxxxx ]]; then
294 # partitioned it with fai partitioner outside of fai,
295 # because it\'s worth it to have 1% space reserved for boot and
296 # swap partitions in case I ever want to boot off those drives.
297 # as root:
298 # . /a/bin/fai/fai-wrapper
299 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
300 # fai-setclass ROTATIONAL
301 # export LUKS_DIR=/q/root/luks/
302 # # because the partition nums existed already
303 # fai-setclass REPARTITION
304 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
305
306 devs=(
307 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
308 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
309 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
310 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
311 )
312 first=true
313 for dev in ${devs[@]}; do
314 if $first; then
315 first=false
316 tu /etc/fstab <<EOF
317 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
318 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
319 EOF
320 fi
321 tu /etc/crypttab <<EOF
322 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-kd discard,luks
323 EOF
324 if [[ ! -e /dev/mapper/crypt_dev_$dev ]]; then
325 cryptdisks_start crypt_dev_$dev
326 fi
327 done
328 # note, could do an else here and have some kind of mount for /i
329 # on other hosts.
330 fi
331
332 exit $ret