]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.zone
header-zone: refactor hook_config_edit
[people/ms/network.git] / src / functions / functions.zone
CommitLineData
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 22zone_dir() {
1848564d
MT
23 local zone=${1}
24
d2a21d01 25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
1848564d
MT
26}
27
1c6a4e30 28zone_exists() {
1848564d 29 local zone=${1}
711ffac1
MT
30 assert isset zone
31
1848564d
MT
32 [ -d "$(zone_dir ${zone})" ]
33}
34
1c6a4e30 35zone_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 46zone_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 55zone_is_local() {
1848564d
MT
56 local zone=${1}
57
7de0637a 58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
5e42d659
MT
59}
60
1c6a4e30 61zone_is_nonlocal() {
5e42d659
MT
62 local zone=${1}
63
7de0637a 64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
1848564d
MT
65}
66
1c6a4e30 67zone_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 74zone_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 84zone_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 106zone_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 116zone_reload() {
e6fd23fd
MT
117 local zone="${1}"
118 assert zone_exists "${zone}"
119
120 service_reload "network@${zone}.service"
121}
122
1c6a4e30 123zone_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 132zone_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 152zone_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 172zone_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 184zone_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 195zone_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 202zone_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 216zone_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 258zone_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
2bb20bbd
SS
287zone_rename() {
288 assert [ $# -eq 2 ]
289
290 local zone="${1}"
291 local name="${2}"
292
293 assert zone_exists "${zone}"
294 assert not zone_has_destroy_tag "${zone}"
295 assert not zone_exists "${name}"
296
297 # The zone must be shut down before, is then renamed and
298 # potentially brought up again
299
300 # Save if the zone is running right now
301 zone_is_active "${zone}"
302 local zone_was_active="${?}"
303
304 # Save if the zone is enabled (i.e. auto-start)
305 zone_is_enabled "${zone}"
306 local zone_was_enabled="${?}"
307
308 # Stop the zone
309 zone_stop "${zone}"
310
311 # Disable the zone
312 zone_disable "${zone}"
313
314 # Rename the configuration files
315 mv -f "$(zone_dir "${zone}")" "$(zone_dir "${name}")"
316
317 # Enable the zone if it was enabled before
318 [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
319
320 # Start the zone if it was up before
321 [ ${zone_was_active} -eq ${EXIT_TRUE} ] && zone_start "${name}"
322
323 log INFO "Zone ${zone} was renamed to ${name}"
324 return ${EXIT_OK}
325}
326
69ace22b 327
1c6a4e30 328zone_destroy() {
cf0fc8ab
MT
329 local zone="${1}"
330 assert zone_exists "${zone}"
1848564d 331
69ace22b 332 # Make the zone for removal.
cf0fc8ab 333 touch "$(zone_dir "${zone}")/.destroy"
69ace22b
MT
334
335 log INFO "Zone '${zone}' has been tagged for removal."
336}
337
1c6a4e30 338zone_has_destroy_tag() {
cf0fc8ab
MT
339 local zone="${1}"
340 assert zone_exists "${zone}"
69ace22b 341
cf0fc8ab 342 [ -e "$(zone_dir "${zone}")/.destroy" ]
69ace22b
MT
343}
344
345# This function will remove the given zone
cf0fc8ab 346# RIGHT NOW. Use zone_destroy to remove it
69ace22b 347# at the next status change.
1c6a4e30 348zone_destroy_now() {
cf0fc8ab
MT
349 local zone="${1}"
350 assert zone_exists "${zone}"
69ace22b
MT
351
352 log INFO "Removing zone '${zone}' right now."
1848564d 353
69ace22b 354 # Force the zone down.
d5c9bd17 355 zone_is_active "${zone}" && zone_stop "${zone}"
1848564d 356
5c5b8e36
SS
357 # Disable zone.
358 zone_disable "${zone}"
359
cf0fc8ab 360 rm -rf "$(zone_dir "${zone}")"
1848564d
MT
361}
362
1c6a4e30 363zone_up() {
1848564d
MT
364 local zone=${1}
365 shift
366
367 if ! zone_exists ${zone}; then
368 error "Zone '${zone}' does not exist."
369 return ${EXIT_ERROR}
370 fi
371
69ace22b 372 # Check if a zone has got the remove tag.
fa0eb21f 373 if zone_has_destroy_tag ${zone}; then
69ace22b
MT
374 error "Cannot bring up any zone which is to be removed."
375 return ${EXIT_ERROR}
376 fi
377
2472e0ea 378 local hook="$(zone_get_hook "${zone}")"
1848564d
MT
379 if [ -z "${hook}" ]; then
380 error "Config file did not provide any hook."
381 return ${EXIT_ERROR}
382 fi
383
d61a01d4 384 if ! hook_zone_exists ${hook}; then
1848564d
MT
385 error "Hook '${hook}' does not exist."
386 return ${EXIT_ERROR}
387 fi
388
059469a8
MT
389 zone_db ${zone} starting
390
d61a01d4
MT
391 hook_zone_exec ${hook} up ${zone} $@
392
059469a8 393 zone_db ${zone} started
de3cecef
MT
394
395 # Execute all triggers after the zone got up
396 triggers_execute_all "up" ZONE="${zone}"
1848564d
MT
397}
398
1c6a4e30 399zone_down() {
1848564d
MT
400 local zone=${1}
401 shift
402
403 if ! zone_exists ${zone}; then
404 error "Zone '${zone}' does not exist."
405 return ${EXIT_ERROR}
406 fi
407
2472e0ea 408 local hook="$(zone_get_hook "${zone}")"
1848564d
MT
409 if [ -z "${hook}" ]; then
410 error "Config file did not provide any hook."
411 return ${EXIT_ERROR}
412 fi
413
d61a01d4 414 if ! hook_zone_exists ${hook}; then
1848564d
MT
415 error "Hook '${hook}' does not exist."
416 return ${EXIT_ERROR}
417 fi
418
059469a8
MT
419 zone_db ${zone} stopping
420
d61a01d4 421 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
422
423 zone_db ${zone} stopped
69ace22b 424
de3cecef
MT
425 # Execute all triggers after the zone went down
426 triggers_execute_all "down" ZONE="${zone}"
427
69ace22b 428 # Remove the zone, if it has got a remove tag.
cf0fc8ab
MT
429 if zone_has_destroy_tag "${zone}"; then
430 zone_destroy_now "${zone}"
69ace22b 431 fi
1848564d
MT
432}
433
1c6a4e30 434zone_status() {
2472e0ea
MT
435 local zone="${1}"
436 assert isset zone
1848564d
MT
437 shift
438
2472e0ea 439 if ! zone_exists "${zone}"; then
1848564d
MT
440 error "Zone '${zone}' does not exist."
441 return ${EXIT_ERROR}
442 fi
443
2472e0ea 444 local hook="$(zone_get_hook "${zone}")"
1848564d
MT
445 if [ -z "${hook}" ]; then
446 error "Config file did not provide any hook."
447 return ${EXIT_ERROR}
448 fi
449
2472e0ea 450 if ! hook_zone_exists "${hook}"; then
1848564d
MT
451 error "Hook '${hook}' does not exist."
452 return ${EXIT_ERROR}
453 fi
454
2472e0ea 455 hook_zone_exec "${hook}" "status" "${zone}" "$@"
69ace22b
MT
456
457 # Show that the zone it to be removed soon.
fa0eb21f 458 if zone_has_destroy_tag ${zone}; then
69ace22b
MT
459 warning "This zone is tagged for removal."
460 fi
1848564d
MT
461}
462
f5ee091e
MT
463zone_identify() {
464 assert [ $# -ge 1 ]
465
466 local zone="${1}"
467 shift
468
469 assert zone_exists "${zone}"
470
471 log INFO "Identifying zone ${zone}"
472 local pids
473
474 local pid
475 local port
476 for port in $(zone_get_ports "${zone}"); do
477 # Identify all the ports
478 port_identify "${port}" --background $@
479
480 # Save the PIDs of the subprocesses
481 list_append pids "$(cmd_background_get_pid)"
482 done
483
484 # Wait until all port_identfy processes have finished
485 for pid in ${pids}; do
486 cmd_background_result "${pid}"
487 done
488
489 return ${EXIT_OK}
490}
491
1c6a4e30 492zone_get_ports() {
711ffac1
MT
493 local zone=${1}
494
495 assert isset zone
496
497 local port
943e3f7e 498 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 499 port=$(basename ${port})
711ffac1
MT
500
501 if port_exists ${port}; then
502 echo "${port}"
503 fi
504 done
505}
506
1c6a4e30 507zone_get_ports_num() {
529141df
MT
508 local zone="${1}"
509 assert isset zone
510
511 local counter=0
512 local port
513 for port in $(zone_dir "${zone}")/ports/*; do
514 port="$(basename "${port}")"
515
516 if port_exists "${port}"; then
517 counter=$(( ${counter} + 1 ))
518 fi
519 done
520
521 echo "${counter}"
522 return ${EXIT_OK}
523}
524
1c6a4e30 525zone_has_port() {
3a7fef62
MT
526 # Check, if the given port is configured
527 # in this zone.
528
529 local zone=${1}
530 local port=${2}
531 shift 2
532
533 assert isset zone
534 assert isset port
535
536 [ -e "$(zone_dir ${zone})/ports/${port}" ]
537}
538
1c6a4e30 539zone_config() {
ea699552
MT
540 local zone="${1}"
541 local cmd="${2}"
a5ebb169
MT
542 shift 2
543
544 assert isset zone
ea699552
MT
545 assert isset cmd
546 assert zone_exists "${zone}"
a5ebb169 547
ea699552 548 case "${cmd}" in
2a6b2397
MT
549 new)
550 zone_config_new "${zone}" "$@"
551 ;;
552 destroy)
553 zone_config_destroy "${zone}" "$@"
a5ebb169 554 ;;
ea699552
MT
555 edit)
556 zone_config_edit "${zone}" "$@"
557 ;;
a5ebb169 558 *)
ea699552 559 error "Unrecognized argument: ${cmd}"
a5ebb169
MT
560 cli_usage root-zone-config-subcommands
561 exit ${EXIT_ERROR}
562 ;;
563 esac
564}
565
1c6a4e30 566zone_config_cmd() {
ea699552 567 assert [ $# -gt 2 ]
a5ebb169 568
ea699552
MT
569 local cmd="${1}"
570 local zone="${2}"
571 shift 2
572
573 local hook="$(zone_get_hook "${zone}")"
a5ebb169
MT
574 assert isset hook
575
ea699552
MT
576 hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@"
577}
578
2a6b2397 579zone_config_new() {
44b8ade9
SS
580 local zone="${1}"
581 shift
582
583 # Create a new configuration, but exit when that was
584 # not successful.
585 zone_config_cmd "new" "${zone}" "$@" || return ${?}
586
587 # If the config could be created, we will try to bring
588 # it up if the zone is up, too.
589 if zone_is_up "${zone}"; then
590 zone_configs_up "${zone}"
591 fi
ea699552
MT
592}
593
2a6b2397
MT
594zone_config_destroy() {
595 zone_config_cmd "destroy" "$@"
ea699552
MT
596}
597
2a6b2397
MT
598zone_config_edit() {
599 zone_config_cmd "edit" "$@"
ea699552
MT
600}
601
1c6a4e30 602zone_config_show() {
ea699552 603 zone_config_cmd "show" "$@"
a5ebb169
MT
604}
605
1c6a4e30 606zone_show() {
1848564d
MT
607 local zone=${1}
608
609 echo "${zone}"
610 echo " Type: $(zone_get_hook ${zone})"
611 echo
612}
613
1c6a4e30 614zones_show() {
1848564d
MT
615 local zone
616
617 for zone in $(zones_get $@); do
618 zone_show ${zone}
619 done
620}
621
1c6a4e30 622zones_get_all() {
1848564d 623 local zone
d61a01d4 624 for zone in $(zone_dir)/*; do
1848564d
MT
625 zone=$(basename ${zone})
626 zone_exists ${zone} || continue
627
628 echo "${zone}"
03170817 629 done
1848564d
MT
630}
631
1ca5fe9f
JS
632zones_get_next_free() {
633 # This function return the next free zones.
634 # Example net0 upl0 upl1 are configured so the next free zones are:
635 # net1 upl2
636 local i
637 local zone_name
638 for zone_name in ${VALID_ZONES}; do
639 i=0
640
641 while true; do
642 local zone="${zone_name}${i}"
643 if ! zone_exists ${zone}; then
644 echo "${zone}"
645 break
646 fi
647 i=$(( i + 1 ))
648 done
649 done
650}
651
1c6a4e30 652zones_get_local() {
1848564d
MT
653 local zone
654 for zone in $(zones_get_all); do
655 zone_is_local ${zone} && echo "${zone}"
656 done
657}
658
1c6a4e30 659zones_get_nonlocal() {
1848564d
MT
660 local zone
661 for zone in $(zones_get_all); do
5e42d659 662 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
663 done
664}
665
1c6a4e30 666zones_get() {
1848564d
MT
667 local local=1
668 local remote=1
669
670 local zones
671
672 while [ $# -gt 0 ]; do
673 case "${1}" in
674 --local-only)
675 local=1
676 remote=0
677 ;;
678 --remote-only)
679 local=0
680 remote=1
681 ;;
682 --all)
683 local=1
684 remote=1
685 ;;
686 *)
687 if zone_name_is_valid ${1}; then
688 zones="${zones} ${1}"
689 else
690 warning "Unrecognized argument '${1}'"
691 fi
692 ;;
693 esac
694 shift
695 done
696
697 if [ -n "${zones}" ]; then
698 local zone
699 for zone in ${zones}; do
700 zone_exists ${zone} && echo "${zone}"
701 done
702 exit ${EXIT_OK}
703 fi
704
705 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
706 zones_get_all
707 elif [ ${local} -eq 1 ]; then
708 zones_get_local
709 elif [ ${remote} -eq 1 ]; then
710 zones_get_nonlocal
711 fi
712}
713
1c6a4e30 714zone_ports_list() {
1848564d
MT
715 local zone=${1}
716
717 local port
a5ebb169 718 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
719 [ -e "${port}" ] || continue
720
721 echo $(basename ${port})
03170817 722 done
1848564d
MT
723}
724
1c6a4e30 725zone_port_attach() {
ac694a6a
MT
726 local zone="${1}"
727 assert isset zone
728
729 local port="${2}"
730 assert isset port
731
732 shift 2
733
734 # Check if the port actually exists.
735 if ! port_exists "${port}"; then
736 error "Cannot attach port '${port}' which does not exist"
737 return ${EXIT_ERROR}
738 fi
739
740 # Check if the port is already connected to this or any other zone.
741 local z
742 for z in $(zones_get_all); do
743 if zone_has_port "${z}" "${port}"; then
744 error "Port '${port}' is already attached to zone '${z}'"
745 return ${EXIT_ERROR}
746 fi
747 done
748
749 local hook="$(zone_get_hook "${zone}")"
750 assert isset hook
751
f5ee091e 752 # Make the port briefly flash if supported
01a079e0
MT
753 if device_exists ${port}; then
754 port_identify "${port}" --background
755 fi
f5ee091e 756
ac694a6a
MT
757 hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@"
758 local ret="${?}"
759
760 case "${ret}" in
761 ${EXIT_OK})
762 log INFO "${port} has been attached to ${zone}"
763
764 # Automatically connect the port
abba34c1 765 zone_port_start "${zone}" "${port}"
ac694a6a
MT
766 ;;
767 *)
768 log CRITICAL "${port} could not be attached to ${zone}"
769 ;;
770 esac
771
772 return ${ret}
773}
774
1c6a4e30 775zone_port_edit() {
ac694a6a
MT
776 local zone="${1}"
777 assert isset zone
778
779 local port="${2}"
780 assert isset port
781
782 shift 2
783
784 # Check if the port actually exists.
785 if ! port_exists "${port}"; then
786 error "Port '${port}' does not exist"
787 return ${EXIT_ERROR}
788 fi
789
790 # Check if the zone actually has this port.
791 if ! zone_has_port "${zone}" "${port}"; then
792 error "Port '${port}' is not attached to zone '${zone}'"
793 return ${EXIT_ERROR}
794 fi
795
796 local hook=$(zone_get_hook "${zone}")
797 assert isset hook
798
799 hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
800}
801
1c6a4e30 802zone_port_detach() {
ac694a6a
MT
803 local zone="${1}"
804 assert isset zone
805
806 local port="${2}"
807 assert isset port
808
1848564d
MT
809 shift 2
810
ac694a6a
MT
811 # Check if the zone actually has this port.
812 if ! zone_has_port "${zone}" "${port}"; then
813 error "Port '${port}' is not attached to zone '${zone}'"
814 return ${EXIT_ERROR}
815 fi
816
817 local hook=$(zone_get_hook "${zone}")
818 assert isset hook
819
f5ee091e
MT
820 # Make the port briefly flash if supported
821 port_identify "${port}" --background
822
ac694a6a
MT
823 hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@"
824 local ret="${?}"
825
826 case "${ret}" in
827 ${EXIT_OK})
828 log INFO "${port} has been detached from ${zone}"
829
830 # Bring down the port if needed
abba34c1 831 zone_port_stop "${zone}" "${port}"
ac694a6a
MT
832 ;;
833 *)
834 log CRITICAL "${port} could not be detached from ${zone}"
835 ;;
836 esac
837
838 return ${ret}
839}
840
1c6a4e30 841zone_port_cmd() {
ac694a6a 842 local cmd="${1}"
711ffac1 843 assert isset cmd
ac694a6a
MT
844
845 local zone="${2}"
711ffac1 846 assert isset zone
1848564d 847
ac694a6a
MT
848 local port="${3}"
849 assert isset port
1848564d 850
ac694a6a
MT
851 shift 3
852
853 local hook="$(zone_get_hook "${zone}")"
854 assert isset hook
855
856 # Dispatch command to hook
857 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@
858}
859
1c6a4e30 860zone_port_create() {
ac694a6a
MT
861 zone_port_cmd "port_create" $@
862}
863
1c6a4e30 864zone_port_remove() {
ac694a6a
MT
865 zone_port_cmd "port_remove" $@
866}
867
1c6a4e30 868zone_port_up() {
ac694a6a
MT
869 zone_port_cmd "port_up" $@
870}
871
1c6a4e30 872zone_port_down() {
ac694a6a
MT
873 zone_port_cmd "port_down" $@
874}
875
abba34c1
MT
876# The next two functions automagically bring up and down
877# port that are attached to a bridge or similar.
878# The problem that is tried to overcome here is that there
879# are ports which exist all the time (like ethernet ports)
880# and therefore do not dispatch a hotplug event when
881# port_create is called.
882
1c6a4e30 883zone_port_start() {
abba34c1
MT
884 local zone="${1}"
885 local port="${2}"
886
887 if zone_is_active "${zone}"; then
888 if device_exists "${port}"; then
889 zone_port_up "${zone}" "${port}"
890 return ${?}
891 else
892 zone_port_create "${zone}" "${port}"
893 return ${?}
894 fi
895 fi
896
897 return ${EXIT_OK}
898}
899
1c6a4e30 900zone_port_stop() {
abba34c1
MT
901 local zone="${1}"
902 local port="${2}"
903
904 # Shut down the port if necessary
905 if zone_is_active "${zone}" && port_is_up "${port}"; then
906 zone_port_down "${zone}" "${port}"
907 fi
908
909 # Remove the port
910 zone_port_remove "${zone}" "${port}"
911}
912
1c6a4e30 913zone_port_status() {
ac694a6a
MT
914 zone_port_cmd "port_status" $@
915}
916
1c6a4e30 917zone_ports_cmd() {
ac694a6a
MT
918 local cmd="${1}"
919 assert isset cmd
920
921 local zone="${2}"
922 assert isset zone
923
924 shift 2
925
926 local hook="$(zone_get_hook "${zone}")"
711ffac1
MT
927
928 local port
929 for port in $(zone_get_ports ${zone}); do
ac694a6a 930 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@
1848564d
MT
931 done
932}
933
1c6a4e30 934zone_ports_create() {
1ba6a2bb
MT
935 zone_ports_cmd "port_create" $@
936}
937
1c6a4e30 938zone_ports_remove() {
1ba6a2bb
MT
939 zone_ports_cmd "port_remove" $@
940}
941
1c6a4e30 942zone_ports_up() {
ac694a6a 943 zone_ports_cmd "port_up" $@
1848564d
MT
944}
945
1c6a4e30 946zone_ports_down() {
ac694a6a 947 zone_ports_cmd "port_down" $@
711ffac1
MT
948}
949
1c6a4e30 950zone_ports_status() {
ac694a6a 951 zone_ports_cmd "port_status" $@
1848564d
MT
952}
953
1c6a4e30 954zone_configs_cmd() {
2472e0ea 955 assert [ $# -ge 2 ]
ea699552
MT
956
957 local cmd="${1}"
958 local zone="${2}"
1848564d
MT
959 shift 2
960
ea699552 961 assert zone_exists "${zone}"
1848564d 962
1848564d 963 local config
2472e0ea
MT
964 for config in $(zone_configs_list "${zone}"); do
965 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
966 assert isset config_hook
967
968 hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" $@
1848564d
MT
969 done
970}
971
1c6a4e30 972zone_configs_up() {
ea699552 973 zone_configs_cmd "up" $@
1848564d
MT
974}
975
1c6a4e30 976zone_configs_down() {
ea699552 977 zone_configs_cmd "down" $@
1848564d
MT
978}
979
1c6a4e30 980zone_configs_status() {
ea699552
MT
981 zone_configs_cmd "status" $@
982}
983
1c6a4e30 984zone_configs_list() {
ea699552
MT
985 local zone=${1}
986
987 local config
988 for config in $(zone_dir ${zone})/configs/*; do
989 [ -e "${config}" ] || continue
990
991 basename ${config}
992 done
a5ebb169
MT
993}
994
0afedf0d
JS
995zone_config_get_new_id() {
996 # This functions returns the next free id for a zone
997
998 assert [ $# -eq 1 ]
999 local zone=${1}
1000
1001 local zone_path=$(zone_dir ${zone})
1002 local i=0
1003
1004 while true; do
1005 if [ ! -f ${zone_path}/configs/*.${i} ]; then
1006 echo "${i}"
1007 return ${EXIT_OK}
1008 fi
1009 (( i++ ))
1010 done
1011}
1012
1c6a4e30 1013zone_config_get_hook() {
2472e0ea
MT
1014 assert [ $# -eq 2 ]
1015
1016 local zone="${1}"
1017 assert isset zone
1018
1019 local config="${2}"
1020 assert isset config
1021
1022 local HOOK
1023 zone_config_settings_read "${zone}" "${config}" \
1024 --ignore-superfluous-settings HOOK
1025
1026 print "${HOOK}"
1027}
1028
41f8233d
JS
1029zone_config_hook_is_configured() {
1030 # Checks if a zone has already at least one config with the given hook.
1031 # Returns True when yes and False when no
1032
1033 assert [ $# -eq 2 ]
1034 local zone=${1}
1035 local hook=${2}
1036
1037 local config
1038 for config in $(zone_configs_list "${zone}"); do
1039 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1040 assert isset config_hook
1041 if [[ ${hook} == ${config_hook} ]]; then
1042 return ${EXIT_TRUE}
1043 fi
1044
1045 done
1046
1047 # If we get here the zone has no config with the given hook
1048 return ${EXIT_FALSE}
1049}
1050
f158dc66
JS
1051zone_config_id_is_valid() {
1052 # This function checks if a given id is valid for a zone
1053 # Return True when yes and false when no
1054
1055 assert [ $# -eq 2 ]
1056 local zone=${1}
1057 local id=${2}
1058
1059 local zone_path=$(zone_dir ${zone})
1060
1061 [ -f ${zone_path}/configs/*.${id} ];
1062}
1063
cdf4266d
JS
1064zone_config_get_hook_from_id() {
1065 # Returns the hook for a given id
1066 assert [ $# -eq 2 ]
1067 local zone=${1}
1068 local id=${2}
1069
1070 local config
1071 for config in $(zone_configs_list "${zone}"); do
1072 if [[ ${config} == *.${id} ]]; then
1073 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1074 assert isset config_hook
1075 print "${config_hook}"
1076 return "${EXIT_OK}"
1077 fi
1078 done
1079
1080 # If we get here the zone has no config with the given id
1081 return ${EXIT_ERROR}
1082}
1083
1c6a4e30 1084zone_has_ip() {
38f61548 1085 device_has_ip $@
4231f419
MT
1086}
1087
1c6a4e30 1088zone_db() {
059469a8
MT
1089 local zone=${1}
1090 local action=${2}
1091 shift 2
1092
1093 case "${action}" in
1094 starting|started|stopping|stopped)
1095 db_connection_update ${zone} ${action}
1096 ;;
1097 esac
1098}
5e42d659 1099
1c6a4e30 1100zone_is_up() {
5e42d659
MT
1101 local zone=${1}
1102
1103 device_is_up ${zone}
1104}
1105
1c6a4e30 1106zone_is_down() {
5e42d659
MT
1107 ! zone_is_up $@
1108}
711ffac1 1109
1c6a4e30 1110zone_get_supported_port_hooks() {
711ffac1
MT
1111 local zone=${1}
1112
1113 local hook=$(zone_get_hook ${zone})
1114
1115 hook_zone_ports_get_all ${hook}
1116}
1117
1c6a4e30 1118zone_get_supported_config_hooks() {
ea699552 1119 hook_config_get_all
a5ebb169
MT
1120}
1121
1c6a4e30 1122zone_file() {
711ffac1
MT
1123 local zone=${1}
1124
1125 assert isset zone
1126
1127 echo "$(zone_dir ${zone})/settings"
1128}
1129
1c6a4e30 1130zone_settings_read() {
711ffac1 1131 local zone=${1}
711ffac1 1132 assert isset zone
52acd25f
MT
1133 shift
1134
1135 local args
1136 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then
1137 list_append args ${HOOK_SETTINGS}
1138 else
1139 list_append args $@
1140 fi
711ffac1 1141
bfd6c282
MT
1142 # Save the HOOK variable.
1143 local hook="${HOOK}"
1144
52acd25f 1145 settings_read "$(zone_file "${zone}")" ${args}
bfd6c282
MT
1146
1147 # Restore hook.
1148 HOOK="${hook}"
711ffac1
MT
1149}
1150
1c6a4e30 1151zone_settings_write() {
1e6f187e 1152 local zone="${1}"
711ffac1
MT
1153 assert isset zone
1154
1e6f187e
MT
1155 local args
1156 if function_exists "hook_check_settings"; then
1157 list_append args "--check=\"hook_check_settings\""
1158 fi
1159 list_append args ${HOOK_SETTINGS}
1160
1161 settings_write "$(zone_file ${zone})" ${args}
711ffac1
MT
1162}
1163
1c6a4e30 1164zone_settings_set() {
711ffac1
MT
1165 local zone=${1}
1166 shift
1167 local args="$@"
1168
1169 assert isset zone
1170
1171 (
e9df08ad 1172 zone_settings_read ${zone}
711ffac1
MT
1173
1174 for arg in ${args}; do
1175 eval "${arg}"
1176 done
1177
e9df08ad 1178 zone_settings_write ${zone}
711ffac1
MT
1179 )
1180}
6b3f9c85 1181
1c6a4e30 1182zone_settings_get() {
6b3f9c85
MT
1183 local zone=${1}
1184 local key=${2}
1185
1186 assert isset zone
1187 assert isset key
1188
1189 (
1e6f187e
MT
1190 zone_settings_read "${zone}" "${key}" \
1191 --ignore-superfluous-settings
6b3f9c85
MT
1192
1193 echo "${!key}"
1194 )
1195}
e9df08ad 1196
1c6a4e30 1197zone_config_settings_read() {
c8132752 1198 assert [ $# -ge 2 ]
e9df08ad
MT
1199
1200 local zone="${1}"
1201 local config="${2}"
1202 shift 2
1203
c8132752
MT
1204 local args
1205 if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then
1206 list_append args ${HOOK_CONFIG_SETTINGS}
1207 else
1208 list_append args $@
1209 fi
1210
e9df08ad 1211 local path="$(zone_dir "${zone}")/configs/${config}"
c8132752 1212 settings_read "${path}" ${args}
e9df08ad
MT
1213}
1214
1c6a4e30 1215zone_config_settings_write() {
c8132752 1216 assert [ $# -ge 2 ]
e9df08ad
MT
1217
1218 local zone="${1}"
0959b0b9
JS
1219 local hook="${2}"
1220 local id=${3}
1221
1222 if ! isset id; then
1223 id=$(zone_config_get_new_id ${zone})
1224 log DEBUG "ID for the config is: ${id}"
1225 fi
e9df08ad 1226
c8132752
MT
1227 local args
1228 if function_exists "hook_check_config_settings"; then
1229 list_append args "--check=\"hook_check_config_settings\""
1230 fi
1231 list_append args ${HOOK_CONFIG_SETTINGS}
1232
0959b0b9 1233 local path="$(zone_dir "${zone}")/configs/${hook}.${id}"
c8132752 1234 settings_write "${path}" ${args}
e9df08ad
MT
1235}
1236
1c6a4e30 1237zone_port_settings_read() {
ac694a6a 1238 assert [ $# -ge 2 ]
e9df08ad
MT
1239
1240 local zone="${1}"
1241 local port="${2}"
1242 shift 2
1243
ac694a6a
MT
1244 local args
1245 if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
1246 list_append args ${HOOK_PORT_SETTINGS}
1247 else
1248 list_append args $@
1249 fi
1250
e9df08ad 1251 local path="$(zone_dir "${zone}")/ports/${port}"
ac694a6a 1252 settings_read "${path}" ${args}
e9df08ad
MT
1253}
1254
1c6a4e30 1255zone_port_settings_write() {
02236ca6 1256 assert [ $# -ge 2 ]
e9df08ad
MT
1257
1258 local zone="${1}"
1259 local port="${2}"
1260 shift 2
1261
1e6f187e
MT
1262 local args
1263 if function_exists "hook_check_port_settings"; then
1264 list_append args "--check=\"hook_check_port_settings\""
1265 fi
ac694a6a 1266 list_append args ${HOOK_PORT_SETTINGS}
1e6f187e 1267
e9df08ad 1268 local path="$(zone_dir "${zone}")/ports/${port}"
1e6f187e 1269 settings_write "${path}" ${args}
e9df08ad
MT
1270}
1271
1c6a4e30 1272zone_port_settings_remove() {
e9df08ad
MT
1273 assert [ $# -eq 2 ]
1274
1275 local zone="${1}"
1276 local port="${2}"
1277
1278 local path="$(zone_dir "${zone}")/ports/${port}"
1279 settings_remove "${path}"
1280}
410d2e85
JS
1281
1282zone_get_color() {
1283 # This function return the color of a zone
1284 assert [ $# -eq 1 ]
1285
1286 local name=${1}
1287 color_read "zone" ${name}
1288}
5de34b4c
JS
1289
1290zone_get_description_title() {
1291 assert [ $# -eq 1 ]
1292
1293 local name=${1}
1294 description_title_read $(description_format_filename "zone" "${name}")
1295}