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
18 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
21 set -E; shopt -s extdebug
26 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
28 echo "$0: exiting with code $err"
35 local -i argc_index
=0 frame i start
=${1:-1} max_indent
=8 indent
38 if [[ $
(shopt -p extdebug
) == *-s* ]]; then
42 for ((frame
=0; frame
< ${#FUNCNAME[@]}-1; frame
++)); do
43 argc
=${BASH_ARGC[frame]}
45 ((frame
< start
)) && continue
46 if (( ${#BASH_SOURCE[@]} > 1 )); then
47 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
49 indent
=$
((frame-start
+1))
50 indent
=$
((indent
< max_indent ? indent
: max_indent
))
51 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
53 for ((i
=argc_index-1
; i
>= argc_index-argc
; i--
)); do
54 printf " %s" "${BASH_ARGV[i]}"
63 while read -r line
; do
65 grep -xFq "$line" "$file" ||
tee -a "$file"<<<"$line"
68 e
() { printf "%s\n" "$*"; "$@"; }
71 if ! mountpoint
$dir &>/dev
/null
; then
77 while read -r start mpoint end
; do
78 l
="$start $mpoint $end"
79 # kill off any lines that duplicate the mount point.
80 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc
/fstab
87 if pids
=$
(timeout
4 lsof
-t $dir); then
89 timeout
4 lsof
-w $dir
92 # fuser will find open sockets that lsof won't, for example from gpg-agent.
93 # note: -v shows kernel processes, which then doesn't return true when we want
94 if timeout
4 fuser
-m $dir &>/dev
/null
; then
106 if [[ $1 == -f ]]; then
112 ##### begin setup fstab for subvols we care about ######
113 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
115 $first_root_crypt /a btrfs noatime,subvol=a 0 0
120 # ssh and probably some other things care about parent directory
121 # ownership, and ssh doesn\'t allow any group writable parent
122 # directories, so we are forced to use a directory structure similar
123 # to home directories
124 f
=(/mnt
/root
/btrbk
/q.
*)
127 $first_root_crypt /q btrfs noatime,subvol=q,gid=1000 0 0
128 /q/p /p none bind 0 0
132 f
=(/mnt
/root
/btrbk
/o.
*)
135 $first_root_crypt /o btrfs noatime,subvol=o 0 0
136 /o/m /m none bind 0 0
140 if [[ $HOSTNAME == frodo
]]; then
142 $first_root_crypt /i btrfs noatime,subvol=i 0 0
145 ##### end setup fstab for subvols we care about ######
147 for vol
in q a o i
; do
149 if ! awk '{print $2}' /etc
/fstab |
grep -xF $d &>/dev
/null
; then
154 ##### begin building up list of bind mounts ######
155 binds
=() # list of bind mounts
156 roots
=($d) # list of bind mounts, plus the original mount
159 for r
in ${roots[@]}; do
160 # eg. when r=/q/p, for lines like
161 # /q/p /p none bind 0 0
163 new_roots
+=($
(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc
/fstab
))
165 (( ${#new_roots} )) ||
break
166 binds
+=(${new_roots[@]})
167 roots
=( ${new_roots[@]} )
169 ##### end building up list of bind mounts ######
172 # if latest is already mounted, make sure binds are mounted and move on
173 if e check-subvol-stale
$d; then
175 for b
in ${binds[@]}; do
181 fresh_snap
=$
(</nocow
/btrfs-stale
/$vol)
182 if [[ ! $fresh_snap ]]; then
183 echo "$0: error. empty fresh_snap var"
190 for dir
in $
(echo $d ${binds[*]}\ |
tac -s\
); do
191 if mountpoint
$dir; then
192 if e umount
-R $dir; then
195 kill-dir || kill-dir INT || kill-dir HUP ||
! $force || kill-dir KILL ||
:
197 if e umount
-R $dir; then
200 echo "$0: failed to umount $dir"
209 if ! $umount_ret; then
210 for dir
in ${unmounted[@]}; do
216 # todo: decipher /mnt/root, like we do in check-subvol-stale
218 if [[ -e $vol ]]; then
219 e
mv $vol $vol.leaf.$
(date +%Y
%m
%dT
%H
%M
%S
%z
)
221 # Note, we make a few assumptions in this script, like
222 # $d was not a different subvol id than $vol, and
223 # things otherwise didn't get mounted very strangely.
224 e btrfs sub snapshot
$fresh_snap $vol
225 for dir
in $d ${binds[@]}; do
228 stale_dir
=/nocow
/btrfs-stale
233 if [[ $HOSTNAME == treetowlxxxxxxxxx
]]; then
234 # partitioned it with fai partitioner outside of fai,
235 # because it\'s worth it to have 1% space reserved for boot and
236 # swap partitions in case I ever want to boot off those drives.
238 # . /a/bin/fai/fai-wrapper
239 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
240 # fai-setclass ROTATIONAL
241 # export LUKS_DIR=/q/root/luks/
242 # # because the partition nums existed already
243 # fai-setclass REPARTITION
244 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
247 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
248 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
249 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
250 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
253 for dev
in ${devs[@]}; do
257 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
258 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
261 tu
/etc
/crypttab
<<EOF
262 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
264 if [[ ! -e /dev
/mapper
/crypt_dev_
$dev ]]; then
265 cryptdisks_start crypt_dev_
$dev
268 # note, could do an else here and have some kind of mount for /i