From 6d14ae9f98a9c28c0ca30c3697882e164f1a0dfb Mon Sep 17 00:00:00 2001 From: LamerLink <36551116+LamerLink@users.noreply.github.com> Date: Sun, 1 Oct 2023 23:38:21 -0400 Subject: [PATCH] Add support for arm64. --- .env.template | 3 ++- Dockerfile-bedrock | 12 ++++++++++++ README.md | 18 ++++++++++++++++++ docker-compose.yml | 2 ++ src/craftlink/bot.py | 2 ++ src/craftlink/command.py | 5 +++++ src/craftlink/console.py | 13 +++++++++++++ 7 files changed, 54 insertions(+), 1 deletion(-) diff --git a/.env.template b/.env.template index c220e43..a838569 100644 --- a/.env.template +++ b/.env.template @@ -3,4 +3,5 @@ DISCORD_CHANNEL_ID= SERVER_INSTALL_DIRECTORY= SERVER_TYPE=bedrock JAVA_MEMORY_MIN=1024 -JAVA_MEMORY_MAX=1024 \ No newline at end of file +JAVA_MEMORY_MAX=1024 +IS_ARM64=false \ No newline at end of file diff --git a/Dockerfile-bedrock b/Dockerfile-bedrock index 94502f3..9d301ba 100644 --- a/Dockerfile-bedrock +++ b/Dockerfile-bedrock @@ -21,6 +21,18 @@ ENV PATH="${PATH}:/root/.local/bin" RUN wget "https://minecraft.azureedge.net/bin-linux/bedrock-server-1.20.15.01.zip" -O server.zip RUN unzip server.zip -d server/ +ARG TARGETARCH +RUN if [ "$TARGETARCH" != "arm64" ] ; then \ + echo "Not targetting arm64, skipping box64 install" \ + ; else \ + apt install -y git build-essential cmake && \ + git clone https://github.com/ptitSeb/box64.git && \ + cd box64 && mkdir build && cd build && \ + cmake .. -DRPI4ARM64=1 -DCMAKE_BUILD_TYPE=RelWithDebInfo && \ + make -j$(nproc) && make install && \ + cd /craftlink \ + ; fi + COPY poetry.lock pyproject.toml ./ COPY . . diff --git a/README.md b/README.md index 617379a..8969837 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,24 @@ Optional arguments: - `-y`, `--server-type`, `SERVER_TYPE` - Type of server to be run ("bedrock" or "java"), defaults to bedrock. - `-m`, `--java-memory-min`, `JAVA_MEMORY_MIN` - (Java only) minimum server memory to allocate, defaults to 1024. - `-x`, `--java-memory-max`, `JAVA_MEMORY_MAX` - (Java only) maximum server memory to allocate, defaults to 1024. +- `-is-arm64`, `IS_ARM64` - Flag to indicate running on arm64 architecture. + +### ARM64 and Bedrock + +You must use the switch `--isarm-64` and set the environment variable `IS_ARM64` +so the command is dispatched correctly. + +The Bedrock binary is meant for x86_64 architecture; to get it to run +on arm64 devices, [`box64`](https://github.com/ptitSeb/box64) emulation is used. + +If building the Bedrock Docker image, specify `--platform=linux/arm64` and +`box64` will be installed automatically. + +Otherwise, ensure your system has `box64` installed and available in the `PATH`. + +(Shoutout to [this issue comment](https://github.com/itzg/docker-minecraft-bedrock-server/issues/140#issuecomment-1126406059) +from [`docker-minecraft-bedrock-server`](https://github.com/itzg/docker-minecraft-bedrock-server) +for pointing me in the right direction here.) ### Docker diff --git a/docker-compose.yml b/docker-compose.yml index a0477ae..52e7181 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,8 @@ services: # Server directory env var inside the container will be different. environment: SERVER_INSTALL_DIRECTORY: "/craftlink/server/" + # Uncomment this if using image on arm64 arch machine. + #IS_ARM64: "true" volumes: - "${SERVER_INSTALL_DIRECTORY}/allowlist.json:/craftlink/server/allowlist.json" - "${SERVER_INSTALL_DIRECTORY}/permissions.json:/craftlink/server/permissions.json" diff --git a/src/craftlink/bot.py b/src/craftlink/bot.py index 30c13a2..03e0614 100644 --- a/src/craftlink/bot.py +++ b/src/craftlink/bot.py @@ -21,6 +21,7 @@ def __init__( channel_id: str, server_type: str, java_mem_range: tuple[int], + use_box64: bool, ) -> None: self.token = token self.server_type = server_type @@ -31,6 +32,7 @@ def __init__( self.server_message_queue, server_type, java_mem_range, + use_box64, ) self.channel_id = int(channel_id) intents = discord.Intents.default() diff --git a/src/craftlink/command.py b/src/craftlink/command.py index 0ecf654..e4ab5b1 100644 --- a/src/craftlink/command.py +++ b/src/craftlink/command.py @@ -35,8 +35,10 @@ def __init__( server_message_queue: deque, server_type: str, java_mem_range: tuple[int], + use_box64: bool, ) -> None: self.server_type = server_type + self.use_box64 = use_box64 self.server_proc = None self.server_message_queue = server_message_queue if not server_path.is_dir(): @@ -166,8 +168,11 @@ async def _cmd_startserver(self, *args) -> str: return "The server is already running." if self.server_type == "bedrock": server_cmd = [self.server_cmd] + if self.use_box64: + server_cmd.insert(0, "box64") elif self.server_type == "java": server_cmd = self.server_cmd.split(" ") + LOGGER.info(f"Starting process: {' '.join(server_cmd)}") self.server_proc = ( await asyncio.subprocess.create_subprocess_exec( *server_cmd, diff --git a/src/craftlink/console.py b/src/craftlink/console.py index bab341d..b7a8e72 100644 --- a/src/craftlink/console.py +++ b/src/craftlink/console.py @@ -1,4 +1,5 @@ import asyncio +import json import logging import os from argparse import ArgumentParser @@ -21,6 +22,7 @@ async def amain(options): channel_id=options.channel_id, server_type=options.server_type, java_mem_range=(options.java_memory_min, options.java_memory_max), + use_box64=options.is_arm64, ) as bot: await bot.run() @@ -72,7 +74,18 @@ def main(): required=False, help="(Java only) maximum server memory to allocate, defaults to 1024.", ) + parser.add_argument( + "--is-arm64", + action="store_true", + required=False, + help="Flag to indicate running on arm64 architecture.", + ) options = parser.parse_args() + try: + if json.loads(os.environ.get("IS_ARM64")): + options.is_arm64 = True + except json.JSONDecodeError: + LOGGER.warning("Invalid value given for IS_ARM64.") try: asyncio.run(amain(options)) except KeyboardInterrupt: