-
Notifications
You must be signed in to change notification settings - Fork 1
/
minidlna
94 lines (83 loc) · 3.37 KB
/
minidlna
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
# Copyright (C) 2022 Rodrigo Silva (MestreLion) <[email protected]>
# License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
# -----------------------------------------------------------------------------
DESCRIPTION='MiniDLNA Server'
ARGS='[MEDIA_DIR] [FRIENDLY_NAME]'
setuplib=${SETUP_LIB_PATH:-"$(dirname "$(readlink -f "$0")")"/../setuplib}
# shellcheck source=../setuplib
if [[ -r "$setuplib" ]]; then source "$setuplib"; else
echo "Setup library not found: $setuplib" >&2; exit 1;
fi
# -----------------------------------------------------------------------------
media_dir=${1:-${SETUP_MINIDLNA_MEDIA_DIR:-}}
friendly_name=${2:-${SETUP_MINIDLNA_FRIENDLY_NAME:-}}
# -----------------------------------------------------------------------------
clamp() {
local value=$1
local min=$2
local max=$3
awk -v a="$min" -v b="$max" '$0<a {$0=a} $0>b {$0=b} 1' <<< "$value"
}
# -----------------------------------------------------------------------------
install_package minidlna
# Fix "WARNING: Inotify max_user_watches [8192] is low ..."
current=$(</proc/sys/fs/inotify/max_user_watches)
kernel=8192 # Approximate watches per GiB RAM in kernel >= 5.11, fixed before
limit=1048576 # 1 Mi watches
expected=$(awk -v k=$kernel '/MemTotal/{printf("%d", k * $2 / 1048576)}' /proc/meminfo)
if ((current < 2 * expected)); then
watches=$(clamp $((4 * expected)) "$kernel" "$limit")
config=/etc/sysctl.d/90-increase-inotify_max_user_watches.conf
message "Increase inotify maximum user watches to ${watches}"
setup_run sudo tee -- "$config" <<-EOF
$(signature)
# Increase inotify watch limit for MiniDLNA and possibly others.
#
# Original setting:
# ${current}
# Reference values:
# Kernel < 5.10 (Ubuntu 18.04, 20.04.2): fixed at 8192 (8Ki)
# Kernel >= 5.11 (Ubuntu 20.04.3, 22.04): up to ~64Ki with 8GiB RAM (use up to ~1% RAM)
# See https://github.com/torvalds/linux/commit/92890123749bafc317bbfacbe0a62
# Some sources say each _used_ watch takes ~1080B of RAM in a 64-bit system
# Current watch actual usage:
# sudo lsof | grep inotify | wc -l
# Relevant manpages:
# man inotify
# man sysctl.conf
#
# Increasing maximum up to approximately 4 times the (modern) kernel default,
# with a minimum of ${kernel} and a limit of ${limit}
fs.inotify.max_user_watches = ${watches}
EOF
echo
sudo sysctl --load "$config"
fi
message "Enable Firewall rules"
multicast=239.255.255.250
setup_run sudo tee /etc/ufw/applications.d/minidlna.conf <<-EOF
$(signature)
# May be restricted to destination ${multicast}, minidlnad only binds to that IP for SSDP
# sudo ufw allow to "$multicast" app 'MiniDLNA SSDP'
[MiniDLNA SSDP]
title=DLNA Media Server (MiniDLNA UPnP/SSDP Discovery)
description=MiniDLNA (aka ReadyDLNA) aims to be fully compliant with DLNA/UPnP-AV clients
ports=1900/udp
[MiniDLNA Server]
title=DLNA Media Server (MiniDLNA UPnP-AV/HTTP Server)
description=MiniDLNA (aka ReadyDLNA) aims to be fully compliant with DLNA/UPnP-AV clients
ports=8200/tcp
EOF
setup_run sudo ufw allow to "$multicast" app 'MiniDLNA SSDP'
setup_run sudo ufw allow app 'MiniDLNA Server'
try sudo ufw reload
# TODO: Patch config file!
if [[ "$media_dir" ]]; then
: #media_dir=${media_dir}
fi
# Name that the DLNA server presents to clients.
# Defaults to "hostname: username".
if [[ "$friendly_name" ]]; then
: #friendly_name=${friendly_name}
fi