Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.00 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. #. /etc/rc.conf
  4.  
  5.  
  6. #. /etc/rc.d/functions
  7. #=====================================================================================
  8. #!/bin/bash
  9. # initscripts functions
  10. #
  11.  
  12. # sanitize PATH (will be overridden later when /etc/profile is sourced but is useful for udev)
  13. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  14.  
  15. # clear the TZ envvar, so daemons always respect /etc/localtime
  16. unset TZ
  17.  
  18. . /etc/profile.d/locale.sh
  19.  
  20. if [[ $1 == "start" ]]; then
  21. if [[ $STARTING ]]; then
  22. echo "A daemon is starting another daemon; this is unlikely to work as intended."
  23. else
  24. export STARTING=1
  25. fi
  26. fi
  27.  
  28. # width:
  29. calc_columns () {
  30. STAT_COL=80
  31. if [[ ! -t 1 ]]; then
  32. USECOLOR=""
  33. elif [[ -t 0 ]]; then
  34. # stty will fail when stdin isn't a terminal
  35. STAT_COL=$(stty size)
  36. # stty gives "rows cols"; strip the rows number, we just want columns
  37. STAT_COL=${STAT_COL##* }
  38. elif tput cols &>/dev/null; then
  39. # is /usr/share/terminfo already mounted, and TERM recognized?
  40. STAT_COL=$(tput cols)
  41. fi
  42. if (( STAT_COL == 0 )); then
  43. # if output was 0 (serial console), set default width to 80
  44. STAT_COL=80
  45. USECOLOR=""
  46. fi
  47.  
  48. # we use 13 characters for our own stuff
  49. STAT_COL=$(( STAT_COL - 13 ))
  50.  
  51. if [[ -t 1 ]]; then
  52. SAVE_POSITION="\e[s"
  53. RESTORE_POSITION="\e[u"
  54. DEL_TEXT="\e[$(( STAT_COL + 4 ))G"
  55. else
  56. SAVE_POSITION=""
  57. RESTORE_POSITION=""
  58. DEL_TEXT=""
  59. fi
  60. }
  61.  
  62. calc_columns
  63.  
  64. # disable colors on broken terminals
  65. TERM_COLORS=$(tput colors 2>/dev/null)
  66. if (( $? != 3 )); then
  67. case $TERM_COLORS in
  68. *[!0-9]*) USECOLOR="";;
  69. [0-7]) USECOLOR="";;
  70. '') USECOLOR="";;
  71. esac
  72. fi
  73. unset TERM_COLORS
  74.  
  75. unquote() {
  76. local -r quotes=$'[\'"]'
  77.  
  78. if [[ ${1:0:1} = $quotes && ${1:(-1)} = "${1:0:1}" ]]; then
  79. printf '%s' "${1:1:(-1)}"
  80. else
  81. printf '%s' "$1"
  82. fi
  83. }
  84.  
  85. parse_envfile() {
  86. local file=$1 validkeys=("${@:2}") ret=0 lineno=0 key= val=
  87. local -r comments=$'[;#]*'
  88.  
  89. if [[ -z $file ]]; then
  90. printf "error: no environment file specified\n"
  91. return 1
  92. fi
  93.  
  94. if [[ ! -f $file ]]; then
  95. printf "error: cannot parse \`%s': No such file or directory\n" "$file"
  96. return 1
  97. fi
  98.  
  99. if [[ ! -r $file ]]; then
  100. printf "error: cannot read \`%s': Permission denied\n" "$file"
  101. return 1
  102. fi
  103.  
  104. while IFS='=' read -r key val; do
  105. (( ++lineno ))
  106.  
  107. # trim whitespace, avoiding usage of a tempfile
  108. key=$(echo "$key" | { read -r key; echo "$key"; })
  109.  
  110. # key must exist and line must not be a comment
  111. [[ -z $key || ${key:0:1} = $comments ]] && continue
  112.  
  113. # trim whitespace, strip matching quotes
  114. val=$(echo "$val" | { read -r val; unquote "$val"; })
  115.  
  116. if [[ -z $val ]]; then
  117. printf "error: found key \`%s' without value on line %s of %s\n" \
  118. "$key" "$lineno" "$file"
  119. (( ++ret ))
  120. continue
  121. fi
  122.  
  123. # ignore invalid keys if we have a list of valid ones
  124. if (( ${#validkeys[*]} )) && ! in_array "$key" "${validkeys[@]}"; then
  125. continue
  126. fi
  127.  
  128. export "$key=$val" || (( ++ret ))
  129. done <"$file"
  130.  
  131. return $ret
  132. }
  133.  
  134. # functions:
  135.  
  136. deltext() {
  137. printf "${DEL_TEXT}"
  138. }
  139.  
  140. print_depr() {
  141. printf "${C_FAIL} ${1} is deprecated. See ${2} for details.${C_CLEAR} \n"
  142. }
  143.  
  144. printhl() {
  145. printf "${C_OTHER}${PREFIX_HL} ${C_H1}${1}${C_CLEAR} \n"
  146. }
  147.  
  148. printsep() {
  149. printf "\n${C_SEPARATOR} ------------------------------\n"
  150. }
  151.  
  152. stat_bkgd() {
  153. printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
  154. deltext
  155. printf " ${C_OTHER}[${C_BKGD}BKGD${C_OTHER}]${C_CLEAR} \n"
  156. }
  157.  
  158. stat_busy() {
  159. printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
  160. printf "${SAVE_POSITION}"
  161. deltext
  162. printf " ${C_OTHER}[${C_BUSY}BUSY${C_OTHER}]${C_CLEAR} "
  163. }
  164.  
  165. stat_append() {
  166. printf "${RESTORE_POSITION}"
  167. printf -- "${C_MAIN}${1}${C_CLEAR}"
  168. printf "${SAVE_POSITION}"
  169. }
  170.  
  171. stat_done() {
  172. deltext
  173. printf " ${C_OTHER}[${C_DONE}DONE${C_OTHER}]${C_CLEAR} \n"
  174. }
  175.  
  176. stat_fail() {
  177. deltext
  178. printf " ${C_OTHER}[${C_FAIL}FAIL${C_OTHER}]${C_CLEAR} \n"
  179. }
  180.  
  181. stat_die() {
  182. stat_fail
  183. exit ${1:-1}
  184. }
  185.  
  186. status() {
  187. local quiet
  188. case $1 in
  189. -q)
  190. quiet=1
  191. ;;&
  192. -v)
  193. # NOOP: supported for backwards compat
  194. shift
  195. ;;
  196. esac
  197. stat_busy "$1"
  198. shift
  199. if (( quiet )); then
  200. "$@" &>/dev/null
  201. else
  202. "$@"
  203. fi
  204. local ret=$?
  205. (( ret == 0 )) && stat_done || stat_fail
  206. return $ret
  207. }
  208.  
  209. # usage : in_array( $needle, $haystack )
  210. # return : 0 - found
  211. # 1 - not found
  212. in_array() {
  213. local needle=$1; shift
  214. local item
  215. for item; do
  216. [[ $item = "${needle}" ]] && return 0
  217. done
  218. return 1 # Not Found
  219. }
  220.  
  221. # daemons:
  222.  
  223. add_daemon() {
  224. [[ -d /run/daemons ]] || mkdir -p /run/daemons
  225. >| /run/daemons/"$1"
  226. }
  227.  
  228. rm_daemon() {
  229. rm -f /run/daemons/"$1"
  230. }
  231.  
  232. ck_daemon() {
  233. [[ ! -f /run/daemons/$1 ]]
  234. }
  235.  
  236. # Check if $1 is a valid daemon name
  237. have_daemon() {
  238. [[ -f /etc/rc.d/$1 && -x /etc/rc.d/$1 ]]
  239. }
  240.  
  241. # Check if $1 is started at boot
  242. ck_autostart() {
  243. local daemon
  244. for daemon in "${DAEMONS[@]}"; do
  245. [[ $1 = "${daemon#@}" ]] && return 1
  246. done
  247. return 0
  248. }
  249.  
  250. start_daemon() {
  251. have_daemon "$1" && /etc/rc.d/"$1" start
  252. }
  253.  
  254. start_daemon_bkgd() {
  255. stat_bkgd "Starting $1"
  256. (start_daemon "$1") >/dev/null &
  257. }
  258.  
  259. stop_daemon() {
  260. have_daemon "$1" && /etc/rc.d/"$1" stop
  261. }
  262.  
  263. # Status functions
  264. status_started() {
  265. deltext
  266. echo -ne "$C_OTHER[${C_STRT}STARTED$C_OTHER]$C_CLEAR "
  267. }
  268.  
  269. status_stopped() {
  270. deltext
  271. echo -ne "$C_OTHER[${C_STRT}STOPPED$C_OTHER]$C_CLEAR "
  272. }
  273.  
  274. ck_status() {
  275. ! ck_daemon "$1" && status_started || status_stopped
  276. }
  277.  
  278. # Return PID of $1
  279. get_pid() {
  280. pidof -o %PPID $1 || return 1
  281. }
  282.  
  283. # Check if PID-file $1 is still the active PID-file for command $2
  284. ck_pidfile() {
  285. if [[ -f $1 ]]; then
  286. local fpid ppid
  287. read -r fpid <"$1"
  288. ppid=$(get_pid "$2")
  289. [[ $fpid = "${ppid}" ]] && return 0
  290. fi
  291. return 1
  292. }
  293.  
  294. # PIDs to be omitted by killall5
  295. declare -a omit_pids
  296.  
  297. add_omit_pids() {
  298. omit_pids+=( $@ )
  299. }
  300.  
  301. # Stop all daemons
  302. # This function should *never* ever perform any other actions beside calling stop_daemon()!
  303. # It might be used by a splash system etc. to get a list of daemons to be stopped.
  304. stop_all_daemons() {
  305. # Find daemons NOT in the DAEMONS array. Shut these down first
  306. local daemon
  307. for daemon in /run/daemons/*; do
  308. [[ -f $daemon ]] || continue
  309. daemon=${daemon##*/}
  310. ck_autostart "$daemon" && stop_daemon "$daemon"
  311. done
  312.  
  313. # Shutdown daemons in reverse order
  314. local i daemon
  315. for (( i=${#DAEMONS[@]}-1; i>=0; i-- )); do
  316. [[ ${DAEMONS[i]} = '!'* ]] && continue
  317. daemon=${DAEMONS[i]#@}
  318. ck_daemon "$daemon" || stop_daemon "$daemon"
  319. done
  320. }
  321.  
  322. # $1 - signal
  323. # $2 - iterations
  324. kill_all_wait() {
  325. # Send SIGTERM/SIGKILL all processes and wait until killall5
  326. # reports all done or timeout.
  327. # Unfortunately killall5 does not support the 0 signal, so just
  328. # use SIGCONT for checking (which should be ignored).
  329.  
  330. local i
  331.  
  332. killall5 -${1} ${omit_pids[@]/#/-o } &>/dev/null
  333.  
  334. for (( i=0; i<${2}; i++ )); do
  335.  
  336. sleep .25 # 1/4 second
  337.  
  338. # sending SIGCONT to processes to check if they are there
  339. killall5 -18 ${omit_pids[@]/#/-o } &>/dev/null
  340.  
  341. if (( $? == 2 )); then
  342. return 0
  343. fi
  344. done
  345.  
  346. return 1
  347. }
  348.  
  349. kill_all() {
  350. stat_busy "Sending SIGTERM to processes"
  351. kill_all_wait 15 40
  352. if (( $? == 0 )); then
  353. stat_done
  354. else
  355. stat_fail
  356. status "Sending SIGKILL to processes" kill_all_wait 9 60
  357. fi
  358. }
  359.  
  360. print_welcome() {
  361. # see os-release(5)
  362. . /etc/os-release
  363.  
  364. echo " "
  365. printhl "${PRETTY_NAME}\n"
  366. printhl "${C_H2}${HOME_URL}"
  367. printsep
  368. }
  369.  
  370. load_modules() {
  371. local rc=0
  372.  
  373. if [[ $MODULES ]]; then
  374. print_depr "MODULES=" "rc.conf(5) and modules-load.d(5)"
  375. /usr/lib/systemd/arch-modules-load
  376. rc=$?
  377. fi
  378.  
  379. /usr/lib/systemd/systemd-modules-load
  380. (( rc+=$? ))
  381.  
  382. return $rc
  383. }
  384.  
  385. # Start/trigger udev, load MODULES, and settle udev
  386. udevd_modprobe() {
  387. # $1 = where we are being called from.
  388. # This is used to determine which hooks to run.
  389. status "Starting udev daemon" /usr/lib/systemd/systemd-udevd --daemon
  390.  
  391. run_hook "$1_udevlaunched"
  392.  
  393. stat_busy "Triggering udev uevents"
  394. udevadm trigger --action=add --type=subsystems
  395. udevadm trigger --action=add --type=devices
  396. stat_done
  397.  
  398. # Load modules from the MODULES array and modules-load.d
  399. status "Loading user-specified modules" load_modules
  400.  
  401. status "Waiting for udev uevents to be processed" \
  402. udevadm settle
  403.  
  404. run_hook "$1_udevsettled"
  405.  
  406. # in case loading a module changed the display mode
  407. calc_columns
  408. }
  409.  
  410. activate_vgs() {
  411. [[ $USELVM = [yY][eE][sS] && -x $(type -P lvm) && -d /sys/block ]] || return 0
  412. stat_busy "Activating LVM2 groups"
  413. vgchange --sysinit -a y >/dev/null
  414. (( $? == 0 )) && stat_done || stat_fail
  415. }
  416.  
  417. do_unlock_legacy() {
  418. # $1 = requested name
  419. # $2 = source device
  420. # $3 = password
  421. # $4 = options
  422. print_depr "The legacy crypttab format" "crypttab(5)"
  423. local open=create a=$1 b=$2 failed=0
  424. # Ordering of options is different if you are using LUKS vs. not.
  425. # Use ugly swizzling to deal with it.
  426. # isLuks only gives an exit code but no output to stdout or stderr.
  427. if cryptsetup isLuks "$2" 2>/dev/null; then
  428. open=luksOpen
  429. a=$2
  430. b=$1
  431. fi
  432. case $3 in
  433. SWAP)
  434. local _overwriteokay=0
  435. if [[ -b $2 && -r $2 ]]; then
  436. # This is DANGEROUS! If there is any known file system,
  437. # partition table, RAID, or LVM volume on the device,
  438. # we don't overwrite it.
  439. #
  440. # 'blkid' returns 2 if no valid signature has been found.
  441. # Only in this case should we allow overwriting the device.
  442. #
  443. # This sanity check _should_ be sufficient, but it might not.
  444. # This may cause data loss if it is not used carefully.
  445. blkid -p "$2" &>/dev/null
  446. (( $? == 2 )) && _overwriteokay=1
  447. fi
  448. if (( _overwriteokay == 0 )); then
  449. false
  450. elif cryptsetup -d /dev/urandom $4 $open "$a" "$b" >/dev/null; then
  451. printf "creating swapspace..\n"
  452. mkswap -f -L $1 /dev/mapper/$1 >/dev/null
  453. fi;;
  454. ASK)
  455. printf "\nOpening '$1' volume:\n"
  456. cryptsetup $4 $open "$a" "$b" < /dev/console;;
  457. /dev*)
  458. local ckdev=${3%%:*}
  459. local cka=${3#*:}
  460. local ckb=${cka#*:}
  461. local cka=${cka%:*}
  462. local ckfile=/dev/ckfile
  463. local ckdir=/dev/ckdir
  464. case ${cka} in
  465. *[!0-9]*)
  466. # Use a file on the device
  467. # cka is not numeric: cka=filesystem, ckb=path
  468. mkdir ${ckdir}
  469. mount -r -t ${cka} ${ckdev} ${ckdir}
  470. dd if=${ckdir}/${ckb} of=${ckfile} >/dev/null 2>&1
  471. umount ${ckdir}
  472. rmdir ${ckdir};;
  473. *)
  474. # Read raw data from the block device
  475. # cka is numeric: cka=offset, ckb=length
  476. dd if=${ckdev} of=${ckfile} bs=1 skip=${cka} count=${ckb} >/dev/null 2>&1;;
  477. esac
  478. cryptsetup -d ${ckfile} $4 $open "$a" "$b" >/dev/null
  479. dd if=/dev/urandom of=${ckfile} bs=1 count=$(stat -c %s ${ckfile}) conv=notrunc >/dev/null 2>&1
  480. rm ${ckfile};;
  481. /*)
  482. cryptsetup -d "$3" $4 $open "$a" "$b" >/dev/null;;
  483. *)
  484. echo "$3" | cryptsetup $4 $open "$a" "$b" >/dev/null;;
  485. esac
  486. return $?
  487. }
  488.  
  489. do_unlock_systemd() {
  490. local name=$1 device=$2 password=$3 options=$4 failed=0
  491.  
  492. # lazily convert tags to udev symlinks
  493. case $device in
  494. UUID=*)
  495. device=/dev/disk/by-uuid/$(unquote "${device#UUID=}")
  496. ;;
  497. PARTUUID=*)
  498. device=/dev/disk/by-partuuid/$(unquote "${device#PARTUUID=}")
  499. ;;
  500. esac
  501.  
  502. if ! /usr/lib/systemd/systemd-cryptsetup attach "$name" "$device" "$password" $options; then
  503. failed=1
  504. else
  505. options=${options//,/ }
  506. if in_array swap ${options[@]}; then
  507. # create swap on the device only if no fs signature exists
  508. blkid -p "$2" &>/dev/null
  509. if (( $? != 2 )) || ! mkswap -f /dev/mapper/$name >/dev/null; then
  510. failed=1
  511. fi
  512. elif in_array tmp ${options[@]}; then
  513. # create fs on the device only if no fs signature exists
  514. blkid -p "$2" &>/dev/null
  515. if (( $? != 2 )) || ! mke2fs /dev/mapper/$name >/dev/null; then
  516. failed=1
  517. fi
  518. fi
  519. fi
  520. return $failed
  521. }
  522.  
  523. do_unlock() {
  524. local name=$1 device=$2 password=$3 options=$4
  525.  
  526. printf "${C_MAIN}Unlocking $1${C_CLEAR}\n"
  527.  
  528. if [[ ${options:0:2} =~ -. ]]; then
  529. do_unlock_legacy "$name" "$device" "$password" "$options"
  530. return $?
  531. fi
  532.  
  533. case $password in
  534. ASK|SWAP)
  535. do_unlock_legacy "$name" "$device" "$password" "$options"
  536. ;;
  537. /dev/*)
  538. if [[ ${password##*:} == $password ]]; then
  539. do_unlock_systemd "$name" "$device" "$password" "$options"
  540. else
  541. do_unlock_legacy "$name" "$device" "$password" "$options"
  542. fi
  543. ;;
  544. /*|none|-|'')
  545. do_unlock_systemd "$name" "$device" "$password" "$options"
  546. ;;
  547. *)
  548. do_unlock_legacy "$name" "$device" "$password" "$options"
  549. ;;
  550. esac
  551. failed=$?
  552. if (( $failed )); then
  553. printf "${C_FAIL}Unlocking of $1 failed.${C_CLEAR}\n"
  554. fi
  555. return $?
  556. }
  557.  
  558. do_lock() {
  559. status "Detaching encrypted device ${1}" /usr/lib/systemd/systemd-cryptsetup detach "$1" >/dev/null
  560. }
  561.  
  562. read_crypttab() {
  563. # $1 = function to call with the split out line from the crypttab
  564. local line nspo failed=0
  565. while read line <&3; do
  566. [[ $line && $line != '#'* ]] || continue
  567. eval nspo=("${line%#*}")
  568. if $1 "${nspo[0]}" "${nspo[1]}" "${nspo[2]}" "${nspo[*]:3}"; then
  569. crypto_unlocked=1
  570. else
  571. failed=1
  572. fi
  573. done 3< /etc/crypttab
  574. return $failed
  575. }
  576.  
  577. set_timezone() {
  578. local tz=$1 zonefile=/usr/share/zoneinfo/$1
  579.  
  580. [[ $tz ]] || return 1
  581.  
  582. if [[ ! -e $zonefile ]]; then
  583. printf "error: \`%s' is not a valid time zone\n" "$tz"
  584. return 1
  585. fi
  586.  
  587. if [[ -L /etc/localtime && /etc/localtime -ef $zonefile ]]; then
  588. return 0
  589. else
  590. ln -sf "/usr/share/zoneinfo/$tz" /etc/localtime
  591. fi
  592. }
  593.  
  594. # Filesystem functions
  595. # These can be overridden/reused for customizations like shutdown/loop-fsck.
  596. NETFS="nfs,nfs4,smbfs,cifs,codafs,ncpfs,shfs,fuse,fuseblk,glusterfs,davfs,fuse.glusterfs"
  597.  
  598. # Check local filesystems
  599. fsck_all() {
  600. if [[ -f /forcefsck ]] || in_array forcefsck $(< /proc/cmdline); then
  601. FORCEFSCK="-f"
  602. elif [[ -f /fastboot ]] || in_array fastboot $(< /proc/cmdline); then
  603. return 0
  604. elif [[ -e /run/initramfs/root-fsck ]]; then
  605. IGNORE_MOUNTED="-M"
  606. fi
  607.  
  608. fsck -A -T -C${FSCK_FD} -a -t no${NETFS//,/,no},noopts=_netdev ${IGNORE_MOUNTED} -- ${FORCEFSCK}
  609. }
  610.  
  611. # Single-user login and/or automatic reboot after fsck (if needed)
  612. fsck_reboot() {
  613. # $1 = exit code returned by fsck
  614. # Ignore conditions 'FS errors corrected' and 'Cancelled by the user'
  615. (( ($1 | 33) == 33 )) && return 0
  616. if (( $1 & 2 )); then
  617. echo
  618. echo "********************** REBOOT REQUIRED *********************"
  619. echo "* *"
  620. echo "* The system will be rebooted automatically in 15 seconds. *"
  621. echo "* *"
  622. echo "************************************************************"
  623. echo
  624. sleep 15
  625. else
  626. echo
  627. echo "***************** FILESYSTEM CHECK FAILED ****************"
  628. echo "* *"
  629. echo "* Please repair manually and reboot. Note that the root *"
  630. echo "* file system is currently mounted read-only. To remount *"
  631. echo "* it read-write, type: mount -o remount,rw / *"
  632. echo "* When you exit the maintenance shell, the system will *"
  633. echo "* reboot automatically. *"
  634. echo "* *"
  635. echo "************************************************************"
  636. echo
  637. sulogin -p
  638. fi
  639. echo "Automatic reboot in progress..."
  640. umount -a
  641. mount -o remount,ro /
  642. reboot -f
  643. exit 0
  644. }
  645.  
  646. mount_all() {
  647. mount -a -t "no${NETFS//,/,no}" -O no_netdev
  648. }
  649.  
  650. umount_all() {
  651. # $1: restrict to fstype
  652.  
  653. findmnt -mrunRo TARGET,FSTYPE,OPTIONS / | {
  654. while read -r target fstype options; do
  655. # match only targeted fstypes
  656. if [[ $1 && $1 != "$fstype" ]]; then
  657. continue
  658. fi
  659.  
  660. # do not unmount API filesystems
  661. if [[ $target = /@(proc|sys|run|dev|dev/pts) ]]; then
  662. continue
  663. fi
  664.  
  665. # avoid networked devices
  666. IFS=, read -ra opts <<< "$options"
  667. if in_array _netdev "${opts[@]}"; then
  668. continue
  669. fi
  670.  
  671. mounts=("$target" "${mounts[@]}")
  672. done
  673.  
  674. if (( ${#mounts[*]} )); then
  675. umount -r "${mounts[@]}"
  676. fi
  677. }
  678.  
  679. }
  680.  
  681. remove_leftover() {
  682. status 'Removing leftover files' systemd-tmpfiles --create --remove --clean
  683. }
  684.  
  685. bootlogd_stop() {
  686. [[ -f /run/bootlogd.pid ]] || return 0
  687. touch /var/log/boot
  688. kill $(< /run/bootlogd.pid)
  689. rm -f /run/bootlogd.pid
  690. }
  691.  
  692. ###############################
  693. # Custom hooks in initscripts #
  694. ###############################
  695. # Hooks can be used to include custom code in various places in the rc.* scripts
  696. #
  697. # Define a hook function in a functions.d file using:
  698. # function_name() {
  699. # ...
  700. # }
  701. # add_hook hook_name function_name
  702. # It is allowed to register several hook functions for the same hook
  703. # Is is also allowed to register the same hook function for several hooks
  704. #
  705. # Currently, the following hooks exist:
  706. # sysinit_start: at the beginning of rc.sysinit
  707. # multi_start: at the beginning of rc.multi
  708. # single_start: at the beginning of rc.single
  709. # shutdown_start: at the beginning of rc.shutdown
  710. # sysinit_end: at the end of rc.sysinit
  711. # multi_end: at the end of rc.multi
  712. # single_end: at the end of rc.single
  713. # sysinit_udevlaunched: after udev has been launched in rc.sysinit
  714. # single_udevlaunched: after udev has been launched in rc.single
  715. # sysinit_udevsettled: after uevents have settled in rc.sysinit
  716. # single_udevsettled: after uevents have settled in rc.single
  717. # sysinit_premount: before local filesystems are mounted, but after root is mounted read-write in rc.sysinit
  718. # sysinit_postmount: after local filesystems are mounted
  719. # shutdown_prekillall: before all processes are being killed in rc.shutdown
  720. # single_prekillall: before all processes are being killed in rc.single
  721. # shutdown_postkillall: after all processes have been killed in rc.shutdown
  722. # single_postkillall: after all processes have been killed in rc.single
  723. # shutdown_preumount: after last filesystem write, but before filesystems are unmounted
  724. # shutdown_postumount: after filesystems are unmounted
  725. # shutdown_poweroff: directly before powering off in rc.shutdown
  726. #
  727. # Declare add_hook and run_hook as read-only to prevent overwriting them.
  728. # Too bad we cannot do the same thing with hook_funcs
  729.  
  730. if (( RC_FUNCTIONS_HOOK_FUNCS_DEFINED != 1 )); then
  731. declare -A hook_funcs
  732.  
  733. add_hook() {
  734. [[ $1 && $2 ]] || return 1
  735. hook_funcs[$1]+=" $2"
  736. }
  737.  
  738. run_hook() {
  739. [[ $1 ]] || return 1
  740. local func
  741. for func in ${hook_funcs["$1"]}; do
  742. "${func}"
  743. done
  744. }
  745.  
  746. declare -fr add_hook run_hook
  747. declare -r RC_FUNCTIONS_HOOK_FUNCS_DEFINED=1
  748. fi
  749.  
  750. # set colors
  751. if [[ $USECOLOR != [nN][oO] ]]; then
  752. if tput setaf 0 &>/dev/null; then
  753. C_CLEAR=$(tput sgr0) # clear text
  754. C_MAIN=${C_CLEAR}$(tput bold) # main text
  755. C_OTHER=${C_MAIN}$(tput setaf 4) # prefix & brackets
  756. C_SEPARATOR=${C_MAIN}$(tput setaf 0) # separator
  757. C_BUSY=${C_CLEAR}$(tput setaf 6) # busy
  758. C_FAIL=${C_MAIN}$(tput setaf 1) # failed
  759. C_DONE=${C_MAIN} # completed
  760. C_BKGD=${C_MAIN}$(tput setaf 5) # backgrounded
  761. C_H1=${C_MAIN} # highlight text 1
  762. C_H2=${C_MAIN}$(tput setaf 6) # highlight text 2
  763. else
  764. C_CLEAR="\e[m" # clear text
  765. C_MAIN="\e[;1m" # main text
  766. C_OTHER="\e[1;34m" # prefix & brackets
  767. C_SEPARATOR="\e[1;30m" # separator
  768. C_BUSY="\e[;36m" # busy
  769. C_FAIL="\e[1;31m" # failed
  770. C_DONE=${C_MAIN} # completed
  771. C_BKGD="\e[1;35m" # backgrounded
  772. C_H1=${C_MAIN} # highlight text 1
  773. C_H2="\e[1;36m" # highlight text 2
  774. fi
  775. fi
  776.  
  777. # prefixes:
  778.  
  779. PREFIX_REG="::"
  780. PREFIX_HL=" >"
  781.  
  782. # Source additional functions at the end to allow overrides
  783. for f in /etc/rc.d/functions.d/*; do
  784. [[ -e $f ]] && . "$f"
  785. done
  786.  
  787. # End of file
  788. # vim: set ts=2 sw=2 noet:
  789. #=====================================================================================
  790.  
  791.  
  792.  
  793.  
  794. . /etc/conf.d/key-file-check.conf
  795.  
  796. abbruch () {
  797. stat_fail
  798. exit 1
  799. }
  800. if [ "$2" = "vg" -o "$2" = "novg" ]; then
  801. case "$1" in
  802. start)
  803. stat_busy "Unlocking encrypted Devices"
  804. VERIFY=$(mount | grep $LV | cut --delimiter=" " -f3)
  805. closeusb () {
  806. echo "stop"
  807. sleep 1
  808. umount $MOUNTPOINT1 && echo "USB-storage unmounted"
  809. abbruch
  810. }
  811. USBSTICK=$(ls /dev/disk/by-uuid/ | grep $UUID)
  812. if [[ $VERIFY != $MOUNTPOINT2 ]]; then
  813. if [[ $USBSTICK = $UUID ]]; then
  814. echo "USB-storage with key-file: found"
  815. echo "mounting (read-only) USB-storage:"
  816. mount -o ro /dev/disk/by-uuid/$USBSTICK $MOUNTPOINT1 || abbruch
  817. echo "ok"
  818. echo "opening encrypted device ..."
  819. cryptsetup luksOpen $VOLUME data-$LV-luks-io --key-file=$MOUNTPOINT1/.Thumb0.db || closeusb
  820. umount $MOUNTPOINT1 || abbruch
  821. echo "USB-storage unmounted"
  822. else
  823. echo "USB-storage with key-file: not found"
  824. cryptsetup luksOpen $VOLUME data-$LV-luks-io || abbruch
  825. echo "encrypted device unlocked"
  826. fi || abbruch
  827. if [ "$2" = "vg" ]; then
  828. vgchange -ay || abbruch
  829. mount /dev/disk/by-label/$LV $MOUNTPOINT2 || abbruch
  830. echo "encrypted device unlocked and logical volume mounted"
  831. fi
  832. else
  833. echo "logical volume already mounted on $MOUNTPOINT2"
  834. fi
  835. stat_done
  836. ;;
  837. stop)
  838. stat_busy "Locking Encrypted Devices"
  839. if [ "$2" = "vg" ]; then
  840. umount $MOUNTPOINT2 || abbruch
  841. vgchange -an || abbruch
  842. echo "logical device unmounted and encrypted volume locked"
  843. fi
  844. cryptsetup luksClose /dev/mapper/data-$LV-luks-io || abbruch
  845. stat_done
  846. ;;
  847. restart)
  848. $0 stop
  849. sleep 1
  850. $0 start
  851. ;;
  852. *)
  853. echo "usage: $0 {start|stop|restart} {vg|novg}"
  854. esac
  855. else
  856. echo "usage: $0 {start|stop|restart} {vg|novg}"
  857. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement