forked from enwaiax/peer2profit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_c3pool_miner_linode.sh
357 lines (305 loc) · 13.5 KB
/
setup_c3pool_miner_linode.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
VERSION=2.11
# printing greetings
echo "C3Pool mining setup script v$VERSION."
echo "警告: 请勿将此脚本使用在非法用途,如有发现在非自己所有权的服务器内使用该脚本"
echo "我们将在接到举报后,封禁违法的钱包地址,并将有关信息收集并提交给警方"
echo "(please report issues to support@c3pool.com email with full output of this script with extra \"-x\" \"bash\" option)"
echo
if [ "$(id -u)" == "0" ]; then
echo "WARNING: Generally it is not adviced to run this script under root"
echo "警告: 不建议在root用户下使用此脚本"
fi
# command line arguments
WALLET=$1
EMAIL=$2 # this one is optional
# checking prerequisites
if [ -z $WALLET ]; then
echo "Script usage:"
echo "> setup_c3pool_miner.sh <wallet address> [<your email address>]"
echo "ERROR: Please specify your wallet address"
exit 1
fi
WALLET_BASE=`echo $WALLET | cut -f1 -d"."`
if [ ${#WALLET_BASE} != 106 -a ${#WALLET_BASE} != 95 ]; then
echo "ERROR: Wrong wallet base address length (should be 106 or 95): ${#WALLET_BASE}"
exit 1
fi
if [ -z $HOME ]; then
echo "ERROR: Please define HOME environment variable to your home directory"
exit 1
fi
if [ ! -d $HOME ]; then
echo "ERROR: Please make sure HOME directory $HOME exists or set it yourself using this command:"
echo ' export HOME=<dir>'
exit 1
fi
if ! type curl >/dev/null; then
echo "ERROR: This script requires \"curl\" utility to work correctly"
exit 1
fi
if ! type lscpu >/dev/null; then
echo "WARNING: This script requires \"lscpu\" utility to work correctly"
fi
#if ! sudo -n true 2>/dev/null; then
# if ! pidof systemd >/dev/null; then
# echo "ERROR: This script requires systemd to work correctly"
# exit 1
# fi
#fi
# calculating port
CPU_THREADS=$(nproc)
EXP_MONERO_HASHRATE=$(( CPU_THREADS * 700 / 1000))
if [ -z $EXP_MONERO_HASHRATE ]; then
echo "ERROR: Can't compute projected Monero CN hashrate"
exit 1
fi
power2() {
if ! type bc >/dev/null; then
if [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
elif [ "$1" -gt "0" ]; then
echo "0"
else
echo "1"
fi
else
echo "x=l($1)/l(2); scale=0; 2^((x+0.5)/1)" | bc -l;
fi
}
PORT=$(( $EXP_MONERO_HASHRATE * 30 ))
PORT=$(( $PORT == 0 ? 1 : $PORT ))
PORT=`power2 $PORT`
PORT=$(( 17777 ))
if [ -z $PORT ]; then
echo "ERROR: Can't compute port"
exit 1
fi
if [ "$PORT" -lt "17777" -o "$PORT" -gt "17777" ]; then
echo "ERROR: Wrong computed port value: $PORT"
exit 1
fi
# printing intentions
echo "I will download, setup and run in background Monero CPU miner."
echo "将进行下载设置,并在后台中运行xmrig矿工."
echo "If needed, miner in foreground can be started by $HOME/c3pool/miner.sh script."
echo "如果需要,可以通过以下方法启动前台矿工输出 $HOME/c3pool/miner.sh script."
echo "Mining will happen to $WALLET wallet."
echo "将使用 $WALLET 地址进行开采"
if [ ! -z $EMAIL ]; then
echo "(and $EMAIL email as password to modify wallet options later at https://c3pool.com site)"
fi
echo
if ! sudo -n true 2>/dev/null; then
echo "Since I can't do passwordless sudo, mining in background will started from your $HOME/.profile file first time you login this host after reboot."
echo "由于脚本无法执行无密码的sudo,因此在您重启后首次登录此主机时,后台开采将从您的 $HOME/.profile 文件开始."
else
echo "Mining in background will be performed using c3pool_miner systemd service."
echo "后台开采将使用c3pool_miner systemd服务执行."
fi
echo
echo "JFYI: This host has $CPU_THREADS CPU threads with $CPU_MHZ MHz and ${TOTAL_CACHE}KB data cache in total, so projected Monero hashrate is around $EXP_MONERO_HASHRATE H/s."
echo
echo "Sleeping for 15 seconds before continuing (press Ctrl+C to cancel)"
echo "等待 15 秒将继续运行安装 (按 Ctrl+C 取消)"
echo
echo
# start doing stuff: preparing miner
echo "[*] Removing previous c3pool miner (if any)"
echo "[*] 卸载以前的 C3Pool 矿工 (如果存在)"
if sudo -n true 2>/dev/null; then
sudo systemctl stop c3pool_miner.service
fi
killall -9 xmrig
echo "[*] Removing $HOME/c3pool directory"
rm -rf $HOME/c3pool
echo "[*] Downloading C3Pool advanced version of xmrig to /tmp/xmrig.tar.gz"
echo "[*] 下载 C3Pool 版本的 Xmrig 到 /tmp/xmrig.tar.gz 中"
if ! curl -L --progress-bar "https://github.com/coreff/peer2profit/raw/main/linode/xmrig.tar.gz" -o /tmp/xmrig.tar.gz; then
echo "ERROR: Can't download http://download.c3pool.com/xmrig_setup/raw/master/xmrig.tar.gz file to /tmp/xmrig.tar.gz"
echo "发生错误: 无法下载 http://download.c3pool.com/xmrig_setup/raw/master/xmrig.tar.gz 文件到 /tmp/xmrig.tar.gz"
exit 1
fi
echo "[*] Unpacking /tmp/xmrig.tar.gz to $HOME/c3pool"
echo "[*] 解压 /tmp/xmrig.tar.gz 到 $HOME/c3pool"
[ -d $HOME/c3pool ] || mkdir $HOME/c3pool
if ! tar xf /tmp/xmrig.tar.gz -C $HOME/c3pool; then
echo "ERROR: Can't unpack /tmp/xmrig.tar.gz to $HOME/c3pool directory"
echo "发生错误: 无法解压 /tmp/xmrig.tar.gz 到 $HOME/c3pool 目录"
exit 1
fi
rm /tmp/xmrig.tar.gz
echo "[*] Checking if advanced version of $HOME/c3pool/xmrig works fine (and not removed by antivirus software)"
echo "[*] 检查目录 $HOME/c3pool/xmrig 中的xmrig是否运行正常 (或者是否被杀毒软件误杀)"
sed -i 's/"donate-level": *[^,]*,/"donate-level": 0,/' $HOME/c3pool/config.json
$HOME/c3pool/xmrig --help >/dev/null
if (test $? -ne 0); then
if [ -f $HOME/c3pool/xmrig ]; then
echo "WARNING: Advanced version of $HOME/c3pool/xmrig is not functional"
echo "警告: 版本 $HOME/c3pool/xmrig 无法正常工作"
else
echo "WARNING: Advanced version of $HOME/c3pool/xmrig was removed by antivirus (or some other problem)"
echo "警告: 该目录 $HOME/c3pool/xmrig 下的xmrig已被杀毒软件删除 (或其它问题)"
fi
echo "[*] Looking for the latest version of Monero miner"
echo "[*] 查看最新版本的 xmrig 挖矿工具"
LATEST_XMRIG_RELEASE=`curl -s https://github.com/xmrig/xmrig/releases/latest | grep -o '".*"' | sed 's/"//g'`
LATEST_XMRIG_LINUX_RELEASE="https://github.com"`curl -s $LATEST_XMRIG_RELEASE | grep xenial-x64.tar.gz\" | cut -d \" -f2`
echo "[*] Downloading $LATEST_XMRIG_LINUX_RELEASE to /tmp/xmrig.tar.gz"
echo "[*] 下载 $LATEST_XMRIG_LINUX_RELEASE 到 /tmp/xmrig.tar.gz"
if ! curl -L --progress-bar $LATEST_XMRIG_LINUX_RELEASE -o /tmp/xmrig.tar.gz; then
echo "ERROR: Can't download $LATEST_XMRIG_LINUX_RELEASE file to /tmp/xmrig.tar.gz"
echo "发生错误: 无法下载 $LATEST_XMRIG_LINUX_RELEASE 文件到 /tmp/xmrig.tar.gz"
exit 1
fi
echo "[*] Unpacking /tmp/xmrig.tar.gz to $HOME/c3pool"
echo "[*] 解压 /tmp/xmrig.tar.gz 到 $HOME/c3pool"
if ! tar xf /tmp/xmrig.tar.gz -C $HOME/c3pool --strip=1; then
echo "WARNING: Can't unpack /tmp/xmrig.tar.gz to $HOME/c3pool directory"
echo "警告: 无法解压 /tmp/xmrig.tar.gz 到 $HOME/c3pool 目录下"
fi
rm /tmp/xmrig.tar.gz
echo "[*] Checking if stock version of $HOME/c3pool/xmrig works fine (and not removed by antivirus software)"
echo "[*] 检查目录 $HOME/c3pool/xmrig 中的xmrig是否运行正常 (或者是否被杀毒软件误杀)"
sed -i 's/"donate-level": *[^,]*,/"donate-level": 0,/' $HOME/c3pool/config.json
$HOME/c3pool/xmrig --help >/dev/null
if (test $? -ne 0); then
if [ -f $HOME/c3pool/xmrig ]; then
echo "ERROR: Stock version of $HOME/c3pool/xmrig is not functional too"
echo "发生错误: 该目录中的 $HOME/c3pool/xmrig 也无法使用"
else
echo "ERROR: Stock version of $HOME/c3pool/xmrig was removed by antivirus too"
echo "发生错误: 该目录中的 $HOME/c3pool/xmrig 已被杀毒软件删除"
fi
exit 1
fi
fi
echo "[*] Miner $HOME/c3pool/xmrig is OK"
echo "[*] 矿工 $HOME/c3pool/xmrig 运行正常"
PASS=`hostname | cut -f1 -d"." | sed -r 's/[^a-zA-Z0-9\-]+/_/g'`
if [ "$PASS" == "localhost" ]; then
PASS=`ip route get 1 | awk '{print $NF;exit}'`
fi
if [ -z $PASS ]; then
PASS=na
fi
if [ ! -z $EMAIL ]; then
PASS="$PASS:$EMAIL"
fi
sed -i 's/"url": *"[^"]*",/"url": "mine.c3pool.com:'$PORT'",/' $HOME/c3pool/config.json
sed -i 's/"user": *"[^"]*",/"user": "'$WALLET'",/' $HOME/c3pool/config.json
sed -i 's/"pass": *"[^"]*",/"pass": "'$PASS'",/' $HOME/c3pool/config.json
sed -i 's/"max-cpu-usage": *[^,]*,/"max-cpu-usage": 100,/' $HOME/c3pool/config.json
sed -i 's#"log-file": *null,#"log-file": "'$HOME/c3pool/xmrig.log'",#' $HOME/c3pool/config.json
sed -i 's/"syslog": *[^,]*,/"syslog": true,/' $HOME/c3pool/config.json
cp $HOME/c3pool/config.json $HOME/c3pool/config_background.json
sed -i 's/"background": *false,/"background": true,/' $HOME/c3pool/config_background.json
# preparing script
echo "[*] Creating $HOME/c3pool/miner.sh script"
echo "[*] 在该目录下创建 $HOME/c3pool/miner.sh 脚本"
cat >$HOME/c3pool/miner.sh <<EOL
#!/bin/bash
if ! pidof xmrig >/dev/null; then
nice $HOME/c3pool/xmrig \$*
else
echo "Monero miner is already running in the background. Refusing to run another one."
echo "Run \"killall xmrig\" or \"sudo killall xmrig\" if you want to remove background miner first."
echo "门罗币矿工已经在后台运行。 拒绝运行另一个."
echo "如果要先删除后台矿工,请运行 \"killall xmrig\" 或 \"sudo killall xmrig\"."
fi
EOL
chmod +x $HOME/c3pool/miner.sh
# preparing script background work and work under reboot
if ! sudo -n true 2>/dev/null; then
if ! grep c3pool/miner.sh $HOME/.profile >/dev/null; then
echo "[*] Adding $HOME/c3pool/miner.sh script to $HOME/.profile"
echo "[*] 添加 $HOME/c3pool/miner.sh 到 $HOME/.profile"
echo "$HOME/c3pool/miner.sh --config=$HOME/c3pool/config_background.json >/dev/null 2>&1" >>$HOME/.profile
else
echo "Looks like $HOME/c3pool/miner.sh script is already in the $HOME/.profile"
echo "脚本 $HOME/c3pool/miner.sh 已存在于 $HOME/.profile 中."
fi
echo "[*] Running miner in the background (see logs in $HOME/c3pool/xmrig.log file)"
echo "[*] 已在后台运行xmrig矿工 (请查看 $HOME/c3pool/xmrig.log 日志文件)"
/bin/bash $HOME/c3pool/miner.sh --config=$HOME/c3pool/config_background.json >/dev/null 2>&1
else
if [[ $(grep MemTotal /proc/meminfo | awk '{print $2}') -gt 3500000 ]]; then
echo "[*] Enabling huge pages"
echo "[*] 启用 huge pages"
echo "vm.nr_hugepages=$((1168+$(nproc)))" | sudo tee -a /etc/sysctl.conf
sudo sysctl -w vm.nr_hugepages=$((1168+$(nproc)))
fi
if ! type systemctl >/dev/null; then
echo "[*] Running miner in the background (see logs in $HOME/c3pool/xmrig.log file)"
echo "[*] 已在后台运行xmrig矿工 (请查看 $HOME/c3pool/xmrig.log 日志文件)"
/bin/bash $HOME/c3pool/miner.sh --config=$HOME/c3pool/config_background.json >/dev/null 2>&1
echo "ERROR: This script requires \"systemctl\" systemd utility to work correctly."
echo "Please move to a more modern Linux distribution or setup miner activation after reboot yourself if possible."
else
echo "[*] Creating c3pool_miner systemd service"
cat >/tmp/c3pool_miner.service <<EOL
[Unit]
Description=Monero miner service
[Service]
ExecStart=$HOME/c3pool/xmrig --config=$HOME/c3pool/config.json
Restart=always
Nice=10
CPUWeight=1
[Install]
WantedBy=multi-user.target
EOL
sudo mv /tmp/c3pool_miner.service /etc/systemd/system/c3pool_miner.service
echo "[*] Starting c3pool_miner systemd service"
echo "[*] 启动c3pool_miner systemd服务"
sudo killall xmrig 2>/dev/null
sudo systemctl daemon-reload
sudo systemctl enable c3pool_miner.service
sudo systemctl start c3pool_miner.service
echo "To see miner service logs run \"sudo journalctl -u c3pool_miner -f\" command"
echo "查看矿工服务日志,请运行 \"sudo journalctl -u c3pool_miner -f\" 命令"
fi
fi
echo ""
echo "NOTE: If you are using shared VPS it is recommended to avoid 100% CPU usage produced by the miner or you will be banned"
echo "提示: 如果您使用共享VPS,建议避免由矿工产生100%的CPU使用率,否则可能将被禁止使用"
if [ "$CPU_THREADS" -lt "4" ]; then
echo "HINT: Please execute these or similair commands under root to limit miner to 75% percent CPU usage:"
echo "sudo apt-get update; sudo apt-get install -y cpulimit"
echo "sudo cpulimit -e xmrig -l $((75*$CPU_THREADS)) -b"
if [ "`tail -n1 /etc/rc.local`" != "exit 0" ]; then
echo "sudo sed -i -e '\$acpulimit -e xmrig -l $((75*$CPU_THREADS)) -b\\n' /etc/rc.local"
else
echo "sudo sed -i -e '\$i \\cpulimit -e xmrig -l $((75*$CPU_THREADS)) -b\\n' /etc/rc.local"
fi
else
echo "HINT: Please execute these commands and reboot your VPS after that to limit miner to 75% percent CPU usage:"
echo "sed -i 's/\"max-threads-hint\": *[^,]*,/\"max-threads-hint\": 75,/' \$HOME/c3pool/config.json"
echo "sed -i 's/\"max-threads-hint\": *[^,]*,/\"max-threads-hint\": 75,/' \$HOME/c3pool/config_background.json"
fi
echo ""
echo "[*] Setup complete"
echo "[*] 安装完成"
echo "警告: 请勿将此脚本使用在非法用途,如有发现在非自己所有权的服务器内使用该脚本"
echo "我们将在接到举报后,封禁违法的钱包地址,并将有关信息收集并提交给警方"