Centos7自定义镜像
安装自定义镜像工具
-
安装
yum -y install createrepo mkisofs openssl rsync syslinux
挂载镜像
-
创建挂载点
mkdir /mnt/cdrom
-
挂载
# 上传镜像到/home/baseISO目录下 mount -o loop /home/baseISO/CentOS-7-x86_64-DVD-2009.iso /mnt/cdrom/
-
同步/mnt/cdrom/下的文件到/home/ISO/
mkdir /home/ISO /usr/bin/rsync -a /mnt/cdrom/ /home/ISO/
ks.cfg文件自定义
-
复制ks.cfg文件
cp /root/anaconda-ks.cfg /root/ks.cfg
-
密码生成
# -1: md5加密 # -salt: 加盐 # 盐: Wkajs # pass: password openssl passwd -1 -salt Wkajs password
-
编辑ks.cfg文件
# vim /root/ks.cfg #version=DEVEL # System authorization information auth --enableshadow --enablemd5 # Use CDROM installation media cdrom # Use graphical install 图形化界面式安装 # graphical # Use text install 文本式安装 text # Run the Setup Agent on first boot firstboot --enable # 启用会忽略其他硬盘 ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=eth0 --ipv6=auto --activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $1$8q7ORxjl$v8yGB70WIHiLjNcaJRdCh. # System services services --disabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc --nontp # Partition clearing information clearpart --all --initlabel # System bootloader configuration bootloader --append=" crashkernel=512M " --location=mbr --boot-drive=sda # Clear the Master Boot Record zerombr %addon com_redhat_kdump --enable --reserve-mb='512M' %end part /boot --asprimary --fstype="ext4" --size=500 --ondisk=sda # uefi模式需要 part /boot/efi --asprimary --fstype="efi" --size=120 --ondisk=sda #----------------------以下是创建lvm逻辑卷--------------------- # 创建第一个物理卷PV分区,序号是01,大小为剩余所有空间 # --size=1 --grow参数是以剩余所有空间来分区 part pv.01 --size=1 --grow --ondisk=sda # 创建一个逻辑卷组名,名字为virt,包含在物理卷PV.01 volgroup virt pv.01 # 创建一个LV逻辑卷,挂载到/ ,从virt组中创建,大小为剩余所有空间,LV的名字是vg_root logvol / --fstype="ext4" --vgname=virt --size=1 --grow --name=vg-root # 创建第二个物理卷PV分区,序号是02,大小为剩余所有空间 #part pv.02 --size=1 --grow --ondisk=sdb #创建一个逻辑卷组名,名字为data ,包含在物理卷PV.02 #volgroup vg_data pv.02 #创建一个LV逻辑卷,挂载到/data ,从vg_data组中创建,LV的名字是volume-data #logvol /data --fstype="ext4" --vgname=vg_data --size=1 --grow --name=volume-data # 最小化安装 %packages @^minimal @core kexec-tools %end reboot
ISO文件目录
-
目录
# tree /home/ISO/ -d /home/ISO/ ├── EFI │ └── BOOT │ └── fonts ├── images │ └── pxeboot ├── isolinux ├── LiveOS ├── Packages └── repodata
-
复制ks.cfg到isolinux
cp /root/ks.cfg /home/ISO/isolinux
-
修改isolinux.cfg文件
# vim /home/ISO/isolinux/isolinux.cfg label linux menu label ^Install CentOS 7 menu default kernel vmlinuz append initrd=initrd.img ks=cdrom:/isolinux/ks.cfg net.ifnames=0 biosdevname=0 quiet
describe:
net.ifnames=0 biosdevname=0 禁用自动设备命名规则,恢复传统的
ethx设备名称
生成iso文件
-
制作iso命令
# mkisofs制作 mkisofs \ -o CentOS-7-9.iso \ -b isolinux/isolinux.bin \ -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \ -boot-info-table -R -J -v -T /home/ISO # genisoimage 制作 genisoimage \ -joliet-long \ -o CentOS-7-9.iso \ -b isolinux/isolinux.bin \ -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \ -boot-info-table -R -J -v -cache-inodes -T -eltorito-alt-boot \ -e images/efiboot.img -no-emul-boot /home/ISO
-
uefi bios兼容
mkisofs -o /home/ISO/CentOS-7-9.iso -v -cache-inodes -joliet-long -R -J -T -V centos7 -c isolinux/boot.cat -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b images/efiboot.img -no-emul-boot /home/ISO
-
uefi模式需修改文件
# vim EFI/BOOT/grub.cfg # 修改内容 menuentry 'Install CentOS 7' --class fedora --class gnu-linux --class gnu --class os { linuxefi /images/pxeboot/vmlinuz ks=cdrom:/isolinux/ks.cfg net.ifnames=0 biosdevname=0 quiet initrdefi /images/pxeboot/initrd.img } #下面内容可注释,只保留当前install选项 ...
-
Hybird模式
isohybrid -v /home/ISO/CentOS-7-9.iso
notice:
isohybrid command not found,需要安装syslinux.命令: yum -y install syslinux
describe:
采用“hybird模式”(混合模式),操作系统可以直接刻录成物理光盘,也可以直接做成可引导的U盘
ubuntu自定义镜像
安装自定义镜像工具
apt-get install genisoimage
挂载镜像
-
创建挂载点
mkdir /mnt/cdrom
-
挂载
# 上传镜像到/home/user目录下 mount -o loop /home/user/ubuntu-20.04.6-live-server-amd64.iso /mnt/cdrom/
-
同步/mnt/cdrom/下的文件到/home/ISO/
mkdir /home/ISO rsync -avz /mnt/cdrom/ /home/ISO/
创建应答文件
- 20.04meta-data
instance-id: focal-autoinstall
- 22.04meta-data
instance-id: jammyl-autoinstall
- 密码生成
apt install whois -y
mkpasswd -m sha-512 mypassword
- 20.04user-data
#cloud-config
autoinstall:
version: 1
apt:
primary:
- arches: [default]
uri: https://mirrors.aliyun.com/ubuntu
disable_components: []
preserve_sources_list: false
identity:
hostname: ubuntu-server
password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
username: ubuntu
ssh:
allow-pw: true
authorized-keys: []
install-server: true
network:
version: 2
ethernets:
ens33:
dhcp4: true
optional: true
storage:
config:
- ptable: gpt
path: /dev/sda
wipe: superblock-recursive
preserve: false
name: ''
grub_device: true
id: disk-sda
type: disk
- device: disk-sda
size: 1048576
flag: bios_grub
number: 1
preserve: false
grub_device: false
offset: 1048576
path: /dev/sda1
id: partition-0
type: partition
- device: disk-sda
size: 1073741824
wipe: superblock
number: 2
preserve: false
grub_device: false
offset: 2097152
path: /dev/sda2
id: partition-1
type: partition
- fstype: ext4
volume: partition-1
preserve: false
id: format-0
type: format
- device: disk-sda
size: -1
wipe: superblock
number: 3
preserve: false
grub_device: false
offset: 1075839488
path: /dev/sda3
id: partition-2
type: partition
- name: ubuntu-vg
devices:
- partition-2
preserve: false
id: lvm_volgroup-0
type: lvm_volgroup
- name: ubuntu-root
volgroup: lvm_volgroup-0
size: -1
wipe: superblock
preserve: false
path: /dev/ubuntu-vg/ubuntu-root
id: lvm_partition-0
type: lvm_partition
- fstype: ext4
volume: lvm_partition-0
preserve: false
id: format-1
type: format
- path: /
device: format-1
id: mount-1
type: mount
- path: /boot
device: format-0
id: mount-0
type: mount
packages:
- vim
- curl
late-commands:
- curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
- 22.04 user-data
#cloud-config
autoinstall:
version: 1
apt:
primary:
- arches: [default]
uri: https://mirrors.aliyun.com/ubuntu
disable_components: []
preserve_sources_list: false
user-data:
timezone: Asia/Shanghai
identity:
hostname: ubuntu-server
password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
username: ubuntu
ssh:
allow-pw: true
authorized-keys: []
install-server: true
network:
version: 2
ethernets:
ens33:
dhcp4: true
optional: true
storage:
config:
- ptable: gpt
path: /dev/sda
wipe: superblock-recursive
preserve: false
name: ''
grub_device: true
id: disk-sda
type: disk
- device: disk-sda
size: 1048576
flag: bios_grub
number: 1
preserve: false
grub_device: false
offset: 1048576
path: /dev/sda1
id: partition-0
type: partition
- device: disk-sda
size: 1073741824
wipe: superblock
number: 2
preserve: false
grub_device: false
offset: 2097152
path: /dev/sda2
id: partition-1
type: partition
- fstype: ext4
volume: partition-1
preserve: false
id: format-0
type: format
- device: disk-sda
size: -1
wipe: superblock
number: 3
preserve: false
grub_device: false
offset: 1075839488
path: /dev/sda3
id: partition-2
type: partition
- name: ubuntu-vg
devices:
- partition-2
preserve: false
id: lvm_volgroup-0
type: lvm_volgroup
- name: ubuntu-root
volgroup: lvm_volgroup-0
size: -1
wipe: superblock
preserve: false
path: /dev/ubuntu-vg/ubuntu-root
id: lvm_partition-0
type: lvm_partition
- fstype: ext4
volume: lvm_partition-0
preserve: false
id: format-1
type: format
- path: /
device: format-1
id: mount-1
type: mount
- path: /boot
device: format-0
id: mount-0
type: mount
packages:
- vim
- curl
early-commands:
- sgdisk --clear /dev/sda
- sgdisk -n 1:2048:+1M -c 1:"System Partition" -t 1:ef02 /dev/sda
- sgdisk -n 2:4096:+1024M -c 2:"Boot System Partition" -t 2:8304 /dev/sda
- sgdisk -N 3 -c 3:"Root Partition" -t 3:8304 /dev/sda
late-commands:
- curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
- user-data 进阶
#cloud-config
autoinstall:
version: 1
apt:
primary:
- arches: [default]
uri: https://mirrors.aliyun.com/ubuntu
disable_components: []
preserve_sources_list: false
user-data:
timezone: Asia/Shanghai
identity:
hostname: ubuntu-server
password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
username: ubuntu
ssh:
allow-pw: true
authorized-keys: []
install-server: true
network:
version: 2
ethernets:
ens33:
dhcp4: true
optional: true
storage:
config:
- ptable: gpt
path: /dev/sda
wipe: superblock-recursive
preserve: false
name: ''
grub_device: true
id: disk-sda
type: disk
- device: disk-sda
size: 1048576
flag: bios_grub
number: 1
preserve: false
grub_device: false
offset: 1048576
path: /dev/sda1
id: partition-0
type: partition
- device: disk-sda
size: 1073741824
wipe: superblock
number: 2
preserve: false
grub_device: false
offset: 2097152
path: /dev/sda2
id: partition-1
type: partition
- fstype: ext4
volume: partition-1
preserve: false
id: format-0
type: format
- device: disk-sda
size: -1
wipe: superblock
number: 3
preserve: false
grub_device: false
offset: 1075839488
path: /dev/sda3
id: partition-2
type: partition
- name: ubuntu-vg
devices:
- partition-2
preserve: false
id: lvm_volgroup-0
type: lvm_volgroup
- name: ubuntu-root
volgroup: lvm_volgroup-0
size: -1
wipe: superblock
preserve: false
path: /dev/ubuntu-vg/ubuntu-root
id: lvm_partition-0
type: lvm_partition
- fstype: ext4
volume: lvm_partition-0
preserve: false
id: format-1
type: format
- path: /
device: format-1
id: mount-1
type: mount
- path: /boot
device: format-0
id: mount-0
type: mount
- ptable: gpt
path: /dev/sdb
wipe: superblock-recursive
preserve: false
name: ''
grub_device: false
id: disk-sdb
type: disk
- device: disk-sdb
size: -1
wipe: superblock
number: 1
preserve: false
grub_device: false
offset: 0
path: /dev/sdb1
id: partition-sdb1
type: partition
- fstype: ext4
volume: partition-sdb1
preserve: false
id: format-sdb1
type: format
- path: /var/lib/containerd
device: format-sdb1
id: mount-sdb1
type: mount
packages:
- vim
- curl
- wget
- chrony
- docker.io
early-commands:
- sgdisk --clear /dev/sda
- sgdisk -n 1:2048:+1M -c 1:"System Partition" -t 1:ef02 /dev/sda
- sgdisk -n 2:4096:+1024M -c 2:"Boot System Partition" -t 2:8304 /dev/sda
- sgdisk -N 3 -c 3:"Root Partition" -t 3:8304 /dev/sda
- sgdisk -N 1 /dev/sdb
late-commands:
- curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
- curtin in-target -- /bin/bash -c 'wget -O /tmp/network.sh http://192.168.5.195:/network.sh'
- curtin in-target -- /bin/bash -c 'bash /tmp/network.sh'
创建应答文件目录
mkdir /home/ISO/nocloud
mv ./user--data ./meta-data /home/ISO/nocloud
修改.cfg文件
# 20.04文件路径 /home/ISO/isolinux/txt.cfg
default live
label live
menu label ^Install Ubuntu Server
kernel /casper/vmlinuz
# 修改部分
append initrd=/casper/initrd quiet autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
# 22.04文件路径/home/ISO/boot/grub/grub.cfg
menuentry "Install Ubuntu Server" {
set gfxpayload=keep
linux /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---
initrd /casper/initrd
}
生成iso文件
- 20.04
mkisofs -R -J -T -v -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat -o /home/user/ubuntu-20.04.6-auto-server-amd64.iso /home/ISO
- 22.04
mkisofs -R -J -T -v -joliet-long -no-emul-boot -boot-load-size 4 -boot-info-table -b boot/grub/i386-pc/eltorito.img -c boot/grub/boot.cat -o /home/user/ubuntu-22.04.4-auto-server-amd64.iso /home/ISO
静态网络设置脚本
#!/bin/bash
NETCARDS=$(ls /sys/class/net | grep -v -E "(lo|docker|virb|veth)")
STATIC_NETWORK_CONFIG_DIR="/etc/netplan"
for file in $(ls ${STATIC_NETWORK_CONFIG_DIR}); do
mv ${STATIC_NETWORK_CONFIG_DIR}/${file} ${STATIC_NETWORK_CONFIG_DIR}/${file}.old
done
for netcard in $NETCARDS; do
if ip addr show ${netcard} | grep -E ".*state UP" > /dev/null; then
STATIC_NETWORK_CONFIG_FILE_NEW="01-installer-config-${netcard}.yaml"
echo "Configuring $netcard" # 修正变量名从 $NETCARD 改为 $netcard
addr=$(ip addr show "${netcard}" | grep -E "inet.*${netcard}" | awk '{print $2}')
gateway=$(ip route show | grep -E "default.*${netcard}" | awk '{print $3}')
if [ ! -z "$addr" ] && [ ! -z "$gateway" ]; then
STATIC_LINE=$(cat <<EOF
# This is the network config written by 'subiquity'
network:
ethernets:
${netcard}:
addresses:
- ${addr}
nameservers:
addresses:
- ${gateway}
search: []
routes:
- to: default
via: ${gateway}
version: 2
EOF
)
elif [ ! -z "$addr" ] && [ -z "$gateway" ]; then
STATIC_LINE=$(cat <<EOF
# This is the network config written by 'subiquity'
network:
ethernets:
${netcard}:
addresses:
- ${addr}
version: 2
EOF
)
fi
echo -e "${STATIC_LINE}" > ${STATIC_NETWORK_CONFIG_DIR}/${STATIC_NETWORK_CONFIG_FILE_NEW}
fi
done