ptitSeb / box64

Box64 - Linux Userspace x86_64 Emulator with a twist, targeted at ARM64 Linux devices
https://box86.org
MIT License
3.89k stars 286 forks source link

Successfully initiated a Debian X86 container with box64 #1550

Open shenmo7192 opened 5 months ago

shenmo7192 commented 5 months ago

box64 can both use emulated libs and native ones. Why not create a container with a full OS rootfs and with both arm and x86 libs?

So I created a bwrap container and use box64 as emulator

To solve the dynamic link problem, i choose to mount system ld and system libs in the container

It successfully started! And i can even run some commands

图片 图片 However apt can not be loaded...

I have no name!@AmberCE-Bookworm-x86:~/Desktop/fastfetch/usr/bin$ apt
Dynarec for ARM64, with extension: ASIMD AES CRC32 PMULL ATOMICS SHA1 SHA2/bin/sh: 1: lscpu: not found
/bin/sh: 1: grep: not found
/bin/sh: 1: grep: not found/bin/sh: 1: 
sed: not found
/bin/sh: 1: lscpu: not found
/bin/sh: 1: grep: not found
/bin/sh: 1: /bin/sh: 1: sed: not found
lscpu: not found
/bin/sh: 1: sed: not found
/bin/sh: 1: lscpu: not found
/bin/sh: 1: lscpu: not found
/bin/sh: 1: /bin/sh: 1: grep: not found
sed: not found
/bin/sh: 1: grep: not found
/bin/sh: 1: lscpu: not found
/bin/sh: 1: sed: not found
/bin/sh: 1: sed: not found/bin/sh: 1: 
grep: not found
/bin/sh: 1: lscpu: not found
/bin/sh: 1: sed: not found
/bin/sh: 1: grep: not found
 PageSize:4096 Running on Error: Symbol re_match not found, cannot apply R_X86_64_JUMP_SLOT @0x100031c08 (0x40a6) in /usr/bin/grep Error: Symbol re_search not found, cannot apply R_X86_64_JUMP_SLOT @0x100031e50 (s
Will use Hardware counter measured at 100 MHz emulating 3.2 GHz
Params database has 14 entries
Box64 with Dynarec v0.2.9 dc71840a built on Jun  2 2024 15:31:19
BOX64: Detected 48bits at least of address space
Counted 66 Env var
BOX64 LIB PATH: ./:lib/:lib64/:x86_64/:bin64/:libs64/:./:lib/:lib64/:x86_64/:bin64/:libs64/:/lib/x86_64-linux-gnu/:/usr/lib/x86_64-linux-gnu/
BOX64 BIN PATH: ./:bin/:/flamescion-container-tools/bin-override/:/home/shenmo-ikun/.local/bin/:/usr/local/bin/:/usr/bin/:/bin/:/usr/local/games/:/usr/games/:/sbin/:/usr/sbin/:/home/shenmo-ikun/.pub-cache/bin/
Looking for /usr/bin/apt
Rename process to "apt"
Using emulated /lib/x86_64-linux-gnu/libapt-private.so.0.0
Using emulated /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Using emulated /lib/x86_64-linux-gnu/libstdc++.so.6
Using emulated /lib/x86_64-linux-gnu/libgcc_s.so.1
Using native(wrapped) libc.so.6
Using native(wrapped) ld-linux-x86-64.so.2
Using native(wrapped) libpthread.so.0
Using native(wrapped) libdl.so.2
Using native(wrapped) libutil.so.1
Using native(wrapped) libresolv.so.2
Using native(wrapped) librt.so.1
Using native(wrapped) libbsd.so.0
Using native(wrapped) libm.so.6
Look for arc4random in loaded elfs
Warning, function arc4random not found in lib libbsd.so.0
Look for arc4random in loaded elfs
Warning, function arc4random not found in lib libbsd.so.0
Using native(wrapped) libz.so.1
Using native(wrapped) libbz2.so.1
Using native(wrapped) liblzma.so.5
Using emulated /lib/x86_64-linux-gnu/liblz4.so.1
Using emulated /lib/x86_64-linux-gnu/libzstd.so.1
Using native(wrapped) libudev.so.1
Using emulated /lib/x86_64-linux-gnu/libsystemd.so.0
Using native(wrapped) libgcrypt.so.20
Using emulated /lib/x86_64-linux-gnu/libxxhash.so.0
Using native(wrapped) libcap.so.2
Look for gettid in loaded elfs
Warning, function gettid not found in lib libc.so.6
Error: Symbol gettid not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff060cd390 (0x16386) in /lib/x86_64-linux-gnu/libsystemd.so.0
Look for close_range in loaded elfs
Warning, function close_range not found in lib libc.so.6
Look for close_range in loaded elfs
Warning, function close_range not found in lib libc.so.6
Error: Symbol close_range not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff060cdcc0 (0x175e6) in /lib/x86_64-linux-gnu/libsystemd.so.0
Error: relocating Plt symbols in elf libsystemd.so.0
Error initializing needed lib libgcrypt.so.20
Error loading one of needed lib
Error initializing needed lib libgcc_s.so.1
Error: Symbol LZ4F_compressUpdate not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6620 (0x423b6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_error_free not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f66a8 (0x424c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_64bits_reset not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6728 (0x425c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_compressBegin not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6818 (0x427a6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_call_method not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6cf0 (0x43156) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_64bits_digest not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6d58 (0x43226) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_message_read not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6dd8 (0x43326) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_initCStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6e28 (0x433c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_64bits not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6e58 (0x43426) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_createDecompressionContext not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6f58 (0x43626) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_freeDecompressionContext not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f6fe0 (0x43736) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_compressStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f70b8 (0x438e6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_createState not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7138 (0x439e6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_freeCStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7190 (0x43a96) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_open_system not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f71d0 (0x43b16) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Look for dn_expand in loaded elfs
Warning, function dn_expand not found in lib libresolv.so.2
Error: Symbol dn_expand not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7298 (0x43ca6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_message_unref not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7358 (0x43e26) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_64bits_update not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7510 (0x44196) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_createDStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7680 (0x44476) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_compressBound not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7848 (0x44806) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_decompressStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f78a8 (0x448c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_isError not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7920 (0x449b6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_decompress not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f79e0 (0x44b36) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_getErrorName not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7af0 (0x44d56) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol XXH3_freeState not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7c00 (0x44f76) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_compressEnd not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7c90 (0x45096) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_initDStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7ca0 (0x450b6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol sd_bus_unref not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7e28 (0x453c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_createCStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7e58 (0x45426) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_isError not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f7fb8 (0x456e6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_freeDStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8138 (0x459e6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_getErrorName not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8410 (0x45f96) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol res_nquery not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8460 (0x46036) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_createCompressionContext not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f84a0 (0x460b6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol LZ4F_freeCompressionContext not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8728 (0x465c6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol ZSTD_endStream not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8738 (0x465e6) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: Symbol dn_skipname not found, cannot apply R_X86_64_JUMP_SLOT @0x7fff011f8850 (0x46816) in /lib/x86_64-linux-gnu/libapt-pkg.so.6.0
Error: relocating Plt symbols in elf libapt-pkg.so.6
Error initializing needed lib libc.so.6
Error loading one of needed lib
Error initializing needed lib libc.so.6
Error loading one of needed lib
Error: Loading needed libs in elf /usr/bin/apt

Just share fun, don't think this thing can solve.

shenmo7192 commented 5 months ago

This is the script i use

#!/bin/bash

curdir=`realpath $0`
parent_dir=`dirname $curdir`
pparent_dir=`dirname $parent_dir`
ppparent_dir=`dirname $pparent_dir`
PKGNAME=`basename $ppparent_dir`
export PACKAGE_NAME=$PKGNAME
chrootEnvPath=/opt/apps/$PKGNAME/files/bookworm-env

#if [ ! -e $chrootEnvPath/finish.flag ];then
#
#if [ "$(id -u)" = "0" ]; then
#    `dirname $chrootEnvPath`/bin/bookworm-init
#else
#pkexec `dirname $chrootEnvPath`/bin/bookworm-init
#fi

#fi
non_root_user=$(who  | awk '{print $1}' | head -n 1)
uid=$(id -u $non_root_user)

#### This part is for args pharm
if [ "$1" = "" ];then
container_command="bash"
else
container_command="$1"
shift
for arg in "$@"; do
        arg="$(echo "${arg}x" | sed 's|'\''|'\'\\\\\'\''|g')"
        arg="${arg%x}"
        container_command="${container_command} '${arg}'"
done
fi
#########################################################################################
##########合成bwrap 1. 基础函数配置段
# 初始化 EXEC_COMMAND 为 bwrap 基础指令
EXEC_COMMAND="bwrap --dev-bind / / bwrap"

# add_command 函数定义
function add_command() {
    # 参数拼接,考虑到转义和空格的处理
    for arg in "$@"; do
        EXEC_COMMAND="${EXEC_COMMAND} ${arg}"
    done
}

function add_env_var() {
    local var_name="${1}"
    local var_value="${2}"
    if [ "$var_value" != "" ]; then    
        add_command "--setenv $var_name $var_value"

    fi
}
##########合成bwrap 2. 特殊需求函数配置段
function cursor_theme_dir_integration() {

local directory=""
if [ "$(id -u)" = "0" ]; then #####We don't want bother root to install themes,but will try to fix the unwriteable issue
        mkdir -p $chrootEnvPath/usr/share/icons
        chmod 777 -R $chrootEnvPath/usr/share/icons
        return
fi

for directory in "/usr/share/icons"/*; do
    # 检查是否为目录
    if [ -d "$directory" ]; then
        # 检查目录中是否存在 cursors 文件
        if [ -d "$directory/cursors" ]; then
                if [ -w $chrootEnvPath/usr/share/icons ];then
                        add_command "--ro-bind-try $directory $directory"
                fi
        fi
    fi
done

}
##########合成bwrap 3. 环境变量和目录绑定配置段
# 添加环境变量和其他初始设置
ENV_VARS=(
    "LANG $LANG"
    "LC_COLLATE $LC_COLLATE"
    "LC_CTYPE $LC_CTYPE"
    "LC_MONETARY $LC_MONETARY"
    "LC_MESSAGES $LC_MESSAGES"
    "LC_NUMERIC $LC_NUMERIC"
    "LC_TIME $LC_TIME"
    "LC_ALL $LC_ALL"
    "PULSE_SERVER /run/user/\$uid/pulse/native"
    "PATH /flamescion-container-tools/bin-override:\$PATH"
    "IS_ACE_ENV 1"
)

BIND_DIRS=(
    "--dev-bind $chrootEnvPath/ /"
    "--dev-bind-try /media /media"
    "--dev-bind-try /tmp /tmp"
    "--dev /dev"
    "--dev-bind-try /dev/dri /dev/dri"
    "--proc /proc"
    "--dev-bind /sys /sys"
    "--dev-bind /run /run"
    "--dev-bind-try /run/user/\$uid/pulse /run/user/\$uid/pulse"
    "--dev-bind / /host"
    "--ro-bind-try /usr/share/themes /usr/local/share/themes"
    "--ro-bind-try /usr/share/icons /usr/local/share/icons"
    "--ro-bind-try /usr/share/fonts /usr/local/share/fonts"
    "--dev-bind-try /etc/resolv.conf /etc/resolv.conf"
    "--dev-bind-try /home /home"
    "--ro-bind /usr/lib/aarch64-linux-gnu/ /usr/lib/aarch64-linux-gnu/"
    "--ro-bind $(realpath /lib/ld-linux-aarch64.so.1) /lib/ld-linux-aarch64.so.1"

)
EXTRA_ARGS=(
    "--hostname AmberCE-Bookworm-x86"
    "--unshare-uts"
    "--cap-add CAP_SYS_ADMIN"
)

EXTRA_SCRIPTS=(
    cursor_theme_dir_integration
)

##########合成bwrap 4. 合成并执行指令
# 逐一添加到 EXEC_COMMAND
for var in "${ENV_VARS[@]}"; do
    add_env_var $var
done

for var in "${BIND_DIRS[@]}"; do
    add_command "$var"
done

for var in "${EXTRA_ARGS[@]}"; do
    add_command "$var"
done

for var in "${EXTRA_SCRIPTS[@]}"; do
    $var
done

# 添加最终的 bash 命令
add_command "bash -c \"${container_command}\""

# 输出完整的 EXEC_COMMAND 以查看
# echo "${EXEC_COMMAND}"

# 注意: 实际执行时,请确保所有变量(如 $uid, $chrootEnvPath 等)都已正确定义
eval ${EXEC_COMMAND}