b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | |
| 3 | START=13 |
| 4 | STOP=98 |
| 5 | |
| 6 | USE_PROCD=1 |
| 7 | PROG=/sbin/mdadm |
| 8 | NAME=mdadm |
| 9 | |
| 10 | CONF="/var/etc/mdadm.conf" |
| 11 | |
| 12 | append_list_item() { |
| 13 | append "$2" "$1" "$3" |
| 14 | } |
| 15 | |
| 16 | append_option() { |
| 17 | local var="$1" |
| 18 | local cfg="$2" |
| 19 | local opt="$3" |
| 20 | local name="$4" |
| 21 | local sep="$5" |
| 22 | local str |
| 23 | |
| 24 | if [ -n "$sep" ]; then |
| 25 | config_list_foreach "$cfg" "$opt" append_list_item str "$sep" |
| 26 | else |
| 27 | config_get str "$cfg" "$opt" |
| 28 | fi |
| 29 | |
| 30 | [ -n "$str" ] && append "$var" $(printf "%s=%s" "${name:-${opt//_/-}}" "$str") |
| 31 | } |
| 32 | |
| 33 | mdadm_common() { |
| 34 | local cfg="$1" |
| 35 | local email devices |
| 36 | |
| 37 | if [ -x /usr/sbin/sendmail ]; then |
| 38 | config_get email "$cfg" email |
| 39 | [ -n "$email" ] && printf "MAILADDR %s\n" "$email" >> $CONF |
| 40 | fi |
| 41 | |
| 42 | config_list_foreach "$cfg" devices append_list_item devices " " |
| 43 | [ -n "$devices" ] && printf "DEVICE %s\n" "$devices" >> $CONF |
| 44 | } |
| 45 | |
| 46 | mdadm_array() { |
| 47 | local cfg="$1" |
| 48 | local uuid device devices name array |
| 49 | |
| 50 | config_get uuid "$cfg" uuid |
| 51 | config_get name "$cfg" name |
| 52 | config_get device "$cfg" device |
| 53 | |
| 54 | if [ -z "$device" ] || [ -z "$uuid$name" ]; then |
| 55 | echo "Skipping array without device, uuid or name" >&2 |
| 56 | return |
| 57 | fi |
| 58 | |
| 59 | [ -n "$uuid" ] && append array "uuid=$uuid" |
| 60 | [ -n "$name" ] && append array "name=$name" |
| 61 | |
| 62 | append_option array "$cfg" super_minor |
| 63 | append_option array "$cfg" spares |
| 64 | append_option array "$cfg" spare_group |
| 65 | append_option array "$cfg" bitmap |
| 66 | append_option array "$cfg" container |
| 67 | append_option array "$cfg" member |
| 68 | append_option array "$cfg" devices devices "," |
| 69 | |
| 70 | printf "ARRAY %s %s\n" "$device" "$array" >> $CONF |
| 71 | } |
| 72 | |
| 73 | start_service() { |
| 74 | local email |
| 75 | |
| 76 | mkdir -p "${CONF%/*}" |
| 77 | printf "# Autogenerated from /etc/config/mdadm, do not edit!\n" > $CONF |
| 78 | |
| 79 | config_load mdadm |
| 80 | config_foreach mdadm_common mdadm |
| 81 | config_foreach mdadm_array array |
| 82 | |
| 83 | $PROG --assemble --scan --config="$CONF" |
| 84 | |
| 85 | procd_open_instance |
| 86 | procd_set_param command "$PROG" --monitor --syslog --scan --config="$CONF" |
| 87 | procd_close_instance |
| 88 | } |
| 89 | |
| 90 | stop_service() { |
| 91 | $PROG --stop --scan |
| 92 | } |
| 93 | |