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.
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 arg 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
79 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
81 $first_root_crypt /q btrfs noatime,subvol=q 0 0
87 $first_root_crypt /p btrfs noatime,subvol=p 0 0
94 if ! awk '{print $2}' /etc
/fstab |
grep -xF $d &>/dev
/null
; then
103 for r
in ${roots[@]}; do
104 # /q/a /a none bind 0 0
105 new_roots
+=($
(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc
/fstab
))
107 (( ${#new_roots} )) ||
break
108 binds
+=(${new_roots[@]})
109 roots
=( ${new_roots[@]} )
112 # if latest is already mounted, make sure binds are mounted and move on
113 if e check-subvol-stale
$d; then
114 for b
in ${binds[@]}; do
120 last_snap
=$
(</nocow
/btrfs-stale
/$vol)
121 if [[ ! $last_snap ]]; then
122 echo "$0: error. empty last_snap var"
129 for dir
in $
(echo $d ${binds[*]}\ |
tac -s\
); do
130 if mountpoint
$dir; then
131 if e umount
-R $dir; then
136 echo "$0: failed to umount $dir"
142 if ! $umount_ret; then
143 for dir
in ${unmounted[@]}; do
150 if [[ -e $vol ]]; then
153 # Note, we make a few assumptions in this script, like
154 # $d was not a different subvol id than $vol, and
155 # things otherwise didn't get mounted very strangely.
156 e btrfs sub snapshot btrbk
/$last_snap $vol
157 for dir
in $d ${binds[@]}; do
160 stale_dir
=/nocow
/btrfs-stale
164 if [[ $HOSTNAME == treetowl
]]; then
165 # partitioned it with fai partitioner outside of fai,
166 # because it\'s worth it to have 1% space reserved for boot and
167 # swap partitions in case I ever want to boot off those drives.
169 # . /a/bin/fai/fai-wrapper
170 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
171 # fai-setclass ROTATIONAL
172 # export LUKS_DIR=/q/root/luks/
173 # # because the partition nums existed already
174 # fai-setclass REPARTITION
175 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
178 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
179 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
180 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
181 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
184 for dev
in ${devs[@]}; do
188 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
189 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
192 tu
/etc
/crypttab
<<EOF
193 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
195 if [[ ! -e /dev
/mapper
/crypt_dev_
$dev ]]; then
196 cryptdisks_start crypt_dev_
$dev
200 tu
/etc
/fstab
<<'EOF'
201 /q/i /i none bind,noauto 0 0