智简魔方文档

5.5 解决debian+ubuntu16以上开机脚本不生效

本文介绍如何在智简魔方 DCIM 系统中处理 Debian 和 Ubuntu 16 及以后版本的开机脚本不生效问题,重点说明安装环境的文件路径与脚本指令限制。

A、Debian 开机脚本不生效

关键点

所有涉及操作系统文件的路径,都必须在根路径前加上 /target。Debian 安装过程中,由于系统尚未完成安装,其根目录位于 /target

例如,需要在开机脚本中创建 /root/123.txt 文件时,应使用以下路径:

# 错误:不能直接使用真实根路径
touch /root/123.txt

# 正确
touch /target/root/123.txt

不要将需要永久保留的文件创建在 /tmp 目录中。重装完成后系统会重启,并删除 /tmp 目录下的文件。

案例:为 Debian 11 镜像更换国内源

更换国内源时,文件路径需要以 /target 开头:

cat > /target/etc/apt/sources.list << EOF
deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
deb http://deb.debian.org/debian bullseye-backports main
EOF

为 Debian 11 安装软件时,可执行:

chroot /target && apt update
chroot /target && apt install mtr -y

也可使用以下方式:

chroot /target apt update
chroot /target apt install mtr -y

Debian KS 自动应答脚本参考

https://github.com/oxypwn/kickstart/blob/master/debian/debian.ks

B、Ubuntu 16 及以后版本 开机脚本不生效

关键点

自定义脚本中不能使用 echocatprintftee>>> 这 6 个指令或重定向符。

存在的问题

  1. d-i preseed/late_command 命令不能使用 echocatprintftee>>> 这 6 个指令或重定向符。
touch /etc/sysctl.d/custom.conf
echo 'net.core.default_qdisc=fq' > /etc/sysctl.d/custom.conf

上述命令中,只有 touch 能成功创建文件,echo 无法成功写入内容。

  1. sed 不能向空文件写入内容,这是 sed 自身的问题,不是 Ubuntu 的问题。

解决方法

可以使用 cpsed 等 Shell 指令完成文件创建和内容写入。

如果不创建新文件,可以通过以下脚本在 Ubuntu 18 安装完成后开启 BBR:

sed -i '$a\net.ipv4.tcp_ecn = 1' /etc/sysctl.conf
sed -i '$a\net.core.default_qdisc = fq' /etc/sysctl.conf
sed -i '$a\net.ipv4.tcp_congestion_control = bbr' /etc/sysctl.conf

如果需要创建新文件,可参考以下脚本,在 Ubuntu 18 安装完成后开启 BBR:

cp -a /etc/hosts /etc/sysctl.d/custom.conf
sed -i '2,$d' /etc/sysctl.d/custom.conf
sed -i '1s\.*\net.ipv4.tcp_ecn = 1\' /etc/sysctl.d/custom.conf
sed -i '$a\net.core.default_qdisc = fq' /etc/sysctl.d/custom.conf
sed -i '$a\net.ipv4.tcp_congestion_control = bbr' /etc/sysctl.d/custom.conf