使用preseed安装debian系统

问题

在安装debian系统时,有时候会遇到installer包不能满足安装需求的情况,比如installer自带的raid驱动包不适配特定的raid卡,导致磁盘分区无法完成。有时候会需要在安装系统后,手动执行某些操作或者安装某些特定软件,比如允许远程登录root账号。

这些操作可以通过额外的操作来完成,比如在机器上插入携带了适配特定raid卡驱动包的USB盘;或者在装完系统后,修改/etc/sshd_config文件,取消对#PermitRootLogin yes的注释。

但是如果你要在几十台,甚至上百台机器上安装操作系统并解决上面的问题,那操作过程无疑是重复且烦人的。

Preseed使用方法

好在debian提供了preceed,可以帮助我们把一些烦人且重复的操作自动化。接下来我们就以上面的两个例子为例,来看看如何使用preseed来自动化安装raid驱动和允许远程登录root账号。

环境准备

下面演示如何在debian10 installer上使用preseed

  1. 准备一个debian10的环境(虚拟机或者云机都可以,方便起见需要连通公网)。
  2. 下载buster DVD镜像
bash 复制代码
wget https://cdimage.debian.org/cdimage/archive/10.13.0/amd64/iso-dvd/debian-10.13.0-amd64-DVD-1.iso
  1. 准备raid卡驱动程序包(这里用博通提供的07.727.03版本的megaraid_sas驱动)
bash 复制代码
wget https://docs.broadcom.com/docs-and-downloads/07.727.03.00-1_MR-linuxdrv_rel.zip
apt-get install unzip build-essential ctags linux-headers-$(uname -r)
unzip 07.727.03.00-1_MR-linuxdrv_rel.zip
cd 07.727.03.00-1_MR-linuxdrv_rel
tar -xvzf megaraid_sas_components.tgz
tar -zxvf megaraid_sas-07.727.03.00-src.tar.gz
cd megaraid_sas-07.727.03.00
# 可能会提示没有clean.sh,可以不用管
./compile.sh
modinfo megaraid_sas.ko
# 把ko文件放到/tmp目录下
cp megaraid_sas.ko /tmp/megaraid_sas.ko
  1. 创建文件/tmp/preseed.cfg,内容如下
bash 复制代码
d-i keyboard-configuration/xkb-keymap select us
d-i debian-installer/language string en
d-i debian-installer/country string CN
d-i debian-installer/locale select en_US.UTF-8
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string uk

d-i clock-setup/utc boolean true
d-i time/zone select Asia/Shanghai
d-i clock-setup/ntp boolean false

d-i passwd/root-password password please_change_me
d-i passwd/root-password-again password please_change_me
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
d-i passwd/user-fullname string tiger
d-i passwd/username string tiger
d-i passwd/user-password password please_change_me
d-i passwd/user-password-again password please_change_me
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false

d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false

d-i apt-setup/disable-cdrom-entries boolean true
d-i apt-setup/use_mirror boolean false

tasksel tasksel/first multiselect standard ssh-server
popularity-contest popularity-contest/participate boolean false

d-i preseed/late_command string \
    cp -a /lib/modules/4.19.0-21-amd64/kernel/drivers/scsi/megaraid/megaraid_sas.ko /target/lib/modules/4.19.0-21-amd64/kernel/drivers/scsi/megaraid/megaraid_sas.ko ; \
    in-target sed -i -r 's/^#?PermitRootLogin.*$/PermitRootLogin yes/' /etc/ssh/sshd_config ; \
    in-target echo "megaraid_sas" >> /etc/initramfs-tools/modules ; \
    in-target update-initramfs -u 

上面的内容重点看第33-36行,执行一些脚本,将镜像中的megaraid_sas.ko拷贝到目标机器中,并且修改/etc/ssh/sshd_config配置,允许远程登录root账户。

  1. 打包新的iso镜像
bash 复制代码
apt-get install bsdtar xorriso
mkdir cd
bsdtar -xf debian-10.13.0-amd64-DVD-1.iso -C cd/
dd if=debian-10.13.0-amd64-DVD-1.iso bs=1 count=432 of=isohdpfx.bin
mkdir modified_initrd
mkdir modified_gtk_initrd
mkdir modified_xen_initrd
# 修改initrd.gz文件
cd modified_initrd
zcat ../cd/install.amd/initrd.gz | cpio -idmv
cp /tmp/megaraid_sas.ko lib/modules/4.19.0-21-amd64/kernel/drivers/scsi/megaraid/megaraid_sas.ko
cp /tmp/preseed.cfg .
find . | cpio -o  -R root:root --format='newc' | gzip -9 > /tmp/initrd.gz
# 修改gtk/initrd
cd ../modified_gtk_initrd
zcat ../cd/install.amd/gtk/initrd.gz | cpio -idmv
cp /tmp/megaraid_sas.ko lib/modules/4.19.0-21-amd64/kernel/drivers/scsi/megaraid/megaraid_sas.ko
# 把preseed.cfg文件放到目录下
cp /tmp/preseed.cfg .
find . | cpio -o  -R root:root --format='newc' | gzip -9 > /tmp/gtk_initrd.gz
# 修改xen/initrd
cd ../modified_xen_initrd
zcat ../cd/install.amd/xen/initrd.gz | cpio -idmv
cp /tmp/megaraid_sas.ko lib/modules/4.19.0-21-amd64/kernel/drivers/scsi/megaraid/megaraid_sas.ko
find . | cpio -o  -R root:root --format='newc' | gzip -9 > /tmp/xen_initrd.gz
cd ..
cp /tmp/initrd.gz cd/install.amd/initrd.gz
cp /tmp/gtk_initrd.gz cd/install.amd/gtk/initrd.gz
cp /tmp/xen_initrd.gz cd/install.amd/xen/initrd.gz
# 更新md5.txt文件
cd cd
chmod +w md5sum.txt
find -follow -type f ! -name md5sum.txt -print0 | xargs -0 md5sum > md5sum.txt
chmod -w md5sum.txt
cd ..
# 打包新的镜像
xorriso -as mkisofs -r -V 'Debian 10.13.0 amd64 1 with raid driver' -o debian-10.13.0-amd64-DVD-1-with-broadcom.iso -J -joliet-long -cache-inodes -isohybrid-mbr isohdpfx.bin -b isolinux/isolinux.bin -c isolinux/boot.cat -boot-load-size 4 -boot-info-table -no-emul-boot -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot -isohybrid-gpt-basdat -isohybrid-apm-hfsplus cd

后面使用新打包出来的iso文件,就可以在安装buster的时候自动安装raid驱动并且允许远程登录root账户。

结语

上面利用preseed实现了一些debian installer不具备的功能。 preseed可以帮助安装过程中去掉一些问询,比如上面的preseed.cfg文件,会设置一些自动的回答,对于安装过程也可以节省时间

具体可以看 wiki.debian.org/DebianInsta...

相关推荐
令狐少侠201115 小时前
devops-Jenkins一键部署多台实例
运维·jenkins·devops
网络安全King5 天前
devops 工具 网络安全
运维·web安全·devops
梁萌5 天前
04-DevOps-安装并初始化Jenkins
运维·jenkins·devops
Anna_Tong6 天前
阿里云 ACS:高效、弹性、低成本的容器计算解决方案
人工智能·阿里云·容器·kubernetes·serverless·云计算·devops
ITPUB-微风6 天前
网易严选DevOps实践:从传统到云原生的演进
运维·云原生·devops
Incredibuild7 天前
深入了解 DevOps 基础架构:可追溯性的关键作用
ci/cd·devops
炸鸡物料库7 天前
Kubernetes 使用 Kube-Prometheus 构建指标监控 +飞书告警
运维·云原生·kubernetes·飞书·prometheus·devops
ITPUB-微风7 天前
云原生DevOps:Zadig架构设计与企业实践分析
运维·云原生·devops
m0_6845985310 天前
优化DevOps环境中的容器化交付流程:实践指南
运维·devops
Anna_Tong11 天前
DevOps 进化论:腾讯云 CODING-TM 如何助力企业敏捷转型?
运维·ci/cd·项目管理·腾讯云·敏捷开发·devops·代码托管