#cloud-config autoinstall: # version is an Autoinstall required field. version:1
# This adds the default ubuntu-desktop packages to the system. # Any desired additional packages may also be listed here. packages: -ubuntu-server
# User creation can occur in one of 3 ways: # 1. Create a user using this `identity` section. # 2. Create users as documented in cloud-init inside the user-data section, # which means this single-user identity section may be removed. # 3. Prompt for user configuration on first boot. Remove this identity # section and see the "Installation without a default user" section. identity: realname:'UntouchedWagons' username:untouchedwagons # A password hash is needed. `openssl passwd -6 $CLEARTEXT_PASSWORD` can help. password:'' hostname:ubuntu-test
locale:en_US.UTF-8 keyboard: layout:us
package_update:true package_upgrade:true
# Subiquity will, by default, configure a partition layout using LVM. # The 'direct' layout method shown here will produce a non-LVM result. storage: swap: size:0 layout: name:direct
functioncleanup() { if [ -n "${tmpdir}" ]; then rm -rf "$tmpdir" log"🚽 Deleted temporary working directory $tmpdir" fi if [ ! -f BOOT ]; then rm -rf BOOT log"🚽 Deleted BOOT directory " fi }
functiondie() { local msg=$1 local code=${2-1}# Bash parameter expansion - default exit status 1. See https://wiki.bash-hackers.org/syntax/pe#use_a_default_value log"$msg" exit"$code" }
functionusage() { cat <<EOF Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-a] [-u user-data-file] [-m meta-data-file] [-s source-iso-file] [-d destination-iso-file] 💁 This script will create fully-automated Ubuntu 22.04 Focal Fossa installation media. 💁 Ubuntu ISO Download Url: https://releases.ubuntu.com/20.04/ubuntu-20.04.6-live-server-amd64.iso 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 -u, --user-data Path to user-data file. Required if using -a -m, --meta-data Path to meta-data file. Will be an empty file if not specified and using -a -s, --source Source ISO file. That file will be used by default if it already exists. -d, --destination Destination ISO file. By default ${script_dir}/ubuntu-autoinstall-$today.iso will be created, overwriting any existing file. EOF exit }
#检查环境 functioncheckEnvironment () { log"🔎 Checking for required utilities..." #检查 date 命令是否可用 [[ ! -x "$(command -v date)" ]] && die "💥 date command not found." && exit 1 [[ ! -x "$(command -v xorriso)" ]] && die "💥 xorriso is not installed. On Ubuntu, install the 'xorriso' package." && exit 1 [[ ! -x "$(command -v 7z)" ]] && die "💥 sed is not installed. On Ubuntu, install the '7zip' package." && exit 1 log"👍 All required utilities are installed." }
functioncheckParam() { if [ ${all_in_one} -ne 0 ]; then [[ -z "${user_data_file}" ]] && die "💥 user-data file was not specified." && exit 1 [[ ! -f "$user_data_file" ]] && die "💥 user-data file could not be found." && exit 1 [[ -n "${meta_data_file}" ]] && [[ ! -f "$meta_data_file" ]] && die "💥 meta-data file could not be found." && exit 1 fi
if [[ -z "${source_iso}" ]]; then die "💥 Source ISO file was not specified." && exit 1 else [[ ! -f "${source_iso}" ]] && die "💥 Source ISO file could not be found." && exit 1 fi
log"🧩 Extracting ISO files..." 7z -y -bso0 -bsp1 x "$source_iso" -o"$tmpdir" log"👍 Extracted ISO file."
mv$tmpdir/'[BOOT]' ./BOOT
if [ ${all_in_one} -eq 1 ]; then log"🧩 Adding user-data and meta-data files..." mkdir"$tmpdir/server" cp"$user_data_file""$tmpdir/$install_conf_dir/user-data" if [ -n "${meta_data_file}" ]; then cp"$meta_data_file""$tmpdir/$install_conf_dir/meta-data" else touch"$tmpdir/$install_conf_dir/meta-data" fi log"👍 Added data and configured kernel command line."
log"🧩 Adding autoinstall parameter to kernel command line..." sed -i -e "s,---, autoinstall ds=nocloud-net;s=\"file:///cdrom/$install_conf_dir/\" ---,g""$tmpdir/boot/grub/grub.cfg" log"👍 Added parameter to UEFI and BIOS kernel command lines." fi