制作 Debian12 全自动安装镜像

参考 附录 B. 使用预置自动进行安装DebianInstaller/Preseed/EditIso - Debian Wiki

准备工作

安装依赖包

安装 genisoimage, xorriso, qemu-system, 分别用于制作镜像,解压镜像和测试镜像

apt install genisoimage xorriso qemu-system

下载镜像

wget https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.1.0-amd64-netinst.iso

解压镜像

xorriso -osirrox on -indev debian-12.*.iso -extract / isofiles

制作全自动安全镜像

创建自动安装配置文件

将下面的内容保存为 preseed.cfg。可以看到root密码是123123,非常简单,但我关闭了ssh的密码登录,只能公钥登录,所以不用担心。

#_preseed_V1
d-i debian-installer/language string en
d-i debian-installer/country string CN
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/xkb-keymap select us
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string debian
d-i netcfg/get_domain string 
d-i netcfg/wireless_wep string
d-i mirror/protocol string http
d-i mirror/country string maunal
d-i mirror/http/hostname string mirrors.pku.edu.cn
d-i mirror/http/directory string /debian/
d-i mirror/http/proxy string
d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password password 123123
d-i passwd/root-password-again password 123123
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean true
d-i partman-auto/method string regular
d-i partman-auto-lvm/guided_size string max
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman-md/confirm boolean true
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i apt-setup/cdrom/set-first boolean false
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
d-i apt-setup/disable-cdrom-entries boolean true
d-i apt-setup/services-select multiselect security, updates, backports
d-i apt-setup/enable-source-repositories boolean false
d-i apt-setup/security_host string security.debian.org
d-i debian-installer/allow_unauthenticated boolean false
tasksel tasksel/first multiselect standard, ssh-server
d-i pkgsel/include string vim curl wget git htop sudo tmux qemu-guest-agent
d-i pkgsel/upgrade select full-upgrade
popularity-contest popularity-contest/participate boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i grub-installer/bootdev  string default
d-i finish-install/reboot_in_progress note
d-i cdrom-detect/eject boolean true
d-i preseed/late_command string \
  in-target mkdir -p /target/root/.ssh; \
  in-target chmod 700 /target/root/.ssh; \
  in-target chown root:root /target/root/.ssh; \
  >>/target/root/.ssh/authorized_keys echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCtWqoD3y180ud5b9JcTkeX3Xl7GqvB1BD9AjEQxHqiXXYgzS52ZOMEjXB6OF5RRY7QhP1UnziaEru9At2Sw0sDwwrsUYLDpVIAM9fqpiLxbuuoUJTlG20uR3KoIXvwYheFV/g0Dp88F1bBtxs1ER52rW5JXaycNaaOwvNXM4Zv53h8g2by52tRavp1AxABQ+gZo3Ptl9LeNoJY0s7NsNmQe4rsvxO6suJBM6lf7bnT6ZqcXHCa5T5Co1BbD2omaTSK243LzRCRG+cMuSfew5szRmAFAVVdpcHhvNTg8ZjNi+ZIfrbG282u/XzIuDjoh35UrmrmZYd4JxyaUpGmzG1MkbmkrjtpIcqr6byatHRXIXdh2JAEXZtkqUUVwwreVdSIo4nbPWUWNP9PJp+OcYO0NmN8L0CLI2HKsNp4SAJ89i1SRTOPn0p2M+HF3Se6YjDGvYpOiTvF9Udqgp94+Ux9xIPdSPsI+k6AdxrNI2YI9IsW8nQtVjD9VJVFwD92xWc0sM7FB2/7E28P4SKvzBrgEuheeOdkD0y8J0DOOwZo3HC2LkfpMYC8RY9gZBhwc5zq/56EEkc5xY0+fx3CO9tTOMVnwJXsvanqofAXJyPqspnQewAUKhTH75B0XDetom3CLyh51F7kzSMvKiFLBYhGfGsuRoNWO4iBG783mmBaXQ== cardno:18_042_835; \
  >>/target/etc/ssh/sshd_config.d/root.conf echo PermitRootLogin prohibit-password; \
  >>/target/etc/ssh/sshd_config.d/root.conf echo PasswordAuthentication no; \
  >>/target/etc/ssh/sshd_config.d/root.conf echo PubkeyAuthentication yes; \
  >>/target/etc/ssh/sshd_config.d/root.conf echo AuthorizedKeysFile .ssh/authorized_keys;

将预设文件添加到Initrd

chmod +w -R isofiles/install.amd/
gunzip isofiles/install.amd/initrd.gz

echo preseed.cfg | cpio -H newc -o -A -F isofiles/install.amd/initrd

gzip isofiles/install.amd/initrd
chmod -w -R isofiles/install.amd/

修改启动屏幕超时

修改启动参数

vim isofiles/isolinux/isolinux.cfg

默认超时为0,这表示一直等待到用户操作,这里超时改为5秒,表示5秒之后自动开始安装

#timeout 0
timeout 5

修改启动屏幕默认菜单

默认菜单选项是图形化安装,这里取消默认,修改图形化安装菜单

vim isofiles/isolinux/gtk.cfg

将内容改成以下

label installgui
        menu label ^Graphical install
        kernel /install.amd/vmlinuz
        append vga=788 initrd=/install.amd/gtk/initrd.gz --- quiet

把文本安装改为默认菜单选项,修改文本安装菜单

vim isofiles/isolinux/txt.cfg

将内容改成以下

default install
label install
        menu label ^Install
        menu default
        kernel /install.amd/vmlinuz
        append vga=788 initrd=/install.amd/initrd.gz --- quiet
timeout 300
ontimeout /install.amd/vmlinuz vga=788 initrd=/install.amd/initrd.gz --- quiet
menu autoboot Press a key, otherwise txt auto install will be started in # second{,s}...

删除默认30s倒计时启动项

rm isofiles/isolinux/spkgtk.cfg

重新生成md5sum.txt

cd isofiles
chmod +w md5sum.txt

find -follow -type f ! -name md5sum.txt -print0 | xargs -0 md5sum > md5sum.txt

chmod -w md5sum.txt
cd ..

创建新的可启动ISO镜像

genisoimage -r -J -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o preseed-debian-12.1.0-amd64-netinst.iso isofiles

此时已经制作好了全自动安装镜像,可以用虚拟机测试一下,测试成功则可以传到服务器上进行安装了。

一键脚本

在当前目录准备好 Debian 安装镜像,preseed.cfg配置文件,以及修改后的 gtk.cfg, txt.cfg, isolinux.cfg 文件,然后执行下面的脚本即可。

#/bin/bash

set -ex

rm -rf isofiles
xorriso -osirrox on -indev debian-12.1.0-amd64-netinst.iso -extract / isofiles

chmod +w -R isofiles/install.amd/
gunzip isofiles/install.amd/initrd.gz

echo preseed.cfg | cpio -H newc -o -A -F isofiles/install.amd/initrd

gzip isofiles/install.amd/initrd
chmod -w -R isofiles/install.amd/

cp ./isolinux.cfg isofiles/isolinux/isolinux.cfg
cp ./gtk.cfg isofiles/isolinux/gtk.cfg
cp ./txt.cfg isofiles/isolinux/txt.cfg
rm isofiles/isolinux/spkgtk.cfg

cd isofiles
chmod +w md5sum.txt
find -follow -type f ! -name md5sum.txt -print0 | xargs -0 md5sum > md5sum.txt
chmod -w md5sum.txt
cd ..

genisoimage -r -J -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o preseed-debian-12.1.0-amd64-netinst.iso isofiles
最后修改:2023 年 10 月 07 日
如果觉得我的文章对你有用,请随意赞赏