ubuntu18 uses preseed file to customize ISO image to realize automatic installation

need

Usually, when installing the ubuntu operating system, you need to manually select the problems that arise during the installation process, such as the settings of the region, network, and partition. However, when operating systems need to be installed in large quantities, the manual method will be cumbersome and inefficient, so there is a need for automated installation.

The solution provided by ubuntu is to directly set the answer to the above questions by setting the preseed file or directly skip the method to achieve a fully automated installation (Note: the official download ubuntu-18.04.5-server-amd64.isoinstallation is automatically installed unless there are special circumstances, because the built-in preseed file)

The main problems to be solved in this production are:

  • 1. Multi-network card automatic selection
  • 2. Automatic selection of multiple hard disks
  • 3. Custom shell script execution
  • 4. grub automatically chooses to install
  • There are no special instructions for other configurations, which are general content.
    Look at the location of the preseed file in the iso.
    insert image description here
    This is the content contained in the iso image
    insert image description here
    . Except for custom.seed, others are self-contained

Note: This installation is to realize the automatic installation system by adding the preseed file and rebuilding the iso image (that is, by means of CD)

environment

1. One server (virtual machine), which can be connected to
the Internet 2. Operating system: ubuntu-18.04.5-server-amd64
3. ISO image: ubuntu-18.04.5-server-amd64.iso
4. vmware-15.5
5. Production system architecture: amd64 (the next step is to produce arm)

accomplish

Description of the customization process

1. Copy the contents of the ISO
2. Create a custom.seed file with a custom name
3. Modify isolinux.cfg to recognize the custom preseed file
4. Modify grub.cfg to recognize the custom preseed file
5 , Apt offline source production and shell script writing
6. Create ISO, the tool used is mkisofs

Implementation

1. Copy ubuntu-18.04.5-server-amd64.isothe image to any path on the server. This article is /home/hybased on the directory

2. Mount and copy the iso file

mkdir /home/hy/mnt  # 用于挂载iso镜像
mkdir /home/hy/livecd  # 将iso内容拷贝到这里
mount ubuntu-18.04.5-server-amd64.iso /home/hy/mnt # 挂载之后可以看到mnt内有文件了
cp -rT /home/hy/mnt/ /home/hy/livecd  # 拷贝出来到livecd目录内,便于后边修改

3. Edit custom.seedthe key points of the file

vim /home/hy/livecd/preseed/custom.seed

The content involved is as follows:

# Language:
d-i     debian-installer/locale     string en_US.UTF-8
# Keyboard selection. 
d-i     console-setup/ask_detect     boolean false 
d-i     console-setup/layoutcode     string us 

# Suggest LVM by default.
d-i     partman-auto/init_automatically_partition       string some_device_lvm
d-i     partman-auto/init_automatically_partition       seen false
# 多硬盘情况下,这里设置选择 /dev/sda,即第一个硬盘
d-i     partman-auto/disk	string /dev/sda
# 可供设置参数有lvm、regular(常规)、crypto(加密)
d-i     partman-auto/method     string lvm 
d-i     partman-lvm/confirm     boolean true
d-i     partman/choose_partition     select finish 
d-i     partman-lvm/confirm_nooverwrite     boolean true
d-i     partman-auto-lvm/guided_size     string max
# 可选参数,atomic(所有文件在一个分区)、home、multi
d-i     partman-auto/choose_recipe     select atomic
# 下述配置使得partman无需确认即可自动分区
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

# Install the Ubuntu Server seed.
tasksel tasksel/force-tasks     string server
# Only install basic language packs. Let tasksel ask about tasks.
d-i     pkgsel/language-pack-patterns   string
# No language support packages.
d-i     pkgsel/install-language-support boolean false
# Only ask the UTC question if there are other operating systems installed.
d-i     clock-setup/utc boolean true
d-i     time/zone string Asia/Shanghai
# Verbose output and no boot splash screen.
d-i     debian-installer/quiet  boolean false
d-i     debian-installer/splash boolean false
# Wait for two seconds in grub
d-i     grub-installer/timeout  string 2

# netcfg will choose an interface that has link if possible. This makes it
# skip displaying a list if there is more than one interface.
# 多网卡时自动选择
d-i netcfg/choose_interface select auto

# Any hostname and domain names assigned from dhcp take precedence over
# values set here. However, setting the values still prevents the questions
# from being shown, even if values come from dhcp.
d-i netcfg/get_hostname string com-aarch64
d-i netcfg/get_domain string com-aarch64

# If you want to force a hostname, regardless of what either the DHCP
# server returns or what the reverse DNS entry for the IP is, uncomment
# and adjust the following line.
d-i netcfg/hostname string com-aarch64

## Individual additional packages to install
d-i     tasksel/first       multiselect openssh-server
# 这里预装了openssh-server和vim
d-i     pkgsel/include     string openssh-server vim

## set root password
d-i passwd/root-login boolean true
d-i passwd/root-password password abcd@1234
d-i passwd/root-password-again password abcd@1234
d-i user-setup/allow-password-weak boolean true
## creat user
d-i passwd/make-user boolean false

# Start ufw automatically?
ufw     ufw/enable      boolean false

# Open-SSH Server
openssh-server  openssh-server/permit-root-login        boolean true
openssh-server  openssh-server/password-authentication  boolean true

### Mirror settings
# If you select ftp, the mirror/country string does not need to be set.
# d-i mirror/protocol string ftp
# d-i mirror/country string manual
# d-i mirror/http/hostname string archive.ubuntu.com
# d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string

# Policy for applying updates. May be "none" (no automatic updates),
# "unattended-upgrades" (install security updates automatically), or
# "landscape" (manage system with Landscape).
d-i pkgsel/update-policy select none

# grub在x86架构时设置,自动选择
# This is fairly safe to set, it makes grub install automatically to the MBR
# if no other operating system is detected on the machine.
d-i grub-installer/only_debian boolean true
# Due notably to potential USB sticks, the location of the MBR can not be
# determined safely in general, so this needs to be specified:
d-i grub-installer/bootdev  string /dev/sda


# Avoid that last message about the install being complete.
d-i finish-install/reboot_in_progress note

# This is how to make the installer shutdown when finished, but not
# reboot into the installed system.
d-i debian-installer/exit/poweroff  boolean true

# run script,最后执行一些脚本用于自动安装软件
d-i preseed/late_command string cp /cdrom/custom/install_software.sh /target/opt/;cp /cdrom/custom/offline-packages.tar.gz /target/opt;chroot /target chmod +x /opt/install_software.sh;chroot /target bash /opt/install_software.sh

The meaning of each configuration item needs to be read in the comments and the explanation in https://help.ubuntu.com/lts/installation-guide/example-preseed.txt. There is no way it is all in English, so I still have to read it.
In particular, if it is an x86 architecture, you will encounter GRUB problems, as shown in the following figure:

install the grub boot loader on a hard disk
install the GRUB boot loader to the master boot record?

insert image description here
After clicking yes, the situation shown in the figure below will also appear when there are multiple hard disks

install the grub boot loader on a hard disk
Device for boot loader installation:

insert image description here
The following contents in the configuration file correspond to the answers in the above two screenshots

d-i grub-installer/only_debian boolean true
d-i grub-installer/bootdev  string /dev/sda

There are two files in the last line of the configuration file: install_software.shand offline-packages.tar.gz
Among them offline-packages.tar.gzis the apt offline source of the software to be installed. For the specific creation process, see ubuntu. The apt-get offline source production
install_software.sh is the script for installing the software. The specific content is as follows. problem with:

# 创建离线apt本地源
function create_offline_repo(){
    
    
    tar -xvf /opt/offline-packages.tar.gz -C /opt
    cp /etc/apt/sources.list /etc/apt/sources.list.bak
    for repo in $(ls /etc/apt/sources.list.d/*.list);
    do
        mv /etc/apt/sources.list.d/${repo} /etc/apt/sources.list.d/${repo}.bak
    done

    echo "deb  [trusted=yes]  file:///opt/offline-packages archives/" > /etc/apt/sources.list
    unset DEBCONF_REDIR
    unset DEBCONF_FRONTEND
    unset DEBIAN_HAS_FRONTEND
    unset DEBIAN_FRONTEND
    apt-get clean all
    apt-get update
}
# 安装软件
function install_apt(){
    
    
    echo "base env"
    apt install -y chrony wget logrotate crudini git chrony
    apt install -y python-dev python3 python3-pip python3-dev python3-selinux

    echo "docker"
    apt install -y bridge-utils docker.io  python3-cffi build-essential libssl-dev libffi-dev docker-compose
    apt install -y apt-transport-https ca-certificates gnupg-agent memcached
}
create_offline_repo
install_apt

The focus of the released code lies in the following four lines:

    unset DEBCONF_REDIR
    unset DEBCONF_FRONTEND
    unset DEBIAN_HAS_FRONTEND
    unset DEBIAN_FRONTEND

If you do not cancel the above environment variables before apt installs the software, it will cause the installation to be stuck on the following interface. If it is normal, the custom software will be installed here, and the installation will continue after waiting for a while: 4. Edit
insert image description here
Finishing the installation Running preseed...
the grub.cfgfile

cd /home/hy/livecd/boot/grub
vim grub.cfg

Change to the following

if loadfont /boot/grub/font.pf2 ; then
	set gfxmode=auto
	insmod efi_gop
	insmod efi_uga
	insmod gfxterm
	terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

set timeout=30
menuentry "Install Ubuntu-custom Server" {
    
    
	set gfxpayload=keep
	linux	/install/vmlinuz  file=/cdrom/preseed/custom.seed quiet ---
	initrd	/install/initrd.gz
}
menuentry "Check disc for defects" {
    
    
        set gfxpayload=keep
        linux   /install/vmlinuz  MENU=/bin/cdrom-checker-menu quiet ---
        initrd  /install/initrd.gz
}
menuentry "Rescue a broken system" {
    
    
        set gfxpayload=keep
        linux   /install/vmlinuz  rescue/enable=true ---
        initrd  /install/initrd.gz
}

Pay attention to change the file to your own path
4. Edit isolinux.cfgthe file

cd /home/hy/livecd/isolinux
vim isoliunx.cfg
# D-I config version 2.0
# search path for the c32 support libraries (libcom32, libutil etc.)
path 
include menu.cfg
default vesamenu.c32
prompt 0
timeout 300
ui gfxboot bootlogo
label autoinstall
menu label autoinstall - auto install customcd
kernel /install/vmlinuz
append initrd=/install/initrd.gz file=/cdrom/preseed/custom.seed boot=install debian-installer/locale=en_US.UTF-8 console-setup/ask_detect=false keyboard-configuration/layoutcode=us automatic-ubiquity quiet splash --

The main thing is to add a label. When the home page displays options during installation, the label defined here will be displayed.
5. Make an iso image

mkisofs -U -r -v -T -J -joliet-long -V "Custom CD" \
        -volset "Custom CD" \
        -A "Custom CD" -b isolinux/isolinux.bin \
        -c isolinux/boot.cat -no-emul-boot \
        -boot-load-size 4 -boot-info-table \
        -eltorito-alt-boot -iso-level 3 -allow-limited-size \
        -e boot/grub/efi.img -no-emul-boot \
        -o ./custom.iso ./livecd/

Note: Do not try to use the UltralSO of windows to make, it will fail

reference link

This installation started from scratch, and I didn't know what preseed was before the operation, so I took a lot of detours and gained a lot.
There are really not many such materials on the Internet in China, and what is written is hard to describe and unclear. Most of the solutions to all problems are solved by referring to official documents, here only valuable links are put here for your reference

1. InstallCDCustomization
2. Create Preseed Installation File
3. Appendix B. Automatic installation using presets
4. Ubuntu ISO customization
5. example-preseed.txt This is the key reference content for configuring preseed files
6. ubuntu preseed no answer installation

about

Welcome to follow my blog

Guess you like

Origin blog.csdn.net/HYESC/article/details/123573948