]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/extrahd/extrahd.pl
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / config / extrahd / extrahd.pl
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2023 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 log() {
23 local message="${@}"
24
25 logger -t "extrahd" "${message}"
26 }
27
28 extrahd_mount() {
29 local _mountpoint="${1}"
30
31 local device
32 local filesystem
33 local mountpoint
34 local rest
35 local failed=0
36
37 while IFS=';' read -r device filesystem mountpoint rest; do
38 # Filter by UUID or mountpoint
39 case "${_mountpoint}" in
40 UUID=*)
41 if [ "${device}" != "${_mountpoint}" ]; then
42 continue
43 fi
44 ;;
45
46 /*)
47 if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then
48 continue
49 fi
50 ;;
51 esac
52
53 # Check that the mountpoint starts with a slash
54 if [ "${mountpoint:0:1}" != "/" ]; then
55 log "Skipping invalid mountpoint: ${mountpoint}"
56 continue
57 fi
58
59 # Skip mounting if something is already mounted at the mountpoint
60 if mountpoint "${mountpoint}" &>/dev/null; then
61 continue
62 fi
63
64 # Ensure the mountpoint exists
65 mkdir --parents --mode=777 "${mountpoint}" &>/dev/null
66
67 if mount --types "${filesystem}" "${device}" "${mountpoint}"; then
68 log "Successfully mounted ${device} to ${mountpoint}"
69 else
70 log "Could not mount ${device} to ${mountpoint}: $?"
71 failed=1
72 fi
73 done < /var/ipfire/extrahd/devices
74
75 return ${failed}
76 }
77
78 extrahd_umount() {
79 local _mountpoint="${1}"
80
81 local device
82 local filesystem
83 local mountpoint
84 local rest
85 local failed=0
86
87 while IFS=';' read -r device filesystem mountpoint rest; do
88 # Filter by UUID or mountpoint
89 case "${_mountpoint}" in
90 UUID=*)
91 if [ "${device}" != "${_mountpoint}" ]; then
92 continue
93 fi
94 ;;
95
96 /*)
97 if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then
98 continue
99 fi
100 ;;
101 esac
102
103 # Do not try to umount if nothing is mounted
104 if ! mountpoint "${mountpoint}" &>/dev/null; then
105 continue
106 fi
107
108 # Umount and try lazy umount if failed
109 if umount --quiet --recursive "${mountpoint}" || \
110 umount --quiet --recursive --lazy "${mountpoint}"; then
111 log "Successfully umounted ${device} from ${mountpoint}"
112 else
113 log "Could not umount ${device} from ${mountpoint}: $?"
114 failed=1
115 fi
116 done < /var/ipfire/extrahd/devices
117 }
118
119 handle_udev_event() {
120 case "${ACTION}" in
121 add)
122 if [ -n "${ID_FS_UUID}" ]; then
123 extrahd_mount "UUID=${ID_FS_UUID}" || return $?
124 fi
125 ;;
126 esac
127
128 return 0
129 }
130
131 main() {
132 ( echo "$@"; set ) > /tmp/extrahd.$$
133
134 local command="${1}"
135 shift
136
137 local rc=0
138
139 case "${command}" in
140 mount)
141 extrahd_mount "${@}" || rc="${?}"
142 ;;
143 umount)
144 extrahd_umount "${@}" || rc="${rc}"
145 ;;
146 udev-event)
147 handle_udev_event "${@}" || rc="${rc}"
148 ;;
149 scanhd)
150 exec /usr/local/bin/scanhd "${@}"
151 ;;
152
153 # No command
154 "")
155 echo "${0}: No command given" >&2
156 rc=2
157 ;;
158
159 # Unknown command
160 *)
161 echo "${0}: Unsupported command: ${command}" >&2
162 rc=2
163 ;;
164 esac
165
166 return ${rc}
167 }
168
169 # Call main()
170 main "${@}" || exit ${?}