Debian运行其它架构的程序的方法

【使用dpkg多架构支持运行】

只需要在dpkg支持的架构里添加你需要安装运行库的架构,然后apt安装运行库时使用冒号:后缀指定架构即可

# 本机不支持的架构需要qemu-user-static
sudo apt install qemu-user-static

# 添加想要安装运行库的架构
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install libc6:i386 libstdc++6:i386

# 对于本机支持的32位架构,如果需要让程序认为系统是32位,可以使用下面的命令
setarch linux32 [命令 [参数]]

Debian设计为不同架构的动态链接器和运行库、架构相关头文件等使用不同的路径,自动共享架构无关的文件,因此lib*运行库和lib*-dev开发库等一般不会发生冲突,但其它软件包则不一定,可能会自动卸载冲突的版本

如果需要编译软件的话,不能直接安装对应架构的build-essential,会冲突,需要安装交叉编译工具链:

sudo apt install build-essential           # 安装本机编译工具链,包括g++、make和dpkg-dev
sudo apt install crossbuild-essential-i386 # 安装i386交叉编译工具链,包括g++-i686-linux-gnu和dpkg-cross

这里不建议安装g++-multilib等multilib工具链,会和所有交叉编译工具链冲突(不只是和对应架构的交叉编译工具链冲突,是和所有架构的交叉编译工具链都冲突),会互相自动卸载

【使用debootstrap和schroot运行其它架构的Debian】

如果需要使用完整的根目录环境运行,或者上面的方法出现了冲突的情况,就需要使用chroot环境了,步骤如下:

1、下载所需软件包

# 安装所需软件包(运行本机支持的32位架构不需要qemu-user-static)
sudo apt install debootstrap schroot qemu-user-static

2、下载chroot环境

Debian支持的架构如amd64、i386、arm64、armel、armhf等:

# 下载基本系统,其中bookworm换成你要下载的debian版本代号,i386换成你想要下载的架构,后面的URL换成你想使用的软件源镜像
sudo mkdir -p /srv/chroot/debian12-i386
sudo debootstrap --arch=i386 bookworm /srv/chroot/debian12-i386 https://mirrors.tuna.tsinghua.edu.cn/debian/

Debian Ports支持的架构如riscv64等(只有unstable/sid): 

sudo apt install debian-ports-archive-keyring
sudo mkdir -p /srv/chroot/debian-riscv64
# 以下地方有Debian Ports源:
# 官方2个
# http://deb.debian.org/debian-ports/
# http://ftp.ports.debian.org/debian-ports/
# 国内5个
# https://mirrors.hit.edu.cn/debian-ports/
# https://mirror.iscas.ac.cn/debian-ports/
# https://mirror.lzu.edu.cn/debian-ports/
# https://mirror.nju.edu.cn/debian-ports/
# https://mirror.sjtu.edu.cn/debian-ports/
sudo debootstrap --arch=riscv64 --keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg --include=debian-ports-archive-keyring unstable /srv/chroot/debian-riscv64 https://mirrors.hit.edu.cn/debian-ports/

如果keyring不管用可以去这里下载:  

然后sudo apt remove debian-ports-archive-keyring卸载旧的,sudo dpkg -i <pkgname>.deb安装新的

3、配置chroot环境

# 查看schroot最新配置手册
man schroot.conf

# 然后配置一个schroot配置文件
sudo nano /etc/schroot/chroot.d/debian12-i386.conf

# 以下是debian12-i386.conf文件内容
[debian12-i386]
description=Debian 12 for i386
directory=/srv/chroot/debian12-i386
type=directory
# 设置profile,配置文件为/etc/schroot/<profile>/*
# 推荐使用default或desktop,它们功能全面,但会共享/home
# 如果不想共享/home可以自己创建一个
profile=desktop
# 运行本机支持的32位架构需要设置personality=linux32,可以让程序认为系统是32位,对应的命令是setarch linux32
# 运行本机不支持的架构不需要设置这个
personality=linux32
# 设置能使用root和只能使用普通用户的用户名
root-users=bob
users=bob

4、进入chroot环境

# 使用下面的命令列出所有chroot环境
schroot -l

# 进入chroot环境
schroot -c debian12-i386 -u root

# 安装并配置locales
apt install locales
dpkg-reconfigure locales

# 如果你喜欢使用sudo,可以安装sudo
apt install sudo

# 退出并使用普通用户登录
exit
schroot -c debian12-i386
06-07 00:27