Install Resilio-Sync into openwrt

Hi. There are some posts, including official one, related to this topic. But so far I searched, none of them are 100% up to date. After tested myself, here is the guide for installing Resilio-Sync into openwrt, today.

Preparation:

  • A router with at least 1GB free memory left after all services you required is on, with ~50MB left on the internal storage.
  • A dedicated USB drive, or an internal storage with enough space left. I use a 500 GB USB drive. When it’s done, more than 30 GB is used. But I guess most of them is for ext4 journal.
  • A tool to transfer file into openwrt like scp or any other with scp support.
  • Upgrade your openwrt to the latest.
  • Know the arch of your router chip, like armhf for the new, armel for those old, and arm64 for 64-bit enabled. Note: arm64 not only means your chip is 64-bit ready, but also working in 64-bit mode with your system supported.

Now let’s start.

Step 1: USB drive preparation

If you need to use internal storage, see Step 1A instead.

  1. Login to terminal. Web based TTYD Terminal or SSH are both fine.
  2. Check current device list by command
ls /dev/sd*

There shouldn’t be anything printed if no disk attached. Remember all reported.

  1. Attach your USB drive. Re-run the command above, you will notice that something more are added. Normally, they should be like /dev/sdX and /dev/sdXY. X is a letter, Y is a number. If this is the 1st drive found, the X should be a, /dev/sda for full. I will take /dev/sda for example in below. Change it to the correct one if it’s not.
  2. Use fdisk to initialize this disk. Command is
fdisk /dev/sda

First, use command p to check the current partitions. Use d to remove them one by one. Then use n to create a primary partition. If any sign is found when creating partition, you can remove it when asked. Finally, use w to save. If anything goes wrong, use q without w will quit fdisk without changes. If everything is right, you will see a /dev/sda1 which is the only numbered return by command ls /dev/sda*.

  1. Make ext4 file system on the /dev/sda1 with command
mkfs.ext4 /dev/sda1
  1. Reboot your router with the USB drive attached. Go back to terminal.
  2. Use command
df -h

to check your device. You should see /dev/sda1 is mounted to somewhere like /mnt/sda1. Note down the location (like /mnt/sda1) for further steps. If the value is not /mnt/sda1, replace it with the right one on your device.

Step 1A: Use internal storage

This step is for whom want to use internal storage instead. No need to follow this when Step 1 is taken.

You need to find a place to store all folders and files created in later steps and note down the path to the place. I will use the path /mnt/sda1 in following steps. You should replace them to your path.

Step 2: Prepare debian system

Openwrt is lightweight linux without many system files shipped. Before installing Resilio-Sync, we need to prepare a full linux system core.

  1. First, install a tool for installing debian. These packages will be placed into internal storage.
opkg install debootstrap binutils
  1. Install debian files. Note: If the chip of your router is not arm64, you need to change the command with the correct arch name. Path /mnt/sda1 is used.
debootstrap --arch=arm64 buster /mnt/sda1/debian http://ftp.de.debian.org/debian

This command will download the debian files into a new folder named debian under /mnt/sda1 which should be your USB drive. If something goes wrong, remove the folder /mnt/sda1/debian using command rm -fr /mnt/sda1/debian before re-run this command.

  1. Link system folders from openwrt to the debian system by these commands.
mount --bind /dev /mnt/sda1/debian/dev/
mount --bind /proc /mnt/sda1/debian/proc/
mount --bind /sys /mnt/sda1/debian/sys/
ln -s /bin/bash bin/ash
  1. Then we start the debian bash by this command.
chroot /mnt/sda1/debian/ /bin/bash

Now, the bash is created under debian system. The file system root is changed to the folder debian on your USB drive also.

  1. (Optional) For avoiding scene confusing, a good way is change the shell prompt for chroot. You can run these commands to do that. This is an optional but recommended step.
echo 'PS1="CHROOT:\w# "' >> ~/.bashrc
exit
chroot /mnt/sda1/debian/ /bin/bash

You will notice a CHROOT is shown on the left of the prompt when debian system is using.

  1. Now let’s prepare the debian system by installing locales.
apt-get install locales
dpkg-reconfigure locales

You can select en_US.UTF8 or any others you like.

Step 3: Install Resilio-Sync

  1. Download the right DEB package of Resilio-Sync for your router chip from here.
  2. Transfer the deb file to path /mnt/sda1/debian on your router using scp or any other tool.
  3. Use ls / under chroot terminal to make sure the file is ready.
  4. Install the package using command dpkg -i. If the file name is resilio-sync_2.7.2.1375-1_arm64.deb, the command should be
dpkg -i /resilio-sync_2.7.2.1375-1_arm64.deb
  1. (Optional) Mark the service as auto start by following command. Actually, because the router will not boot debian directly, this command is useless at all. I still place it as an optional step for my obsessive-compulsive disorder 🙂
systemctl enable resilio-sync
  1. (Optional) Edit the config if you need. The config file can be located as /etc/resilio-sync/config.json under chroot terminal.
  2. Exit the chroot by tying exit and press enter.
  3. Create a file for start Resilio-Sync with openwrt. This is actually work, not the step 5. Place a file /etc/init.d/resilio-sync with the content below.
#!/bin/sh /etc/rc.common
#

START=99
STOP=10

. $IPKG_INSTROOT/lib/functions.sh
. $IPKG_INSTROOT/lib/functions/service.sh

start() {
        mount --bind /proc /mnt/sda1/debian/proc
        chroot /mnt/sda1/debian /bin/bash /etc/init.d/resilio-sync start
}

restart() {
        mount --bind /proc /mnt/sda1/debian/proc
        chroot /mnt/sda1/debian /bin/bash /etc/init.d/resilio-sync restart
}

stop() {
        chroot /mnt/sda1/debian /bin/bash /etc/init.d/resilio-sync stop
        umount /mnt/sda1/debian/proc
}
enable() {
        err=1
        name="$(basename "${initscript}")"
        [ "$START" ] && \
                ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name}"
                err=0
        [ "$STOP" ] && \
                ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name}"
                err=0
        return $err
}
disable() {
        name="$(basename "${initscript}")"
        rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
        rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
}

And mark the file with execution permission and set it auto start by commands

chmod +x /etc/init.d/resilio-sync
/etc/init.d/resilio-sync enable

You can check there should be a service named resilio-sync with number 99 in page System – Startup of the router management portal. It should be marked as enabled as well.

  1. Append path to resilio-sync config to /etc/sysupgrade.conf to preserve this config file in backup package for keeping it while upgrading as well. Command is:
echo "/etc/init.d/resilio-sync" >> /etc/sysupgrade.conf

Now everything is done. I left here for you to reboot your router and start to fulfill your Resilio-Sync mission.

Thanks for help from: