使用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...

相关推荐
会飞的小蛮猪2 小时前
Prometheus运维(Prometheus基础结构组件)
自动化运维
一只拉古10 小时前
DevOps 基础到精通 - 宏观概览
自动化运维·devops
一只拉古2 天前
DevOps 的 Linux 基础(第一部分)
linux·自动化运维·devops
SirLancelot13 天前
K8s-kubernetes(二)资源限制-详细介绍
微服务·云原生·容器·kubernetes·k8s·devops·kubelet
数据智能老司机5 天前
AI 原生软件交付——混沌工程与服务可靠性
aigc·devops·aiops
数据智能老司机5 天前
基于 Kubernetes 的平台工程——云原生应用的挑战
云原生·kubernetes·devops
数据智能老司机5 天前
基于 Kubernetes 的平台工程——Kubernetes 上的平台化浪潮
kubernetes·云计算·devops
苦逼IT运维5 天前
Jenkins + SonarQube 从原理到实战三:SonarQube 打通 Windows AD(LDAP)认证与踩坑记录
运维·服务器·windows·docker·云计算·jenkins·devops
vivo互联网技术5 天前
vivo Pulsar 万亿级消息处理实践(4)-Ansible运维部署
大数据·ansible·自动化运维·pulsar·消息处理·分布式消息中间件