本文将从源码开始对 XMRig 工具进行各平台各架构的静态编译。
Intro 根据国家发展改革委于2021年09月03日发布的《国家发展改革委等部门关于整治虚拟货币“挖矿”活动的通知》(发改运行〔2021〕1283号) 明确将虚拟货币“挖矿”活动列为淘汰类产业 ;严禁利用数据中心开展虚拟货币“挖矿”活动 ,禁止以发展数字经济、战略性新兴产业等名义宣传、扩大虚拟货币“挖矿”项目;按照《产业结构调整指导目录》有关规定,采取有力措施对存量虚拟货币“挖矿”项目即行有序整改淘汰。对不按期淘汰的企业,要依据国家有关法律法规责令其停产或予以关闭。对违反规定者,依法追究相关责任 。
根据2008年8月29日第十一届全国人民代表大会常务委员会第四次会议通过的《中华人民共和国循环经济促进法》 第五十条规定:使用列入淘汰名录的技术、工艺、设备、材料的,由县级以上地方人民政府循环经济发展综合管理部门责令停止使用,没收违法使用的设备、材料,并处五万元以上二十万元以下的罚款;情节严重的,由县级以上人民政府循环经济发展综合管理部门提出意见,报请本级人民政府按照国务院规定的权限责令停业或者关闭。
本文仅涉及使用该工具对硬件设备进行性能测试,测试后相关数据已全部抹掉 。
Linux 平台静态编译 ubuntu 本小节使用ubuntu 20.04 arm64设备进行编译操作,实际x64和arm64架构编译方法和依赖项均相同。
1 Linux lxnchan-testbench 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:49 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
安装依赖项1 sudo apt install git build-essential cmake automake libtool autoconf
获取源码1 git clone https://github.com/xmrig/xmrig.git
创建build文件夹并编译依赖1 2 mkdir xmrig/build && cd xmrig/scripts ./build_deps.sh && cd ../build
编译:1 2 cmake .. -DXMRIG_DEPS=scripts/deps make -j$(nproc)
编译好后可以用ldd xmrig
测试,二进制文件在./xmrig/build
目录下。 1 2 3 4 5 6 7 8 root@lxnchan-testbench:~/xmrig/build# ./xmrig --version XMRig 6.22.2 built on Jun 2 2025 with GCC 9.4.0 features: 64-bit AES libuv/1.49.2 OpenSSL/3.0.15 hwloc/2.11.2
移除捐赠 打开src/donate.h
,将如下值改为0:1 2 constexpr const int kDefaultDonateLevel = 0 ;constexpr const int kMinimumDonateLevel = 0 ;
打开src/net/strategies/DonateStrategy.cpp
,找到如下常量并修改为你自己的矿池地址或127.0.0.1
:1 2 3 4 static const char *kDonateHost = "127.0.0.1" ;#ifdef XMRIG_FEATURE_TLS static const char *kDonateHostTls = "127.0.0.1" ;#endif
我这里只是做跑分用,所以修改为了127.0.0.1
。 最后编译即可。
systemd配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [Unit] Description =XMRigWants =network-on line.targetAfter =network-on line.targetAssertFileIsExecutable =/opt/xmrig/build/xmrig[Service] WorkingDirectory =/optExecStart =/opt/xmrig/build/xmrig -o 10.233 .233.233 :23333 -u 4 AFzkfJm2QNiZWmXbMGZgyGooB7edrLq9WWHyBLMxBME8uZXJNks2a5fw9rxrfWQXeF3PrGw1AtMZ6sbUBEvjXnnDogvo1n -k --tls -t 4 Restart =alwaysNice =-20 LimitNOFILE =1048576 TasksMax =infinityTimeoutStopSec =infinitySendSIGKILL =no [Install] WantedBy =multi-user.target
在docker中运行 dockerfile 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 LABEL version="1.2" \ author="lxnchan" FROM ubuntu:20.04 ARG DEBIAN_FRONTEND=noninteractiveENV TZ=Asia/ShanghaiADD sources.list /root/sources.list RUN mv /etc/apt/sources.list /etc/apt/sources.list_bak RUN cp /root/sources.list /etc/apt/sources.list RUN apt update && apt install -y wget curl git build-essential cmake automake libtool autoconf RUN mkdir -p /var/run/sshd RUN git clone https://github.com/xmrig/xmrig.git /xmrig RUN mkdir /xmrig/build && cd /xmrig/scripts RUN cd /xmrig/scripts/ && /xmrig/scripts/build_deps.sh RUN cd /xmrig/build && cmake .. -DXMRIG_DEPS=scripts/deps && make -j$(nproc) ADD run.sh /run.sh RUN chmod 755 /run.sh EXPOSE 22 CMD ["/run.sh" ]
run.sh 该文件作为容器的执行点,存入和dockerfile
同一路径即可。
1 2 #!/bin/bash /xmrig/build/xmrig -o auto.c3pool.org:33333 -u 4AFzkfJm2QNiZWmXbMGZgyGooB7edrLq9WWHyBLMxBME8uZXJNks2a5fw9rxrfWQXeF3PrGw1AtMZ6sbUBEvjXnnDogvo1n -k --tls -t 6
sources.list 该文件是你指定镜像的apt源文件,比如我这里是ubuntu 20.04,那么我的sources.list
就长这样,写好了也是丢同一目录:
1 2 3 4 deb http://mirrors4.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb http://mirrors4.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors4.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb http://mirrors4.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
生成镜像 执行命令:
1 docker build -t xmrig-docker:1.2 .
然后等待编译完成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [+] Building 526.7s (21/21) FINISHED docker:default => [internal] load build definition from dockerfile 0.0s => => transferring dockerfile: 1.28kB 0.0s => [internal] load metadata for docker.io/library/ubuntu:20.04 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build context 0.0s => => transferring context: 60B 0.0s => [ 1/12] FROM docker.io/library/ubuntu:20.04 0.0s => CACHED [ 2/12] ADD sources.list /root/sources.list 0.0s => CACHED [ 3/12] RUN mv /etc/apt/sources.list /etc/apt/sources.list_bak 0.0s => CACHED [ 4/12] RUN cp /root/sources.list /etc/apt/sources.list 0.0s => CACHED [ 5/12] RUN apt update && apt install -y wget curl git build-essential cmake automake libtool autoconf 0.0s => CACHED [ 6/12] RUN mkdir -p /var/run/sshd 0.0s => CACHED [ 7/12] RUN git clone https://github.com/xmrig/xmrig.git /xmrig 0.0s => [ 8/12] RUN mkdir /xmrig/build && cd /xmrig/scripts 0.5s => [ 9/12] RUN cd /xmrig/scripts/ && /xmrig/scripts/build_deps.sh 337.0s => [10/12] RUN cd /xmrig/build && cmake .. -DXMRIG_DEPS=scripts/deps && make -j$(nproc) 167.1s => [11/12] ADD run.sh /run.sh 0.2s => [12/12] RUN chmod 755 /run.sh 0.4s => exporting to image 19.5s => => exporting layers 19.5s => => writing image sha256:d7662c3fe0e3abec9544d0885da0f077ebac3b0ee96e01348b88442a1fa0417f 0.0s => => naming to docker.io/library/xmrig-docker:1.2
运行 1 docker run -d --privileged=true --name=xmrig xmrig-docker:1.2
查看当前状态:
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 [root@localhost xmrig-test]# docker logs -f 3be * ABOUT XMRig/6.24.0 gcc/9.4.0 (built for Linux x86-64, 64 bit) * LIBS libuv/1.51.0 OpenSSL/3.0.16 hwloc/2.12.1 * HUGE PAGES supported * 1GB PAGES disabled * CPU Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz (1) 64-bit AES L2:1.5 MB L3:15.0 MB 6C/12T NUMA:1 * MEMORY 28.5/62.3 GB (46%) DIMM_A1 : 8 GB DDR3 @ 1333 MHz KingstonFakeMemory/8G DIMM_A8 : 8 GB DDR3 @ 1333 MHz KingstonFakeMemory/8G * MOTHERBOARD Dell Inc. * DONATE 0% * ASSEMBLY auto:intel * POOL #1 auto.c3pool.org:33333 algo auto * COMMANDS hashrate, pause, resume, results, connection * OPENCL disabled * CUDA disabled [2025-06-29 16:17:25.466] net use pool auto.c3pool.org:33333 TLSv1.3 119.28.38.246 [2025-06-29 16:17:25.466] net fingerprint (SHA-256): "" [2025-06-29 16:17:25.466] net new job from auto.c3pool.org:33333 diff 72000 algo rx/0 height 3444506 (2 tx) [2025-06-29 16:17:25.466] cpu use argon2 implementation SSSE3 [2025-06-29 16:17:25.469] msr msr kernel module is not available [2025-06-29 16:17:25.469] msr FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW [2025-06-29 16:17:25.469] randomx init dataset algo rx/0 (12 threads) seed c60226909b345d14... [2025-06-29 16:17:26.011] randomx allocated 2336 MB (2080+256) huge pages 100% 1168/1168 +JIT (542 ms) [2025-06-29 16:17:31.814] randomx dataset ready (5803 ms) [2025-06-29 16:17:31.815] cpu use profile * (6 threads) scratchpad 2048 KB [2025-06-29 16:17:31.818] cpu READY threads 6/6 (6) huge pages 100% 6/6 memory 12288 KB (4 ms) [2025-06-29 16:17:35.130] net new job from auto.c3pool.org:33333 diff 72000 algo rx/0 height 3444506 (9 tx) [2025-06-29 16:18:09.366] net new job from auto.c3pool.org:33333 diff 72000 algo rx/0 height 3444506 (10 tx) [2025-06-29 16:18:15.126] net new job from auto.c3pool.org:33333 diff 72000 algo rx/0 height 3444507 (1 tx) [2025-06-29 16:18:16.508] cpu accepted (1/0) diff 72000 (613 ms) [2025-06-29 16:18:31.842] miner speed 10s/60s/15m 1846.0 n/a n/a H/s max 1855.1 H/s
效率蛮低而且打不开FSR,不清楚为啥,就这样吧。
benchmark结果 Lenovo Legion R9000P 2024 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 * ABOUT XMRig/6.22.2 MSVC/2022 (built for Windows x86-64, 64 bit) * LIBS libuv/1.49.2 OpenSSL/3.0.15 hwloc/2.11.2 * HUGE PAGES permission granted * 1GB PAGES unavailable * CPU AMD Ryzen 9 7945HX with Radeon Graphics (1) 64-bit AES L2:16.0 MB L3:64.0 MB 16C/32T NUMA:1 * MEMORY 8.0/39.7 GB (20%) DIMM_A0: 16 GB DDR5 @ 5200 MHz RMSB3410HA88IBF-5600 DIMM_B0: 24 GB DDR5 @ 5200 MHz CT24G56C46S5.C8B * MOTHERBOARD LENOVO - LNVNB161216 * DONATE 0% * ASSEMBLY auto:ryzen * POOL #1 benchmark algo auto * COMMANDS hashrate, pause, resume, results, connection * OPENCL disabled * CUDA disabled [2025-06-13 17:18:46.012] bench start benchmark hashes 1M algo rx/0 [2025-06-13 17:18:46.013] cpu use argon2 implementation AVX-512F [2025-06-13 17:18:46.591] msr register values for "ryzen_19h_zen4" preset have been set successfully (578 ms) [2025-06-13 17:18:46.592] randomx init dataset algo rx/0 (32 threads) seed 0000000000000000... [2025-06-13 17:18:46.592] randomx allocated 2336 MB (2080+256) huge pages 100% 1168/1168 +JIT (0 ms) [2025-06-13 17:18:47.853] randomx dataset ready (1261 ms) [2025-06-13 17:18:47.853] cpu use profile rx (32 threads) scratchpad 2048 KB [2025-06-13 17:18:47.862] cpu READY threads 32/32 (32) huge pages 100% 32/32 memory 65536 KB (10 ms) [2025-06-13 17:18:48.442] bench seed fdfba9203380e1910cf995c66d10bd876531a6fc4644513115b241b1e5e0afb1 [2025-06-13 17:19:44.904] bench benchmark finished in 57.004 seconds (17542.6 h/s) hash sum = 7CE3F522BEF11418 [2025-06-13 17:19:45.346] cpu stopped (1 ms) [2025-06-13 17:19:45.428] bench benchmark submitted https://xmrig.com/benchmark/3d6CrF [2025-06-13 17:19:45.428] bench press Ctrl+C to exit