Skip to content

Commit

Permalink
Add a workaround for qemu-user memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagb committed Mar 21, 2022
1 parent 080db62 commit 502bcca
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ COPY ./docker-root /

COPY --from=fake-hwaddr fake-hwaddr/fake-hwaddr.so /usr/local/lib/fake-hwaddr.so

ENV QEMU_ECAGENT_MEM_LIMIT=256

VOLUME /root/ /usr/share/sangfor/EasyConnect/resources/logs/

CMD ["start.sh"]
2 changes: 2 additions & 0 deletions Dockerfile.cli
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ COPY ./docker-root /

COPY --from=fake-hwaddr fake-hwaddr/fake-hwaddr.so /usr/local/lib/fake-hwaddr.so

ENV QEMU_ECAGENT_MEM_LIMIT=256

VOLUME /root/ /usr/share/sangfor/EasyConnect/resources/logs/

CMD _EC_CLI=1 start.sh
2 changes: 2 additions & 0 deletions Dockerfile.vncless
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ COPY ./docker-root /

COPY --from=fake-hwaddr fake-hwaddr/fake-hwaddr.so /usr/local/lib/fake-hwaddr.so

ENV QEMU_ECAGENT_MEM_LIMIT=256

VOLUME /root/ /usr/share/sangfor/EasyConnect/resources/logs/

CMD TYPE=x11 start.sh
2 changes: 2 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

- `NODANTED`: 默认为空。不为空时提供 socks5 代理的`danted`将不会启动(可用于和`--net host`参数配合,提供全局透明代理)。

- `QEMU_ECAGENT_MEM_LIMIT`:在非 amd64 架构上用 `qemu-x86_64` 运行 `ECAgent` 时,限制 `ECAgent` 的最大内存(MB),默认为 256。(qemu-user 内存泄漏问题 https://gitlab.com/qemu-project/qemu/-/issues/866 的 workaround)

### 仅适用于纯命令行版本的环境变量

- `EC_VER`: 指定运行的 EasyConnect 版本,必填
Expand Down
21 changes: 21 additions & 0 deletions docker-root/usr/local/bin/start-sangfor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ if [ -n "$FAKE_HWADDR" ]; then
fi
fi
[ -z "$_EC_CLI" ] && /usr/share/sangfor/EasyConnect/resources/bin/EasyMonitor

# 对 qemu-user 内存泄漏 https://gitlab.com/qemu-project/qemu/-/issues/866 的一个 workaround
# 在 ECAgent 超过一定内存时杀掉它,经试验并不会对 vpn 服务造成很大影响
# https://github.com/Hagb/docker-easyconnect/issues/128#issuecomment-1074058067
[ -e /usr/local/libexec/qemu-hack ] && {
page_size=$(getconf PAGESIZE)
while true; do
rss_pages=0
for pid in $(pidof ECAgent); do
statm=($(cat /proc/$pid/statm))
((rss_pages+=statm[1]))
done
((rss_size_mb=rss_pages*page_size/1024/1024))
if ((rss_size_mb>QEMU_ECAGENT_MEM_LIMIT)); then
killall ECAgent
echo "ECAgent spend memory $rss_size_mb MB > $QEMU_ECAGENT_MEM_LIMIT MB! Kill ECAgent!"
fi
sleep 15
done
} &

sleep 1
while true
do
Expand Down

0 comments on commit 502bcca

Please sign in to comment.