12.3.Linux 用户空间

Linux 软件的运行不仅需要 ABI,还需要安装一个 Linux 用户空间环境。

# pkg install linux-sublime-text4

12.3.1. 来自 FreeBSD 软件包的 Rocky Linux 基本系统

要安装 Rocky Linux 9 用户空间,可执行以下命令:

# pkg install linux_base-rl9

emulators/linux_base-rl9 会将源自 Rocky Linux 9 的基本系统放置在 /compat/linux 目录中。

安装该软件包后,可通过以下命令确认 Rocky Linux 用户空间是否已安装到 /compat/linux

# ls -l /compat/linux/

输出应类似于:

total 36
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 afs
lrwxr-xr-x   1 root wheel    7 May 16  2022 bin -> usr/bin
drwxr-xr-x   3 root wheel  512 Oct  9 17:28 dev
drwxr-xr-x  24 root wheel 1536 Oct  9 17:28 etc
lrwxr-xr-x   1 root wheel    7 May 16  2022 lib -> usr/lib
lrwxr-xr-x   1 root wheel    9 May 16  2022 lib64 -> usr/lib64
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 opt
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 proc
lrwxr-xr-x   1 root wheel    8 Oct  1 03:11 run -> /var/run
lrwxr-xr-x   1 root wheel    8 May 16  2022 sbin -> usr/sbin
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 srv
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 sys
drwxr-xr-x   8 root wheel  512 Oct  9 17:28 usr
drwxr-xr-x  16 root wheel  512 Oct  9 17:28 var

12.3.2. 来自 FreeBSD 软件包的 CentOS 基本系统

警告

由于上游项目已停止维护,emulators/linux_base-c7 也已弃用,因此将不再收到安全更新。除非需要 32 位兼容性,建议使用 Rocky Linux 基本系统

要安装 CentOS 用户空间,可执行以下命令:

# pkg install linux_base-c7

emulators/linux_base-c7 会将源自 CentOS 7 的基本系统放置在 /compat/linux 中。

安装完成后,可通过以下命令验证 CentOS 用户空间是否已安装:

# ls -l /compat/linux/

输出应类似于:

total 30
lrwxr-xr-x   1 root  wheel    7 Apr 11  2018 bin -> usr/bin
drwxr-xr-x  13 root  wheel  512 Apr 11 21:10 dev
drwxr-xr-x  25 root  wheel   64 Apr 11 21:10 etc
lrwxr-xr-x   1 root  wheel    7 Apr 11  2018 lib -> usr/lib
lrwxr-xr-x   1 root  wheel    9 Apr 11  2018 lib64 -> usr/lib64
drwxr-xr-x   2 root  wheel    2 Apr 11 21:10 opt
dr-xr-xr-x   1 root  wheel    0 Apr 11 21:25 proc
lrwxr-xr-x   1 root  wheel    8 Feb 18 02:10 run -> /var/run
lrwxr-xr-x   1 root  wheel    8 Apr 11  2018 sbin -> usr/sbin
drwxr-xr-x   2 root  wheel    2 Apr 11 21:10 srv
dr-xr-xr-x   1 root  wheel    0 Apr 11 21:25 sys
drwxr-xr-x   8 root  wheel    9 Apr 11 21:10 usr
drwxr-xr-x  16 root  wheel   17 Apr 11 21:10 var

12.3.3. 使用 debootstrap 的 Debian / Ubuntu 基本系统

另一种提供 Linux 共享库的方式是使用 sysutils/debootstrap,它的优点是可以提供完整的 Debian 或 Ubuntu 发行版。

安装 debootstrap 的命令如下:

# pkg install debootstrap

debootstrap(8) 需要启用 linux(4) ABI。待启用,可使用以下命令在 /compat/ubuntu 中安装 Ubuntu 或 Debian:

# debootstrap focal /compat/ubuntu

注意

虽然技术上可以安装到 /compat/linux,但由于可能与基于 CentOS 的软件包发生冲突,不建议这么做。应使用基于发行版或版本名的路径,例如 /compat/ubuntu

输出应类似于:

I: Retrieving InRelease
I: Checking Release signature
I: Valid Release signature (key id F6ECB3762474EDA9D21B7022871920D1991BC93C)
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Checking component main on http://archive.ubuntu.com/ubuntu...
[...]
I: Configuring console-setup...
I: Configuring kbd...
I: Configuring ubuntu-minimal...
I: Configuring libc-bin...
I: Configuring ca-certificates...
I: Base system installed successfully.

然后在 /etc/fstab 中设置挂载点。

技巧

如果希望共享 home 目录的内容并运行 X11 应用程序,应使用 nullfs(5) 进行回环挂载,将 /home 和 /tmp 挂载到 Linux 兼容区中。

可以将以下示例添加到 /etc/fstab:

# Device        Mountpoint              FStype          Options                      Dump    Pass#
devfs           /compat/ubuntu/dev      devfs           rw,late                      0       0
tmpfs           /compat/ubuntu/dev/shm  tmpfs           rw,late,size=1g,mode=1777    0       0
fdescfs         /compat/ubuntu/dev/fd   fdescfs         rw,late,linrdlnk             0       0
linprocfs       /compat/ubuntu/proc     linprocfs       rw,late                      0       0
linsysfs        /compat/ubuntu/sys      linsysfs        rw,late                      0       0
/tmp            /compat/ubuntu/tmp      nullfs          rw,late                      0       0
/home           /compat/ubuntu/home     nullfs          rw,late                      0       0

然后执行 mount(8)

# mount -al

要使用 chroot(8) 进入系统,可执行:

# chroot /compat/ubuntu /bin/bash

然后可通过 uname(1) 检查当前 Linux 环境:

# uname -s -r -m

输出应类似于:

Linux 3.17.0 x86_64

进入 chroot 后,系统行为与普通 Ubuntu 安装基本一致。尽管 systemd 不可用,但 service(8) 命令仍可照常使用。

技巧

要添加默认缺失的软件包仓库,请编辑文件 /compat/ubuntu/etc/apt/sources.list

对于 amd64,可以使用以下示例:

deb http://archive.ubuntu.com/ubuntu focal main universe restricted multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security universe multiverse restricted main
deb http://archive.ubuntu.com/ubuntu focal-backports universe multiverse restricted main
deb http://archive.ubuntu.com/ubuntu focal-updates universe multiverse restricted main

arm64 可使用以下例子:

deb http://ports.ubuntu.com/ubuntu-ports bionic main universe restricted multiverse

最后更新于