1. 前言
Debian 的自动化安装主要通过 Preseed 技术实现,它允许用户在安装过程中自动填写配置参数,从而实现无人值守的全自动安装。Preseed 文件是一个包含安装选项的配置文件,适用于大规模部署或需要重复安装的场景(如服务器、虚拟机等)。
当前稳定版本的 Preseed 示例文件可从官方网站下载:https://www.debian.org/releases/stable/example-preseed.txt
其他版本示例文件可将链接中的 ‘stable’ 替换成目标版本代号即可,如 Debian 11 的版本代号为 bullseye,则链接为:https://www.debian.org/releases/bullseye/example-preseed.txt
2. 定制 preseed 文件
Preseed 示例文件中有完整的注释,本文不会一一说明,只说明一些在使用的时候需要注意的问题。
2.1. 语言、国家、本地化
示例文件的开头,有这样一段配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# Preseeding only locale sets language, country and locale.
d-i debian-installer/locale string en_US
# The values can also be preseeded individually for greater flexibility.
#d-i debian-installer/language string en
#d-i debian-installer/country string NL
#d-i debian-installer/locale string en_GB.UTF-8
# Optionally specify additional locales to be generated.
#d-i localechooser/supported-locales multiselect en_US.UTF-8, nl_NL.UTF-8
# Keyboard selection.
d-i keyboard-configuration/xkb-keymap select us
# d-i keyboard-configuration/toggle select No toggling
|
在实际使用的过程中发现,该配置并不会生效,在启动自动安装时,依然会询问并让用户选择语言、国家、本地化这些信息。
解决方法是在命令行参数中修改这些值,后文会具体说。
2.2. hostname
1
2
3
4
5
6
7
8
9
10
|
# Any hostname and domain names assigned from dhcp take precedence over
# values set here. However, setting the values still prevents the questions
# from being shown, even if values come from dhcp.
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
# If you want to force a hostname, regardless of what either the DHCP
# server returns or what the reverse DNS entry for the IP is, uncomment
# and adjust the following line.
#d-i netcfg/hostname string somehost
|
一开始我将 d-i netcfg/get_hostname string unassigned-hostname
这一行注释了,使用最好一行中的 d-i netcfg/hostname string somehost
设置 hostname。但实际安装时,依然会询问并让用户输入 hostname。
实际上只要使用 d-i netcfg/get_hostname string unassigned-hostname
这一行的配置设置 hostname 即可。
2.3. 硬盘分区
如果有多块硬盘(包括U盘),需要手动指定要分区的硬盘,我这边是选择第一块 NVMe 硬盘:
1
|
d-i partman-auto/disk string /dev/nvme0n1
|
分区方案配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
d-i partman-auto/expert_recipe string \
boot-root :: \
525 525 525 free \
$iflabel{ gpt } \
$primary{ } $bootable{ } \
method{ efi } format{ } \
. \
500 1024000 -1 ext4 \
$primary{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } \
. \
8590 8590 8590 linux-swap \
$primary{ } \
method{ swap } format{ } \
.
|
此处配置的分区方案为:500MiB EFI 分区,8GiB Swap 分区,剩余空间为根文件系统。
配置中的数字为 MB 单位,如 500 表示 500MB。3个数字分别表示最小大小、优先级大小、最大大小。负数表示剩余空间。安装程序会根据磁盘空间和优先级大小来决定分区的实际大小。
此处说明一下,为什么 500MiB 的分区大小要写成 525,这里涉及到单位转换的问题。1MB = 10^6 Byte,1MiB = 2^20 Byte,所以 525 ≈ 500 * 2^20 / 10^6。
2.4. 软件包选择
1
2
3
4
5
6
|
### Package selection
tasksel tasksel/first multiselect standard, web-server, kde-desktop
# Or choose to not get the tasksel dialog displayed at all (and don't install
# any packages):
#d-i pkgsel/run_tasksel boolean false
|
此处选择需要安装的其他软件包,如 web-server、kde-desktop 等等。
1
2
|
# Individual additional packages to install
d-i pkgsel/include string vim curl wget
|
此处选择需要额外安装的软件包,如 vim、curl、wget 等等。
2.5. 遥测数据收集
1
2
3
4
5
|
# You can choose, if your system will report back on what software you have
# installed, and what software you use. The default is not to report back,
# but sending reports helps the project determine what software is most
# popular and should be included on the first CD/DVD.
#popularity-contest popularity-contest/participate boolean false
|
这里默认是注释掉的,可以将最后一行的注释符号去掉,不然在安装过程中会提示用户选择是否愿意收集遥测数据。
2.6. 安装前命令
1
2
3
|
# This first command is run as early as possible, just after
# preseeding is read.
#d-i preseed/early_command string anna-install some-udeb
|
该配置是在安装前(在 preseed 文件读取后)将要执行的命令。
在实际使用过程中,发现如果待安装的硬盘中已经存在 EFI 分区,安装系统会自动将 EFI 分区挂载到 /media
,导致在执行分区操作时会出现分区失败的情况,此处可以使用以下安装前命令来解决:
1
|
d-i preseed/early_command string umount /media || true;
|
命令中 || true
是为了解决执行命令时,如果没有挂载 EFI 分区,会报错,所以加上 || true
是为了忽略报错,防止自动安装中断。
2.7. 安装后命令
1
2
3
4
5
6
7
8
9
10
|
# This command is run immediately before the partitioner starts. It may be
# useful to apply dynamic partitioner preseeding that depends on the state
# of the disks (which may not be visible when preseed/early_command runs).
#d-i partman/early_command \
# string debconf-set partman-auto/disk "$(list-devices disk | head -n1)"
# This command is run just before the install finishes, but when there is
# still a usable /target directory. You can chroot to /target and use it
# directly, or use the apt-install and in-target commands to easily install
# packages and run commands in the target system.
#d-i preseed/late_command string apt-install zsh; in-target chsh -s /bin/zsh
|
该配置是在安装结束前将要执行的命令。此时 /target 目录是可用的,可以使用 chroot 命令进入 /target 目录,使用 apt-install 和 in-target 命令来安装软件和运行命令。
例如可以预先设置 apt 的源(因为安装结束后,源可能是 CDROM),使用以下命令:
1
2
3
4
5
6
7
8
9
|
d-i preseed/late_command string \
in-target bash -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list'; \
in-target bash -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian bookworm main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports main contrib non-free non-free-firmware" >> /etc/apt/sources.list'; \
in-target bash -c 'echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list';
|
3. 执行无人值守安装
进行无人值守安装时,Preseed 文件可以从 http 服务获取,也可以从文件系统中获取,也可以放在 initrd.gz
中。
如果放在 initrd.gz
中,则每次修改 preseed 文件后,都要重新制作 initrd.gz
,比较麻烦。
本人将 Debian 12 的 ISO 文件解压到 U 盘进行无人值守安装,所以将 preseed 文件放在 U 盘的根目录下。
安装引导启动时,会将 U 盘挂载到 /cdrom
。在首屏安装方式选择时,按 C
快捷键,编辑启动参数,原始的启动参数可能为:
1
|
/install.amd/vmlinuz vga=788 initrd=/install.amd/gtk/initrd.gz --- quiet
|
修改为如下:
1
|
/install.amd/vmlinuz vga=788 initrd=/install.amd/gtk/initrd.gz auto file=/cdrom/preseed.cfg --- quiet
|
如果使用 http 服务器获取 preseed 文件,则修改为如下:
1
|
/install.amd/vmlinuz vga=788 initrd=/install.amd/gtk/initrd.gz auto url=/cdrom/preseed.cfg --- quiet
|
其中 auto
为 auto-install/enable
的别名,file
为 preseed/file
的别名,url
为 preseed/url
的别名。
但此时还不够,会出现 2.1. 语言、国家、本地化 中说明的问题,会询问用户选择语言、国家、本地化这些,此时需要在启动参数中增加额外的参数,完整的启动参数为:
1
|
linux /install.amd/vmlinuz vga=788 language=en country=CN locale=en_US.UTF-8 keymap=us priority=critical auto file=/cdrom/preseed.cfg --- quiet
|
最后按照提示,按下快捷键 CTRL+X
使用修改的参数进行启动,即可开始无人值守安装。
3.1. 额外的配置
每次执行无人值守安装时,都需要手动修改启动参数,甚是麻烦,所以我们可以修改 grub 的配置文件,增加一个安装菜单选项。
grub 配置文件位于 [ISO 镜像文件根目录]/boot/grub/grub.cfg
,在其中增加一个拥有子菜单的菜单项,这样我们可以通过子菜单,选择不同的 preseed 无人值守安装配置,示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
submenu --hotkey=m 'Automated install ...' {
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue
set theme=/boot/grub/theme/1-1
set gfxpayload=keep
menuentry 'Configuration 1' {
set background_color=black
linux /install.amd/vmlinuz vga=788 language=en country=CN locale=en_US.UTF-8 keymap=us priority=critical auto file=/cdrom/preseed/conf1.cfg --- quiet
initrd /install.amd/initrd.gz
}
menuentry 'Configuration 2' {
set background_color=black
linux /install.amd/vmlinuz vga=788 language=en country=CN locale=en_US.UTF-8 keymap=us priority=critical auto file=/cdrom/preseed/conf2.cfg --- quiet
initrd /install.amd/initrd.gz
}
}
|
这样在安装引导启动时,就可以直接进入子菜单进行选择,而无需每次手动修改启动参数。
4. 参考链接
- https://wiki.debian.org/DebianInstaller/Preseed
- https://www.debian.org/releases/stable/amd64/apbs02.zh-cn.html
- https://www.debian.org/releases/stable/example-preseed.txt