finish bind mount, removed in next commit
[distro-setup] / conflink
1 #!/bin/bash
2
3 source /a/bin/errhandle/errcatch-function
4 source /a/bin/errhandle/bash-trace-function
5
6 errcatch
7
8 m() {
9 echo "$*"
10 "$@"
11 }
12 s() { sudo "$@"; }
13 lnf() { /a/exe/lnf "$@"; }
14
15
16 shopt -s nullglob
17 shopt -s extglob # note, already set with bash -l
18
19 # If we make a link back to the root, we stop going deeper into subdir_files.
20 # This makes it so we can do subdir directories.
21 #
22 # Also note, under filesystem/, symlinks are expanded.
23
24 subdir-link-r() {
25 local root="$1"
26 local targets=()
27 if [[ $2 ]]; then
28 targets=( "$2"/!(.git|..|.) )
29 else
30 for f in "$1"/!(.git|..|.); do
31 [[ -d $f ]] && targets+=("$f") ||:
32 done
33 fi
34 local below="$( readlink -f "$root/..")"
35 for path in "${targets[@]}"; do
36 local fullpath="$(readlink -f "$path")"
37 #e $fullpath $below # debug
38 if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
39 m lnf -T "$path" "$HOME/${path#$root/}"
40 elif [[ -d "$path" ]]; then
41 subdir-link-r "$root" "$path"
42 fi
43 done
44 }
45
46
47
48 common-file-setup() {
49 local dir fs x bdir f dst
50 for dir in "$@"; do
51 fs=$dir/filesystem
52 if [[ -e $fs && $USER == ian ]]; then
53 # note, symlinks get resolved, not copied.
54 m s cp -RLT --preserve=mode,timestamps $fs /
55 fi
56
57 # one program so far does not work with a symlink,
58 # so we have to use a bind mount
59 bdir=$dir/binds
60 if [[ -e $bdir && $USER == ian ]]; then
61 for f in $(find $bdir -type f); do
62 dst=/home/ian/${f##$bdir}
63 binds+="$f $dst none bind 0 0"$'\n'
64 if [[ ! -e $dst || $(stat -c%i $f $dst |uniq -u) ]]; then
65 if mountpoint -q $dst; then
66 s umount $dst
67 fi
68 # note: in future, may need to mkdir also
69 [[ -e $dst ]] || touch $dst
70 s mount --bind $f $dst
71 fi
72 done
73 fi
74
75 if [[ -e $dir/subdir_files ]]; then
76 subdir-link-r $dir/subdir_files
77 fi
78 local x=( $dir/!(binds|subdir_files|filesystem|machine_specific|..|.) )
79 (( ${#x[@]} >= 1 )) || continue
80 m lnf ${x[@]} ~
81 done
82 }
83
84 binds=""
85 all_dirs=({/a/c,/p/c}{,/machine_specific/$HOSTNAME})
86 # note, we assume a group of hosts does not have the
87 # same name as a single host, which is no problem on our scale.
88 for x in /p/c/machine_specific/*.hosts; do
89 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
90 done
91
92 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
93 case $USER in
94 ian)
95 # p needs to go first so .ssh link is created, then config link inside it
96 common-file-setup ${all_dirs[@]}
97 if [[ $binds ]]; then
98 s /a/exe/cedit conflink /etc/fstab <<<"$binds" || [[ $? == 1 ]]
99 fi
100 if [[ -d /etc/bind/bind-writable ]]; then
101 # need bind writable dir for nsupdate, or else we get
102 # named[20823]: /etc/bind/db.iank.pw.jnl: create: permission denied
103 s chgrp bind /etc/bind/bind-writable
104 fi
105 sudo -u traci "$BASH_SOURCE"
106 ;;
107 traci)
108 common-file-setup ${c_dirs[@]}
109 ;;
110 *)
111 echo "$0: error: unexpected user"; exit 1
112 ;;
113 esac