# 11.5 Slackware Linux 兼容层

Slackware Linux 未提供预构建的基本系统镜像，需通过脚本自行生成。本节给出在 FreeBSD 上构建 Slackware 兼容层的脚本及依赖说明（需预装 Bash 和 gmake）。

原版脚本归档地址为 <https://archive.softwareheritage.org/browse/origin/directory/?origin_url=https://github.com/nnnewb/slackware-WSL>。

由于没有官方服务器提供支持，请自行生成基本系统镜像并放置在兼容层对应的路径下。

兼容层脚本内容如下：

```sh
#!/bin/sh

export ROOT_DIR=/compat
export DIST=slackware
export DIST_FULLNAME=Slackware
VER=15.0
FILE=${DIST}64-${VER}.tar.xz
SUBDIR=""
URL=https://book.bsdcn.org # 修改为文件所在链接
UPDATE_CMD="slackpkg update"
UPGRADE_CMD="slackpkg upgrade-all"
INSTALL_CMD="slackpkg install"
UPDATE=1
UPGRADE=1
INSTALL=1

echo "Starting ${DIST_FULLNAME} installation"
sleep 0.5

# 检查 Linux 模块
echo "Checking required modules"
if [ "$(sysrc -n linux_enable)" != "YES" ]; then
    echo "linux service is not enabled. Enable it now? (Y|n)"
    read ANSWER
    case $ANSWER in
        [Nn][Oo]|[Nn])
            echo "Warning: You MUST start linux service with \"service linux start\" EACH TIME FreeBSD IS REBOOTED."
            echo "Sure to continue without enabling linux service? (y|N)"
            read ANSWER
            case $ANSWER in
                [Yy][Ee][Ss]|[Yy])
                    echo "WARNING: linux module not enabled"
                    ;;
                [Nn][Oo]|[Nn]|"")
                    echo "Enabling linux module"
                    sysrc linux_enable=YES
                    ;;
                *)
                    echo "Aborting."
                    exit 4
                    ;;
            esac
            ;;
        [Yy][Ee][Ss]|[Yy]|"")
            echo "Enabling linux module"
            sysrc linux_enable=YES
            ;;
        *)
            echo "Aborting."
            exit 4
            ;;
    esac
fi

echo "Starting linux service"
service linux start

# 检查 dbus
if ! which -s dbus-daemon; then
    echo "dbus-daemon not found. Install dbus now?(Y|n)"
    read ANSWER
    case $ANSWER in
        [Nn][Oo]|[Nn])
            echo "Aborting. dbus not installed"
            exit 2
            ;;
        [Yy][Ee][Ss]|[Yy]|"")
            echo "Installing dbus"
            pkg install -y dbus
            ;;
        *)
            echo "Aborting."
            exit 4
            ;;
    esac
fi

if [ "$(sysrc -n dbus_enable)" != "YES" ]; then
    echo "dbus is not enabled. Enable it now? (Y|n)"
    read ANSWER
    case $ANSWER in
        [Nn][Oo]|[Nn])
            echo "WARNING: You MUST start dbus with \"service dbus start\" EACH TIME FreeBSD IS REBOOTED."
            echo "Sure to continue without enabling dbus? (y|N)"
            read ANSWER
            case $ANSWER in
                [Yy][Ee][Ss]|[Yy])
                    echo "Warning: dbus not enabled"
                    ;;
                [Nn][Oo]|[Nn]|"")
                    echo "Enabling dbus service"
                    service dbus enable
                    ;;
                *)
                    echo "Aborting."
                    exit 4
                    ;;
            esac
            ;;
        [Yy][Ee][Ss]|[Yy]|"")
            echo "Enabling dbus service"
            service dbus enable
            ;;
        *)
            echo "Aborting."
            exit 4
            ;;
    esac
fi

if [ -z "$(ps aux | grep dbus | grep -v grep)" ]; then
    echo "Starting dbus service"
    service dbus start
fi

# 检查 nullfs
if [ "$(sysrc -f /boot/loader.conf -qn nullfs_load)" != "YES" ]; then
    echo "nullfs should be loaded on boot. Set nullfs_load to YES? (Y|n)"
    read ANSWER
    case $ANSWER in
        [Nn][Oo]|[Nn])
            echo "Warning: You MUST load nullfs with \"kldload -v nullfs\" EACH TIME FreeBSD IS REBOOTED."
            echo "Sure to continue without setting nullfs_load to YES? (y|N)"
            read ANSWER
            case $ANSWER in
                [Yy][Ee][Ss]|[Yy])
                    echo "Warning: nullfs_load not set"
                    ;;
                [Nn][Oo]|[Nn]|"")
                    echo "Setting nullfs_load to YES"
                    sysrc -f /boot/loader.conf nullfs_load="YES"
                    ;;
                *)
                    echo "Aborting."
                    exit 4
                    ;;
            esac
            ;;
        [Yy][Ee][Ss]|[Yy]|"")
            echo "Setting nullfs_load to YES"
            sysrc -f /boot/loader.conf nullfs_load="YES"
            ;;
        *)
            echo "Aborting."
            exit 4
            ;;
    esac
fi

if ! kldstat -n nullfs >/dev/null 2>&1; then
    echo "Loading nullfs module"
    kldload -v nullfs
fi

# 下载和解压基本系统
echo "${DIST_FULLNAME} will be installed in ${ROOT_DIR}/${DIST}"
echo "Downloading basic system"
fetch ${URL}/${FILE}

echo "Extracting basic system"
sleep 0.5
mkdir -p ${ROOT_DIR}/${DIST}
tar xvpf ${FILE} ${SUBDIR:-} -C ${ROOT_DIR}/${DIST} --numeric-owner

# 挂载必要文件系统
echo "Mounting required file system"
sleep 0.5
echo "devfs	${ROOT_DIR}/${DIST}/dev	devfs	rw,late	0	0" >> /etc/fstab
echo "tmpfs	${ROOT_DIR}/${DIST}/dev/shm	tmpfs	rw,late,size=1g,mode=1777	0	0" >> /etc/fstab
echo "fdescfs	${ROOT_DIR}/${DIST}/dev/fd	fdescfs	rw,late,linrdlnk	0	0" >> /etc/fstab
echo "linprocfs	${ROOT_DIR}/${DIST}/proc	linprocfs	rw,late	0	0" >> /etc/fstab
echo "linsysfs	${ROOT_DIR}/${DIST}/sys	linsysfs	rw,late	0	0" >> /etc/fstab
echo "/tmp	${ROOT_DIR}/${DIST}/tmp	nullfs	rw,late	0	0" >> /etc/fstab
mount -al

# 配置 DNS
echo "Should ${DIST_FULLNAME} use Alibaba DNS or local resolv.conf? ((A)li | (L)ocal | (C)ancel)"
read ANSWER
case $ANSWER in
    [Aa][Ll][Ii]|[Aa]|"")
        echo "Setting Alibaba DNS"
        echo "nameserver 223.5.5.5" >> ${ROOT_DIR}/${DIST}/etc/resolv.conf
        DNS_CONFIGURED=1
        ;;
    [Ll][Oo][Cc][Aa][Ll]|[Ll])
        echo "Using local resolv.conf"
        cp /etc/resolv.conf ${ROOT_DIR}/${DIST}/etc/resolv.conf
        DNS_CONFIGURED=1
        ;;
    *)
        echo "Canceled."
        echo "You have to edit ${ROOT_DIR}/${DIST}/etc/resolv.conf by yourself!"
        DNS_CONFIGURED=0
        ;;
esac

# 更新、升级
if [ $DNS_CONFIGURED = 1 ]; then
    [ $UPDATE = 1 ] && chroot ${ROOT_DIR}/${DIST} /bin/bash -c "${UPDATE_CMD}"
    [ $UPGRADE = 1 ] && chroot ${ROOT_DIR}/${DIST} /bin/bash -c "${UPGRADE_CMD}"
else
    echo "DNS not configured, skipping upgrade."
fi

# 设置默认 bash 配置
cp ${ROOT_DIR}/${DIST}/etc/profile ${ROOT_DIR}/${DIST}/root/.bashrc

# 清理
echo "Cleaning up"
rm ${FILE}

echo "Done."
echo "Now you can switch to ${DIST_FULLNAME} with \"chroot ${ROOT_DIR}/${DIST} /bin/bash\""
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/di-11-zhang-linux-er-jin-zhi-jian-rong-ceng/di-11.5-jie-slackware-linux-jian-rong-ceng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
