Skip to content

powerkissing/ubuntu-autoinstall-generator-tools

Β 
Β 

Repository files navigation

Ubuntu Release Version 20 To 22 Server Autoinstall

A script to generate a fully-automated ISO image for installing Ubuntu onto a machine without human interaction. This uses the new autoinstall method for Ubuntu and newer.

Requirements

Tested on a host running Ubuntu System

  • Utilities required:
xorriso
sed
curl
gpg
isolinux
p7zip-full
dpkg-dev

Note:

We all know that each release version number of ubuntu will be mapped to a name, the following table is the correspondence between them。

Nubmer Name
20.04.5 focal
22.04.1 jammy
22.10 kinetic

Basic Usage

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -h
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-a] [-e] [-u user-data-file] [-m meta-data-file] [-p package-name] [-c config-data-file] [-t temaplate-config-file] [-s service-dir-name] [-j job-name] [-k] [-o] [-r] [-d destination-iso-file]

πŸ’ This script will create fully-automated Ubuntu release version 20 to 22 installation media.

Available options:

-h, --help              Print this help and exit
-v, --verbose           Print script debug info
-a, --all-in-one        Bake user-data and meta-data into the generated ISO. By default you will
                        need to boot systems with a CIDATA volume attached containing your
                        autoinstall user-data and meta-data files.
                        For more information see: https://ubuntu.com/server/docs/install/autoinstall-quickstart
-e, --use-hwe-kernel    Force the generated ISO to boot using the hardware enablement (HWE) kernel. Not supported
                        by early Ubuntu 20.04 release ISOs.
-u, --user-data         Path to user-data file. Required if using -a
-n, --release-name      Specifies the code name to download the ISO image distribution, You must select any string
                        from the list as an argument. eg: focal, jammy, kinetic.
-m, --meta-data         Path to meta-data file. Will be an empty file if not specified and using -a
-p, --package-name      Bake he package-name downloaded installation into the generated ISO. if the package-name is empty,
                        no installation package will be downloaded. Path to package-name file. Required if using -a
-c, --config-data       Path to config-data file. Required if using -a
-t  --temaplate-config  Path to temaplate-config file. Required if using -a
-k, --no-verify         Disable GPG verification of the source ISO file. By default SHA256SUMS-$today and
                        SHA256SUMS-$today.gpg in ${script_dir} will be used to verify the authenticity and integrity
                        of the source ISO file. If they are not present the latest daily SHA256SUMS will be
                        downloaded and saved in ${script_dir}. The Ubuntu signing key will be downloaded and
                        saved in a new keyring in ${script_dir}
-o, --no-md5            Disable MD5 checksum on boot
-r, --use-release-iso   Use the current release ISO instead of the daily ISO. The file will be used if it already
                        exists.
-d, --destination       Destination ISO file. By default ${script_dir}/ubuntu-autoinstall-$today.iso will be
                        created, overwriting any existing file.
-s  --service-dir-name  Bake service-dir-name into the generated ISO. if service-dir-name is not specified, no local application
                        will be uploaded to complete the ISO build. Path to service-dir-name directory. Required if using -a
-j  --job-name         Bake job-name into the generated ISO. if job-name is not specified, there will be
                       no action to change after the service starts. Path to job-name file. Required if using -a

Example

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -d ubuntu-autoinstall-jammytest.iso      
[2022-12-14 01:03:12] πŸ‘Ά Starting up...
[2022-12-14 01:03:12] πŸ”Ž Checking for current release...
[2022-12-14 01:03:13] πŸ’Ώ Current release is 22.04.1
[2022-12-14 01:03:14] πŸ“ Created temporary working directory /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:14] πŸ”Ž Checking for required utilities...
[2022-12-14 01:03:14] πŸ‘ All required utilities are installed.
[2022-12-14 01:03:14] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-14 01:03:14] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-14 01:03:14] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-14 01:03:14] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-14 01:03:24] πŸ‘ Verification succeeded.
[2022-12-14 01:03:24] πŸ”§ Extracting ISO image...
[2022-12-14 01:03:27] πŸ‘ Extracted to /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:16:23] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-14 01:16:23] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-14 01:16:23] 🧩 Adding user-data and meta-data files...
[2022-12-14 01:16:23] πŸ‘ Added data and configured kernel command line.
[2022-12-14 01:16:23] πŸ‘· Updating /tmp/tmp.OYliQ5b0VL/md5sum.txt with hashes of modified files...
[2022-12-14 01:16:23] πŸ‘ Updated hashes.
[2022-12-14 01:16:23] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-14 01:16:38] πŸ‘ Repackaged into /root/ubuntu/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammy.iso
[2022-12-14 01:16:38] βœ… Completed.
[2022-12-14 01:16:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

Now you can boot your target machine using ubuntu-autoinstall-jammy.iso and it will automatically install Ubuntu using the configuration from user-data-example. Also, you can select the version you want to build with the required parameter -n.

Advanced Usage

Only download the installation package

When you just download the installation package from the Internet, you do not have to modify the configuration file of the installation package, just specify -p

Example

first,you shoule be configure the package-name.txt of the installation packages names, for example:

# Define the name of the package to be downloaded from the Internet
 net-tools
mariadb-server
gcc
keepalived
samba samba-common

Then you also need to add the following parameter to the late-command configuration field in user-data, for example:

Note: that the parameters are fixed and are not allowed to be modified

  late-commands:
    - cp -rp /cdrom/mnt /target/
    - chmod +x /target/mnt/script/install-pkgs.sh
    - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh

Finally, you need to specify on the command line the package-name.txt that you want to download the installer from, with the -p parameter.

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -p package-name.txt -d ubuntu-autoinstall-jammytest.iso      
[2022-12-14 01:03:12] πŸ‘Ά Starting up...
[2022-12-14 01:03:12] πŸ”Ž Checking for current release...
[2022-12-14 01:03:13] πŸ’Ώ Current release is 22.04.1
[2022-12-14 01:03:14] πŸ“ Created temporary working directory /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:14] πŸ”Ž Checking for required utilities...
[2022-12-14 01:03:14] πŸ‘ All required utilities are installed.
[2022-12-14 01:03:14] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-14 01:03:14] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-14 01:03:14] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-14 01:03:14] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-14 01:03:24] πŸ‘ Verification succeeded.
[2022-12-14 01:03:24] πŸ”§ Extracting ISO image...
[2022-12-14 01:03:27] πŸ‘ Extracted to /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:27] 🌎 Downloading and saving packages tcpdump
[2022-12-14 01:04:48] 🌎 Downloading and saving packages net-tools
[2022-12-14 01:04:51] 🌎 Downloading and saving packages gcc
[2022-12-14 01:10:52] 🌎 Downloading and saving packages mysql-server
[2022-12-14 01:16:23] πŸ‘ Downloaded packages and saved to /tmp/tmp.OYliQ5b0VL/mnt/pkgs
[2022-12-14 01:16:23] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-14 01:16:23] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-14 01:16:23] 🧩 Adding user-data and meta-data files...
[2022-12-14 01:16:23] πŸ‘ Added data and configured kernel command line.
[2022-12-14 01:16:23] πŸ‘· Updating /tmp/tmp.OYliQ5b0VL/md5sum.txt with hashes of modified files...
[2022-12-14 01:16:23] πŸ‘ Updated hashes.
[2022-12-14 01:16:23] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-14 01:16:38] πŸ‘ Repackaged into /root/ubuntu/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammy.iso
[2022-12-14 01:16:38] βœ… Completed.
[2022-12-14 01:16:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

Download the installation package, and modify it before the APP Service is started.

When you specify -p in your script to download the dependencies from the Internet, If you want to change the default values of the configuration file through a template or command before starting the service.

Example

The following is an example of a mysql config file change operation, Three flexible methods are provided here, choose any one of them.

1. You can modify the configuration file by using the linux command, for example:

You can modify the parameters of the configuration file directly with the linux command in the late-command configuration section of user-dat, for example: Note: Except for the sed command line, which can be changed, other commands are not allowed.

  late-commands:
    - cp -rp /cdrom/mnt /target/
    - chmod +x /target/mnt/script/install-pkgs.sh
    - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh
    # modfiy mysql parameters of the configruation, as the following:
    - sed -i '/^bind-address/c\port = 13306' /target/etc/mysql/mariadb.conf.d/50-server.cnf
    - sed -i '/^#key_buffer_size/c\key_buffer_size = 128M' /target/etc/mysql/mariadb.conf.d/50-server.cnf

Finally, you need to specify on the command line the package-name.txt that you want to download the installer from, with the -p parameter.

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -p package-name.txt -d ubuntu-autoinstall-jammytest.iso      
[2022-12-14 01:03:12] πŸ‘Ά Starting up...
[2022-12-14 01:03:12] πŸ”Ž Checking for current release...
[2022-12-14 01:03:13] πŸ’Ώ Current release is 22.04.1
[2022-12-14 01:03:14] πŸ“ Created temporary working directory /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:14] πŸ”Ž Checking for required utilities...
[2022-12-14 01:03:14] πŸ‘ All required utilities are installed.
[2022-12-14 01:03:14] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-14 01:03:14] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-14 01:03:14] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-14 01:03:14] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-14 01:03:24] πŸ‘ Verification succeeded.
[2022-12-14 01:03:24] πŸ”§ Extracting ISO image...
[2022-12-14 01:03:27] πŸ‘ Extracted to /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:27] 🌎 Downloading and saving packages tcpdump
[2022-12-14 01:04:48] 🌎 Downloading and saving packages net-tools
[2022-12-14 01:04:51] 🌎 Downloading and saving packages gcc
[2022-12-14 01:10:52] 🌎 Downloading and saving packages mysql-server
[2022-12-14 01:16:23] πŸ‘ Downloaded packages and saved to /tmp/tmp.OYliQ5b0VL/mnt/pkgs
[2022-12-14 01:16:23] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-14 01:16:23] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-14 01:16:23] 🧩 Adding user-data and meta-data files...
[2022-12-14 01:16:23] πŸ‘ Added data and configured kernel command line.
[2022-12-14 01:16:23] πŸ‘· Updating /tmp/tmp.OYliQ5b0VL/md5sum.txt with hashes of modified files...
[2022-12-14 01:16:23] πŸ‘ Updated hashes.
[2022-12-14 01:16:23] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-14 01:16:38] πŸ‘ Repackaged into /root/ubuntu/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammy.iso
[2022-12-14 01:16:38] βœ… Completed.
[2022-12-14 01:16:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

2. You can modify the configration file by useing the shell script, use the following methods

You should custom configure the config.sh script, and then reference it in the late-command, for example:

#!/bin/bash
#
sed -i '/^bind-address/c\port = 13306' /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i '/^#key_buffer_size/c\key_buffer_size = 128M' /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i '/^#max_allowed_packet/c\max_allowed_packet = 1G' /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i '/^#thread_stack/c\thread_stack = 512K' /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i '/^#thread_cache_size/c\thread_cache_size = 16' /etc/mysql/mariadb.conf.d/50-server.cnf

Note: that the parameters are fixed and are not allowed to be modified

  late-commands:
    - cp -rp /cdrom/mnt /target/
    - chmod +x /target/mnt/script/install-pkgs.sh
    - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh
    - chmod +x /target/mnt/script/config.sh
    - curtin in-target --target=/target -- /mnt/script/config.sh

Finally, you need to specify on the command line the package-name.txt that you want to download the installer from, with the -p parameter, And you also have to specify the configuration file with -c

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -p package-name.txt -c config.sh -d ubuntu-autoinstall-jammytest.iso      
[2022-12-14 01:03:12] πŸ‘Ά Starting up...
[2022-12-14 01:03:12] πŸ”Ž Checking for current release...
[2022-12-14 01:03:13] πŸ’Ώ Current release is 22.04.1
[2022-12-14 01:03:14] πŸ“ Created temporary working directory /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:14] πŸ”Ž Checking for required utilities...
[2022-12-14 01:03:14] πŸ‘ All required utilities are installed.
[2022-12-14 01:03:14] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-14 01:03:14] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-14 01:03:14] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-14 01:03:14] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-14 01:03:24] πŸ‘ Verification succeeded.
[2022-12-14 01:03:24] πŸ”§ Extracting ISO image...
[2022-12-14 01:03:27] πŸ‘ Extracted to /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:27] 🌎 Downloading and saving packages tcpdump
[2022-12-14 01:04:48] 🌎 Downloading and saving packages net-tools
[2022-12-14 01:04:51] 🌎 Downloading and saving packages gcc
[2022-12-14 01:10:52] 🌎 Downloading and saving packages mysql-server
[2022-12-14 01:16:23] πŸ‘ Downloaded packages and saved to /tmp/tmp.OYliQ5b0VL/mnt/pkgs
[2022-12-14 01:16:23] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-14 01:16:23] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-14 01:16:23] 🧩 Adding user-data and meta-data files...
[2022-12-14 01:16:23] πŸ‘ Added data and configured kernel command line.
[2022-12-14 01:16:23] πŸ‘· Updating /tmp/tmp.OYliQ5b0VL/md5sum.txt with hashes of modified files...
[2022-12-14 01:16:23] πŸ‘ Updated hashes.
[2022-12-14 01:16:23] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-14 01:16:38] πŸ‘ Repackaged into /root/ubuntu/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammy.iso
[2022-12-14 01:16:38] βœ… Completed.
[2022-12-14 01:16:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

3. You can modify the configration file by useing the template file

You need to make a copy of the template configuration file beforehand, and modify it to your desired state, and then reference it in the late-command, for example:

Here I am using the database template file is template.conf, Not in the specific display content

Note: Except for the source and destination configuration files of the template file, which can be changed, nothing else is allowed

  late-commands:    
    - cp -rp /cdrom/mnt /target/
    - chmod +x /target/mnt/script/install-pkgs.sh
    - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh
    - curtin in-target --target=/target -- cp /mnt/template.conf /etc/mysql/mariadb.conf.d/50-server.cnf
   #- curtin in-target --target=/target -- cp /mnt/template.conf /etc/nginx/config.d/app.conf

Finally, you need to specify on the command line the package-name.txt that you want to download the installer from, with the -p parameter, And you also have to specify the template file with -t

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -p package-name.txt -t template.conf -d ubuntu-autoinstall-jammytest.iso      
[2022-12-14 01:03:12] πŸ‘Ά Starting up...
[2022-12-14 01:03:12] πŸ”Ž Checking for current release...
[2022-12-14 01:03:13] πŸ’Ώ Current release is 22.04.1
[2022-12-14 01:03:14] πŸ“ Created temporary working directory /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:14] πŸ”Ž Checking for required utilities...
[2022-12-14 01:03:14] πŸ‘ All required utilities are installed.
[2022-12-14 01:03:14] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-14 01:03:14] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-14 01:03:14] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-14 01:03:14] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-14 01:03:24] πŸ‘ Verification succeeded.
[2022-12-14 01:03:24] πŸ”§ Extracting ISO image...
[2022-12-14 01:03:27] πŸ‘ Extracted to /tmp/tmp.OYliQ5b0VL
[2022-12-14 01:03:27] 🌎 Downloading and saving packages tcpdump
[2022-12-14 01:04:48] 🌎 Downloading and saving packages net-tools
[2022-12-14 01:04:51] 🌎 Downloading and saving packages gcc
[2022-12-14 01:10:52] 🌎 Downloading and saving packages mysql-server
[2022-12-14 01:16:23] πŸ‘ Downloaded packages and saved to /tmp/tmp.OYliQ5b0VL/mnt/pkgs
[2022-12-14 01:16:23] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-14 01:16:23] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-14 01:16:23] 🧩 Adding user-data and meta-data files...
[2022-12-14 01:16:23] πŸ‘ Added data and configured kernel command line.
[2022-12-14 01:16:23] πŸ‘· Updating /tmp/tmp.OYliQ5b0VL/md5sum.txt with hashes of modified files...
[2022-12-14 01:16:23] πŸ‘ Updated hashes.
[2022-12-14 01:16:23] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-14 01:16:38] πŸ‘ Repackaged into /root/ubuntu/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammy.iso
[2022-12-14 01:16:38] βœ… Completed.
[2022-12-14 01:16:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

Download the installation package, and modify it after the APP Service is started.

When you specify -p in your script to download the dependencies from the Internet, if you need to make changes after the image is installed and the service status is running, then you need to customize the script parameters in the rc.local file.

Example

The following is an example of a mysql password change operation

#!/bin/bash

#This script will be executed *after* all the other init scripts.
#You can put your own initialization stuff in here if you don't
#want to do the full Sys V style init stuff.

file="/etc/rc.local"

# the following functions are used for logging purposes and are not recommended to be modified
# set extraiable value
DATE=`date "+%Y-%m-%d %H:%M:%S"`
USER=`whoami`
HOST_NAME=`hostname`
LOG_FILE="/var/log/rc-local.log"

# Execution successful log printing path
function log_info () {
    echo "${DATE} ${HOST_NAME} ${USER} execute $0 [INFO] $@" >> ${LOG_FILE}
}

# Execution successful ⚠️ warning log print path
function log_warn () {
    echo "${DATE} ${HOST_NAME} ${USER} execute $0 [WARN] $@" >> ${LOG_FILE}
}

# Execution failure log print path
function log_error () {
    echo -e "\033[41;37m ${DATE} ${HOST_NAME} ${USER} execute $0 [ERROR] $@ \033[0m"  >> ${LOG_FILE}
}

function fn_log ()  {
    if [  $? -eq 0  ]
    then
            log_info "πŸ‘ $@ sucessed."
            echo -e "\033[32m $@ sucessed. \033[0m"
    else
            log_error "πŸ‘Ώ $@ failed."
            echo -e "\033[41;37m $@ failed. \033[0m"
            exit 1
    fi
}

# this is an example of password mysql change
mysql_user="root"
# default password is null
mysql_password="123456"
new_mysql_password="MsTac@2001"
while true; do
    processNum=`ps aux | grep mysql | grep -v grep | wc -l`;
    # change mysql password
    if [ $processNum -ne 0 ]; then
      log_info "waiting for 2s"
      sleep 2
      sudo mysql -u${mysql_user} -p${mysql_password}  << EOF
      GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY "${new_mysql_password}" WITH GRANT OPTION;
      GRANT ALL ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY "${new_mysql_password}" WITH GRANT OPTION;
      GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY "${new_mysql_password}" WITH GRANT OPTION;
      FLUSH PRIVILEGES;
      COMMIT;
EOF
      fn_log "Update mysql password"
      break
    else
      sleep 2
      log_info "waiting for 2s"
    fi
done
rm  -f ${file}
fn_log "Clean files ${file}"
exit 0

Then you also need to add the following parameter to the late-command configuration field in user-data, for example:

Note: that the parameters are fixed and are not allowed to be modified.

  late-commands:
    - cp -rp /cdrom/mnt /target/
    - chmod +x /target/mnt/script/install-pkgs.sh
    - chmod +x /target/mnt/script/config.sh
    - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh
    - cp /cdrom/rc-local.service /target/lib/systemd/system/rc-local.service
    - curtin in-target --target=/target -- ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
    - cp -p /cdrom/rc.local /target/etc/rc.local
    - chmod +x /target/etc/rc.local
    - systemctl daemon-reload

Finally, you need to specfiy the file name of the one-time task rc.lcoal on the command line, via the -j parameter

root@john-desktop:~/ubuntu20/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy -p package-name.txt -j rc.local -d ubuntu-autoinstall-jammytest.iso  
[2022-12-16 09:46:19] πŸ‘Ά Starting up...
[2022-12-16 09:46:19] πŸ”Ž Checking for current release...
[2022-12-16 09:46:21] πŸ’Ώ Current release is 22.04.1
[2022-12-16 09:46:21] πŸ“ Created temporary working directory /tmp/tmp.tRYNYKdmxv
[2022-12-16 09:46:21] πŸ”Ž Checking for required utilities...
[2022-12-16 09:46:21] πŸ‘ All required utilities are installed.
[2022-12-16 09:46:21] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-16 09:46:21] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-16 09:46:21] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-16 09:46:21] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-16 09:46:31] πŸ‘ Verification succeeded.
[2022-12-16 09:46:31] πŸ”§ Extracting ISO image...
[2022-12-16 09:46:38] πŸ‘ Extracted to /tmp/tmp.tRYNYKdmxv
[2022-12-16 09:46:38] 🌎 Downloading and saving packages net-tools
[2022-12-16 09:46:53] 🌎 Downloading and saving packages keepalived
[2022-12-16 09:48:03] 🌎 Downloading and saving packages nginx
[2022-12-16 09:50:13] 🌎 Downloading and saving packages mariadb-server
[2022-12-16 09:51:19] 🌎 Downloading and saving packages mariadb-client
[2022-12-16 09:51:21] 🚽 Deleted temporary file /tmp/tmp.tRYNYKdmxv/package-name.txt.
[2022-12-16 09:51:21] πŸ‘ Downloaded packages and saved to /tmp/tmp.tRYNYKdmxv/mnt/pkgs
[2022-12-16 09:51:21] πŸ“ Moving rc.local file to temporary working directory /tmp/tmp.tRYNYKdmxv/mnt/script.
[2022-12-16 09:51:21] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-16 09:51:21] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-16 09:51:21] 🧩 Adding user-data and meta-data files...
[2022-12-16 09:51:21] πŸ‘ Added data and configured kernel command line.
[2022-12-16 09:51:21] πŸ‘· Updating /tmp/tmp.tRYNYKdmxv/md5sum.txt with hashes of modified files...
[2022-12-16 09:51:21] πŸ‘ Updated hashes.
[2022-12-16 09:51:21] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-16 09:51:38] πŸ‘ Repackaged into /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammytest.iso
[2022-12-16 09:51:38] βœ… Completed.
[2022-12-16 09:51:38] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

Define your own local installer upload build ISO.

If you need to build a local application into the ISO image, you need to specify the -s parameter to provide the directory.

Example

The following is an example of local application

First, let's look at the service directory structure

root@john-desktop:~# tree service/
service/
β”œβ”€β”€ app      # binary file
β”œβ”€β”€ app.service
└── config
      └── config.ini

1 directory, 3 files
root@john-desktop:~#

Customize the app.service file as follows:

[Unit]
Description=Application Service Programs
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/service/
ExecStart=/opt/service/app
Restart=always

[Install]
WantedBy=multi-user.target

Then you also need to add the following parameter to the late-command configuration field in user-data, for example:

Note: In the late-command, I will copy the service directory to /cdrom/mnt, this path is not allowed to be modified, other can be modified, must be the name of the service, etc.

  late-commands:
    - cp -rp /cdrom/mnt/service /target/opt
    - cp /cdrom/mnt/service/app.service /target/lib/systemd/system/app.service
    - curtin in-target --target=/target -- ln -sn /lib/systemd/system/app.service /etc/systemd/system/multi-user.target.wants/app.service

Finally, You need to specify the app directory of the local application, specified by the -s parameter, The commands are as follows:

root@john-desktop:~/ubuntu/ubuntu-autoinstall-generator-tools# ./ubuntu-autoinstall-generator-tools.sh -a  -u user-data -n jammy  -p  package-name.txt -j rc.local  -s /root/service -d ubuntu-autoinstall-jammytest.iso             
[2022-12-16 10:43:27] πŸ‘Ά Starting up...
[2022-12-16 10:43:27] πŸ”Ž Checking for current release...
[2022-12-16 10:43:29] πŸ’Ώ Current release is 22.04.1
[2022-12-16 10:43:29] πŸ“ Created temporary working directory /tmp/tmp.X12RSWTKVK
[2022-12-16 10:43:29] πŸ”Ž Checking for required utilities...
[2022-12-16 10:43:29] πŸ‘ All required utilities are installed.
[2022-12-16 10:43:29] β˜‘οΈ Using existing /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso file.
[2022-12-16 10:43:29] β˜‘οΈ Using existing SHA256SUMS-22.04.1 & SHA256SUMS-22.04.1.gpg files.
[2022-12-16 10:43:29] β˜‘οΈ Using existing Ubuntu signing key saved in /root/ubuntu20/ubuntu-autoinstall-generator-tools/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-12-16 10:43:29] πŸ” Verifying /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-22.04.1-live-server-amd64.iso integrity and authenticity...
[2022-12-16 10:43:39] πŸ‘ Verification succeeded.
[2022-12-16 10:43:39] πŸ”§ Extracting ISO image...
[2022-12-16 10:43:49] πŸ‘ Extracted to /tmp/tmp.X12RSWTKVK
[2022-12-16 10:43:49] 🌎 Downloading and saving packages net-tools
[2022-12-16 10:43:55] 🌎 Downloading and saving packages keepalived
[2022-12-16 10:44:15] 🌎 Downloading and saving packages nginx
[2022-12-16 10:44:48] 🌎 Downloading and saving packages mariadb-server
[2022-12-16 10:45:05] 🌎 Downloading and saving packages mariadb-client
[2022-12-16 10:45:07] 🚽 Deleted temporary file /tmp/tmp.X12RSWTKVK/package-name.txt.
[2022-12-16 10:45:07] πŸ‘ Downloaded packages and saved to /tmp/tmp.X12RSWTKVK/extra/pkgs
[2022-12-16 10:45:07] πŸ“ Moving rc.local file to temporary working directory /tmp/tmp.X12RSWTKVK/mnt/script.
[2022-12-16 10:45:07] πŸ“ Moving /root/tmp directory to temporary working directory /tmp/tmp.X12RSWTKVK/mnt/ 
[2022-12-16 10:45:07] 🧩 Adding autoinstall parameter to kernel command line...
[2022-12-16 10:45:07] πŸ‘ Added parameter to UEFI and BIOS kernel command lines.
[2022-12-16 10:45:07] 🧩 Adding user-data and meta-data files...
[2022-12-16 10:45:07] πŸ‘ Added data and configured kernel command line.
[2022-12-16 10:45:07] πŸ‘· Updating /tmp/tmp.X12RSWTKVK/md5sum.txt with hashes of modified files...
[2022-12-16 10:45:07] πŸ‘ Updated hashes.
[2022-12-16 10:45:07] πŸ“¦ Repackaging extracted files into an ISO image...
[2022-12-16 10:45:23] πŸ‘ Repackaged into /root/ubuntu20/ubuntu-autoinstall-generator-tools/ubuntu-autoinstall-jammytest.iso
[2022-12-16 10:45:23] βœ… Completed.
[2022-12-16 10:45:23] 🚽 Deleted temporary working directory /tmp/tmp.OYliQ5b0VL

Thanks

The tool was created with reference to a large number of articles, including: ubuntu-jammy-netinstall-pxe,ubuntu-autoinstall-generator, ubuntu-desktop-22.04-autoinstall,ubuntu 22.04 autoinstall, The script ubuntu-autoinstall-generator is based on the version control, and some parameters optimization, thanks to the developer's open source contribution.

About

Generate a fully-automated Ubuntu ISO for unattended installations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%