• 使用iPXE自动化安装ubuntu22.04


    通过查阅资料发现,网上的ubuntu自动化安装教程,多以pxe的方式安装,现分享一种基于iPXE的安装教程,欢迎交流!

    1.新建VM虚拟机

    首先新建一台VMware,我使用的是centos7.9的操作系统,并配置能连接外网。这一步具体操作请自行查询资料。

    2.配置虚拟机为IPXE服务器

    2.1安装必要的服务

    [root@localhost ~]# yum install tftp-server xinetd dhcp httpd  dnsmasq -y
    
    • 1

    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镜像
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2.2.3挂载iso, 提取initrd和 vmlinuz

     mkdir /opt/disk    
     mount -o loop /root/ubuntu-22.04-live-server-amd64.iso /opt/disk/
    
    • 1
    • 2

    将initrd和 vmlinuz拷贝至 刚才创建的iso目录

    cp /opt/disk/casper/initrd	 /var/www/html/iso
    cp /opt/disk/casper/vmlinuz	 /var/www/html/iso
    
    • 1
    • 2

    2.2.4 创建 user-data和meta-data

    mkdir -p /var/www/html/ks/ubuntu
    touch meta-data       #meta-data 是空文件,但是必须要有   
    
    • 1
    • 2

    在这里插入图片描述

    创建一个user-data,文件内容如下:

    ps: 也可以参照https://ubuntu.com/server/docs/install/autoinstall-reference自行创建,最方便的方式是先通过图形化界面安装一次系统,然后系统中/var/log/install/auto-user-data 拷贝出来使用。注意在修改时严格遵守yaml语法。

    #cloud-config
    autoinstall:
      drivers:
        install: false
      identity:
        hostname: ZHH
        password: $6$5.4Db4xd76bu0i3v$ujjOoIhX2cs.TBA8XFi/TZ/mrNaNA9CdClWpVpOUeFhqIINuq8w4cpp9BUDAVIN88h7oPoYDCofRt9zs0zPWo1
        realname: ZHH
        username: zhh
      keyboard:
        layout: us
        toggle: null
        variant: ''
      locale: en_US.UTF-8
      ssh:
        allow-pw: true
        authorized-keys: []
        install-server: true
      storage:
        config:
        - ptable: gpt
          wipe: superblock-recursive
          preserve: false
          name: ''
          grub_device: true
          type: disk
          id: disk-sda
          match: 
            size: largest
        - device: disk-sda
          size: 1048576
          flag: bios_grub
          number: 1
          preserve: false
          grub_device: false
          type: partition
          id: partition-0
        - device: disk-sda
          size: 8589934592
          wipe: superblock
          flag: swap
          number: 2
          preserve: false
          grub_device: false
          type: partition
          id: partition-1
        - fstype: swap
          volume: partition-1
          preserve: false
          type: format
          id: format-0
        - path: ''
          device: format-0
          type: mount
          id: mount-0
        - device: disk-sda
          size: 1073741824
          wipe: superblock
          flag: ''
          number: 3
          preserve: false
          grub_device: false
          type: partition
          id: partition-2
        - fstype: xfs
          volume: partition-2
          preserve: false
          type: format
          id: format-1
        - device: disk-sda
          size: 8589934592
          wipe: superblock
          flag: ''
          number: 4
          preserve: false
          grub_device: false
          type: partition
          id: partition-3
        - fstype: xfs
          volume: partition-3
          preserve: false
          type: format
          id: format-2
        - device: disk-sda
          size: 21474836480
          wipe: superblock
          flag: ''
          number: 5
          preserve: false
          grub_device: false
          type: partition
          id: partition-4
        - fstype: xfs
          volume: partition-4
          preserve: false
          type: format
          id: format-3
        - path: /
          device: format-3
          type: mount
          id: mount-3
        - device: disk-sda
          size: -1
          wipe: superblock
          flag: ''
          number: 6
          preserve: false
          grub_device: false
          type: partition
          id: partition-5
        - fstype: xfs
          volume: partition-5
          preserve: false
          type: format
          id: format-4
        - path: /data
          device: format-4
          type: mount
          id: mount-4
        - path: /boot
          device: format-1
          type: mount
          id: mount-1
        - path: /var
          device: format-2
          type: mount
          id: mount-2
        swap:
          swap: 0
      updates: security
      version: 1
    
    
    • 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
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    执行完之后,在bin中会有一个undionly.kpxe文件,在bin-x86_64-efi会有ipxe.efi,之后会用到这两个文件。

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2.3.2 修改 tftp配置

    [root@localhost ~]# vim /etc/xinetd.d/tftp
    
    • 1

    在这里插入图片描述

    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/
    
    
    • 1
    • 2
    • 3

    2.3.4 配置启动脚本

    vi /var/lib/tftpboot/ubuntu2204.cfg
    
    • 1

    内容如下:

    #!ipxe
    set product-name ubuntu2204
    set os-name ubuntu2204
    
    set 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
    
    
    • 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

    如果没有串口,就将’console=ttyS0,115200n8 ‘删除

    2.4 配置DNS服务

    [root@localhost ~]# vim /etc/dnsmasq.conf 
    
    • 1

    在这里插入图片描述

    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
    
    • 1
    • 2

    编辑如下内容

    # 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";
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    并在dhcp配置文件中,指定DNS服务器

    在这里插入图片描述

    2.6 修改虚拟机ip

    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
    [root@localhost ~]# systemctl restart netowrk
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    2.7 修改虚拟机为桥接模式,配置物理网卡

    选择虚拟机为桥接模式:

    在这里插入图片描述

    打开虚拟网络编辑器:
    在这里插入图片描述

    点击更改设置:
    在这里插入图片描述

    修改桥接的网口:
    在这里插入图片描述

    2.8 关闭防火墙,重复服务

    关闭防火墙 和 强访

    [root@localhost ~]# systemctl stop firewalld &&systemctl disable firewalld
    [root@localhost ~]# systemctl stop getenforce  &&systemctl disable getenforce
    
    • 1
    • 2

    开启dhcp,http,dns,tftp 服务

    [root@localhost ~]#  systemctl start dhcpd tftp httpd dnsmasq 
    
    • 1

    至此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章

  • 相关阅读:
    网络安全(黑客)自学
    idea Springboot 校园助学贷款系统VS开发mysql数据库web结构java编程计算机网页源码maven项目
    【二叉树-中等】1104. 二叉树寻路
    如何高效且优雅地使用Redis
    自学 TypeScript 第四天,手把手项目搭建
    【论文复现】DAE:《Annotating and Modeling Fine-grained Factuality in Summarization》
    初识网络
    ICCV2021|你以为这是一个填色模型?其实我是检索模型!
    c++ goto语句
    mysql-sql执行流程
  • 原文地址:https://blog.csdn.net/hg_zhh/article/details/127670774