首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Debian10.7 自动化安装镜像制作

Debian10.7 自动化安装镜像制作

一、debian的自动化安装方式介绍

第一种、FAI方式自动化安装

FAI是用于Linux的无人值守大规模部署的工具

具体方法可以参考https://fai-project.org/其官方站点

(图片可放大查看)

其在线打包的功能相当方便

https://fai-project.org/FAIme/

(图片可放大查看)

第二种、preseed预置文件方式完成自动化安装

Debian 安装程序支持使用预先配置的文件(preseed)进行自动安装。preseed预置文件可以从网络或移动介质上加载,并自动回答安装过程中的问题

具体步骤:从原始的ISO映像中提取initrd,将预置文件preseed添加到initrd,最后再封装创建一个新可引导的自动化安装的ISO映像

(图片可放大查看)

(图片可放大查看)

Simple-CDD方式

还有一种是Simple-CDD方式 The All-In-One Solution

(图片可放大查看)

我采用的是第二种 在preseed.cfg脚本编写上踩坑无数,经过20多次的debian自动化安装镜像打包和安装验证测试, debian10.7自动化安装终于搞定了

二、Debian10.7 preseed自动化安装镜像制作步骤

具体的操作步骤如下

1、debian官网下载debian10.7的完整版镜像文件

https://cdimage.debian.org/debian-cd/current/amd64/bt-dvd/

  • 1)种子文件的方式下载更快
  • 2)只用下载第一张:DVD1

2、安装debian虚拟机

用于制作镜像 我这边安装成了图形化界面模式,当然安装命令行模式也是一样 安装过程就不详细描述了,参考之前的文章图解Debian10Linux系统的安装步骤

3、配置apt源,并安装镜像制作相关的软件包工具

Debian10.7的apt源配置参考之前的文章几种常用Linux系统的软件镜像源配置

安装镜像制作相关的软件包工具

代码语言:javascript
复制
 apt install vim lrzsz
 apt install debconf-utils
 apt install whois
 #[whois工具包用于mkpasswd生成root密码的密文]
 apt install bsdtar
 apt install genisoimage

4、preseed.cfg脚本

脚本主要说明

  • 1、网卡默认静态地址192.168.100.100/24
  • 2、主机名debian,域walkingcloud.cn
  • 3、时区Asia/Shanghai
  • 4、分区为LVM方案 *# /boot 500M ext4 *# /home 20G ext4 LVM *# / 20G+ ext4 LVM
  • 5、自动安装htop net-tools vim wget iftop iotop这些常用工具包
  • 6、preseed/late_command安装后执行命令 实现root用户可以直接SSH登录
  • 7、mkpasswd -m sha-512设置root用户密码

当然你可以根据自身需要进行定制,修改脚本

代码语言:javascript
复制
vi preseed.cfg
添加如下内容 
# US locale/kbd map
d-i debian-installer/locale string en_US
d-i keyboard-configuration/xkb-keymap select us

# automatically select network interface
d-i netcfg/choose_interface select auto
d-i netcfg/disable_dhcp boolean true
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/get_ipaddress string 192.168.100.100
d-i netcfg/get_nameservers string 223.5.5.5
d-i netcfg/get_netmask string 255.255.255.0
d-i netcfg/get_gateway string 192.168.100.254
d-i netcfg/confirm_static boolean true


# set host and domain
d-i netcfg/get_hostname string debian
d-i netcfg/get_domain string walkingcloud.cn

# disable WEP dialogue
d-i netcfg/wireless_wep string

# use http.us.debian.org as mirror with no proxy
d-i mirror/country string manual
d-i mirror/http/hostname string  mirrors.cloud.tencent.com
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string  

# don't make a regular user / set root password
d-i passwd/make-user boolean false
# mkpasswd -m sha-512 <pwd>
d-i passwd/root-password-crypted password $6$zRyZgBe.MEy$fDGbMeV/4DHSuVrCepJC6Md4adDkV9jKjAXaaEHBhx8xs/AmTcoBo80AsYC3gyuoJX/ww2zNGdi1oYQkkd9KN/
d-i passwd/root-login boolean true

d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean false
d-i clock-setup/ntp-server string cn.pool.ntp.org

### Partitioning
d-i preseed/early_command string anna-install fdisk-udeb 

# use lvm partitioning
d-i partman-auto/method string lvm
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true

# make lvm the max size
d-i partman-auto-lvm/guided_size string max
d-i partman-auto-lvm/new_vg_name string debian

# use the following partition scheme on /dev/sda
#d-i partman-auto/disk string /dev/sda
d-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n1)"

d-i partman-auto/choose_recipe select boot-lvm

# /boot 500M ext4
# /home 20G ext4
# / 20G+ ext4
d-i partman-auto/expert_recipe string               \
    boot-lvm ::                                     \
        500 500 500 ext4                            \
            $primary{ } $bootable{ }                \
            method{ format } format{ }              \
            use_filesystem{ } filesystem{ ext4 }    \
            mountpoint{ /boot }                     \
        .                                           \
        20480 20480 20480 ext4                            \
            $lvmok{ }                               \
            lv_name{ lv_home } in_vg { debian }     \
            $primary{ }                             \
            method{ format } format{ }              \
            use_filesystem{ } filesystem{ ext4 }    \
            mountpoint{ /home }                  \
        .                                           \
        20480 20480 -1 ext4                           \
            $lvmok{ }                               \
            lv_name{ lv_root } in_vg { debian }        \
            $primary{ }                             \
            method{ format } format{ }              \
            use_filesystem{ } filesystem{ ext4 }    \
            mountpoint{ / }                         \
        .

# remove any RAID partitioning
d-i partman-md/device_remove_md boolean true

# don't confirm anything
d-i partman-basicfilesystems/no_mount_point boolean false
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish

d-i partman-auto/purge_lvm_from_device  boolean true
d-i partman-lvm/device_remove_lvm       boolean true



d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

# disable swap warning
d-i partman-basicfilesystems/no_swap boolean false


# install standard system with ssh-server
tasksel tasksel/first multiselect standard, ssh-server

# also install other package
d-i pkgsel/include string htop net-tools vim wget  iftop  iotop 

# upgrade all packages
d-i pkgsel/upgrade select full-upgrade

# disable popularity contest
popularity-contest popularity-contest/participate boolean false

# Uncomment this if you don't want to use a network mirror.
#d-i apt-setup/use_mirror boolean false
d-i apt-setup/no_mirror     boolean true
d-i apt-setup/use_mirror    boolean false
d-i apt-setup/contrib       boolean false
d-i apt-setup/non-free      boolean false
d-i apt-setup/cdrom/set-next        boolean false
d-i apt-setup/services-select       multiselect

# force grub install to /dev/sda
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
#d-i grub-installer/bootdev  string /dev/sda
d-i grub-installer/bootdev  string default
#d-i grub-installer/early_command string debconf-set grub-installer/bootdev "$(list-devices disk | head -n1)"

# don't wait for confirm, just reboot when finished
d-i finish-install/reboot_in_progress note

d-i  preseed/late_command       string \
            cp -vrf /cdrom/tools/* /target/home/ ;  \
            sed -i "s/^#PermitRootLogin.*\$/PermitRootLogin yes/g" /target/etc/ssh/sshd_config ; 

其中mkpasswd -m sha-512 <pwd>用于获取root密码密文

(图片可放大查看)

5、iso制作脚本make_iso.sh

/opt/preseed_debian_iso_make目录下创建 make_iso.sh

(图片可放大查看)

脚本来源自github

代码语言:javascript
复制
#!/bin/bash
temp_dir="isofiles"

if [ $# -ne 2 ]; then
        echo usage $0 ISO preseed.cfg
        exit 1
fi

source_iso=$1
source_iso_name=$(basename $source_iso)
preseed=$2

[ -f "$source_iso" ] || (echo source_iso does not exist && exit 1)
[ -f "$preseed" ] || (echo preseed does not exist && exit 1)

if [ ! -d "$temp_dir" ]; then
        mkdir -p "$temp_dir"
        bsdtar -C "$temp_dir" -xf "$source_iso"
        gunzip "$temp_dir"/install.amd/initrd.gz
        echo "$preseed" | cpio -H newc -o -A -F "$temp_dir"/install.amd/initrd
        gzip "$temp_dir"/install.amd/initrd
fi

sed -i 's/^timeout.*/timeout 1/' "$temp_dir"/isolinux/isolinux.cfg
cat >"$temp_dir"/isolinux/menu.cfg <<EOF
menu hshift 4
menu width 70

menu title Debian GNU/Linux installer menu (BIOS mode)
include stdmenu.cfg
include txt.cfg
EOF

cat >"$temp_dir"/isolinux/txt.cfg <<EOF
label install
        menu label ^AutoInstall Debian10.7 DIY-OS v1(cdrom) 
        menu default
        kernel /install.amd/vmlinuz
        append vga=788 auto=true priority=critical file=/preseed.cfg initrd=/install.amd/initrd.gz --- quiet
default install
EOF
\cp -vrf packages_add/*.deb $temp_dir/tools/
\cp -vrf packages_add/*.sh  $temp_dir/tools/

md5sum `find $temp_dir -follow -type f` > "$temp_dir"/md5sum.txt
genisoimage -r -J -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o "preseed-$source_iso_name" "$temp_dir"/
#rm -r "$temp_dir"

其中packages_add目录为额外的工具包或者系统优化脚本

用于自动化安装完成后,若有需要,可以手动dpkg安装这些工具包以及执行该系统优化脚本

7、制作镜像

代码语言:javascript
复制
sh  -x make_iso.sh /root/debian-10.7.0-amd64-DVD-1.iso preseed.cfg

sh -x可以看到脚本执行过程

(图片可放大查看)

(图片可放大查看)

8、下载ISO镜像文件并创建虚拟机测试镜像

这里用SFTP方式下载打好的镜像包 preseed-debian-10.7.0-amd64-DVD-1.iso

(图片可放大查看)

创建虚拟机测试

(图片可放大查看)

9、全程自动化安装

从光盘启动,无需手工干预

安装过程截图

(图片可放大查看)

10、安装完成后登录验证

1)分区与网络

ip addr 可以看到分区与IP与定制的preseed.cfg文件中一致

(图片可放大查看)

2)htop工具

(图片可放大查看)

3)home目录也有额外的工具包或者系统优化脚本

代码语言:javascript
复制
cd /home 
dpkg --install lrzsz_0.12.21-10_amd64.deb ncdu_1.13-1+b1_amd64.deb nmon_16i+debian-2_amd64.deb

(图片可放大查看)

(图片可放大查看)

4)Env4Buster.sh 系统优化脚本

(图片可放大查看)

(图片可放大查看)

本文完成过程参考如下文档

  • 1、https://salsa.debian.org/installer-team/debian-installer/tree/master/doc/devel
  • 2、https://salsa.debian.org/installer-team/debian-installer/-/raw/master/doc/devel/partman-auto-recipe.txt
  • 3、preseed预置例子文件 https://www.debian.org/releases/buster/example-preseed.txt
  • 4、debian安装手册之使用预置自动进行安装 https://www.debian.org/releases/stable/amd64/ch04s06.zh-cn.html https://www.debian.org/releases/stable/amd64/apb.zh-cn.html https://www.debian.org/releases/stable/amd64/apbs04.zh-cn.html
  • 5、debian管理员手册及Preseed的wiki参考文档 https://wiki.debian.org/DebianInstaller/Preseed https://www.debian.org/doc/manuals/debian-handbook/sect.automated-installation.zh-cn.html https://wiki.debian.org/DebianInstaller/Preseed/EditIso
  • 6、使用 build-simple-cdd 快速定制 Debian 安装盘 https://my.oschina.net/anglix/blog/506702 制作Debian的Preseed自动安装U盘版 https://my.oschina.net/anglix/blog/501235
  • 7、https://github.com/Mazhive/fs-uae-autoboot-debian10
  • 8、打包测试中遇到问题时搜索到的相关问题链接https://askubuntu.com/questions/935565/install-openssh-server-package-from-preseed-file https://community.theforeman.org/t/no-root-file-system-is-defined-error-using-preseed-atomic-partitioning-scheme/5969/8
下一篇
举报
领券