12.3.Linux 用户空间

Linux 软件的运行需要的不仅仅是 ABI。为了运行 Linux 软件,首先必须安装 Linux 用户空间。

技巧

如果只想运行已包含在 Ports 中的一些软件,可以通过软件包管理器安装它们,pkg(8)将自动设置所需的 Linux 用户空间。例如,要安装 Sublime Text 4 及其所有依赖的 Linux 库,请运行以下命令:

# pkg install linux-sublime-text4

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

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

# pkg install linux_base-c7

emulators/linux_base-c7将从 CentOS 7 派生的基本系统放置到 /compat/linux

安装软件包后,可以通过运行以下命令验证 /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.2. 使用 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 中设置挂载。

技巧

如果希望共享主目录的内容并能够运行 X11 应用程序,则应使用nullfs(5) 进行环回在 linux 兼容层中挂载 /home/tmp

可以将以下示例添加到 /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

最后更新于

FreeBSD 中文社区