various minor improvements
[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
17
18 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
19
20 errcatch() {
21 set -E; shopt -s extdebug
22 _err-trap() {
23 err=$?
24 exec >&2
25 set +x
26 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
27 bash-trace 2
28 echo "$0: exiting with code $err"
29 exit $err
30 }
31 trap _err-trap ERR
32 set -o pipefail
33 }
34 bash-trace() {
35 local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent
36 local source
37 local extdebug=false
38 if [[ $(shopt -p extdebug) == *-s* ]]; then
39 extdebug=true
40 fi
41
42 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
43 argc=${BASH_ARGC[frame]}
44 argc_index+=$argc
45 ((frame < start)) && continue
46 if (( ${#BASH_SOURCE[@]} > 1 )); then
47 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
48 fi
49 indent=$((frame-start+1))
50 indent=$((indent < max_indent ? indent : max_indent))
51 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
52 if $extdebug; then
53 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
54 printf " %s" "${BASH_ARGV[i]}"
55 done
56 fi
57 echo \'
58 done
59 }
60 errcatch
61
62 tu() {
63 while read -r line; do
64 file="$1"
65 grep -xFq "$line" "$file" || tee -a "$file"<<<"$line"
66 done
67 }
68 e() { printf "%s\n" "$*"; "$@"; }
69 mnt() {
70 dir=$1
71 if ! mountpoint $dir &>/dev/null; then
72 mkdir -p $dir
73 e mount $dir
74 fi
75 }
76
77 ret=0
78
79 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
80 tu /etc/fstab <<EOF
81 $first_root_crypt /a btrfs noatime,subvol=a 0 0
82 EOF
83 case $HOSTNAME in
84 treetowl|x2|frodo)
85 tu /etc/fstab <<EOF
86 $first_root_crypt /q btrfs noatime,subvol=q 0 0
87 /q/p /p none bind 0 0
88 EOF
89 ;;
90 esac
91
92 for vol in q a; do
93 d=/$vol
94 if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
95 continue
96 fi
97
98
99 binds=()
100 roots=($d)
101 while true; do
102 new_roots=()
103 for r in ${roots[@]}; do
104 # example
105 # /q/p /p none bind 0 0
106 new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc/fstab))
107 done
108 (( ${#new_roots} )) || break
109 binds+=(${new_roots[@]})
110 roots=( ${new_roots[@]} )
111 done
112
113 # if latest is already mounted, make sure binds are mounted and move on
114 if e check-subvol-stale $d; then
115 for b in ${binds[@]}; do
116 mnt $b
117 done
118 continue
119 fi
120
121 last_snap=$(</nocow/btrfs-stale/$vol)
122 if [[ ! $last_snap ]]; then
123 echo "$0: error. empty last_snap var"
124 ret=1
125 continue
126 fi
127
128 umount_ret=true
129 unmounted=()
130 for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
131 if mountpoint $dir; then
132 if e umount -R $dir; then
133 unmounted+=($dir)
134 else
135 umount_ret=false
136 ret=1
137 echo "$0: failed to umount $dir"
138 break
139 fi
140 fi
141 done
142
143 if ! $umount_ret; then
144 for dir in ${unmounted[@]}; do
145 mnt $dir
146 done
147 continue
148 fi
149
150 cd /mnt/root
151 if [[ -e $vol ]]; then
152 e btrfs sub del $vol
153 fi
154 # Note, we make a few assumptions in this script, like
155 # $d was not a different subvol id than $vol, and
156 # things otherwise didn't get mounted very strangely.
157 e btrfs sub snapshot btrbk/$last_snap $vol
158 for dir in $d ${binds[@]}; do
159 e mnt $dir
160 done
161 stale_dir=/nocow/btrfs-stale
162 rm -f $stale_dir/$d
163 done
164
165 if [[ $HOSTNAME == treetowl ]]; then
166 # partitioned it with fai partitioner outside of fai,
167 # because it\'s worth it to have 1% space reserved for boot and
168 # swap partitions in case I ever want to boot off those drives.
169 # as root:
170 # . /a/bin/fai/fai-wrapper
171 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
172 # fai-setclass ROTATIONAL
173 # export LUKS_DIR=/q/root/luks/
174 # # because the partition nums existed already
175 # fai-setclass REPARTITION
176 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
177
178 devs=(
179 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
180 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
181 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
182 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
183 )
184 first=true
185 for dev in ${devs[@]}; do
186 if $first; then
187 first=false
188 tu /etc/fstab <<EOF
189 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
190 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
191 EOF
192 fi
193 tu /etc/crypttab <<EOF
194 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
195 EOF
196 if [[ ! -e /dev/mapper/crypt_dev_$dev ]]; then
197 cryptdisks_start crypt_dev_$dev
198 fi
199 done
200 # note, could do an else here and have some kind of mount for /i
201 # on other hosts.
202 fi
203
204 exit $ret