| 1 |
#!/bin/sh |
| 2 |
# /usr/local/sbin/dphys-config-filesys - add filesystem stuff |
| 3 |
# author Neil Franklin, last modification 2006.10.12 |
| 4 |
# derived from dphys-setup-extra-fs 1.0, Elmar S. Heeb <[email protected]> |
| 5 |
# 2009.07.02 because of [ -o ] changed to ( [] || [] ), not tested after!!! |
| 6 |
# copyright ETH Zuerich Physics Departement, |
| 7 |
# use under either modified/non-advertising BSD or GPL license |
| 8 |
|
| 9 |
# this script is called by dphys-config |
| 10 |
# once on each config list file or script install/update |
| 11 |
# no parameters or environment variables are evaluated |
| 12 |
# reads all input from dirnames list file /etc/dphys-config-filesys.list |
| 13 |
# it requires in /etc/dphys-config.list 2 lines, for config file and script: |
| 14 |
# dphys-config-filesys.list:/etc/:if [ -f /usr/local/sbin/dphys-config-filesys ] ; then /usr/local/sbin/dphys-config-filesys ; fi |
| 15 |
# local/sbin/dphys-config-filesys:/usr/:chmod 755 {}; {} |
| 16 |
|
| 17 |
|
| 18 |
### ------ configuration for this site |
| 19 |
|
| 20 |
# --- CONF_* various site or subnet dependant user config variables |
| 21 |
|
| 22 |
# log what we have done |
| 23 |
CONF_LOG_DONE=yes |
| 24 |
|
| 25 |
|
| 26 |
# --- DEBUG_*, various debugging settings |
| 27 |
|
| 28 |
# force lots of debugging output |
| 29 |
# this can also be enabled by touch /dphys-config-filesys.debug |
| 30 |
#DEBUG_LOG_STEP=yes |
| 31 |
|
| 32 |
|
| 33 |
# --- SYS_*, various system internal values |
| 34 |
|
| 35 |
# name of directory names list file on server |
| 36 |
SYS_CONFLIST=/etc/dphys-config-filesys.list |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
### ------ actual implementation from here on |
| 41 |
# no user settings any more below this point |
| 42 |
|
| 43 |
set -e |
| 44 |
|
| 45 |
|
| 46 |
# --- get ready to work |
| 47 |
|
| 48 |
# sanitise this place, else some commands may fail |
| 49 |
# must be before any commands are executed, incl config/input processing |
| 50 |
# /usr/local added for FreeBSD, because their ports end up in there |
| 51 |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin |
| 52 |
export PATH |
| 53 |
|
| 54 |
|
| 55 |
# --- tidy up some commands, make systematic |
| 56 |
|
| 57 |
# stuff that goes wrong, not expected by user, not in data output, use >&2 |
| 58 |
# so also with $0 in case this script was called by an other script |
| 59 |
# something within the system that user does not expect, give up |
| 60 |
CMD_FATAL="echo $0: FATAL:" |
| 61 |
# something from users input, user will correct this and continue |
| 62 |
CMD_ERROR="echo $0: ERROR:" |
| 63 |
# something we can continue with, but may be wrong, and user may not suspect it |
| 64 |
CMD_WARNING="echo $0: WARNING:" |
| 65 |
# something most likely not wrong, but tell user for the odd case it is wrong |
| 66 |
CMD_NOTE="echo $0: NOTE:" |
| 67 |
|
| 68 |
# normal stuff users expect, so to stdout as normal output, no $0, no marking |
| 69 |
CMD_INFO="echo" |
| 70 |
# stuff users asked for, so add to stdout as normal output, no $0, but mark it |
| 71 |
CMD_DEBUG="echo DEBUG:" |
| 72 |
|
| 73 |
|
| 74 |
# and also put out some permanent messages, in case running as cron/init.d job |
| 75 |
BASENAME="`basename $0`" |
| 76 |
# something within the system that user does not expect, give up |
| 77 |
CMD_LOG_FATAL="logger -t ${BASENAME} -p user.error FATAL:" |
| 78 |
# something from users input, user can possibly correct this and continue |
| 79 |
CMD_LOG_ERROR="logger -t ${BASENAME} -p user.error ERROR:" |
| 80 |
# something we can continue with, but may be wrong, and user may not suspect it |
| 81 |
CMD_LOG_WARNING="logger -t ${BASENAME} -p user.warning WARNING:" |
| 82 |
# something most likely not wrong, but tell user for the odd case it is wrong |
| 83 |
CMD_LOG_NOTE="logger -t ${BASENAME} -p user.notice NOTE:" |
| 84 |
|
| 85 |
# normal stuff users expect, so no marking |
| 86 |
CMD_LOG_INFO="logger -t ${BASENAME} -p user.info" |
| 87 |
# stuff users asked for, but mark it as special |
| 88 |
CMD_LOG_DEBUG="logger -t ${BASENAME} -p user.debug DEBUG:" |
| 89 |
|
| 90 |
|
| 91 |
# other stuff we may want to use |
| 92 |
CMD_SLEEP="sleep 2" |
| 93 |
CMD_WAIT="read -p ---DEBUG-wait-after-step--- dummy" |
| 94 |
|
| 95 |
|
| 96 |
# --- config file stuff |
| 97 |
|
| 98 |
# what we are |
| 99 |
NAME=dphys-config-filesys |
| 100 |
PNAME=dphys-config-filesys |
| 101 |
|
| 102 |
# check user config file(s), let user override settings |
| 103 |
if [ -e /etc/${PNAME} ] ; then |
| 104 |
. /etc/${PNAME} |
| 105 |
fi |
| 106 |
|
| 107 |
# allow us to set logging despite no command line options and parser |
| 108 |
if [ -f /${NAME}.debug ] ; then |
| 109 |
DEBUG_LOG_STEP=yes |
| 110 |
fi |
| 111 |
|
| 112 |
|
| 113 |
# --- control variable output |
| 114 |
|
| 115 |
# set config option controllable stuff |
| 116 |
if [ x${CONF_LOG_DONE} = xyes ] ; then |
| 117 |
CMD_LOG_IF_DONE="${CMD_LOG_INFO}" |
| 118 |
else |
| 119 |
CMD_LOG_IF_DONE=true |
| 120 |
fi |
| 121 |
|
| 122 |
# set debug option controllable stuff |
| 123 |
if [ x${DEBUG_LOG_STEP} = xyes ] ; then |
| 124 |
CMD_LOG_IF_DEBUG="${CMD_LOG_DEBUG}" |
| 125 |
else |
| 126 |
CMD_LOG_IF_DEBUG=true |
| 127 |
fi |
| 128 |
|
| 129 |
|
| 130 |
# show what config settings this debug run is using |
| 131 |
${CMD_LOG_IF_DEBUG} "CONF_LOG_DONE=${CONF_LOG_DONE}" |
| 132 |
|
| 133 |
|
| 134 |
# --- install each directory name |
| 135 |
|
| 136 |
if ( [ ! -f "${SYS_CONFLIST}" ] || [ ! -s "${SYS_CONFLIST}" ] ) ; then |
| 137 |
|
| 138 |
# directory name list not found, so we can not work |
| 139 |
${CMD_FATAL} "no or empty directory name list ${SYS_CONFLIST}" >&2 |
| 140 |
${CMD_LOG_FATAL} "no or empty directory name list ${SYS_CONFLIST}" |
| 141 |
exit 1 |
| 142 |
|
| 143 |
fi |
| 144 |
|
| 145 |
# get rid of comments and empty lines (may be emptied comment lines) |
| 146 |
cut -f 1 -d '#' "${SYS_CONFLIST}" | grep -v '^ *$' | while read LINE ; do |
| 147 |
|
| 148 |
${CMD_LOG_IF_DEBUG} "LINE=\"${LINE}\"" |
| 149 |
|
| 150 |
# extract from LINE format: type:parameter[:parameter...] |
| 151 |
if ! echo "${LINE}" | grep -q ':' ; then |
| 152 |
${CMD_ERROR} "config line \"${LINE}\" has no \":\" to split on" >&2 |
| 153 |
${CMD_LOG_ERROR} "config line \"${LINE}\" has no \":\" to split on" |
| 154 |
exit 1 |
| 155 |
fi |
| 156 |
|
| 157 |
TYPE="`echo "${LINE}" | cut -f 1 -d ":"`" |
| 158 |
|
| 159 |
${CMD_LOG_IF_DEBUG} "TYPE=${TYPE}" |
| 160 |
|
| 161 |
case "${TYPE}" in |
| 162 |
|
| 163 |
directory) |
| 164 |
# line format: directory:<dir>:<owner>:<group>:<mode> |
| 165 |
# example: directory:/scratch:root:root:1777 |
| 166 |
DIR="`echo "${LINE}" | cut -f 2 -d ':'`" |
| 167 |
OWNER="`echo "${LINE}" | cut -f 3 -d ':'`" |
| 168 |
GROUP="`echo "${LINE}" | cut -f 4 -d ':'`" |
| 169 |
MODE="`echo "${LINE}" | cut -f 5 -d ':'`" |
| 170 |
|
| 171 |
if [ ! -e "${DIR}" ] ; then |
| 172 |
mkdir -p "${DIR}" |
| 173 |
${CMD_LOG_IF_DEBUG} "made DIR=${DIR}" |
| 174 |
else |
| 175 |
if [ ! -d "${DIR}" ] ; then |
| 176 |
${CMD_ERROR} "File ${DIR} already exists and is not a directory" >&2 |
| 177 |
${CMD_LOG_ERROR} "File ${DIR} already exists and is not a directory" |
| 178 |
exit 1 |
| 179 |
fi |
| 180 |
fi |
| 181 |
chown "${OWNER}:${GROUP}" "${DIR}" |
| 182 |
chmod "${MODE}" "${DIR}" |
| 183 |
${CMD_LOG_IF_DEBUG} "DIR=${DIR}" \ |
| 184 |
"OWNER=${OWNER} GROUP=${GROUP} MODE=${MODE}" |
| 185 |
;; |
| 186 |
|
| 187 |
symlink) |
| 188 |
# line format: symlink:<link>:<target> |
| 189 |
# example: symlink:/pub:/net/debian/export/pub |
| 190 |
LINK="`echo "${LINE}" | cut -f 2 -d ':'`" |
| 191 |
TARGET="`echo "${LINE}" | cut -f 3 -d ':'`" |
| 192 |
if [ "${LINK}" = "" ]; then |
| 193 |
LINK="`basename "${TARGET}"`" |
| 194 |
fi |
| 195 |
|
| 196 |
if [ ! -e "${LINK}" ] ; then |
| 197 |
ln -sf "${TARGET}" "${LINK}" |
| 198 |
${CMD_LOG_IF_DEBUG} "made LINK=${LINK}" |
| 199 |
else |
| 200 |
if [ ! -L "${LINK}" ] ; then |
| 201 |
${CMD_ERROR} "File ${LINK} already exists and is not a symlink" >&2 |
| 202 |
${CMD_LOG_ERROR} "File ${LINK} already exists and is not a symlink" |
| 203 |
exit 1 |
| 204 |
fi |
| 205 |
fi |
| 206 |
${CMD_LOG_IF_DEBUG} "LINK=${LINK} TARGET=${TARGET}" |
| 207 |
;; |
| 208 |
|
| 209 |
partition) |
| 210 |
# line format: partition:<mountpoint>:<device>:<fs-type> |
| 211 |
# example: partition:/export/data1:/dev/sdb1:ext3 |
| 212 |
MPOINT="`echo "${LINE}" | cut -f 2 -d ':'`" |
| 213 |
DEV="`echo "${LINE}" | cut -f 3 -d ':'`" |
| 214 |
FS="`echo "${LINE}" | cut -f 4 -d ':'`" |
| 215 |
|
| 216 |
if [ ! -e "${DEV}" ]; then |
| 217 |
${CMD_ERROR} "line for ${MPOINT} has non-existant" \ |
| 218 |
"device ${DEV}" >&2 |
| 219 |
${CMD_LOG_ERROR} "line for ${MPOINT} has non-existant" \ |
| 220 |
"device ${DEV}" |
| 221 |
exit 1 |
| 222 |
fi |
| 223 |
|
| 224 |
if [ ! -e "${MPOINT}" ] ; then |
| 225 |
mkdir -p "${MPOINT}" |
| 226 |
${CMD_LOG_IF_DEBUG} "made MPOINT=${MPOINT}" |
| 227 |
else |
| 228 |
if [ ! -d "${MPOINT}" ] ; then |
| 229 |
${CMD_ERROR} "File ${MPOINT} already exists and is not directory" >&2 |
| 230 |
${CMD_LOG_ERROR} "File ${MPOINT} already exists and is not directory" |
| 231 |
exit 1 |
| 232 |
fi |
| 233 |
fi |
| 234 |
|
| 235 |
# possible existing line (or empty) |
| 236 |
awk '$2=="'"${MPOINT}"'"{print}' /etc/fstab > /etc/fstab.old.$$ |
| 237 |
# potentially new line we want |
| 238 |
printf "%s\t%s\t%s\t%s\t%s\t%s\n" "${DEV}" "${MPOINT}" "${FS}" \ |
| 239 |
"defaults" "0" "2" > /etc/fstab.new.$$ |
| 240 |
if ! cmp -s /etc/fstab.new.$$ /etc/fstab.old.$$ ; then |
| 241 |
# differs, remove old entry (if present) ... |
| 242 |
awk '$2!="'"${MPOINT}"'"{print}' /etc/fstab > /etc/fstab.tmp.$$ |
| 243 |
# ... and add new entry |
| 244 |
cat /etc/fstab.tmp.$$ /etc/fstab.new.$$ > /etc/fstab |
| 245 |
rm /etc/fstab.tmp.$$ |
| 246 |
fi |
| 247 |
rm /etc/fstab.new.$$ /etc/fstab.old.$$ |
| 248 |
|
| 249 |
mount "${MPOINT}" 2> /dev/null || true |
| 250 |
${CMD_LOG_IF_DEBUG} "MPOINT=${MPOINT} DEV=${DEV} FS=${fFS}" |
| 251 |
;; |
| 252 |
|
| 253 |
nfsmount) |
| 254 |
# line format: nfsmount:<mountpoint>:<server>:<export>:<nfs-options> |
| 255 |
# example: nfsmount:/var/mail:mail.phys:/var/mail:rw,soft,noac,rsize=8192,wsize=8192,retry=1 |
| 256 |
MPOINT="`echo "${LINE}" | cut -f 2 -d ':'`" |
| 257 |
SERVER="`echo "${LINE}" | cut -f 3 -d ':'`" |
| 258 |
EXPORT="`echo "${LINE}" | cut -f 4 -d ':'`" |
| 259 |
NFSOPT="`echo "${LINE}" | cut -f 5 -d ':'`" |
| 260 |
if [ "${NFSOPT}" = "" ]; then |
| 261 |
NFSOPT=defaults |
| 262 |
fi |
| 263 |
|
| 264 |
if [ ! -e "${MPOINT}" ] ; then |
| 265 |
mkdir -p "${MPOINT}" |
| 266 |
${CMD_LOG_IF_DEBUG} "made MPOINT=${MPOINT}" |
| 267 |
else |
| 268 |
if [ ! -d "${MPOINT}" ] ; then |
| 269 |
${CMD_ERROR} "File ${MPOINT} already exists and is not directory" >&2 |
| 270 |
${CMD_LOG_ERROR} "File ${MPOINT} already exists and is not directory" |
| 271 |
exit 1 |
| 272 |
fi |
| 273 |
fi |
| 274 |
|
| 275 |
# possible existing line (or empty) |
| 276 |
awk '$2=="'"${MPOINT}"'"{print}' /etc/fstab > /etc/fstab.old.$$ |
| 277 |
# potentially new line we want |
| 278 |
printf "%s\t%s\t%s\t%s\t%s\t%s\n" "${SERVER}:${EXPORT}" "${MPOINT}" \ |
| 279 |
"nfs" "${NFSOPT}" "0" "0" > /etc/fstab.new.$$ |
| 280 |
if ! cmp -s /etc/fstab.new.$$ /etc/fstab.old.$$ ; then |
| 281 |
# differs, remove old entry (if present) ... |
| 282 |
awk '$2!="'"${MPOINT}"'"{print}' /etc/fstab > /etc/fstab.tmp.$$ |
| 283 |
# ... and add new entry |
| 284 |
cat /etc/fstab.tmp.$$ /etc/fstab.new.$$ > /etc/fstab |
| 285 |
rm /etc/fstab.tmp.$$ |
| 286 |
fi |
| 287 |
rm /etc/fstab.new.$$ /etc/fstab.old.$$ |
| 288 |
|
| 289 |
mount "${MPOINT}" 2> /dev/null || true |
| 290 |
${CMD_LOG_IF_DEBUG} "MPOINT=${MPOINT}" \ |
| 291 |
"SERVER=${SERVER} EXPORT=${EXPORT} NFSOPT=${NFSOPT}" |
| 292 |
;; |
| 293 |
|
| 294 |
*) |
| 295 |
|
| 296 |
${CMD_ERROR} "unknown line type: ${TYPE}" >&2 |
| 297 |
${CMD_LOG_ERROR} "unknown line type: ${TYPE}" |
| 298 |
exit 1 |
| 299 |
|
| 300 |
esac |
| 301 |
|
| 302 |
done |
| 303 |
|
| 304 |
|
| 305 |
# --- finish off |
| 306 |
|
| 307 |
${CMD_LOG_IF_DONE} "has updated filesystem and /etc/fstab" |
| 308 |
exit 0 |