#!/bin/sh
rm -rf /boot/debian

apt install wget -y

mkdir /boot/debian && cd /boot/debian

wget http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz

wget http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux

gunzip initrd.gz

touch preseed.cfg

cat > /boot/debian/preseed.cfg << "EOF"
# 配置语言
d-i debian-installer/language string en
d-i debian-installer/country string US
d-i debian-installer/locale string en_US.UTF-8
d-i lowmem/low note

# 配置键盘
d-i keyboard-configuration/xkb-keymap select us

# 使用自动的方式配置网络（DHCP）
d-i netcfg/choose_interface select auto

# 配置hostname和domain
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/hostname string debian

# 配置软件源
d-i mirror/country string manual
d-i mirror/http/hostname string ftp.us.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string

# 开启root登录并设置root密码，关闭普通用户创建
d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password password aliwap.cf
d-i passwd/root-password-again password aliwap.cf

# 设置时区为东八区
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean false

# 硬盘分区，注意这里的设备名，要改为你自己的设备名，一般虚拟机如VMware/VirtualBox都是/dev/sda，VPS是/dev/vda
d-i partman-auto/disk string /dev/vda
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic

# 如果硬盘内之前有lvm或是raid的分区，全部删除
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/device_remove_lvm 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

# 禁止在安装的时候弹出CD/DVD扫描提示
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib 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

# 软件包选择
tasksel tasksel/first multiselect standard

# 安装额外的软件包，不更新系统
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select none

# 禁止在安装的时候弹出popularity
popularity-contest popularity-contest/participate boolean false

# grub安装
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

# 允许SSH服务使用root登录
d-i preseed/late_command string in-target sed -i 's/#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config

EOF

echo preseed.cfg | cpio -H newc -o -A -F /boot/debian/initrd

gzip /boot/debian/initrd

cat > /etc/grub.d/40_custom << "EOF"
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Debian 11 install' {
set root='hd0,msdos1'
linux /boot/debian/linux
initrd /boot/debian/initrd.gz
}
EOF

update-grub

grub-reboot "Debian 11 install"






