]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/extrahd/extrahd.pl
Merge branch 'next'
[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 mountpoint if set
39 if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then
40 continue
41 fi
42
43 # Check that the mountpoint starts with a slash
44 if [ "${mountpoint:0:1}" != "/" ]; then
45 log "Skipping invalid mountpoint: ${mountpoint}"
46 continue
47 fi
48
49 # Skip mounting if something is already mounted at the mountpoint
50 if mountpoint "${mountpoint}" &>/dev/null; then
51 continue
52 fi
53
54 # Ensure the mountpoint exists
55 mkdir --parents --mode=777 "${mountpoint}" &>/dev/null
56
57 if mount --types "${filesystem}" "${device}" "${mountpoint}"; then
58 log "Successfully mounted ${device} to ${mountpoint}"
59 else
60 log "Could not mount ${device} to ${mountpoint}: $?"
61 failed=1
62 fi
63 done < /var/ipfire/extrahd/devices
64
65 return ${failed}
66 }
67
68 extrahd_umount() {
69 local _mountpoint="${1}"
70
71 local device
72 local filesystem
73 local mountpoint
74 local rest
75 local failed=0
76
77 while IFS=';' read -r device filesystem mountpoint rest; do
78 # Filter by mountpoint if set
79 if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then
80 continue
81 fi
82
83 # Do not try to umount if nothing is mounted
84 if ! mountpoint "${mountpoint}" &>/dev/null; then
85 continue
86 fi
87
88 # Umount and try lazy umount if failed
89 if umount --quiet --recursive "${mountpoint}" || \
90 umount --quiet --recursive --lazy "${mountpoint}"; then
91 log "Successfully umounted ${device} from ${mountpoint}"
92 else
93 log "Could not umount ${device} from ${mountpoint}: $?"
94 failed=1
95 fi
96 done < /var/ipfire/extrahd/devices
97 }
98
99 main() {
100 local command="${1}"
101 shift
102
103 local rc=0
104
105 case "${command}" in
106 mount)
107 extrahd_mount "${@}" || rc="${?}"
108 ;;
109 umount)
110 extrahd_umount "${@}" || rc="${rc}"
111 ;;
112 scanhd)
113 exec /usr/local/bin/scanhd "${@}"
114 ;;
115
116 # No command
117 "")
118 echo "${0}: No command given" >&2
119 rc=2
120 ;;
121
122 # Unknown command
123 *)
124 echo "${0}: Unsupported command: ${command}" >&2
125 rc=2
126 ;;
127 esac
128
129 return ${rc}
130 }
131
132 # Call main()
133 main "${@}" || exit ${?}