forked from danb35/freenas-iocage-plex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex-jail-truenas.sh
executable file
·242 lines (221 loc) · 8.09 KB
/
plex-jail-truenas.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/sh
# Build an iocage jail under TrueNAS 12.0 or greater using the current release of Plex Media Server
JAIL_IP=""
DEFAULT_GW_IP=""
NETMASK=""
USE_HW_TRANSCODE="0"
VNET="on"
POOL_PATH=""
PLEX_CONFIG_PATH=""
PLEX_MEDIA_PATH=""
INTERFACE="vnet0"
RELEASE_VERSION=""
# BUGBUG In FreeNAS 11.3-U1, a 'plex' jail would not install pkg; any other name would
# This was caused by the presence of another jail that had been named 'plex' at one
# point. Might be CPE or FreeNAS. Since this script is used to migrate data off an
# old plugin, side-stepping issue by naming jail 'pms'.
JAIL_NAME="pms"
USE_BETA=0
USE_BASEJAIL="-b"
PLEXPKG=""
HW_TRANSCODE_RULESET="10"
DEVFS_RULESET=""
RULESET_SCRIPT="/root/scripts/plex-ruleset.sh"
# Let's check if this is being run on a FreeNAS version. If so let's exit
if [ ! -z "$(echo ${RELEASE} | grep -E '11\.[0-9]|10\.[0-9]')" ]; then
echo "ERROR: This script is meant for TrueNAS 12.0 or greater. If you are running FreeNAS version 11 or earlier, please use this script: ./freenas-plex-jail.sh"
exit 0
fi
# $1 = devfs ruleset number
# $2 = script location
# Creates script for devfs ruleset and i915kms, causes it to execute on boot, and loads it
createrulesetscript() {
if [ -z "$1" ] ; then
echo "ERROR: No plex devfs ruleset number specified. This is an internal script error."
return 1
fi
if [ -z "$2" ] ; then
echo "ERROR: No plex ruleset script location specified. This is an internal script error."
return 1
fi
# Extract the major and minor version numbers
MAJOR_VERSION=$(echo $RELEASE_VERSION | cut -d. -f1)
MINOR_VERSION=$(echo $RELEASE_VERSION | cut -d. -f2)
# Check if the version is 12.0 or greater
if [ $MAJOR_VERSION -gt 12 ] || [ $MAJOR_VERSION -eq 12 -a $MINOR_VERSION -ge 0 ]; then
echo "This script supports TrueNAS 12.0 or greater."
else
echo "This script only knows how to enable hardware transcode in TrueNAS 12.0 or greater."
return 1
fi
IGPU_MODEL=$(lspci -q | grep Intel | grep Graphics)
if [ ! -z "${IGPU_MODEL}" ] ; then
echo "Found Intel GPU model " ${IGPU_MODEL} ", this bodes well."
if [ -z "$(kldstat | grep i915kms.ko)" ] ; then
kldload /boot/modules/i915kms.ko
if [ -z "$(kldstat | grep i915kms.ko)" ] ; then
echo "Unable to load driver for Intel iGPU, please verify it is supported in this version of TrueNAS"
return 1
fi
fi
else
echo "The naive Intel iGPU check didn't find one."
echo "If you know you have supported hardware, please send the authors of this script"
echo "the output of \"lspci\" on your system, and we'll improve the detection logic."
return 1
fi
if [ ! -f $2 ] ; then
echo "Creating script file" $2
cat > $2 <<EOF
#!/bin/sh
echo '[devfsrules_bpfjail=101]
add path 'bpf*' unhide
[plex_drm=$1]
add include \$devfsrules_hide_all
add include \$devfsrules_unhide_basic
add include \$devfsrules_unhide_login
add include \$devfsrules_jail
add include \$devfsrules_bpfjail
add path 'dri*' unhide
add path 'dri/*' unhide
add path 'drm*' unhide
add path 'drm/*' unhide' >> /etc/devfs.rules
service devfs restart
kldload /boot/modules/i915kms.ko
EOF
chmod +x $2
else
if [ -z "$(grep "plex_drm=$1" $2)" ]; then
echo "Script file $2 exists, but does not configure devfs ruleset $1 for Plex as expected."
return 1
fi
fi
if [ -z "$(devfs rule -s $1 show)" ]; then
echo "Executing script file $2"
$2
fi
if [ -z "$(midclt call initshutdownscript.query | grep $2)" ]; then
echo "Setting script $2 to execute on boot"
midclt call initshutdownscript.create "{\"type\": \"SCRIPT\", \"script\": \"$2\", \"when\": \"POSTINIT\", \"enabled\": true, \"timeout\": 10}"
fi
return 0
}
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "${SCRIPT}")
. "${SCRIPTPATH}"/plex-config
CONFIGS_PATH="${SCRIPTPATH}"/configs
# Check for plex-config and set configuration
if ! [ -e "${SCRIPTPATH}"/plex-config ]; then
echo "${SCRIPTPATH}/plex-config must exist."
exit 1
fi
# Check that necessary variables were set by plex-config
if [ -z "${JAIL_IP}" ]; then
echo 'Configuration error: JAIL_IP must be set'
exit 1
fi
if [ -z "${DEFAULT_GW_IP}" ]; then
echo 'Configuration error: DEFAULT_GW_IP must be set'
exit 1
fi
if [ -z "${NETMASK}" ]; then
echo 'Netmask not set, defaulting to /24 (255.255.255.0)'
NETMASK="24"
fi
if [ -z "${POOL_PATH}" ]; then
echo 'Configuration error: POOL_PATH must be set'
exit 1
fi
# If PLEX_CONFIG_PATH isn't specified in plex-config, set it
if [ -z "${PLEX_CONFIG_PATH}" ]; then
echo "Plex metadata path not set, defaulting to ${POOL_PATH}/plex_data"
PLEX_CONFIG_PATH="${POOL_PATH}"/plex_data
fi
if [ -z "${PLEX_MEDIA_PATH}" ]; then
echo "Plex media path not set, please mount media directory manually when done"
else
echo "Plex media path is ${PLEX_MEDIA_PATH}, the script will mount this into /media inside the Plex jail"
fi
if [ -z "${USE_HW_TRANSCODE}" ]; then
USE_HW_TRANSCODE="0"
fi
if [ $USE_HW_TRANSCODE -eq 0 ]; then
echo "Not configuring hardware transcode"
else
if createrulesetscript ${HW_TRANSCODE_RULESET} ${RULESET_SCRIPT}; then
echo "Configuring hardware transcode with ruleset ${HW_TRANSCODE_RULESET}."
DEVFS_RULESET="devfs_ruleset=${HW_TRANSCODE_RULESET}"
else
echo "Not configuring hardware transcode automatically, please do it manually."
DEVFS_RULESET=""
USE_HW_TRANSCODE="0"
fi
fi
if [ $USE_BETA -eq 1 ]; then
echo "Using beta-release plexmediaserver code"
PLEXPKG="plexmediaserver-plexpass"
else
echo "Using stable-release plexmediaserver code"
PLEXPKG="plexmediaserver"
fi
# Establish freebsd release version to be used
if [ -z "$RELEASE_VERSION" ]; then
echo "RELEASE_VERSION is not configured. Pulling system release version"
RELEASE_VERSION=$(freebsd-version | cut -d - -f -1)"-RELEASE"
echo "using: $RELEASE_VERSION"
else
echo "using version from plex-config: $RELEASE_VERSION"
fi
# Create jail
echo "Creating jail "${JAIL_NAME}". This may take a minute, please be patient."
if ! iocage create --name "${JAIL_NAME}" -r "${RELEASE_VERSION}" ip4_addr="${INTERFACE}|${JAIL_IP}/${NETMASK}" defaultrouter="${DEFAULT_GW_IP}" boot="on" host_hostname="${JAIL_NAME}" vnet="${VNET}" ${USE_BASEJAIL} ${DEVFS_RULESET}
then
echo "Failed to create jail"
exit 1
fi
iocage exec "${JAIL_NAME}" mkdir /config
iocage exec "${JAIL_NAME}" mkdir /configs
iocage exec "${JAIL_NAME}" mkdir -p /usr/local/etc/pkg/repos
iocage exec "${JAIL_NAME}" cp /etc/pkg/FreeBSD.conf /usr/local/etc/pkg/repos/
iocage exec "${JAIL_NAME}" sed -i '' "s/quarterly/latest/" /usr/local/etc/pkg/repos/FreeBSD.conf
mkdir -p "${PLEX_CONFIG_PATH}"
chown -R 972:972 "${PLEX_CONFIG_PATH}"
iocage fstab -a "${JAIL_NAME}" "${PLEX_CONFIG_PATH}" /config nullfs rw 0 0
iocage fstab -a "${JAIL_NAME}" "${CONFIGS_PATH}" /configs nullfs rw 0 0
if [ -n "${PLEX_MEDIA_PATH}" ]; then
iocage fstab -a "${JAIL_NAME}" "${PLEX_MEDIA_PATH}" /media nullfs rw 0 0
fi
iocage exec "${JAIL_NAME}" cp /configs/pkg.conf /usr/local/etc
if ! iocage exec "${JAIL_NAME}" pkg install ${PLEXPKG}
then
echo "Failed to install ${PLEXPKG} package"
iocage stop "${JAIL_NAME}"
iocage destroy -f "${JAIL_NAME}"
exit 1
fi
iocage exec "${JAIL_NAME}" rm /usr/local/etc/pkg.conf
if [ $USE_BETA -eq 1 ]; then
iocage exec "${JAIL_NAME}" sysrc plexmediaserver_plexpass_enable="YES"
iocage exec "${JAIL_NAME}" sysrc plexmediaserver_plexpass_support_path="/config"
else
iocage exec "${JAIL_NAME}" sysrc plexmediaserver_enable="YES"
iocage exec "${JAIL_NAME}" sysrc plexmediaserver_support_path="/config"
sed -i '' "s/_plexpass//" "${CONFIGS_PATH}"/update_packages
fi
iocage exec "${JAIL_NAME}" crontab /configs/update_packages
iocage fstab -r "${JAIL_NAME}" "${CONFIGS_PATH}" /configs nullfs rw 0 0
iocage exec "${JAIL_NAME}" rm -rf /configs
if [ $USE_HW_TRANSCODE -ne 0 ]; then
iocage exec "${JAIL_NAME}" pw groupmod -n video -m plex
fi
if [ -z "${PLEX_MEDIA_PATH}" ]; then
iocage stop "${JAIL_NAME}"
else
iocage restart "${JAIL_NAME}"
fi
echo "Installation Complete!"
if [ -z "${PLEX_MEDIA_PATH}" ]; then
echo "Mount your media folder into the jail, then start the jail."
fi
echo "Log in and configure your server by browsing to:"
echo "http://$JAIL_IP:32400/web"