Thang Q. Nguyen | fe4a607 | 2023-06-09 08:55:58 +0700 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | declare -a power_reboot_targets=( |
| 4 | obmc-host-reboot@0.target |
| 5 | obmc-host-warm-reboot@0.target |
| 6 | obmc-host-force-warm-reboot@0.target |
| 7 | ) |
| 8 | |
| 9 | declare -a power_off_targets=( |
| 10 | obmc-chassis-hard-poweroff@0.target |
| 11 | ) |
| 12 | |
| 13 | declare -a power_on_targets=( |
| 14 | obmc-host-start@0.target |
| 15 | ) |
| 16 | |
| 17 | systemd1_service="org.freedesktop.systemd1" |
| 18 | systemd1_object_path="/org/freedesktop/systemd1" |
| 19 | systemd1_manager_interface="org.freedesktop.systemd1.Manager" |
| 20 | mask_method="MaskUnitFiles" |
| 21 | unmask_method="UnmaskUnitFiles" |
| 22 | |
| 23 | function mask_reboot_targets() |
| 24 | { |
| 25 | # To prevent reboot actions, this function will mask all reboot targets |
| 26 | for target in "${power_reboot_targets[@]}" |
| 27 | do |
| 28 | busctl call $systemd1_service $systemd1_object_path $systemd1_manager_interface \ |
| 29 | $mask_method asbb 1 "$target" true true |
| 30 | done |
| 31 | } |
| 32 | |
| 33 | function unmask_reboot_targets() |
| 34 | { |
| 35 | # Allow reboot targets work normal |
| 36 | for target in "${power_reboot_targets[@]}" |
| 37 | do |
| 38 | busctl call $systemd1_service $systemd1_object_path $systemd1_manager_interface \ |
| 39 | $unmask_method asb 1 "$target" true |
| 40 | done |
| 41 | } |
| 42 | |
| 43 | function mask_off_targets() |
| 44 | { |
| 45 | # To prevent off actions,this function will mask all off targets |
| 46 | for target in "${power_off_targets[@]}" |
| 47 | do |
| 48 | busctl call $systemd1_service $systemd1_object_path $systemd1_manager_interface \ |
| 49 | $mask_method asbb 1 "$target" true true |
| 50 | done |
| 51 | } |
| 52 | |
| 53 | function unmask_off_targets() |
| 54 | { |
| 55 | # Allow off targets work normal |
| 56 | for target in "${power_off_targets[@]}" |
| 57 | do |
| 58 | busctl call $systemd1_service $systemd1_object_path $systemd1_manager_interface \ |
| 59 | $unmask_method asb 1 "$target" true |
| 60 | done |
| 61 | } |
| 62 | |
| 63 | function mask_on_targets() |
| 64 | { |
| 65 | # To prevent on actions, this function will mask all on targets |
| 66 | systemctl mask "${power_on_targets[@]}" --runtime |
| 67 | } |
| 68 | |
| 69 | function unmask_on_targets() |
| 70 | { |
| 71 | # Allow on targets work normal |
| 72 | systemctl unmask "${power_on_targets[@]}" --runtime |
| 73 | } |
| 74 | |
| 75 | purpose=$1 |
| 76 | allow=$2 |
| 77 | |
| 78 | if [ "$purpose" == "reboot" ]; then |
| 79 | if [ "$allow" == "false" ] |
| 80 | then |
| 81 | mask_reboot_targets |
| 82 | else |
| 83 | unmask_reboot_targets |
| 84 | fi |
| 85 | elif [ "$purpose" == "off" ]; then |
| 86 | if [ "$allow" == "false" ] |
| 87 | then |
| 88 | mask_off_targets |
| 89 | else |
| 90 | unmask_off_targets |
| 91 | fi |
| 92 | elif [ "$purpose" == "on" ]; then |
| 93 | if [ "$allow" == "false" ] |
| 94 | then |
| 95 | mask_on_targets |
| 96 | else |
| 97 | unmask_on_targets |
| 98 | fi |
| 99 | fi |