1. 程式人生 > 其它 >ubuntu20.04打包iso映象自動安裝

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