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