code-dumps/sh/mount-ro-xinetd.sh
2019-02-19 23:33:44 +08:00

51 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# script for use with xinetd to monitor readonly filesystem
#
# xinetd config
# service fsro
#{
# disable = no
# flags = REUSE
# socket_type = stream
# type = UNLISTED
# port = 3333
# wait = no
# user = nobody
# server = /usr/local/sbin/mount-ro-xinetd.sh
# log_on_failure += USERID
# only_from = 127.0.0.0/8
#}
#
function check_ok() {
MSG="$1 healthy"
LEN=$((${#MSG} + 2))
echo -en "HTTP/1.1 200 OK\r\n"
echo -en "Content-Type: text/plain\r\n"
echo -en "Connection: close\r\n"
echo -en "Content-Length: $LEN\r\n"
echo -en "\r\n"
echo -en "$MSG\r\n"
sleep 0.1
exit 0
}
function check_fail() {
MSG="$1"
LEN=$((${#MSG} + 2))
echo -en "HTTP/1.1 503 Service Unavailable\r\n"
echo -en "Content-Type: text/plain\r\n"
echo -en "Connection: close\r\n"
echo -en "Content-Length: $LEN\r\n"
echo -en "\r\n"
echo -en "$MSG\r\n"
sleep 0.1
exit 1
}
CHECK_PATH=/tmp
grep -q $CHECK_PATH /proc/mounts || check_fail "Mount missing: $CHECK_PATH"
dd if=/dev/urandom of=$CHECK_PATH/.test01 bs=1M count=10 status=none || check_fail "Write failed: $CHECK_PATH"
dd if=$CHECK_PATH/.test01 of=/dev/null bs=1M status=none || check_fail "Read failed: $CHECK_PATH"
check_ok "$CHECK_PATH"