bbeny123
New Around Here
I was recently installing Plex media server on my router and as the existing sources are outdated and/or unavailable I had to fiddle around a bit to get it to work.
Here's a little tutorial on how to do it.
Based on:
Prerequisites:
1 - ssh to router
* - increase swappiness if it is 0 (in my case, it significantly improved the performance of the Plex Media Server)
2 - install required packages
3 - remount Entware partition
4 - install chrooted Debian
5 - prepare Debian's init.d script
* - prepare Debian's remount hotplugged USB script
6 - prepare chrooted services list and create symlink to Debian
* - copy hosts file to Debian
7 - enter debian
8 - upgrade packages and install those required by Plex Media Server
9 - configure timezone
10 - ensure that
11 - instal Plex Media Server
12 - exit Debian
13 - add Plex Media Server to chrooted services list
14 - restart Debian
1 - ssh to router
* - update amtm and entware packages using amtm
2 - enter debian
3 - ensure that
4 - upgrade debian packages
* - restart Debian
Here's a little tutorial on how to do it.
Based on:
- https://hqt.ro/how-to-install-debian-stretch-arm/ (accessed by The Wayback Machine)
- https://www.hqt.ro/plex-media-server-on-asuswrt-armhf-routers/ (accessed by The Wayback Machine)
- https://www.snbforums.com/threads/asus-rt-ac86u-and-debian-bullseye-nextcloud.79428/
Prerequisites:
- installed Entware
- SWAP enabled (2GB or more recommended)
* steps are optional.
1 - ssh to router
* - increase swappiness if it is 0 (in my case, it significantly improved the performance of the Plex Media Server)
Bash:
# check current value of swappiness
cat /proc/sys/vm/swappiness
# change swappiness on runtime and persists it after reboot
echo 10 > /proc/sys/vm/swappiness
echo 'echo 10 > /proc/sys/vm/swappiness' >> /jffs/scripts/post-mount
2 - install required packages
Bash:
opkg install coreutils-sha256sum debootstrap binutils perlbase-autodie
3 - remount Entware partition
Bash:
mount -i -o remount,exec,dev /opt/..
4 - install chrooted Debian
Bash:
debootstrap --variant=minbase --arch=arm64 bookworm /opt/debian/ http://ftp.debian.org/debian/
5 - prepare Debian's init.d script
Bash:
rm /opt/etc/init.d/S99debian
wget -O /opt/etc/init.d/S99debian https://raw.githubusercontent.com/bbeny123/Plex_Asuswrt-Merlin/main/init-debian.sh
chmod 755 /opt/etc/init.d/S99debian
# instead of downloading the script, it can also be copy-pasted:
# cat >> /opt/etc/init.d/S99debian << 'EOF'
# copy-paste script from the spoiler below
# type 'EOF' and press enter
By default, all subdirs of/tmp/mnt/
(except the Entware partition) will be bind-mounted on chrooted Debian's/mnt/
(making them visible to Plex Media Server).
Bash:
#!/bin/sh
PATH=/opt/bin:/opt/sbin:/sbin:/bin:/usr/sbin:/usr/bin
CHROOT_DIR=$(readlink -f /opt/debian)
CHROOT_SERVICES_LIST=/opt/etc/chroot-services.list
if [ ! -e "$CHROOT_SERVICES_LIST" ]; then
echo "Please, define Debian services to start in $CHROOT_SERVICES_LIST first!"
echo "One service per line. Hint: this is a script names from Debian's /etc/init.d/"
exit 1
fi
running() {
if [ $(mount | grep $CHROOT_DIR | wc -l) -gt 0 ]; then
return 0
fi
return 1
}
mount_int() {
mkdir -p $CHROOT_DIR/opt/tmp
for dir in dev proc sys opt/tmp; do
if ! mountpoint -q $CHROOT_DIR/$dir; then
mount -o bind /$dir $CHROOT_DIR/$dir
fi
done
}
mount_ext() {
for dir in /mnt/*; do
dir=$(readlink -f $dir)/
if ! echo "$CHROOT_DIR" | grep -q ^$dir; then
target_dir=$CHROOT_DIR/mnt/$(echo "$dir" | sed 's!^.*mnt/!!')
mkdir -p $target_dir
if ! mountpoint -q $target_dir; then
mount -o bind $dir $target_dir
fi
fi
done
}
start() {
if running; then
echo "Chroot'ed services seems to be already started, exiting..."
exit 1
fi
mount_int
mount_ext
echo "Starting chroot'ed Debian services..."
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item start
done
}
stop() {
if ! running; then
echo "Chroot'ed services seems to be already stopped, exiting..."
exit 1
fi
echo "Stopping chroot'ed Debian services..."
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item stop
sleep 2
done
if mountpoint -q $CHROOT_DIR/dev/pts; then
umount $CHROOT_DIR/dev/pts
fi
mount | grep $CHROOT_DIR | awk '{print $3}' | xargs umount -l
}
restart() {
stop
start
}
enter() {
if ! running; then
start
fi
if ! mountpoint -q $CHROOT_DIR/dev/pts; then
mount -o bind /dev/pts $CHROOT_DIR/dev/pts
fi
chroot $CHROOT_DIR /bin/bash
if mountpoint -q $CHROOT_DIR/dev/pts; then
umount $CHROOT_DIR/dev/pts
fi
}
status() {
if running; then
echo "Chroot'ed services running..."
else
echo "Chroot'ed services not running!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
enter)
enter
;;
status)
status
;;
*)
echo "Usage: (start|stop|restart|enter|status)"
exit 1
;;
esac
echo Done.
exit 0
* - prepare Debian's remount hotplugged USB script
Skipping this step, newly attached USB drives will be accessible from Debian (and thus Plex Media Server) only after rebooting the router or manually executingdebian restart
/debian enter
via SSH.
Prerequisite:JFFS custom scripts and configs
enabled (router WebUI -> Administration -> System
)
Bash:
wget -O /jffs/scripts/mount-debian.sh https://raw.githubusercontent.com/bbeny123/Plex_Asuswrt-Merlin/main/mount-debian.sh
chmod 755 /jffs/scripts/mount-debian.sh
echo './jffs/scripts/mount-debian.sh' >> /jffs/scripts/post-mount
# instead of downloading the script, it can also be copy-pasted:
# cat >> /jffs/scripts/mount-debian.sh << 'EOF'
# copy-paste script from the spoiler below
# type 'EOF' and press enter
Bash:
#!/bin/sh
CHROOT_DIR=$(readlink -f /opt/debian)
if [ $(mount | grep $CHROOT_DIR | wc -l) -gt 0 ]; then
for dir in /mnt/*; do
dir=$(readlink -f $dir)/
if ! echo "$CHROOT_DIR" | grep -q ^$dir; then
target_dir=$CHROOT_DIR/mnt/$(echo "$dir" | sed 's!^.*mnt/!!')
mkdir -p $target_dir
if ! mountpoint -q $target_dir; then
mount -o bind $dir $target_dir
fi
fi
done
fi
6 - prepare chrooted services list and create symlink to Debian
Bash:
touch /opt/etc/chroot-services.list
chmod 755 /opt/etc/chroot-services.list
ln -s /opt/etc/init.d/S99debian /opt/bin/debian
* - copy hosts file to Debian
Bash:
cp /etc/hosts /opt/debian/etc/
7 - enter debian
Bash:
debian enter
8 - upgrade packages and install those required by Plex Media Server
Bash:
apt update && apt upgrade -y
apt install -y apt-transport-https curl gnupg procps
9 - configure timezone
Bash:
dpkg-reconfigure tzdata
10 - ensure that
/usr/sbin/init
is not a symlink pointing to systemd
Bash:
[ -f /usr/sbin/init ] && ls -l /usr/sbin/init | grep -q systemd && mv -f /usr/sbin/init /usr/sbin/init.bak
11 - instal Plex Media Server
Bash:
curl -sS https://downloads.plex.tv/plex-keys/PlexSign.key | gpg --dearmor -o /etc/apt/trusted.gpg.d/plexmediaserver.gpg
echo 'deb https://downloads.plex.tv/repo/deb public main' | tee /etc/apt/sources.list.d/plexmediaserver.list
apt update
apt install plexmediaserver
After installation, the server will start automatically.
During initialization (which will take about 5-15min) CPU/RAM usage will be close to 100%.
The server will be almost unusable during this time so I recommend just waiting it out.
12 - exit Debian
Bash:
exit
13 - add Plex Media Server to chrooted services list
Bash:
echo 'plexmediaserver' >> /opt/etc/chroot-services.list
14 - restart Debian
Bash:
debian restart
After about 30 seconds, the server should be reachable at:<router-ip-address>:32400/web
like 192.168.1.1:32400/web or router.asus.com:32400/web
When configuring libraries, CPU/RAM consumption will also be close to 100%. Web-panel and Debian will be unresponsive during this time.
After configuring the libraries and downloading the metadata, the Plex Media Server should start working well.
I have done some tests and the playback of videos with a bitrate of almost 80Mb/s was seamless and smooth.
Update procedure
1 - ssh to router
* - update amtm and entware packages using amtm
This step may overwrite/opt/etc/init.d/S99debian
file.
In such a case, step 5) of the installation procedure will need to be performed again.
2 - enter debian
Bash:
debian enter
3 - ensure that
/usr/sbin/init
is not a symlink pointing to systemd
Bash:
[ -f /usr/sbin/init ] && ls -l /usr/sbin/init | grep -q systemd && mv -f /usr/sbin/init /usr/sbin/init.bak
4 - upgrade debian packages
Bash:
apt update && apt upgrade -y
* - restart Debian
Bash:
exit
debian restart
Last edited: