使用Preseed自动安装Ubuntu

简介

  • 从官网下载的Ubuntu镜像,在安装时,是交互式模式,需要手动配置,为了快速安装,故制作自动安装镜像,方便安装系统。
  • 文档中针对的OS版本为Ubuntu 16.04的镜像,其他版本的配置方法类似。
  • 请使用genisoimage软件代替mkisofs软件制作镜像,因为mkisofs已经过时了。

文档

  • 通过Google搜索: Customization Ubuntu Server Image
  • Preseed: 《传送门》
  • Syslinux - Menu: 《《传送门》》
  • LiveCDCustomization《传送门》
  • InstallCDCustomization《传送门》

制作方法

安装软件

  • 更新软件源并升级系统:
1
apt update && apt dist-upgrade
  • 安装软件:
1
apt install -y squashfs-tools genisoimage
  • 创建目录:
1
2
mkdir -p /mnt/official-iso
mkdir -p /mnt/custom-iso/auto-iso/

16.04

拷贝镜像

  • 挂载镜像:
1
mount -o loop /mnt/ubuntu-16.04.6-server-amd64.iso /mnt/official-iso
  • 拷贝镜像:
1
cp -rT /mnt/official-iso/ /mnt/custom-iso/auto-iso/
  • 卸载镜像:
1
umount /mnt/official-iso

文件系统

  • 此步骤中定制化了文件系统,此步骤可忽略

  • 提取文件系统:

1
2
cd /mnt/custom-iso/
unsquashfs /mnt/custom-iso/auto-iso/install/filesystem.squashfs
  • 切换文件系统:
1
2
3
4
5
6
7
8
9
mount --bind /dev/ /mnt/custom-iso/squashfs-root/dev
chroot /mnt/custom-iso/squashfs-root/
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C
dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl
  • 修改版本号:
1
2
sed -i "s/16.04.6/16.04.6.1/g" /etc/issue
sed -i "s/16.04.6/16.04.6.1/g" /etc/lsb-release
  • 设置root密码:
1
echo "root:root" | chpasswd
  • 退出文件系统:
1
2
3
4
5
6
7
8
9
10
11
rm -f /var/cache/apt/archives/*.deb
apt autoremove && apt autoclean
rm -rf ~/.bash_history
rm /sbin/initctl
dpkg-divert --rename --remove /sbin/initctl
umount /proc || umount -lf /proc
umount /sys
umount /dev/pts
history -c
exit
umount /mnt/custom-iso/squashfs-root/dev
  • 若卸载dev时,报设备繁忙,无法卸载时,请重启电脑再继续操作。

  • 制作文件系统:

1
2
3
rm -f /mnt/custom-iso/auto-iso/install/filesystem.squashfs
mksquashfs /mnt/custom-iso/squashfs-root /mnt/custom-iso/auto-iso/install/filesystem.squashfs -b 1048576
echo $(du -sx --block-size=1 /mnt/custom-iso/squashfs-root | cut -f1) > /mnt/custom-iso/auto-iso/install/filesystem.size

清除垃圾

1
2
3
4
5
6
7
rm -rf /mnt/custom-iso/auto-iso/preseed/*
rm -rf /mnt/custom-iso/auto-iso/install/netboot
rm -rf /mnt/custom-iso/auto-iso/isolinux/txt.cfg
rm -rf /mnt/custom-iso/auto-iso/install/hwe-netboot
rm -rf /mnt/custom-iso/auto-iso/install/hwe-vmlinuz
rm -rf /mnt/custom-iso/auto-iso/isolinux/stdmenu.cfg
rm -rf /mnt/custom-iso/auto-iso/install/hwe-initrd.gz

配置文件

  • 添加preseed文件:
1
vim /mnt/custom-iso/auto-iso/preseed/custom.seed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Enable automatic installation
d-i auto-install/enable boolean true
d-i debconf/priority string critical
d-i debian-installer/framebuffer false

# Localization
d-i debian-installer/locale string en_US.UTF-8

# Keyboard
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layoutcode string us

# Network
d-i netcfg/enable boolean true
d-i netcfg/disable_autoconfig boolean false
d-i netcfg/wireless_wep string
d-i netcfg/target_network_config select ifupdown
d-i apt-setup/use_mirror boolean false
d-i netcfg/get_hostname string SetHostName

# Mirror
d-i mirror/country string manual

# APT
d-i apt-setup/main boolean true
d-i apt-setup/non-free boolean false
d-i apt-setup/contrib boolean false
d-i apt-setup/source boolean false
d-i apt-setup/use_mirror boolean false
d-i apt-setup/services-select none
d-i apt-setup/security_host string
d-i debian-installer/allow_unauthenticated string true

# Account
d-i user-setup/encrypt-home boolean false
d-i passwd/make-user boolean true
d-i passwd/user-fullname string Ubuntu
d-i passwd/username string ubuntu
d-i passwd/user-password-crypted password $1$askfoQNC$BlJ4xig42ggQNWZoxcDTu/

# Clock and Time Zone
d-i clock-setup/utc boolean false
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean false

# Partitioning
d-i partman/early_command string \
debconf-set partman-auto/disk "$(list-devices disk | head -1)"; \
debconf-set grub-installer/bootdev "$(list-devices disk | head -1)";

d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-md/device_remove_regular boolean true
d-i partman-lvm/confirm boolean false
d-i partman/default_filesystem string ext4

d-i partman-auto/expert_recipe string \
boot-root :: \
5000 512 5000 ext4 \
$primary{ } $bootable{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } . \
2000 1024 2000 linux-swap \
$primary{ } \
method{ swap } format{ } . \
1000 2048 -1 ext4 \
$primary{ } \
method{ keep } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /keep } .

d-i partman/confirm_write_new_label 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
d-i partman/mount_style select uuid
d-i partman-efi/non_efi_system boolean true

# Package
tasksel tasksel/first multiselect standard
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select none
d-i pkgsel/updatedb boolean false
d-i pkgsel/include string openssh-server vim ethtool bridge-utils vlan ifenslave curl dmidecode

# GRUB
d-i lilo-installer/skip boolean true
d-i grub-installer/skip boolean false
d-i grub-installer/with_other_os boolean false
d-i grub-installer/only_debian boolean true

# Finishing
d-i finish-install/reboot_in_progress note
d-i cdrom-detect/eject boolean true
d-i debian-installer/exit/reboot boolean true
  • 如何生成密码:
1
2
3
4
5
# 方式一
openssl passwd -1 PASSWORD

# 方式二
python3 -c 'import crypt; print(crypt.crypt("PASSWORD", crypt.mksalt(crypt.METHOD_SHA512)))'
  • 更新isolinux.cfg文件:
1
2
chmod 644 /mnt/custom-iso/auto-iso/isolinux/isolinux.cfg
vim /mnt/custom-iso/auto-iso/isolinux/isolinux.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
MENU HSHIFT 0
MENU WIDTH 82
MENU TITLE Welcome to Custom Ubuntu Server
MENU BACKGROUND splash.png
MENU COLOR border 0 #ffffffff #00000000
MENU COLOR sel 7 #ffffffff #ff000000
MENU COLOR title 0 #ffffffff #00000000
MENU COLOR tabmsg 0 #ffffffff #00000000
MENU COLOR unsel 0 #ffffffff #00000000
MENU COLOR hotsel 0 #ff000000 #ffffffff
MENU COLOR hotkey 7 #ffffffff #ff000000
MENU COLOR scrollbar 0 #ffffffff #00000000

DEFAULT Custom

LABEL Custom
MENU LABEL ^Install Custom Ubuntu Server
MENU DEFAULT
KERNEL /install/vmlinuz
INITRD /install/initrd.gz
APPEND boot=install preseed/file=/cdrom/preseed/custom.seed vga=788 debian-installer/locale=en_US console-setup/ask_detect=false keyboard-configuration/layoutcode=us quiet ---

MENU CLEAR

UI vesamenu.c32
PROMPT 0
TIMEOUT 50
  • 为了界面美观可以替换背景图片splash.png,像素要求为640 * 480
  • 更新grub.cfg文件:
1
2
chmod 644 /mnt/custom-iso/auto-iso/boot/grub/grub.cfg
vim /mnt/custom-iso/auto-iso/boot/grub/grub.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

menuentry "Install Custom Ubuntu Server" {
set gfxpayload=keep
linux /install/vmlinuz file=/cdrom/preseed/custom.seed debian-installer/locale=en_US console-setup/layoutcode=us quiet ---
initrd /install/initrd.gz
}

工具集

  • vmlinuz: 压缩的内核。

  • initrd.gz: 命令的工具集,内含busybox

  • hwe-vmlinuz: 硬件支持,压缩的内核。

  • hwe-initrd.gz: 硬件支持,命令的工具集,内含busybox

  • 解压hwe-initrd.gz

1
2
3
4
5
6
7
mkdir -p /mnt/custom-iso/hwe-initrd/{src,edit,output}
cp -a /mnt/custom-iso/auto-iso-16.04.5.3/install/hwe-initrd.gz /mnt/custom-iso/hwe-initrd/src/
cd /mnt/custom-iso/hwe-initrd/src/
gzip -k -d hwe-initrd.gz

cd /mnt/custom-iso/hwe-initrd/edit/
cpio -i -F /mnt/custom-iso/hwe-initrd/src/hwe-initrd
  • 添加lib文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 支持 lsblk 命令: ldd /bin/lsblk
cp -a /bin/lsblk /mnt/custom-iso/hwe-initrd/edit/bin/
cp -a /lib/x86_64-linux-gnu/librt.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libdl.so.2 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libpcre.so.3 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libpthread.so.0 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libc.so.6 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libuuid.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libblkid.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libmount.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libselinux.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libsmartcols.so.1 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/

# 支持 awk 命令: ldd /usr/bin/awk
cp -a /usr/bin/awk /mnt/custom-iso/hwe-initrd/edit/usr/bin/
cp -a /lib/x86_64-linux-gnu/libm.so.6 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libc.so.6 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libdl.so.2 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libtinfo.so.5 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /lib/x86_64-linux-gnu/libreadline.so.6 /mnt/custom-iso/hwe-initrd/edit/lib/x86_64-linux-gnu/
cp -a /usr/lib/x86_64-linux-gnu/libmpfr.so.4 /mnt/custom-iso/hwe-initrd/edit/usr/lib/x86_64-linux-gnu/
cp -a /usr/lib/x86_64-linux-gnu/libgmp.so.10 /mnt/custom-iso/hwe-initrd/edit/usr/lib/x86_64-linux-gnu/
cp -a /usr/lib/x86_64-linux-gnu/libsigsegv.so.2 /mnt/custom-iso/hwe-initrd/edit/usr/lib/x86_64-linux-gnu/
1
2
3
4
5
cd /mnt/custom-iso/hwe-initrd/edit/
find . | awk '{print substr($0,3)}' | sort | cpio -o -H newc -O /mnt/custom-iso/hwe-initrd/output/hwe-initrd
gzip -9 -c /mnt/custom-iso/hwe-initrd/output/hwe-initrd > /mnt/custom-iso/hwe-initrd/output/hwe-initrd.gz

cp -af /mnt/custom-iso/hwe-initrd/output/hwe-initrd.gz /mnt/custom-iso/auto-iso-16.04.5.3/install/

制作镜像

1
2
3
4
5
cd /mnt/custom-iso/auto-iso/

find -type f -print0 | xargs -0 md5sum | grep -v isolinux/boot.cat | tee md5sum.txt

genisoimage -D -r -V "Custom Ubuntu ISO" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o /mnt/Ubuntu-16.04.6-AutoInstall.iso .

测试方法

  • 通过VirtualBox测试或者制作为U盘启动盘:

请作者喝瓶肥宅快乐水