Skip to content

Instantly share code, notes, and snippets.

@RikshaDriver
Last active April 27, 2025 21:15
Show Gist options
  • Save RikshaDriver/0388ba3f38f89eb975ebb0b915860ec2 to your computer and use it in GitHub Desktop.
Save RikshaDriver/0388ba3f38f89eb975ebb0b915860ec2 to your computer and use it in GitHub Desktop.
Compiling Realtek 8152, 8156 and 8157 ( RTL8152 ) drivers for QNAP NAS

Compiling Realtek RTL 8152, 8156 and 8157 drivers for QNAP NAS

Note

This guide was last updated on 17th Feb 2025

Many QNAP NAS devices only come with 1Gbe network interfaces. These devices can easily benefit from a simple network upgrade by connecting the ubiquitious 2.5Gbe USB NICs from Realtek. Unfortunately, some of the older QNAP NAS systems do not support the newer r8156 based 2.5Gbe NICs or r8157 based 5Gbe NICs as they run an older 4.x linux kernel and the r8152 source files included with the older linux kernel are based off an older codebase that predates the 8156/8157 series.

With some elbow grease however, we can build support for the 2.5 & 5 Gbe adapters and easily upgrade functionality.

rt_2 5gbe_qnap

VM & Build Environment Setup

Whilst any Linux build environment may be possible, I have chosen the older Debian 8. We need to download and install the latest Debian 8 i386 instance from Debian: https://www.debian.org/releases/jessie/debian-installer/

Build Essentials

Once our Linux VM is installed, we must also setup the environment for build. Login as "root" user and install the development packages using apt:

apt-get install build-essential libelf-dev bc

Other libraries may be required depending on the kernel/module build options

Compiler / Toolchain

In order to compile the kernel module for a respective QNAP system, we must identify the compiler on which the system was built. This can be done by querying the NAS itself using an SSH terminal:

cat /proc/version

This will show an output like:

Linux version 4.2.8 (root@U16BuildServer176) (gcc version 4.8.2 20131014 (prerelease) (crosstool-NG linaro-1.13.1-4.8-2013.10 - Linaro GCC 2013.10) ) #2 SMP Thu Jun 29 05:42:26 CST 2023

In the case of the TS-431P arm based system, the kernel was built using linaro-1.13.1-4.8-2013.10, so we need the relevant cross compiler. QNAP have not included this compiler in their GPL Open Source repository so we must obtain it from linaro: https://releases.linaro.org/archive/13.10/components/toolchain/binaries/gcc-linaro-aarch64_be-linux-gnu-4.8-2013.10_linux.tar.bz2

Extract the contents to a directory such as the /opt folder.

QNAP provides other platform cross-compilers on their sourceforge repository: https://sourceforge.net/projects/qosgpl/files/QNAP%20NAS%20Tool%20Chains/

Kernel Source

The NAS Kernel source is available on sourceforge: https://sourceforge.net/projects/qosgpl/files/QNAP%20NAS%20GPL%20Source/

In the example of the TS-431P, we will download and extract GPL_QTS-5.0.0-20211101_Kernel.tar.gz

To complete the build environment, we must copy the appropriate config file for kernel compilation.

In the case of the TS-431P:

cp ~/cp GPL_QTS/kernel_cfg/TS-X31X/linux-4.2-arm.config ~/GPL_QTS/src/linux-4.2/.config

Alternatively, one could also extract this config from the running NAS by downloading and extracting the contents under /proc/config.gz (if available).

This file can then be copied to the respective linux src folder.

Example:

gunzip config.gz
cp config ~/GPL_QTS/src/linux-4.2/.config

Compile Linux Kernel

With our build environment set up, we must then compile the linux kernel.

We can do so with a simple command (ensuring we reference our extracted folder locations appropriately:

make ARCH=arm CROSS_COMPILE=/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf- -C ~/GPL_QTS/src/linux-4.2/ scripts prepare modules_prepare

Compile Realtek Driver

Download precompiled drivers

Download optimized compiled driver:

Release Kernel Architecture Comments Link
2.19.2 4.2 ARMv7 32-bit Compiled for TS-x31x series Download

Other precompiled driver repositories:

Download from Realtek source

Download and extract the USB Linux drivers from Realtek's website: https://www.realtek.com/en/component/zoo/category/network-interface-controllers-10-100-1000m-gigabit-ethernet-usb-3-0-software https://www.realtek.com/Download/List?cate_id=585

As of (updated) writing, the current version is 2.19.2

If compiling for the older 4.x Linux kernel, we must first modify the contents of the r8152.c source file and replace strscpy with strlcpy for driver versions 2.17.1 onwards. This is to allow compilation on older kernel instances as these versions do not support the strscpy function.

If we don't change this function, we will see an error like below:

make: Entering directory '/root/GPL_QTS/src/linux-4.2'

  WARNING: Symbol version dump ./Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /root/r8152-2.17.1/r8152.o
/root/r8152-2.17.1/r8152.c: In function ‘rtl8152_get_drvinfo’:
/root/r8152-2.17.1/r8152.c:18623:2: error: implicit declaration of function ‘strscpy’ [-Werror=implicit-function-declaration]
  strscpy(info->driver, MODULENAME, sizeof(info->driver));
  ^
cc1: some warnings being treated as errors
scripts/Makefile.build:270: recipe for target '/root/r8152-2.17.1/r8152.o' failed
make[1]: *** [/root/r8152-2.17.1/r8152.o] Error 1
Makefile:1503: recipe for target '_module_/root/r8152-2.17.1' failed
make: *** [_module_/root/r8152-2.17.1] Error 2
make: Leaving directory '/root/GPL_QTS/src/linux-4.2'

We can quickly replace the functions in the file using sed:

sed -i -e 's/strscpy/strlcpy/g' r8152.c

Download From GitHub

Alternatively, download forked source from GitHub which contains the necessary fixes and enhancements: https://github.com/RikshaDriver/realtek-r8152-linux

Compilation

Now we can finally compile the Realtek module:

make ARCH=arm CROSS_COMPILE=/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf- -C ~/GPL_QTS/src/linux-4.2 M=`pwd` modules EXTRA_CFLAGS='-O2'

We should now have the succesfully compiled kernel module r8152.ko

Install Realtek Driver

In order to install the Realtek USB driver on our Linux system, we must overwite the existing r8152.ko module under /lib/modules/<kernel-version>/

For a quick an dirty test, upload the kernel module to a folder on the NAS, copy to the modules directory and run modprobe to remove and probe the kernel module. For example, the command for TS-431P (and related devices):

cp r8152.ko /lib/modules/4.2.8/
modprobe -r r8152
modprobe r8152

The Realtek driver should immediately show up as a network interface:

dmesg | grep 8152
[16401.642702] usbcore: deregistering interface driver r8152
[16404.197265] usbcore: registered new interface driver r8152
[16404.851196] r8152 2-4:1.0 eth2: v2.17.1 (2023/06/13)
[16404.856178] r8152 2-4:1.0 eth2: This product is covered by one or more of the following patents:

Tip

If experiencing adapter stability issues, it is possible the NAS may not be providing sufficient power to the adapter. Consider using an externally powered USB 3 Hub or a USB 3 Y cable which is connected to an external USB power source.

Install using autorun.sh

For a more permanent installation, the same commands can be run at startup using the autorun.sh capability of the NAS.

We must first tick the "Run user defined process during startup" option to allow autorun.sh loading during bootup.

The autorun.sh file must be stored on the flash storage.

QNAP provides a few methods of mounting the flash storage: https://www.qnap.com/en/how-to/faq/article/running-your-own-application-at-startup

In our case, we will do as follows:

/etc/init.d/init_disk.sh mount_flash_config

We can now manually create or upload our autorun.sh file to /tmp/nasconfig_tmp/autorun.sh

Example autorun.sh:

#!/bin/sh

echo "############ autorun.sh script start ############" >> /dev/kmsg

cp /share/CACHEDEV1_DATA/r8152.ko /lib/modules/4.2.8/
cp /share/CACHEDEV1_DATA/50-usb-realtek-net.rules /lib/udev/rules.d/50-usb-realtek-net.rules
modprobe -r r8152
modprobe r8152

sleep 5

udevadm control --reload-rules && udevadm trigger

sleep 10

echo "############  autorun.sh script end  ############" >> /dev/kmsg

Ensure autorun.sh is executable:

chmod +x /tmp/nasconfig_tmp/autorun.sh

Once completed, unmount the flash drive:

cd ~/
/etc/init.d/init_disk.sh umount_flash_config

Reboot and enjoy 2.5Gb/s or 5Gb/s speeds!

Install using QPKG

The Kernel module can also be packaged as a QPKG and run at startup as an application. This is similar to what is done with the Advanced Network Drivers application from QNAP.

Refer to the QDK guide for more details on creating a QPKG: https://cheng-yuan-hong.gitbook.io/qdk-quick-start-guide/

#!/bin/sh
echo "############ autorun.sh script start ############" >> /dev/kmsg
cp /share/CACHEDEV1_DATA/r8152.ko /lib/modules/4.2.8/
cp /share/CACHEDEV1_DATA/50-usb-realtek-net.rules /lib/udev/rules.d/50-usb-realtek-net.rules
modprobe -r r8152
modprobe r8152
sleep 5
udevadm control --reload-rules && udevadm trigger
sleep 10
echo "############ autorun.sh script end ############" >> /dev/kmsg
@astarktc
Copy link

astarktc commented Feb 6, 2025

good news, i was able to get a script working properly. now we'll just wait and see if it helps during the next freeze :) the first time i tested it, it disappeared from crontab after the reboot...so i tried adding to /tmp/cron/crontab/admin using vi instead of doing sudo crontab -e. but i'm not sure if that command just opens that version anyway.

If there's a better way to permanently schedule it, I'd appreciate any advice. when i tested it, i was manually running the .sh file so i don't know if cron is actually running it every minute or not. gpt told me to add this line: */1 * * * * /path to file/network_monitor.sh

@bstirling2307
Copy link

Does anyone have this working on the ts-659 pro ii? if so how?

@RikshaDriver
Copy link
Author

Just an FYI for those using the 5G adapters...

  • MTU cannot currently be set (at least on older devices) - you're stuck at 1500 with the current driver set (2.19.2 at the time)
  • On QNAP devices running Linux 4.2 kernel and earlier, 5G speeds are not supported (kernel limitation)

List of Devices and respective Kernel version: https://www.qnap.com/en-au/release-notes/kernel

Hoping at some point QNAP upgrades the 4.2.x kernel for devices with current software support, but I highly doubt it.

@Laurenzis
Copy link

Laurenzis commented Mar 10, 2025

When trying to compile the kernel, I get the following error messages:

/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x4): multiple definition of `yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [scripts/Makefile.host:100: scripts/dtc/dtc] Error 1
make[1]: *** [scripts/Makefile.build:416: scripts/dtc] Error 2
make: *** [Makefile:546: scripts] Error 2
make: Leaving directory '/root/GPL_QTS/src/linux-4.2'

Complete log is here:

root@QNAP-Build:~# make ARCH=arm CROSS_COMPILE=/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf- -C ~/GPL_QTS/src/linux-4.2/ scripts prepare modules_prepare
make: Entering directory '/root/GPL_QTS/src/linux-4.2'
  HOSTLD  scripts/dtc/dtc
/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x4): multiple definition of `yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [scripts/Makefile.host:100: scripts/dtc/dtc] Error 1
make[1]: *** [scripts/Makefile.build:416: scripts/dtc] Error 2
make: *** [Makefile:546: scripts] Error 2
make: Leaving directory '/root/GPL_QTS/src/linux-4.2'
root@QNAP-Build:~#

What do I have to do now?

@kawan2
Copy link

kawan2 commented Mar 11, 2025

Interesting that you are getting this error using gcc 4.8

This patch describes a one line fix for when you get this error with gcc 10 which started making -fno-common a default option
torvalds/linux@e33a814

@Laurenzis
Copy link

Thx for your hint - I'll try this and will come back with the result

@Laurenzis
Copy link

ok - I found the problem with the failing compile - I used the actual debian 12. Now, using Debian 10, the compile itself runs. But I'm now figuring a new problem, compiling the realtec-driver:

root@QNAP-Build:~# make ARCH=arm CROSS_COMPILE=/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf- -C ~/GPL_QTS/src/linux-4.2/ scripts prepare modules_prepare
make: Entering directory '/root/GPL_QTS/src/linux-4.2'
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
make[1]: 'include/generated/mach-types.h' is up to date.
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
<stdin>:1253:2: warning: #warning syscall qsendfile not implemented [-Wcpp]
<stdin>:1274:2: warning: #warning syscall qtier_anchor not implemented [-Wcpp]
make: Nothing to be done for 'modules_prepare'.
make: Leaving directory '/root/GPL_QTS/src/linux-4.2'
root@QNAP-Build:~# make ARCH=arm CROSS_COMPILE=/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf- -C ~/GPL_QTS/src/linux-4.2 M=`pwd` modules EXTRA_CFLAGS='-O2'
make: Entering directory '/root/GPL_QTS/src/linux-4.2'

  WARNING: Symbol version dump ./Module.symvers
           is missing; modules will have no dependencies and modversions.

scripts/Makefile.build:46: /root/Makefile: No such file or directory
make[1]: *** No rule to make target '/root/Makefile'.  Stop.
make: *** [Makefile:1503: _module_/root] Error 2
make: Leaving directory '/root/GPL_QTS/src/linux-4.2'
root@QNAP-Build:~# ls
GPL_QTS  r8152-2.19.2
root@QNAP-Build:~#

What am I missing?

@Laurenzis
Copy link

Laurenzis commented Mar 11, 2025

Sorry, I should read the message - forgot to copy the makefile - compiling is running fine now. Many thx for your help!
But after modprobe the module I'm getting the following message:

[/share/CACHEDEV1_DATA/.qpkg/autorun] # modprobe -r r8152
[/share/CACHEDEV1_DATA/.qpkg/autorun] # modprobe r8152
[/share/CACHEDEV1_DATA/.qpkg/autorun] # dmesg | grep 8152
[ 9457.035682] WARNING: CPU: 0 PID: 24017 at /root/r8152.c:2586 free_all_mem+0xe                                                                                                         0/0xf8 [r8152]()
[ 9457.044209] Modules linked in: r8152(O) rfcomm iscsi_tcp(O) libiscsi_tcp(O) l                                                                                                         ibiscsi(O) scsi_transport_iscsi(O) fbdisk(O) dummy br_netfilter bridge stp bondi                                                                                                         ng xt_connmark xt_TCPMSS xt_LOG xt_set ip_set_hash_netiface ip_set_hash_net ip_s                                                                                                         et ipt_MASQUERADE xt_REDIRECT nf_nat_redirect iptable_nat nf_nat_masquerade_ipv4                                                                                                          nf_nat_ipv4 nf_nat xt_policy xt_mark 8021q uvcvideo videobuf2_vmalloc videobuf2                                                                                                         _memops videobuf2_core v4l2_common videodev snd_usb_caiaq snd_seq_dummy snd_usb_                                                                                                         audio snd_usbmidi_lib snd_hwdep snd_pcm_oss snd_mixer_oss snd_pcm snd_seq_oss sn                                                                                                         d_seq_midi snd_seq_midi_event snd_seq snd_rawmidi snd_seq_device snd_timer snd f                                                                                                         notify(O) udf isofs ipv6 exfat ufsd(PO) jnl(O) cdc_acm pl2303 usbserial qm2_i2c(                                                                                                         O) flashcache(O) dm_tier_hro_algo dm_thin_pool dm_bio_prison dm_persistent_data                                                                                                          hal_netlink(O) usbnet mii igb e1000e(O) mv14xx(PO) raid_class scsi_transport_fc                                                                                                          usblp al_nand m25p80 [last unloaded: r8152]
[ 9457.169383] [<800241fc>] (warn_slowpath_null) from [<7f32c42c>] (free_all_mem                                                                                                         +0xe0/0xf8 [r8152])
[ 9457.178185] [<7f32c42c>] (free_all_mem [r8152]) from [<7f331278>] (rtl8152_op                                                                                                         en+0x310/0x41c [r8152])
[ 9457.187329] [<7f331278>] (rtl8152_open [r8152]) from [<805fd174>] (__dev_open                                                                                                         +0xa8/0x10c)
[ 9457.266944] WARNING: CPU: 0 PID: 24017 at /root/r8152.c:2586 free_all_mem+0xe                                                                                                         0/0xf8 [r8152]()
[ 9457.275467] Modules linked in: r8152(O) rfcomm iscsi_tcp(O) libiscsi_tcp(O) l                                                                                                         ibiscsi(O) scsi_transport_iscsi(O) fbdisk(O) dummy br_netfilter bridge stp bondi                                                                                                         ng xt_connmark xt_TCPMSS xt_LOG xt_set ip_set_hash_netiface ip_set_hash_net ip_s                                                                                                         et ipt_MASQUERADE xt_REDIRECT nf_nat_redirect iptable_nat nf_nat_masquerade_ipv4                                                                                                          nf_nat_ipv4 nf_nat xt_policy xt_mark 8021q uvcvideo videobuf2_vmalloc videobuf2                                                                                                         _memops videobuf2_core v4l2_common videodev snd_usb_caiaq snd_seq_dummy snd_usb_                                                                                                         audio snd_usbmidi_lib snd_hwdep snd_pcm_oss snd_mixer_oss snd_pcm snd_seq_oss sn                                                                                                         d_seq_midi snd_seq_midi_event snd_seq snd_rawmidi snd_seq_device snd_timer snd f                                                                                                         notify(O) udf isofs ipv6 exfat ufsd(PO) jnl(O) cdc_acm pl2303 usbserial qm2_i2c(                                                                                                         O) flashcache(O) dm_tier_hro_algo dm_thin_pool dm_bio_prison dm_persistent_data                                                                                                          hal_netlink(O) usbnet mii igb e1000e(O) mv14xx(PO) raid_class scsi_transport_fc                                                                                                          usblp al_nand m25p80 [last unloaded: r8152]
[ 9457.400622] [<800241fc>] (warn_slowpath_null) from [<7f32c42c>] (free_all_mem                                                                                                         +0xe0/0xf8 [r8152])
[ 9457.409428] [<7f32c42c>] (free_all_mem [r8152]) from [<7f331278>] (rtl8152_op                                                                                                         en+0x310/0x41c [r8152])
[ 9457.418581] [<7f331278>] (rtl8152_open [r8152]) from [<805fd174>] (__dev_open                                                                                                         +0xa8/0x10c)
[ 9457.498221] WARNING: CPU: 0 PID: 24017 at /root/r8152.c:2586 free_all_mem+0xe                                                                                                         0/0xf8 [r8152]()
[ 9457.506767] Modules linked in: r8152(O) rfcomm iscsi_tcp(O) libiscsi_tcp(O) l                                                                                                         ibiscsi(O) scsi_transport_iscsi(O) fbdisk(O) dummy br_netfilter bridge stp bondi                                                                                                         ng xt_connmark xt_TCPMSS xt_LOG xt_set ip_set_hash_netiface ip_set_hash_net ip_s                                                                                                         et ipt_MASQUERADE xt_REDIRECT nf_nat_redirect iptable_nat nf_nat_masquerade_ipv4                                                                                                          nf_nat_ipv4 nf_nat xt_policy xt_mark 8021q uvcvideo videobuf2_vmalloc videobuf2                                                                                                         _memops videobuf2_core v4l2_common videodev snd_usb_caiaq snd_seq_dummy snd_usb_                                                                                                         audio snd_usbmidi_lib snd_hwdep snd_pcm_oss snd_mixer_oss snd_pcm snd_seq_oss sn                                                                                                         d_seq_midi snd_seq_midi_event snd_seq snd_rawmidi snd_seq_device snd_timer snd f                                                                                                         notify(O) udf isofs ipv6 exfat ufsd(PO) jnl(O) cdc_acm pl2303 usbserial qm2_i2c(                                                                                                         O) flashcache(O) dm_tier_hro_algo dm_thin_pool dm_bio_prison dm_persistent_data                                                                                                          hal_netlink(O) usbnet mii igb e1000e(O) mv14xx(PO) raid_class scsi_transport_fc                                                                                                          usblp al_nand m25p80 [last unloaded: r8152]
[ 9457.631952] [<800241fc>] (warn_slowpath_null) from [<7f32c42c>] (free_all_mem                                                                                                         +0xe0/0xf8 [r8152])
[ 9457.640758] [<7f32c42c>] (free_all_mem [r8152]) from [<7f331278>] (rtl8152_op                                                                                                         en+0x310/0x41c [r8152])
[ 9457.649904] [<7f331278>] (rtl8152_open [r8152]) from [<805fd174>] (__dev_open                                                                                                         +0xa8/0x10c)
[/share/CACHEDEV1_DATA/.qpkg/autorun] #

@Laurenzis
Copy link

Ok, that's weird, looks like it's running after reboot my nas:

[~] # dmesg | grep 8152
[  513.580825] usbcore: deregistering interface driver r8152
[  513.632479] usbcore: registered new interface driver r8152
[  513.906603] r8152 2-1:1.0 eth2: v2.19.2 (2024/10/21)
[  513.911572] r8152 2-1:1.0 eth2: This product is covered by one or more of the following patents:

Thx for all your help, looks like I'm able to have a faster connection to my nas now ;)

@zhy0216
Copy link

zhy0216 commented Mar 23, 2025

thx

@Equinoxs
Copy link

Thanks a lot, the precompiled binary worked flawlessly on my TS-431P running QTS 5.2.4, even after rebooting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment