op-packages/baidudrive/files/baidudrive.init
github-actions[bot] 4a68251cc3 🛸 Sync 2026-07-10 20:47:34
2026-07-10 20:47:34 +08:00

166 lines
4.3 KiB
Bash

#!/bin/sh /etc/rc.common
START=93
USE_PROCD=1
DEVICE_TYPE="202604101148240791"
get_config() {
config_section="$1"
config_get_bool enabled "$1" enabled 1
config_get data_dir "$1" data_dir ""
config_get host "$1" host "0.0.0.0"
config_get port "$1" port "10780"
config_get sdk_dir "$1" sdk_dir "/opt/baidunas-sdk"
config_get sdk_host "$1" sdk_host "127.0.0.1"
config_get sdk_port "$1" sdk_port "8001"
config_get macid "$1" macid ""
config_get usb_path "$1" usb_path ""
config_get download_path "$1" download_path "/"
config_get quota_path "$1" quota_path ""
config_get tmp_path "$1" tmp_path ""
config_get log_level "$1" log_level "7"
}
require_start_value() {
local name="$1"
local value="$2"
if [ -z "$value" ]; then
logger -t baidudrive "$name is empty; please set option $name in /etc/config/baidudrive"
return 1
fi
return 0
}
require_executable() {
local path="$1"
if [ ! -x "$path" ]; then
logger -t baidudrive "missing executable: $path"
return 1
fi
return 0
}
require_file() {
local path="$1"
if [ ! -e "$path" ]; then
logger -t baidudrive "missing runtime file: $path"
return 1
fi
return 0
}
storage_root_from_data_dir() {
local path="$1"
df -P "$path" 2>/dev/null | awk 'NR == 2 { print $6 }'
}
default_macid() {
local netdev mac
for netdev in br-lan eth0 lan0; do
if [ -r "/sys/class/net/$netdev/address" ]; then
mac="$(tr -d ':' <"/sys/class/net/$netdev/address")"
if [ -n "$mac" ]; then
printf '%s\n' "$mac"
return 0
fi
fi
done
return 1
}
ensure_macid() {
if [ -n "$macid" ]; then
return 0
fi
macid="$(default_macid)"
if [ -n "$macid" ] && [ -n "$config_section" ]; then
uci set "baidudrive.$config_section.macid=$macid"
uci commit baidudrive
logger -t baidudrive "generated macid from network interface: $macid"
fi
}
start_service() {
config_load baidudrive
config_foreach get_config baidudrive
if [ "$enabled" != 1 ]; then
return 1
fi
require_start_value data_dir "$data_dir" || return 1
ensure_macid
require_start_value macid "$macid" || return 1
require_executable /usr/sbin/baidudrive || return 1
require_executable "$sdk_dir/baiduNas" || return 1
require_executable "$sdk_dir/P2PClient" || return 1
require_executable "$sdk_dir/P2PClient.bin" || return 1
require_file "$sdk_dir/libkernel.so" || return 1
require_file /opt/baidunas-glibc || return 1
mkdir -p "$data_dir" || return 1
local storage_root
storage_root="$(storage_root_from_data_dir "$data_dir")"
if [ -n "$storage_root" ]; then
[ -n "$usb_path" ] && [ "$usb_path" != "/mnt" ] || usb_path="$storage_root"
[ -n "$quota_path" ] && [ "$quota_path" != "/mnt" ] || quota_path="$storage_root"
fi
[ -n "$usb_path" ] || usb_path="$data_dir"
[ -n "$quota_path" ] || quota_path="$usb_path"
[ -n "$tmp_path" ] || tmp_path="$data_dir/sdk-tmp"
mkdir -p "$tmp_path" || return 1
rm -rf /mnt/mnt /mnt/.baidudrive-write-test-* 2>/dev/null || true
logger -t baidudrive "Starting Baidu NAS SDK"
procd_open_instance baiduNas
procd_set_param command "$sdk_dir/baiduNas" \
-ip "$sdk_host" \
-port "$sdk_port" \
-macid "$macid" \
-type "$DEVICE_TYPE" \
-tmppath "$tmp_path" \
-usbpath "$usb_path" \
-downloadpath "$download_path" \
-order lifo \
-forceipv4 \
-loglevel "$log_level"
procd_set_param dir "$sdk_dir"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
logger -t baidudrive "Starting Baidu NAS SDK init loop"
procd_open_instance sdk-init
procd_set_param command /usr/libexec/baidudrive/sdk-init.sh
procd_set_param env \
DATA_DIR="$data_dir" \
BAIDU_NAS_HOST="$sdk_host" \
BAIDU_NAS_PORT="$sdk_port" \
BAIDU_NAS_USB_PATH="$usb_path" \
BAIDU_NAS_QUOTA_PATH="$quota_path"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
logger -t baidudrive "Starting BaiduDrive Service"
procd_open_instance baidudrive
procd_set_param env \
BAIDU_NAS_MACID="$macid" \
BAIDU_NAS_DEVICE_TYPE="$DEVICE_TYPE" \
BAIDU_NAS_USB_PATH="$usb_path" \
BAIDU_NAS_QUOTA_PATH="$quota_path"
procd_set_param command /usr/sbin/baidudrive --host "$host" --port "$port" --data "$data_dir"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
}
service_triggers() {
procd_add_reload_trigger "baidudrive"
}