通过查阅资料发现,网上的ubuntu自动化安装教程,多以pxe的方式安装,现分享一种基于iPXE的安装教程,欢迎交流!
1.新建VM虚拟机
首先新建一台VMware,我使用的是centos7.9的操作系统,并配置能连接外网。这一步具体操作请自行查询资料。
2.配置虚拟机为IPXE服务器
2.1安装必要的服务
[root@localhost ~]# yum install tftp-server xinetd dhcp httpd dnsmasq -y
2.2 配置http服务
2.2.1 上传ubuntu22.04的iso镜像
将ubuntu22.04的iso镜像传到虚拟机中(使用xftp、共享文件都行,看你喜欢)。这里先暂时放在root目录(ps:放在哪里都可以)。
2.2.2 在http目录下创建iso目录, 并将iso拷贝至iso目录
cd /var/www/html/
mkdir iso
cp /root/ubuntu-22.04-live-server-amd64.iso /var/www/html/iso # 此处最好是cp,因为等会还需要再次使用该iso镜像
2.2.3挂载iso, 提取initrd和 vmlinuz
mkdir /opt/disk mount -o loop /root/ubuntu-22.04-live-server-amd64.iso /opt/disk/
将initrd和 vmlinuz拷贝至 刚才创建的iso目录
cp /opt/disk/casper/initrd /var/www/html/iso
cp /opt/disk/casper/vmlinuz /var/www/html/iso
2.2.4 创建 user-data和meta-data
mkdir -p /var/www/html/ks/ubuntu
touch meta-data #meta-data 是空文件,但是必须要有
创建一个user-data,文件内容如下:
ps: 也可以参照https://ubuntu.com/server/docs/install/autoinstall-reference自行创建,最方便的方式是先通过图形化界面安装一次系统,然后系统中/var/log/install/auto-user-data 拷贝出来使用。注意在修改时严格遵守yaml语法。
#cloud-config
autoinstall:drivers:install: falseidentity:hostname: ZHHpassword: $6$5.4Db4xd76bu0i3v$ujjOoIhX2cs.TBA8XFi/TZ/mrNaNA9CdClWpVpOUeFhqIINuq8w4cpp9BUDAVIN88h7oPoYDCofRt9zs0zPWo1realname: ZHHusername: zhhkeyboard:layout: ustoggle: nullvariant: ''locale: en_US.UTF-8ssh:allow-pw: trueauthorized-keys: []install-server: truestorage:config:- ptable: gptwipe: superblock-recursivepreserve: falsename: ''grub_device: truetype: diskid: disk-sdamatch: size: largest- device: disk-sdasize: 1048576flag: bios_grubnumber: 1preserve: falsegrub_device: falsetype: partitionid: partition-0- device: disk-sdasize: 8589934592wipe: superblockflag: swapnumber: 2preserve: falsegrub_device: falsetype: partitionid: partition-1- fstype: swapvolume: partition-1preserve: falsetype: formatid: format-0- path: ''device: format-0type: mountid: mount-0- device: disk-sdasize: 1073741824wipe: superblockflag: ''number: 3preserve: falsegrub_device: falsetype: partitionid: partition-2- fstype: xfsvolume: partition-2preserve: falsetype: formatid: format-1- device: disk-sdasize: 8589934592wipe: superblockflag: ''number: 4preserve: falsegrub_device: falsetype: partitionid: partition-3- fstype: xfsvolume: partition-3preserve: falsetype: formatid: format-2- device: disk-sdasize: 21474836480wipe: superblockflag: ''number: 5preserve: falsegrub_device: falsetype: partitionid: partition-4- fstype: xfsvolume: partition-4preserve: falsetype: formatid: format-3- path: /device: format-3type: mountid: mount-3- device: disk-sdasize: -1wipe: superblockflag: ''number: 6preserve: falsegrub_device: falsetype: partitionid: partition-5- fstype: xfsvolume: partition-5preserve: falsetype: formatid: format-4- path: /datadevice: format-4type: mountid: mount-4- path: /bootdevice: format-1type: mountid: mount-1- path: /vardevice: format-2type: mountid: mount-2swap:swap: 0updates: securityversion: 1
2.3 配置tftp服务
2.3.1下载并编译ipxe固件
ps: 编译所需的依赖请自行安装
[root@localhost ~]# cd /root
[root@localhost ~]# yum install -y xz-devel
[root@localhost ~]# git clone https://github.com/ipxe/ipxe.git
[root@localhost ~]# cd /root/ipxe/src
[root@localhost src]# make bin/undionly.kpxe
[root@localhost src]# make bin-x86_64-efi/ipxe.efi
执行完之后,在bin中会有一个undionly.kpxe文件,在bin-x86_64-efi会有ipxe.efi,之后会用到这两个文件。
2.3.2 修改 tftp配置
[root@localhost ~]# vim /etc/xinetd.d/tftp
2.3.3复制ipxe固件至tftp根目录
[root@localhost ~]# cp /root/ipxe/src/bin/undionly.kpxe /var/lib/tftpboot/
[root@localhost ~]# cp /root/ipxe/src/bin-x86_64-efi/ipxe.efi /var/lib/tftpboot/
2.3.4 配置启动脚本
vi /var/lib/tftpboot/ubuntu2204.cfg
内容如下:
#!ipxe
set product-name ubuntu2204
set os-name ubuntu2204set menu-timeout 10000
set submenu-timeout ${menu-timeout}
set menu-default exit:start
menu boot from iPXE server
item --gap -- --------------------------------------------
item --gap -- serial:${serial}
item --gap -- mac:${mac}
item --gap -- ip:${ip}
item --gap -- netmask:${netmask}
item --gap -- gateway:${gateway}
item --gap -- dns:${dns}
item
item --gap -- --------------------------------------------
item install-os ${product-name}
choose --timeout ${menu-timeout} --default ${menu-default} selected || goto cancel
goto ${selected}:install-os
set server http://ipxe.server/
initrd ${server}iso/initrd
kernel ${server}iso/vmlinuz initrd=initrd ip=dhcp url=${server}iso/ubuntu-22.04.1-live-server-amd64.iso autoinstall ds=nocloud-net;s=${server}/ks/ubuntu/ root=/dev/ram0 cloud-config-url=/dev/null console=ttyS0,115200n8
boot
如果没有串口,就将’console=ttyS0,115200n8 ‘删除
2.4 配置DNS服务
[root@localhost ~]# vim /etc/dnsmasq.conf
2.5 配置dhcp服务
创建dhcp配置文件
[root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
编辑如下内容
# This is a very basic subnet declaration.
option client-architecture code 93 = unsigned integer 16;
subnet 10.10.0.0 netmask 255.255.255.224 {range 10.10.0.10 10.10.0.20;next-server 10.10.0.2;if exists user-class and option user-class = "iPXE" {filename "ubuntu2204.cfg";} elsif option client-architecture = 00:00 {filename "undionly.kpxe";} else {filename "ipxe.efi";}
}
并在dhcp配置文件中,指定DNS服务器
2.6 修改虚拟机ip
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost ~]# systemctl restart netowrk
2.7 修改虚拟机为桥接模式,配置物理网卡
选择虚拟机为桥接模式:
打开虚拟网络编辑器:
点击更改设置:
修改桥接的网口:
2.8 关闭防火墙,重复服务
关闭防火墙 和 强访
[root@localhost ~]# systemctl stop firewalld &&systemctl disable firewalld
[root@localhost ~]# systemctl stop getenforce &&systemctl disable getenforce
开启dhcp,http,dns,tftp 服务
[root@localhost ~]# systemctl start dhcpd tftp httpd dnsmasq
至此ipxe服务器已经搭建完成了。
附上所有的参考资料:
https://ubuntu.com/server/docs/install/autoinstall-reference
https://forum.level1techs.com/t/linux-installation-using-i-pxe/186721
https://ipxe.org/docs
https://blog.csdn.net/supahero/article/details/115829836
https://blog.csdn.net/weixin_46243964/article/details/119650128?
spm=1001.2101.3001.6650.8&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-8-119650128-blog-117298899.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-8-119650128-blog-117298899.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=12
https://github.com/ipxe/ipxe/discussions/709
https://curtin.readthedocs.io/en/latest/topics/storage.html
https://www.molnar-peter.hu/en/ubuntu-jammy-netinstall-pxe.html
linux就该这么学 第19章