通过 user-data 文件实现 Ubuntu 系统自动部署

651 阅读2分钟

通过 user-data 文件实现 Ubuntu 系统自动部署

一、环境准备

部署 Ubuntu 系统,准备好所需文件

# touch autoinstall.sh && chmod +x autoinstall.sh && vim autoinstall.sh
sed -i "s@http://.*archive.ubuntu.com@https://mirrors.huaweicloud.com@g" /etc/apt/sources.list
sed -i "s@http://.*security.ubuntu.com@https://mirrors.huaweicloud.com@g" /etc/apt/sources.list
apt-get update
apt install xorriso  p7zip-full -y

mkdir -p /opt/autoinstall
# ubuntu 系统自带 user-data 文件
cp /var/log/installer/autoinstall-user-data /opt/autoinstall/user-data.yaml
chmod +w /opt/autoinstall/user-data.yaml
touch /opt/autoinstall/meta-data

二、下载 Ubuntu 镜像文件,使用 7z 工具进行解压

# 使用 wget 下载镜像文件,也可将现有的镜像文上传到 /opt 目录下
wget -P /opt https://releases.ubuntu.com/jammy/ubuntu-22.04.5-live-server-amd64.iso 
7z -y x /opt/ubuntu-22.04.5-live-server-amd64.iso -o/opt/autoinstall
mv /opt/autoinstall/[BOOT] /opt/autoinstall/BOOT

三、编辑自动部署文件

以下是参考配置,配置项的详细解释参考官方文档:Autoinstall

# vim /opt/autoinstall/user-data.yaml
#cloud-config
autoinstall:
  apt:
    disable_components: []
    fallback: abort
    geoip: false 
    mirror-selection:
      primary:
      - uri: https://mirrors.huaweicloud.com/ubuntu/
      - arches: &id001
        - amd64
        - i386
        uri: https://mirrors.huaweicloud.com/ubuntu/
      - arches: &id002
        - s390x
        - arm64
        - armhf
        - powerpc
        - ppc64el
        - riscv64
        uri: http://ports.ubuntu.com/ubuntu-ports
    preserve_sources_list: false
    security:
    - arches: *id001
      uri: https://mirrors.huaweicloud.com/ubuntu/
    - arches: *id002
      uri: http://ports.ubuntu.com/ubuntu-ports
  codecs: 
    install: false
  drivers:
    install: false
  identity:
    hostname: server
    password: $1$xzhK26Gl$DwhaiAr
    realname: master
    username: master
  kernel:
    package: linux-generic
  keyboard:
    layout: us
    toggle: null
    variant: ''
  locale: en_US.UTF-8
  timezone: Asia/Shanghai
  network:
    ethernets:
      ens33:
        dhcp4: false
        addresses:
          - 192.168.1.100/24
        routes:
          - to: default
            via: 192.168.1.1
            metric: 100
        nameservers:
          addresses:
            - 223.5.5.5
            - 192.168.1.254
    version: 2
  storage:
    layout:
      name: lvm
  version: 1

如果是制作桌面版的镜像文件,则不需要这么多参数,可参考以下配置。

autoinstall:
  drivers:
    install: false
  identity:
    hostname: localhost
    password: $6$GCuP6Rcs.Rsrxxxxxxx
    realname: openvision
    username: openvision
  kernel:
    package: linux-generic
  keyboard:
    layout: us
    toggle: null
    variant: ''
  locale: en_US.UTF-8
  network:
    ethernets:
      ens33:
        dhcp4: true
    version: 2
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: false
  storage:
    layout:
      name: lvm
  updates: security
  swap:
    swap: 1G
  # early-commands:
  # late-commands:
  version: 1

四、修改引导文件

sed -i 's|linux\t/casper/vmlinuz|linux\t/casper/vmlinuz autoinstall ds=nocloud\;s=/cdrom/|' /opt/autoinstall/boot/grub/grub.cfg
sed -i 's/timeout=30/timeout=1/g'  /opt/autoinstall/boot/grub/grub.cfg
chmod -w /opt/autoinstall/user-data.yaml
mv /opt/autoinstall/user-data.yaml /opt/autoinstall/user-data

五、生成镜像文件

使用命令查看生成镜像文件需要哪些参数。

xorriso -indev /opt/ubuntu-22.04.5-live-server-amd64.iso -report_el_torito as_mkisofs

修改对应参数内容,重新生成 ISO 镜像文件,以下命令仅供参考。

# 如果遇到构建失败,可以进入到 autoinstall 目录下再执行命令, -c 和 -b 参数随之更改
xorriso -as mkisofs -r \
  -V 'Ubuntu 22.04' \
  -o /opt/autoinstall-ubuntu-22.04.iso \
  --grub2-mbr /opt/autoinstall/BOOT/1-Boot-NoEmul.img \
  -partition_offset 16 \
  --mbr-force-bootable \
  -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b /opt/autoinstall/BOOT/2-Boot-NoEmul.img \
  -appended_part_as_gpt \
  -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 \
  -c '/opt/autoinstall/boot.catalog' \
  -b '/opt/autoinstall/boot/grub/i386-pc/eltorito.img' \
    -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
  -eltorito-alt-boot \
  -e '--interval:appended_partition_2:::' \
  -no-emul-boot \
  /opt/autoinstall

写在最后

如果重新生成的镜像文件部署失败,大概率是因为 user-data 文件内容格式或者配置项参数有误,需仔细检查。 参考文章:hosseinpanahii Ubuntu-Autoinstall-ISO