ubuntu20.04打包iso镜像自动安装

系统环境:ubuntu20.04

下载ubuntu-20.04.2-live-server-amd64.iso

wget https://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso

挂载系统到/mnt下

sudo mount /home/www/ubuntu-20.04.2-live-server-amd64.iso /mnt

拷贝目录&文件

拷贝/mnt目录中的所有内容到/home/www/ubuntu/目录下

mkdir -p /home/www/ubuntu/
cp -rf /mnt/* /mnt/.disk/ /home/www/ubuntu/

所需自动部署文件

资料:

user-data文件:

相关参数参考:
#查看
cat /var/log/installer/autoinstall-user-data

适当进行修改如对磁盘分区大小修改

原完整文件:

#cloud-config
autoinstall:
  apt:
    geoip: true
    preserve_sources_list: false
    primary:
    - arches: [amd64, i386]
      uri: http://cn.archive.ubuntu.com/ubuntu
    - arches: [default]
      uri: http://ports.ubuntu.com/ubuntu-ports
  identity: {hostname: www, password: $6$FqFvANIIt7lD3d4V$j06NXAbMpjJqdxR/l9jcDXjJl/bULXT9W8EMneB4djvZt3TF5hOr399AThq/3T6efVttZKfz8r2Tp2cqF1xDt0,
    realname: www, username: www}
  keyboard: {layout: us, toggle: null, variant: ''}
  locale: en_US
  network:
    ethernets:
      enp0s3: {dhcp4: true}
      enp0s8: {dhcp4: true}
    version: 2
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: true
  storage:
    config:
    - {ptable: gpt, serial: VBOX_HARDDISK_VB8c6ac1fe-c81d121d, path: /dev/sda, wipe: superblock-recursive,
      preserve: false, name: '', grub_device: true, type: disk, id: disk-sda}
    - {device: disk-sda, size: 1048576, flag: bios_grub, number: 1, preserve: false,
      grub_device: false, type: partition, id: partition-0}
    - {device: disk-sda, size: 107371036672, wipe: superblock, flag: '', number: 2,
      preserve: false, grub_device: false, type: partition, id: partition-1}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-0}
    - {device: format-0, path: /, type: mount, id: mount-0}
  version: 1

对存储部分进行了修改

#删除了磁盘序列号参数
serial: VBOX_HARDDISK_VB8c6ac1fe-c81d121d,
#修改磁盘容量(大小为byte),避免出现磁盘总容量不一样导致的自动安装错误
{device: disk-sda, size: 53687091200, wipe: superblock, flag: '', number: 2,

修改后完整文件

#cloud-config
autoinstall:
  apt:
    geoip: true
    preserve_sources_list: false
    primary:
    - arches: [amd64, i386]
      uri: http://cn.archive.ubuntu.com/ubuntu
    - arches: [default]
      uri: http://ports.ubuntu.com/ubuntu-ports
  identity: {hostname: www, password: $6$FqFvANIIt7lD3d4V$j06NXAbMpjJqdxR/l9jcDXjJl/bULXT9W8EMneB4djvZt3TF5hOr399AThq/3T6efVttZKfz8r2Tp2cqF1xDt0,
    realname: www, username: www}
  keyboard: {layout: us, toggle: null, variant: ''}
  locale: en_US
  network:
    ethernets:
      enp0s3: {dhcp4: true}
      enp0s8: {dhcp4: true}
    version: 2
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: true
  storage:
    config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock-recursive,
      preserve: false, name: '', grub_device: true, type: disk, id: disk-sda}
    - {device: disk-sda, size: 1048576, flag: bios_grub, number: 1, preserve: false,
      grub_device: false, type: partition, id: partition-0}
    - {device: disk-sda, size: 53687091200, wipe: superblock, flag: '', number: 2,
      preserve: false, grub_device: false, type: partition, id: partition-1}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-0}
    - {device: format-0, path: /, type: mount, id: mount-0}
  version: 1

meta-data文件:

cat > meta-data << EOF
instance-id: focal-autoinstall
EOF

文件拷贝

将meta-data 、user-data文件拷贝到/home/www/ubuntu/目录下

修改引导配置文件

文件路径:

/home/www/ubuntu/isolinux/txt.cfg

原文件:

default live
label live
  menu label ^Install Ubuntu Server
  kernel /casper/vmlinuz
  append   initrd=/casper/initrd quiet ---
label hwe-live
  menu label ^Install Ubuntu Server with the HWE kernel
  kernel /casper/hwe-vmlinuz
  append   initrd=/casper/hwe-initrd quiet  ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80

修改后文件:

default live
label live
  menu label ^Install Ubuntu Server
  kernel /casper/vmlinuz
  append   initrd=/casper/initrd quiet autoinstall ds=nocloud;s=/cdrom/ ---
label hwe-live
  menu label ^Install Ubuntu Server with the HWE kernel
  kernel /casper/hwe-vmlinuz
  append   initrd=/casper/hwe-initrd quiet  ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80

配置完成。

打包成iso镜像文件

进入/home/www/目录执行如下命令对ubuntu/目录进行打包

#安装mkisofs
sudo apt install mkisofs -y
sudo mkisofs -R -J -T -v -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat -o auto-ubuntu-20.04.4-server-amd64.iso  ubuntu/
  • 会在/home/www/目录下生成auto-ubuntu-20.04.4-server-amd64.iso文件
  • 就可以使用该镜像文件进行自动安装了

测试通过环境:

  • virtualbox
  • 打包环境系统:ubuntu20.04
  • 软件:mkisofs
posted @ 2021-06-29 23:18  炒鸡蛋  阅读(7390)  评论(8编辑  收藏  举报