]>
Commit | Line | Data |
---|---|---|
1848564d MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
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 | ||
1c6a4e30 | 22 | zone_dir() { |
1848564d MT |
23 | local zone=${1} |
24 | ||
d2a21d01 | 25 | echo "${NETWORK_ZONE_DIR}/zones/${zone}" |
1848564d MT |
26 | } |
27 | ||
1c6a4e30 | 28 | zone_exists() { |
1848564d | 29 | local zone=${1} |
711ffac1 MT |
30 | assert isset zone |
31 | ||
1848564d MT |
32 | [ -d "$(zone_dir ${zone})" ] |
33 | } | |
34 | ||
1c6a4e30 | 35 | zone_match() { |
1848564d MT |
36 | local match |
37 | ||
38 | local i | |
39 | for i in ${VALID_ZONES}; do | |
40 | match="${match}|${i}[0-9]{1,5}" | |
41 | done | |
42 | ||
43 | echo "${match:1:${#match}}" | |
44 | } | |
45 | ||
1c6a4e30 | 46 | zone_name_is_valid() { |
1848564d | 47 | local zone=${1} |
4fedddef MT |
48 | |
49 | # Don't accept empty strings. | |
50 | [ -z "${zone}" ] && return ${EXIT_FALSE} | |
711ffac1 | 51 | |
1848564d MT |
52 | [[ ${zone} =~ $(zone_match) ]] |
53 | } | |
54 | ||
1c6a4e30 | 55 | zone_is_local() { |
1848564d MT |
56 | local zone=${1} |
57 | ||
7de0637a | 58 | [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]] |
5e42d659 MT |
59 | } |
60 | ||
1c6a4e30 | 61 | zone_is_nonlocal() { |
5e42d659 MT |
62 | local zone=${1} |
63 | ||
7de0637a | 64 | [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]] |
1848564d MT |
65 | } |
66 | ||
1c6a4e30 | 67 | zone_get_hook() { |
1848564d | 68 | local zone=${1} |
711ffac1 MT |
69 | assert isset zone |
70 | ||
1848564d MT |
71 | config_get_hook $(zone_dir ${zone})/settings |
72 | } | |
73 | ||
1c6a4e30 | 74 | zone_start() { |
5bb2429a MT |
75 | # This function will bring up the zone |
76 | # 'asynchronously' with help of systemd. | |
77 | ||
78 | local zone=${1} | |
79 | assert zone_exists ${zone} | |
80 | ||
1d08b9b3 | 81 | service_start "network@${zone}.service" |
5bb2429a MT |
82 | } |
83 | ||
1c6a4e30 | 84 | zone_start_auto() { |
2a969c27 MT |
85 | local zone="${1}" |
86 | assert zone_exists "${zone}" | |
87 | ||
88 | # If the zone has already been started, we | |
89 | # will reload it so the current configuration | |
90 | # is re-applied. | |
91 | if zone_is_active "${zone}"; then | |
92 | zone_reload "${zone}" | |
93 | return ${?} | |
94 | ||
95 | # If the zone is still down, but in auto-start mode, | |
96 | # we will start it. | |
97 | elif zone_is_enabled "${zone}"; then | |
98 | zone_start "${zone}" | |
99 | return ${?} | |
100 | fi | |
101 | ||
102 | # Otherwise, nothing will be done. | |
103 | return ${EXIT_OK} | |
104 | } | |
105 | ||
1c6a4e30 | 106 | zone_stop() { |
5bb2429a MT |
107 | # This function will bring down the zone |
108 | # 'asynchronously' with help of systemd. | |
109 | ||
110 | local zone=${1} | |
111 | assert zone_exists ${zone} | |
112 | ||
1d08b9b3 | 113 | service_stop "network@${zone}.service" |
5bb2429a MT |
114 | } |
115 | ||
1c6a4e30 | 116 | zone_reload() { |
e6fd23fd MT |
117 | local zone="${1}" |
118 | assert zone_exists "${zone}" | |
119 | ||
120 | service_reload "network@${zone}.service" | |
121 | } | |
122 | ||
1c6a4e30 | 123 | zone_hotplug_event() { |
fb8c7c92 | 124 | local zone="${1}" |
2a969c27 | 125 | assert isset zone |
fb8c7c92 | 126 | |
2a969c27 | 127 | hotplug_assert_in_hotplug_event |
fb8c7c92 | 128 | |
2a969c27 | 129 | zone_cmd "hotplug" "${zone}" |
fb8c7c92 MT |
130 | } |
131 | ||
1c6a4e30 | 132 | zone_enable() { |
5c5b8e36 SS |
133 | # This function will enable the zone |
134 | # with help of systemd. | |
135 | ||
136 | local zone="${1}" | |
137 | assert zone_exists "${zone}" | |
138 | ||
139 | # Enable service for the zone | |
140 | service_enable "network@${zone}.service" | |
141 | local ret=$? | |
142 | ||
143 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
144 | log INFO "Auto-start enabled for zone ${zone}" | |
145 | return ${EXIT_OK} | |
146 | fi | |
147 | ||
148 | log ERROR "Could not enable zone ${zone}: ${ret}" | |
149 | return ${ret} | |
150 | } | |
151 | ||
1c6a4e30 | 152 | zone_disable() { |
5c5b8e36 SS |
153 | # This function will disable the zone |
154 | # with help of systemd. | |
155 | ||
156 | local zone="${1}" | |
157 | assert zone_exists "${zone}" | |
158 | ||
159 | # Disable service for the zone | |
160 | service_disable "network@${zone}.service" | |
161 | local ret=$? | |
162 | ||
163 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
164 | log INFO "Auto-start disabled for zone ${zone}" | |
165 | return ${EXIT_OK} | |
166 | fi | |
167 | ||
168 | log ERROR "Could not disable zone ${zone}: ${ret}" | |
169 | return ${ret} | |
170 | } | |
171 | ||
1c6a4e30 | 172 | zone_is_enabled() { |
5c5b8e36 SS |
173 | local zone="${1}" |
174 | assert isset zone | |
175 | ||
176 | # Ask systemd if the zone is enabled. | |
177 | if service_is_enabled "network@${zone}.service"; then | |
178 | return ${EXIT_TRUE} | |
179 | fi | |
180 | ||
181 | return ${EXIT_FALSE} | |
182 | } | |
183 | ||
1c6a4e30 | 184 | zone_is_active() { |
e6fd23fd MT |
185 | local zone="${1}" |
186 | assert isset zone | |
187 | ||
188 | if service_is_active "network@${zone}.service"; then | |
189 | return ${EXIT_TRUE} | |
190 | fi | |
191 | ||
192 | return ${EXIT_FALSE} | |
193 | } | |
194 | ||
1c6a4e30 | 195 | zone_is_enabled_or_active() { |
2a969c27 MT |
196 | local zone="${1}" |
197 | assert isset zone | |
198 | ||
199 | zone_is_enabled "${zone}" || zone_is_active "${zone}" | |
200 | } | |
201 | ||
1c6a4e30 | 202 | zone_cmd() { |
2a969c27 MT |
203 | local cmd="${1}" |
204 | local port="${2}" | |
205 | shift 2 | |
206 | ||
207 | assert isset cmd | |
208 | assert isset zone | |
209 | ||
210 | local hook="$(zone_get_hook ${zone})" | |
211 | assert isset hook | |
212 | ||
213 | hook_exec zone "${hook}" "${cmd}" "${zone}" $@ | |
214 | } | |
215 | ||
1c6a4e30 | 216 | zone_new() { |
1848564d MT |
217 | local zone=${1} |
218 | local hook=${2} | |
219 | shift 2 | |
220 | ||
221 | if ! zone_name_is_valid ${zone}; then | |
222 | error "Zone name '${zone}' is not valid." | |
223 | return ${EXIT_ERROR} | |
224 | fi | |
225 | ||
226 | if zone_exists ${zone}; then | |
227 | error "Zone '${zone}' does already exist." | |
228 | return ${EXIT_ERROR} | |
229 | fi | |
230 | ||
d61a01d4 | 231 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
232 | error "Hook '${hook}' does not exist." |
233 | return ${EXIT_ERROR} | |
234 | fi | |
235 | ||
236 | mkdir -p $(zone_dir ${zone}) | |
237 | ||
a5ebb169 MT |
238 | # Create directories for configs and ports |
239 | mkdir -p $(zone_dir ${zone})/{configs,ports} | |
943e3f7e | 240 | |
cf0fc8ab | 241 | hook_zone_exec "${hook}" "new" "${zone}" $@ |
1848564d MT |
242 | local ret=$? |
243 | ||
cf0fc8ab | 244 | # Maybe the zone new hook did not exit correctly. |
1848564d MT |
245 | # If this is the case we remove the created zone immediately. |
246 | if [ "${ret}" = "${EXIT_ERROR}" ]; then | |
cf0fc8ab | 247 | zone_destroy_now "${zone}" |
5c5b8e36 | 248 | return ${EXIT_ERROR} |
1848564d | 249 | fi |
5c5b8e36 SS |
250 | |
251 | # Automatically enable zone. | |
252 | zone_enable "${zone}" | |
ac694a6a MT |
253 | |
254 | # Bring up the zone immediately after | |
255 | zone_start "${zone}" | |
1848564d MT |
256 | } |
257 | ||
1c6a4e30 | 258 | zone_edit() { |
1848564d MT |
259 | local zone=${1} |
260 | shift | |
261 | ||
262 | if ! zone_exists ${zone}; then | |
263 | error "Zone '${zone}' does not exist." | |
264 | return ${EXIT_ERROR} | |
265 | fi | |
266 | ||
69ace22b | 267 | # Check if the zone is tagged for removal. |
cf0fc8ab | 268 | if zone_has_destroy_tag ${zone}; then |
69ace22b MT |
269 | error "You cannot edit a zone that is tagged for removal." |
270 | return ${EXIT_ERROR} | |
271 | fi | |
272 | ||
2472e0ea | 273 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
274 | if [ -z "${hook}" ]; then |
275 | error "Config file did not provide any hook." | |
276 | return ${EXIT_ERROR} | |
277 | fi | |
278 | ||
d61a01d4 | 279 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
280 | error "Hook '${hook}' does not exist." |
281 | return ${EXIT_ERROR} | |
282 | fi | |
283 | ||
d61a01d4 | 284 | hook_zone_exec ${hook} edit ${zone} $@ |
1848564d MT |
285 | } |
286 | ||
69ace22b | 287 | |
1c6a4e30 | 288 | zone_destroy() { |
cf0fc8ab MT |
289 | local zone="${1}" |
290 | assert zone_exists "${zone}" | |
1848564d | 291 | |
69ace22b | 292 | # Make the zone for removal. |
cf0fc8ab | 293 | touch "$(zone_dir "${zone}")/.destroy" |
69ace22b MT |
294 | |
295 | log INFO "Zone '${zone}' has been tagged for removal." | |
296 | } | |
297 | ||
1c6a4e30 | 298 | zone_has_destroy_tag() { |
cf0fc8ab MT |
299 | local zone="${1}" |
300 | assert zone_exists "${zone}" | |
69ace22b | 301 | |
cf0fc8ab | 302 | [ -e "$(zone_dir "${zone}")/.destroy" ] |
69ace22b MT |
303 | } |
304 | ||
305 | # This function will remove the given zone | |
cf0fc8ab | 306 | # RIGHT NOW. Use zone_destroy to remove it |
69ace22b | 307 | # at the next status change. |
1c6a4e30 | 308 | zone_destroy_now() { |
cf0fc8ab MT |
309 | local zone="${1}" |
310 | assert zone_exists "${zone}" | |
69ace22b MT |
311 | |
312 | log INFO "Removing zone '${zone}' right now." | |
1848564d | 313 | |
69ace22b | 314 | # Force the zone down. |
d5c9bd17 | 315 | zone_is_active "${zone}" && zone_stop "${zone}" |
1848564d | 316 | |
5c5b8e36 SS |
317 | # Disable zone. |
318 | zone_disable "${zone}" | |
319 | ||
cf0fc8ab | 320 | rm -rf "$(zone_dir "${zone}")" |
1848564d MT |
321 | } |
322 | ||
1c6a4e30 | 323 | zone_up() { |
1848564d MT |
324 | local zone=${1} |
325 | shift | |
326 | ||
327 | if ! zone_exists ${zone}; then | |
328 | error "Zone '${zone}' does not exist." | |
329 | return ${EXIT_ERROR} | |
330 | fi | |
331 | ||
69ace22b | 332 | # Check if a zone has got the remove tag. |
fa0eb21f | 333 | if zone_has_destroy_tag ${zone}; then |
69ace22b MT |
334 | error "Cannot bring up any zone which is to be removed." |
335 | return ${EXIT_ERROR} | |
336 | fi | |
337 | ||
2472e0ea | 338 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
339 | if [ -z "${hook}" ]; then |
340 | error "Config file did not provide any hook." | |
341 | return ${EXIT_ERROR} | |
342 | fi | |
343 | ||
d61a01d4 | 344 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
345 | error "Hook '${hook}' does not exist." |
346 | return ${EXIT_ERROR} | |
347 | fi | |
348 | ||
059469a8 MT |
349 | zone_db ${zone} starting |
350 | ||
d61a01d4 MT |
351 | hook_zone_exec ${hook} up ${zone} $@ |
352 | ||
059469a8 | 353 | zone_db ${zone} started |
de3cecef MT |
354 | |
355 | # Execute all triggers after the zone got up | |
356 | triggers_execute_all "up" ZONE="${zone}" | |
1848564d MT |
357 | } |
358 | ||
1c6a4e30 | 359 | zone_down() { |
1848564d MT |
360 | local zone=${1} |
361 | shift | |
362 | ||
363 | if ! zone_exists ${zone}; then | |
364 | error "Zone '${zone}' does not exist." | |
365 | return ${EXIT_ERROR} | |
366 | fi | |
367 | ||
2472e0ea | 368 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
369 | if [ -z "${hook}" ]; then |
370 | error "Config file did not provide any hook." | |
371 | return ${EXIT_ERROR} | |
372 | fi | |
373 | ||
d61a01d4 | 374 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
375 | error "Hook '${hook}' does not exist." |
376 | return ${EXIT_ERROR} | |
377 | fi | |
378 | ||
059469a8 MT |
379 | zone_db ${zone} stopping |
380 | ||
d61a01d4 | 381 | hook_zone_exec ${hook} down ${zone} $@ |
059469a8 MT |
382 | |
383 | zone_db ${zone} stopped | |
69ace22b | 384 | |
de3cecef MT |
385 | # Execute all triggers after the zone went down |
386 | triggers_execute_all "down" ZONE="${zone}" | |
387 | ||
69ace22b | 388 | # Remove the zone, if it has got a remove tag. |
cf0fc8ab MT |
389 | if zone_has_destroy_tag "${zone}"; then |
390 | zone_destroy_now "${zone}" | |
69ace22b | 391 | fi |
1848564d MT |
392 | } |
393 | ||
1c6a4e30 | 394 | zone_status() { |
2472e0ea MT |
395 | local zone="${1}" |
396 | assert isset zone | |
1848564d MT |
397 | shift |
398 | ||
2472e0ea | 399 | if ! zone_exists "${zone}"; then |
1848564d MT |
400 | error "Zone '${zone}' does not exist." |
401 | return ${EXIT_ERROR} | |
402 | fi | |
403 | ||
2472e0ea | 404 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
405 | if [ -z "${hook}" ]; then |
406 | error "Config file did not provide any hook." | |
407 | return ${EXIT_ERROR} | |
408 | fi | |
409 | ||
2472e0ea | 410 | if ! hook_zone_exists "${hook}"; then |
1848564d MT |
411 | error "Hook '${hook}' does not exist." |
412 | return ${EXIT_ERROR} | |
413 | fi | |
414 | ||
2472e0ea | 415 | hook_zone_exec "${hook}" "status" "${zone}" "$@" |
69ace22b MT |
416 | |
417 | # Show that the zone it to be removed soon. | |
fa0eb21f | 418 | if zone_has_destroy_tag ${zone}; then |
69ace22b MT |
419 | warning "This zone is tagged for removal." |
420 | fi | |
1848564d MT |
421 | } |
422 | ||
1c6a4e30 | 423 | zone_get_ports() { |
711ffac1 MT |
424 | local zone=${1} |
425 | ||
426 | assert isset zone | |
427 | ||
428 | local port | |
943e3f7e | 429 | for port in $(zone_dir ${zone})/ports/*; do |
711ffac1 | 430 | port=$(basename ${port}) |
711ffac1 MT |
431 | |
432 | if port_exists ${port}; then | |
433 | echo "${port}" | |
434 | fi | |
435 | done | |
436 | } | |
437 | ||
1c6a4e30 | 438 | zone_get_ports_num() { |
529141df MT |
439 | local zone="${1}" |
440 | assert isset zone | |
441 | ||
442 | local counter=0 | |
443 | local port | |
444 | for port in $(zone_dir "${zone}")/ports/*; do | |
445 | port="$(basename "${port}")" | |
446 | ||
447 | if port_exists "${port}"; then | |
448 | counter=$(( ${counter} + 1 )) | |
449 | fi | |
450 | done | |
451 | ||
452 | echo "${counter}" | |
453 | return ${EXIT_OK} | |
454 | } | |
455 | ||
1c6a4e30 | 456 | zone_has_port() { |
3a7fef62 MT |
457 | # Check, if the given port is configured |
458 | # in this zone. | |
459 | ||
460 | local zone=${1} | |
461 | local port=${2} | |
462 | shift 2 | |
463 | ||
464 | assert isset zone | |
465 | assert isset port | |
466 | ||
467 | [ -e "$(zone_dir ${zone})/ports/${port}" ] | |
468 | } | |
469 | ||
1c6a4e30 | 470 | zone_config() { |
ea699552 MT |
471 | local zone="${1}" |
472 | local cmd="${2}" | |
a5ebb169 MT |
473 | shift 2 |
474 | ||
475 | assert isset zone | |
ea699552 MT |
476 | assert isset cmd |
477 | assert zone_exists "${zone}" | |
a5ebb169 | 478 | |
ea699552 MT |
479 | case "${cmd}" in |
480 | create) | |
481 | zone_config_create "${zone}" "$@" | |
a5ebb169 | 482 | ;; |
ea699552 MT |
483 | edit) |
484 | zone_config_edit "${zone}" "$@" | |
485 | ;; | |
486 | remove) | |
487 | zone_config_remove "${zone}" "$@" | |
a5ebb169 MT |
488 | ;; |
489 | *) | |
ea699552 | 490 | error "Unrecognized argument: ${cmd}" |
a5ebb169 MT |
491 | cli_usage root-zone-config-subcommands |
492 | exit ${EXIT_ERROR} | |
493 | ;; | |
494 | esac | |
495 | } | |
496 | ||
1c6a4e30 | 497 | zone_config_cmd() { |
ea699552 | 498 | assert [ $# -gt 2 ] |
a5ebb169 | 499 | |
ea699552 MT |
500 | local cmd="${1}" |
501 | local zone="${2}" | |
502 | shift 2 | |
503 | ||
504 | local hook="$(zone_get_hook "${zone}")" | |
a5ebb169 MT |
505 | assert isset hook |
506 | ||
ea699552 MT |
507 | hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@" |
508 | } | |
509 | ||
1c6a4e30 | 510 | zone_config_create() { |
ea699552 MT |
511 | zone_config_cmd "create" "$@" |
512 | } | |
513 | ||
1c6a4e30 | 514 | zone_config_edit() { |
ea699552 MT |
515 | zone_config_cmd "edit" "$@" |
516 | } | |
517 | ||
1c6a4e30 | 518 | zone_config_remove() { |
ea699552 MT |
519 | zone_config_cmd "remove" "$@" |
520 | } | |
521 | ||
1c6a4e30 | 522 | zone_config_show() { |
ea699552 | 523 | zone_config_cmd "show" "$@" |
a5ebb169 MT |
524 | } |
525 | ||
1c6a4e30 | 526 | zone_show() { |
1848564d MT |
527 | local zone=${1} |
528 | ||
529 | echo "${zone}" | |
530 | echo " Type: $(zone_get_hook ${zone})" | |
531 | echo | |
532 | } | |
533 | ||
1c6a4e30 | 534 | zones_show() { |
1848564d MT |
535 | local zone |
536 | ||
537 | for zone in $(zones_get $@); do | |
538 | zone_show ${zone} | |
539 | done | |
540 | } | |
541 | ||
1c6a4e30 | 542 | zones_get_all() { |
1848564d | 543 | local zone |
d61a01d4 | 544 | for zone in $(zone_dir)/*; do |
1848564d MT |
545 | zone=$(basename ${zone}) |
546 | zone_exists ${zone} || continue | |
547 | ||
548 | echo "${zone}" | |
03170817 | 549 | done |
1848564d MT |
550 | } |
551 | ||
1c6a4e30 | 552 | zones_get_local() { |
1848564d MT |
553 | local zone |
554 | for zone in $(zones_get_all); do | |
555 | zone_is_local ${zone} && echo "${zone}" | |
556 | done | |
557 | } | |
558 | ||
1c6a4e30 | 559 | zones_get_nonlocal() { |
1848564d MT |
560 | local zone |
561 | for zone in $(zones_get_all); do | |
5e42d659 | 562 | zone_is_nonlocal ${zone} && echo "${zone}" |
1848564d MT |
563 | done |
564 | } | |
565 | ||
1c6a4e30 | 566 | zones_get() { |
1848564d MT |
567 | local local=1 |
568 | local remote=1 | |
569 | ||
570 | local zones | |
571 | ||
572 | while [ $# -gt 0 ]; do | |
573 | case "${1}" in | |
574 | --local-only) | |
575 | local=1 | |
576 | remote=0 | |
577 | ;; | |
578 | --remote-only) | |
579 | local=0 | |
580 | remote=1 | |
581 | ;; | |
582 | --all) | |
583 | local=1 | |
584 | remote=1 | |
585 | ;; | |
586 | *) | |
587 | if zone_name_is_valid ${1}; then | |
588 | zones="${zones} ${1}" | |
589 | else | |
590 | warning "Unrecognized argument '${1}'" | |
591 | fi | |
592 | ;; | |
593 | esac | |
594 | shift | |
595 | done | |
596 | ||
597 | if [ -n "${zones}" ]; then | |
598 | local zone | |
599 | for zone in ${zones}; do | |
600 | zone_exists ${zone} && echo "${zone}" | |
601 | done | |
602 | exit ${EXIT_OK} | |
603 | fi | |
604 | ||
605 | if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then | |
606 | zones_get_all | |
607 | elif [ ${local} -eq 1 ]; then | |
608 | zones_get_local | |
609 | elif [ ${remote} -eq 1 ]; then | |
610 | zones_get_nonlocal | |
611 | fi | |
612 | } | |
613 | ||
1c6a4e30 | 614 | zone_ports_list() { |
1848564d MT |
615 | local zone=${1} |
616 | ||
617 | local port | |
a5ebb169 | 618 | for port in $(zone_dir ${zone})/ports/*; do |
1848564d MT |
619 | [ -e "${port}" ] || continue |
620 | ||
621 | echo $(basename ${port}) | |
03170817 | 622 | done |
1848564d MT |
623 | } |
624 | ||
1c6a4e30 | 625 | zone_port_attach() { |
ac694a6a MT |
626 | local zone="${1}" |
627 | assert isset zone | |
628 | ||
629 | local port="${2}" | |
630 | assert isset port | |
631 | ||
632 | shift 2 | |
633 | ||
634 | # Check if the port actually exists. | |
635 | if ! port_exists "${port}"; then | |
636 | error "Cannot attach port '${port}' which does not exist" | |
637 | return ${EXIT_ERROR} | |
638 | fi | |
639 | ||
640 | # Check if the port is already connected to this or any other zone. | |
641 | local z | |
642 | for z in $(zones_get_all); do | |
643 | if zone_has_port "${z}" "${port}"; then | |
644 | error "Port '${port}' is already attached to zone '${z}'" | |
645 | return ${EXIT_ERROR} | |
646 | fi | |
647 | done | |
648 | ||
649 | local hook="$(zone_get_hook "${zone}")" | |
650 | assert isset hook | |
651 | ||
652 | hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@" | |
653 | local ret="${?}" | |
654 | ||
655 | case "${ret}" in | |
656 | ${EXIT_OK}) | |
657 | log INFO "${port} has been attached to ${zone}" | |
658 | ||
659 | # Automatically connect the port | |
abba34c1 | 660 | zone_port_start "${zone}" "${port}" |
ac694a6a MT |
661 | ;; |
662 | *) | |
663 | log CRITICAL "${port} could not be attached to ${zone}" | |
664 | ;; | |
665 | esac | |
666 | ||
667 | return ${ret} | |
668 | } | |
669 | ||
1c6a4e30 | 670 | zone_port_edit() { |
ac694a6a MT |
671 | local zone="${1}" |
672 | assert isset zone | |
673 | ||
674 | local port="${2}" | |
675 | assert isset port | |
676 | ||
677 | shift 2 | |
678 | ||
679 | # Check if the port actually exists. | |
680 | if ! port_exists "${port}"; then | |
681 | error "Port '${port}' does not exist" | |
682 | return ${EXIT_ERROR} | |
683 | fi | |
684 | ||
685 | # Check if the zone actually has this port. | |
686 | if ! zone_has_port "${zone}" "${port}"; then | |
687 | error "Port '${port}' is not attached to zone '${zone}'" | |
688 | return ${EXIT_ERROR} | |
689 | fi | |
690 | ||
691 | local hook=$(zone_get_hook "${zone}") | |
692 | assert isset hook | |
693 | ||
694 | hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@" | |
695 | } | |
696 | ||
1c6a4e30 | 697 | zone_port_detach() { |
ac694a6a MT |
698 | local zone="${1}" |
699 | assert isset zone | |
700 | ||
701 | local port="${2}" | |
702 | assert isset port | |
703 | ||
1848564d MT |
704 | shift 2 |
705 | ||
ac694a6a MT |
706 | # Check if the zone actually has this port. |
707 | if ! zone_has_port "${zone}" "${port}"; then | |
708 | error "Port '${port}' is not attached to zone '${zone}'" | |
709 | return ${EXIT_ERROR} | |
710 | fi | |
711 | ||
712 | local hook=$(zone_get_hook "${zone}") | |
713 | assert isset hook | |
714 | ||
715 | hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@" | |
716 | local ret="${?}" | |
717 | ||
718 | case "${ret}" in | |
719 | ${EXIT_OK}) | |
720 | log INFO "${port} has been detached from ${zone}" | |
721 | ||
722 | # Bring down the port if needed | |
abba34c1 | 723 | zone_port_stop "${zone}" "${port}" |
ac694a6a MT |
724 | ;; |
725 | *) | |
726 | log CRITICAL "${port} could not be detached from ${zone}" | |
727 | ;; | |
728 | esac | |
729 | ||
730 | return ${ret} | |
731 | } | |
732 | ||
1c6a4e30 | 733 | zone_port_cmd() { |
ac694a6a | 734 | local cmd="${1}" |
711ffac1 | 735 | assert isset cmd |
ac694a6a MT |
736 | |
737 | local zone="${2}" | |
711ffac1 | 738 | assert isset zone |
1848564d | 739 | |
ac694a6a MT |
740 | local port="${3}" |
741 | assert isset port | |
1848564d | 742 | |
ac694a6a MT |
743 | shift 3 |
744 | ||
745 | local hook="$(zone_get_hook "${zone}")" | |
746 | assert isset hook | |
747 | ||
748 | # Dispatch command to hook | |
749 | hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@ | |
750 | } | |
751 | ||
1c6a4e30 | 752 | zone_port_create() { |
ac694a6a MT |
753 | zone_port_cmd "port_create" $@ |
754 | } | |
755 | ||
1c6a4e30 | 756 | zone_port_remove() { |
ac694a6a MT |
757 | zone_port_cmd "port_remove" $@ |
758 | } | |
759 | ||
1c6a4e30 | 760 | zone_port_up() { |
ac694a6a MT |
761 | zone_port_cmd "port_up" $@ |
762 | } | |
763 | ||
1c6a4e30 | 764 | zone_port_down() { |
ac694a6a MT |
765 | zone_port_cmd "port_down" $@ |
766 | } | |
767 | ||
abba34c1 MT |
768 | # The next two functions automagically bring up and down |
769 | # port that are attached to a bridge or similar. | |
770 | # The problem that is tried to overcome here is that there | |
771 | # are ports which exist all the time (like ethernet ports) | |
772 | # and therefore do not dispatch a hotplug event when | |
773 | # port_create is called. | |
774 | ||
1c6a4e30 | 775 | zone_port_start() { |
abba34c1 MT |
776 | local zone="${1}" |
777 | local port="${2}" | |
778 | ||
779 | if zone_is_active "${zone}"; then | |
780 | if device_exists "${port}"; then | |
781 | zone_port_up "${zone}" "${port}" | |
782 | return ${?} | |
783 | else | |
784 | zone_port_create "${zone}" "${port}" | |
785 | return ${?} | |
786 | fi | |
787 | fi | |
788 | ||
789 | return ${EXIT_OK} | |
790 | } | |
791 | ||
1c6a4e30 | 792 | zone_port_stop() { |
abba34c1 MT |
793 | local zone="${1}" |
794 | local port="${2}" | |
795 | ||
796 | # Shut down the port if necessary | |
797 | if zone_is_active "${zone}" && port_is_up "${port}"; then | |
798 | zone_port_down "${zone}" "${port}" | |
799 | fi | |
800 | ||
801 | # Remove the port | |
802 | zone_port_remove "${zone}" "${port}" | |
803 | } | |
804 | ||
1c6a4e30 | 805 | zone_port_status() { |
ac694a6a MT |
806 | zone_port_cmd "port_status" $@ |
807 | } | |
808 | ||
1c6a4e30 | 809 | zone_ports_cmd() { |
ac694a6a MT |
810 | local cmd="${1}" |
811 | assert isset cmd | |
812 | ||
813 | local zone="${2}" | |
814 | assert isset zone | |
815 | ||
816 | shift 2 | |
817 | ||
818 | local hook="$(zone_get_hook "${zone}")" | |
711ffac1 MT |
819 | |
820 | local port | |
821 | for port in $(zone_get_ports ${zone}); do | |
ac694a6a | 822 | hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@ |
1848564d MT |
823 | done |
824 | } | |
825 | ||
1c6a4e30 | 826 | zone_ports_create() { |
1ba6a2bb MT |
827 | zone_ports_cmd "port_create" $@ |
828 | } | |
829 | ||
1c6a4e30 | 830 | zone_ports_remove() { |
1ba6a2bb MT |
831 | zone_ports_cmd "port_remove" $@ |
832 | } | |
833 | ||
1c6a4e30 | 834 | zone_ports_up() { |
ac694a6a | 835 | zone_ports_cmd "port_up" $@ |
1848564d MT |
836 | } |
837 | ||
1c6a4e30 | 838 | zone_ports_down() { |
ac694a6a | 839 | zone_ports_cmd "port_down" $@ |
711ffac1 MT |
840 | } |
841 | ||
1c6a4e30 | 842 | zone_ports_status() { |
ac694a6a | 843 | zone_ports_cmd "port_status" $@ |
1848564d MT |
844 | } |
845 | ||
1c6a4e30 | 846 | zone_configs_cmd() { |
2472e0ea | 847 | assert [ $# -ge 2 ] |
ea699552 MT |
848 | |
849 | local cmd="${1}" | |
850 | local zone="${2}" | |
1848564d MT |
851 | shift 2 |
852 | ||
ea699552 | 853 | assert zone_exists "${zone}" |
1848564d | 854 | |
1848564d | 855 | local config |
2472e0ea MT |
856 | for config in $(zone_configs_list "${zone}"); do |
857 | local config_hook="$(zone_config_get_hook "${zone}" "${config}")" | |
858 | assert isset config_hook | |
859 | ||
860 | hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" $@ | |
1848564d MT |
861 | done |
862 | } | |
863 | ||
1c6a4e30 | 864 | zone_configs_up() { |
ea699552 | 865 | zone_configs_cmd "up" $@ |
1848564d MT |
866 | } |
867 | ||
1c6a4e30 | 868 | zone_configs_down() { |
ea699552 | 869 | zone_configs_cmd "down" $@ |
1848564d MT |
870 | } |
871 | ||
1c6a4e30 | 872 | zone_configs_status() { |
ea699552 MT |
873 | zone_configs_cmd "status" $@ |
874 | } | |
875 | ||
1c6a4e30 | 876 | zone_configs_list() { |
ea699552 MT |
877 | local zone=${1} |
878 | ||
879 | local config | |
880 | for config in $(zone_dir ${zone})/configs/*; do | |
881 | [ -e "${config}" ] || continue | |
882 | ||
883 | basename ${config} | |
884 | done | |
a5ebb169 MT |
885 | } |
886 | ||
1c6a4e30 | 887 | zone_config_get_hook() { |
2472e0ea MT |
888 | assert [ $# -eq 2 ] |
889 | ||
890 | local zone="${1}" | |
891 | assert isset zone | |
892 | ||
893 | local config="${2}" | |
894 | assert isset config | |
895 | ||
896 | local HOOK | |
897 | zone_config_settings_read "${zone}" "${config}" \ | |
898 | --ignore-superfluous-settings HOOK | |
899 | ||
900 | print "${HOOK}" | |
901 | } | |
902 | ||
1c6a4e30 | 903 | zone_has_ip() { |
38f61548 | 904 | device_has_ip $@ |
4231f419 MT |
905 | } |
906 | ||
1c6a4e30 | 907 | zone_db() { |
059469a8 MT |
908 | local zone=${1} |
909 | local action=${2} | |
910 | shift 2 | |
911 | ||
912 | case "${action}" in | |
913 | starting|started|stopping|stopped) | |
914 | db_connection_update ${zone} ${action} | |
915 | ;; | |
916 | esac | |
917 | } | |
5e42d659 | 918 | |
1c6a4e30 | 919 | zone_is_up() { |
5e42d659 MT |
920 | local zone=${1} |
921 | ||
922 | device_is_up ${zone} | |
923 | } | |
924 | ||
1c6a4e30 | 925 | zone_is_down() { |
5e42d659 MT |
926 | ! zone_is_up $@ |
927 | } | |
711ffac1 | 928 | |
1c6a4e30 | 929 | zone_get_supported_port_hooks() { |
711ffac1 MT |
930 | local zone=${1} |
931 | ||
932 | local hook=$(zone_get_hook ${zone}) | |
933 | ||
934 | hook_zone_ports_get_all ${hook} | |
935 | } | |
936 | ||
1c6a4e30 | 937 | zone_get_supported_config_hooks() { |
ea699552 | 938 | hook_config_get_all |
a5ebb169 MT |
939 | } |
940 | ||
1c6a4e30 | 941 | zone_file() { |
711ffac1 MT |
942 | local zone=${1} |
943 | ||
944 | assert isset zone | |
945 | ||
946 | echo "$(zone_dir ${zone})/settings" | |
947 | } | |
948 | ||
1c6a4e30 | 949 | zone_settings_read() { |
711ffac1 | 950 | local zone=${1} |
711ffac1 | 951 | assert isset zone |
52acd25f MT |
952 | shift |
953 | ||
954 | local args | |
955 | if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then | |
956 | list_append args ${HOOK_SETTINGS} | |
957 | else | |
958 | list_append args $@ | |
959 | fi | |
711ffac1 | 960 | |
bfd6c282 MT |
961 | # Save the HOOK variable. |
962 | local hook="${HOOK}" | |
963 | ||
52acd25f | 964 | settings_read "$(zone_file "${zone}")" ${args} |
bfd6c282 MT |
965 | |
966 | # Restore hook. | |
967 | HOOK="${hook}" | |
711ffac1 MT |
968 | } |
969 | ||
1c6a4e30 | 970 | zone_settings_write() { |
1e6f187e | 971 | local zone="${1}" |
711ffac1 MT |
972 | assert isset zone |
973 | ||
1e6f187e MT |
974 | local args |
975 | if function_exists "hook_check_settings"; then | |
976 | list_append args "--check=\"hook_check_settings\"" | |
977 | fi | |
978 | list_append args ${HOOK_SETTINGS} | |
979 | ||
980 | settings_write "$(zone_file ${zone})" ${args} | |
711ffac1 MT |
981 | } |
982 | ||
1c6a4e30 | 983 | zone_settings_set() { |
711ffac1 MT |
984 | local zone=${1} |
985 | shift | |
986 | local args="$@" | |
987 | ||
988 | assert isset zone | |
989 | ||
990 | ( | |
e9df08ad | 991 | zone_settings_read ${zone} |
711ffac1 MT |
992 | |
993 | for arg in ${args}; do | |
994 | eval "${arg}" | |
995 | done | |
996 | ||
e9df08ad | 997 | zone_settings_write ${zone} |
711ffac1 MT |
998 | ) |
999 | } | |
6b3f9c85 | 1000 | |
1c6a4e30 | 1001 | zone_settings_get() { |
6b3f9c85 MT |
1002 | local zone=${1} |
1003 | local key=${2} | |
1004 | ||
1005 | assert isset zone | |
1006 | assert isset key | |
1007 | ||
1008 | ( | |
1e6f187e MT |
1009 | zone_settings_read "${zone}" "${key}" \ |
1010 | --ignore-superfluous-settings | |
6b3f9c85 MT |
1011 | |
1012 | echo "${!key}" | |
1013 | ) | |
1014 | } | |
e9df08ad | 1015 | |
1c6a4e30 | 1016 | zone_config_settings_read() { |
c8132752 | 1017 | assert [ $# -ge 2 ] |
e9df08ad MT |
1018 | |
1019 | local zone="${1}" | |
1020 | local config="${2}" | |
1021 | shift 2 | |
1022 | ||
c8132752 MT |
1023 | local args |
1024 | if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then | |
1025 | list_append args ${HOOK_CONFIG_SETTINGS} | |
1026 | else | |
1027 | list_append args $@ | |
1028 | fi | |
1029 | ||
e9df08ad | 1030 | local path="$(zone_dir "${zone}")/configs/${config}" |
c8132752 | 1031 | settings_read "${path}" ${args} |
e9df08ad MT |
1032 | } |
1033 | ||
1c6a4e30 | 1034 | zone_config_settings_write() { |
c8132752 | 1035 | assert [ $# -ge 2 ] |
e9df08ad MT |
1036 | |
1037 | local zone="${1}" | |
1038 | local config="${2}" | |
1039 | shift 2 | |
1040 | ||
c8132752 MT |
1041 | local args |
1042 | if function_exists "hook_check_config_settings"; then | |
1043 | list_append args "--check=\"hook_check_config_settings\"" | |
1044 | fi | |
1045 | list_append args ${HOOK_CONFIG_SETTINGS} | |
1046 | ||
e9df08ad | 1047 | local path="$(zone_dir "${zone}")/configs/${config}" |
c8132752 | 1048 | settings_write "${path}" ${args} |
e9df08ad MT |
1049 | } |
1050 | ||
1c6a4e30 | 1051 | zone_port_settings_read() { |
ac694a6a | 1052 | assert [ $# -ge 2 ] |
e9df08ad MT |
1053 | |
1054 | local zone="${1}" | |
1055 | local port="${2}" | |
1056 | shift 2 | |
1057 | ||
ac694a6a MT |
1058 | local args |
1059 | if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then | |
1060 | list_append args ${HOOK_PORT_SETTINGS} | |
1061 | else | |
1062 | list_append args $@ | |
1063 | fi | |
1064 | ||
e9df08ad | 1065 | local path="$(zone_dir "${zone}")/ports/${port}" |
ac694a6a | 1066 | settings_read "${path}" ${args} |
e9df08ad MT |
1067 | } |
1068 | ||
1c6a4e30 | 1069 | zone_port_settings_write() { |
02236ca6 | 1070 | assert [ $# -ge 2 ] |
e9df08ad MT |
1071 | |
1072 | local zone="${1}" | |
1073 | local port="${2}" | |
1074 | shift 2 | |
1075 | ||
1e6f187e MT |
1076 | local args |
1077 | if function_exists "hook_check_port_settings"; then | |
1078 | list_append args "--check=\"hook_check_port_settings\"" | |
1079 | fi | |
ac694a6a | 1080 | list_append args ${HOOK_PORT_SETTINGS} |
1e6f187e | 1081 | |
e9df08ad | 1082 | local path="$(zone_dir "${zone}")/ports/${port}" |
1e6f187e | 1083 | settings_write "${path}" ${args} |
e9df08ad MT |
1084 | } |
1085 | ||
1c6a4e30 | 1086 | zone_port_settings_remove() { |
e9df08ad MT |
1087 | assert [ $# -eq 2 ] |
1088 | ||
1089 | local zone="${1}" | |
1090 | local port="${2}" | |
1091 | ||
1092 | local path="$(zone_dir "${zone}")/ports/${port}" | |
1093 | settings_remove "${path}" | |
1094 | } |