minor improvement
[distro-setup] / check-crypttab
1 #!/bin/bash
2
3 # I somehow ended up with an empty crypttab, and had to rescue my
4 # system. So, lets check for some basic validity.
5
6 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
7 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
8 set -eE -o pipefail
9 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
10
11 debug=false
12 if [[ $1 ]]; then
13 debug=true
14 fi
15 d() {
16 if $debug; then
17 printf "%s\n" "$*"
18 fi
19 }
20
21 metric_dir=/var/lib/prometheus/node-exporter
22 mnt="$(findmnt -fn -o SOURCE /)"
23 case $mnt in
24 /dev/mapper/crypt_dev_*)
25 tmp=${mnt#/dev/mapper/}
26 dev=${tmp%%\[*}
27 if ! grep -qF $dev /etc/crypttab; then
28 d $dev
29 if [[ -e $metric_dir ]]; then
30 echo "crypttab_check 1" > $metric_dir/crypttab_check.prom
31 exit 0
32 fi
33 fi
34 ;;
35 esac
36 if [[ -e $metric_dir ]]; then
37 echo "crypttab_check 0" > $metric_dir/crypttab_check.prom
38 fi