首页 > 运维类 > LINUX运维 > Debian12无人值守自动安装镜像制作

190

浏览

0

评论

Debian12无人值守自动安装镜像制作

作者:stephen | 分类:LINUX运维 | 标签:

在批量创建虚拟机时,或者在无显示器、无键鼠的服务器上安装系统时,通常无法与操作系统的Installer交互。这时就需要制作一个无人值守的自动安装镜像,直接给机器一个空硬盘+挂载iso镜像,即可自动完成网络配置、自动安装软件包、打开ssh、创建指定用户名密码的用户。

首先准备一个Linux环境,我的是ubuntu,下载Debian12.0.0原盘镜像


cd ~

wget https://mirrors.huaweicloud.com/repository/debian-cd/12.0.0/amd64/iso-cd/debian-12.0.0-amd64-netinst.iso

注意这里注意只能下载Debian12.0.0,我尝试使用Debian12.7,使用本文的方法无法成功自动安装,会跳转到GUI安装界面,原因未知


接着创建一个iso挂载点和镜像修改目录(iso挂载后为只读,无法修改),挂载镜像并将镜像内部文件拷贝到镜像修改目录


mkdir mnt

mkdir isofiles

mount debian-12.0.0-amd64-netinst.iso mnt/

cp -rT mnt/ isofiles/

进入isofiles中,创建自动安装配置文件


cd isofiles

mkdir conf

vim auto.preseed

auto.preseed文件内容如下


### 本地化

# 中文

d-i debian-installer/locale string zh_CN.UTF-8

d-i keyboard-configuration/xkb-keymap select us

d-i localechooser/supported-locales multiselect en_US.UTF-8, zh_CN.UTF-8

 

 

### 网络

d-i netcfg/choose_interface select auto

d-i netcfg/enable boolean true

# 注意这里是你的主机名称

d-i netcfg/get_hostname string debian-server

d-i netcfg/get_domain string

 

 

### Network console

#d-i anna/choose_modules string network-console

#d-i network-console/password password passwd

#d-i network-console/password-again password passwd

 

 

### Mirror

d-i mirror/country string manual

d-i mirror/http/hostname string mirrors.aliyun.com

d-i mirror/http/directory string /debian

d-i mirror/http/proxy string

 

 

### Account setup

d-i passwd/root-login boolean true

d-i passwd/make-user boolean false

# 这里是你的root密码,输入两次

d-i passwd/root-password password 123456

d-i passwd/root-password-again password 123456

 

 

### 时区

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/disk string /dev/[sv]da

# 用第一块盘来操作分区

#d-i partman/early_command string \

#       ONEDISK="$(lsblk -l -n -o NAME -d -p | grep '/dev/vda\|/dev/nvme0n1\|/dev/sda\|/dev/xvda'|sort|tail -n 1)"; \

#       debconf-set partman-auto/disk "$ONEDISK"; \

#       debconf-set grub-installer/bootdev "$ONEDISK";

# 常规分区

d-i partman-auto/method string regular

# 禁止swap分区

d-i partman-basicfilesystems/no_swap boolean false

# 如果硬盘内之前有lvm或是raid的分区,全部删除

d-i partman-lvm/device_remove_lvm boolean true

d-i partman-md/device_remove_md boolean true

 

# 分区,一个boot分区最小512M,1优先级,最大1024M

d-i partman-auto/expert_recipe string                    \

      boot-root ::                                       \

              512 1 1024 ext4                            \

                 $primary{ } $bootable{ }                \

                 method{ format } format{ }              \

                 use_filesystem{ } filesystem{ ext4 }    \

                 mountpoint{ /boot }                     \

              .                                          \

              1 2 -1 xfs                             \

                  method{ format } format{ }             \

                  use_filesystem{ } filesystem{ xfs }    \

                  mountpoint{ / }                        \

              .                                         

 

# 非交互式完成分区

## 确认是否写入新的分区标签。设置为"true"表示允许写入新的分区标签。

d-i partman-partitioning/confirm_write_new_label boolean true

## 安装程序在选择分区时选择“完成”选项,即跳过手动选择分区的步骤。

d-i partman/choose_partition select finish

## 确认分区的操作。设置为"true"表示允许自动确认分区操作。

d-i partman/confirm boolean true

## 确认是否覆盖分区。设置为"true"表示允许自动确认分区操作

d-i partman/confirm_nooverwrite boolean true

 

### Apt setup

# 不启用非自由(non-free)软件包源。

d-i apt-setup/non-free boolean true

# 启用贡献(contrib)软件包源。

d-i apt-setup/contrib boolean true

# 选择了主要(main)软件包源,以确保安装基本软件。

d-i apt-setup/services-select multiselect main

# 允许在安装过程中使用未经身份验证的软件包源。

d-i debian-installer/allow_unauthenticated boolean true

# 光盘相关

d-i apt-setup/cdrom/set-first boolean false

d-i apt-setup/cdrom/set-next boolean false  

d-i apt-setup/cdrom/set-failed boolean false

 

 

### Package selection

# 标准安装

tasksel tasksel/first multiselect standard

d-i pkgsel/upgrade select none

d-i pkgsel/language-packs multiselect en, zh

d-i pkgsel/include string openssh-server

d-i pkgsel/update-policy select none

 

# 禁止在安装的时候弹出popularity

popularity-contest popularity-contest/participate boolean false

 

### Boot loader installation

d-i grub-installer/only_debian boolean true

d-i grub-installer/bootdev string /dev/[sv]da

 

# 安装完成之后不要弹出安装完成的界面,直接重启

d-i finish-install/reboot_in_progress note

 

# 允许ssh服务使用root用户登录

d-i preseed/late_command string in-target sed -i 's/#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config

接着使用gtk.cfg(GUI安装界面)模板创建一个自动安装的配置


cd ~/isofiles

cp isolinux/gtk.cfg isolinux/gtk_auto.cfg

chmod +x isolinux/gtk.cfg

vim isolinux/gtk.cfg

gtk.cfg的内容修改如下,把第一行的default installgui和第四行的menu default都删掉


label installgui

        menu label ^Graphical install

        kernel /install.amd/vmlinuz

        append vga=788 initrd=/install.amd/gtk/initrd.gz --- quiet

最后别忘了改回只读模式


chmod -x isolinux/gtk.cfg

接着修改刚刚创建的gtk_auto.cfg


chmod +x isolinux/gtk_auto.cfg

vim isolinux/gtk_auto.cfg

内容修改为


default autoinstallgui

label autoinstallgui

        menu label ^Auto Install

        menu default

        kernel /install.amd/vmlinuz

        append vga=788 initrd=/install.amd/gtk/initrd.gz auto=true file=/cdrom/conf/auto.preseed --- quiet

 

timeout 10

 

ontimeout /install.amd/vmlinuz vga=788 initrd=/install.amd/gtk/initrd.gz auto=true file=/cdrom/conf/auto.preseed --- quiet

接着


chmod +x isolinux/gtk_auto.cfg

接着菜单


chmod +x menu.cfg

vim menu.cfg

在include stdmenu.cfg下面添加一行


include stdmenu.cfg 

include gtk_auto.cfg #把刚刚创建的自动安装配置加进来 

include gtk.cfg

改回只读


chmod -x menu.cfg

重新生成md5sum.txt,否则验证会不通过


chmod +w md5sum.txt

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

chmod -w md5sum.txt

最后打包回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.0.0-amd64-netinst.iso isofiles


本文链接:https://www.stephenwxf.com/post/216.html
原创文章如转载请注明:转载自王显璠的个人博客谢谢!

Copyright Your stephenwxf.com Rights Reserved.