mkarchiso: general cleanup and simplification

- Remove remnants of the now removed legacy commands.
- Improve readability by getting rid of some "if" statements when performing string comparisons.
- Rename functions to make their purpose more clear.
- Move some conditions from functions to their invocations.
This commit is contained in:
nl6720 2020-11-01 09:39:36 +02:00
parent bb91fd52d9
commit 57d510fe7f
No known key found for this signature in database
GPG Key ID: 5CE88535E188D369

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
@ -12,7 +12,6 @@ export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-"$(date +%s)"}"
# mkarchiso defaults # mkarchiso defaults
app_name="${0##*/}" app_name="${0##*/}"
pkg_list=() pkg_list=()
run_cmd=""
quiet="y" quiet="y"
work_dir="work" work_dir="work"
out_dir="out" out_dir="out"
@ -66,15 +65,6 @@ _msg_error() {
fi fi
} }
_chroot_init() {
install -d -m 0755 -o 0 -g 0 -- "${airootfs_dir}"
_pacman base syslinux
}
_chroot_run() {
eval -- arch-chroot "${airootfs_dir}" "${run_cmd}"
}
_mount_airootfs() { _mount_airootfs() {
trap "_umount_airootfs" EXIT HUP INT TERM trap "_umount_airootfs" EXIT HUP INT TERM
install -d -m 0755 -- "${work_dir}/mnt/airootfs" install -d -m 0755 -- "${work_dir}/mnt/airootfs"
@ -123,8 +113,7 @@ ENDUSAGETEXT
exit "${1}" exit "${1}"
} }
# Shows configuration according to command mode. # Shows configuration options.
# $1: build_profile | init | install | run | prepare | iso
_show_config() { _show_config() {
local build_date local build_date
build_date="$(date --utc --iso-8601=seconds -d "@${SOURCE_DATE_EPOCH}")" build_date="$(date --utc --iso-8601=seconds -d "@${SOURCE_DATE_EPOCH}")"
@ -143,50 +132,24 @@ _show_config() {
_msg_info " ISO application: ${iso_application}" _msg_info " ISO application: ${iso_application}"
_msg_info " Boot modes: ${bootmodes[*]}" _msg_info " Boot modes: ${bootmodes[*]}"
_msg_info " Packages: ${pkg_list[*]}" _msg_info " Packages: ${pkg_list[*]}"
[[ "${quiet}" == "y" ]] || printf '\n'
}
# Install desired packages to airootfs
_pacman() {
_msg_info "Installing packages to '${airootfs_dir}/'..."
if [[ "${quiet}" = "y" ]]; then
pacstrap -C "${work_dir}/pacman.conf" -c -G -M -- "${airootfs_dir}" "$@" &> /dev/null
else
pacstrap -C "${work_dir}/pacman.conf" -c -G -M -- "${airootfs_dir}" "$@"
fi
_msg_info "Done! Packages installed successfully."
} }
# Cleanup airootfs # Cleanup airootfs
_cleanup() { _cleanup_airootfs() {
_msg_info "Cleaning up what we can on airootfs..." _msg_info "Cleaning up what we can on airootfs..."
# Delete all files in /boot # Delete all files in /boot
if [[ -d "${airootfs_dir}/boot" ]]; then [[ -d "${airootfs_dir}/boot" ]] && find "${airootfs_dir}/boot" -mindepth 1 -delete
find "${airootfs_dir}/boot" -mindepth 1 -delete
fi
# Delete pacman database sync cache files (*.tar.gz) # Delete pacman database sync cache files (*.tar.gz)
if [[ -d "${airootfs_dir}/var/lib/pacman" ]]; then [[ -d "${airootfs_dir}/var/lib/pacman" ]] && find "${airootfs_dir}/var/lib/pacman" -maxdepth 1 -type f -delete
find "${airootfs_dir}/var/lib/pacman" -maxdepth 1 -type f -delete
fi
# Delete pacman database sync cache # Delete pacman database sync cache
if [[ -d "${airootfs_dir}/var/lib/pacman/sync" ]]; then [[ -d "${airootfs_dir}/var/lib/pacman/sync" ]] && find "${airootfs_dir}/var/lib/pacman/sync" -delete
find "${airootfs_dir}/var/lib/pacman/sync" -delete
fi
# Delete pacman package cache # Delete pacman package cache
if [[ -d "${airootfs_dir}/var/cache/pacman/pkg" ]]; then [[ -d "${airootfs_dir}/var/cache/pacman/pkg" ]] && find "${airootfs_dir}/var/cache/pacman/pkg" -type f -delete
find "${airootfs_dir}/var/cache/pacman/pkg" -type f -delete
fi
# Delete all log files, keeps empty dirs. # Delete all log files, keeps empty dirs.
if [[ -d "${airootfs_dir}/var/log" ]]; then [[ -d "${airootfs_dir}/var/log" ]] && find "${airootfs_dir}/var/log" -type f -delete
find "${airootfs_dir}/var/log" -type f -delete
fi
# Delete all temporary files and dirs # Delete all temporary files and dirs
if [[ -d "${airootfs_dir}/var/tmp" ]]; then [[ -d "${airootfs_dir}/var/tmp" ]] && find "${airootfs_dir}/var/tmp" -mindepth 1 -delete
find "${airootfs_dir}/var/tmp" -mindepth 1 -delete
fi
# Delete package pacman related files. # Delete package pacman related files.
find "${work_dir}" \( -name '*.pacnew' -o -name '*.pacsave' -o -name '*.pacorig' \) -delete find "${work_dir}" \( -name '*.pacnew' -o -name '*.pacsave' -o -name '*.pacorig' \) -delete
# Create an empty /etc/machine-id # Create an empty /etc/machine-id
@ -195,28 +158,18 @@ _cleanup() {
_msg_info "Done!" _msg_info "Done!"
} }
_mkairootfs_create_image() { _run_mksquashfs() {
if (( $# < 1 )); then local image_path="${isofs_dir}/${install_dir}/${arch}/airootfs.sfs"
_msg_error "Function '${FUNCNAME[0]}' requires at least one argument" 1
fi
image_path="${isofs_dir}/${install_dir}/${arch}/airootfs.sfs"
if [[ "${airootfs_image_type}" =~ .*squashfs ]] ; then
if [[ "${quiet}" == "y" ]]; then if [[ "${quiet}" == "y" ]]; then
mksquashfs "$@" "${image_path}" -noappend "${airootfs_image_tool_options[@]}" -no-progress > /dev/null mksquashfs "$@" "${image_path}" -noappend "${airootfs_image_tool_options[@]}" -no-progress > /dev/null
else else
mksquashfs "$@" "${image_path}" -noappend "${airootfs_image_tool_options[@]}" mksquashfs "$@" "${image_path}" -noappend "${airootfs_image_tool_options[@]}"
fi fi
else
_msg_error "Unsupported image type: '${airootfs_image_type}'" 1
fi
} }
# Makes a ext4 filesystem inside a SquashFS from a source directory. # Makes a ext4 filesystem inside a SquashFS from a source directory.
_mkairootfs_img() { _mkairootfs_ext4+squashfs() {
if [[ ! -e "${airootfs_dir}" ]]; then [[ -e "${airootfs_dir}" ]] || _msg_error "The path '${airootfs_dir}' does not exist" 1
_msg_error "The path '${airootfs_dir}' does not exist" 1
fi
_msg_info "Creating ext4 image of 32 GiB..." _msg_info "Creating ext4 image of 32 GiB..."
if [[ "${quiet}" == "y" ]]; then if [[ "${quiet}" == "y" ]]; then
@ -234,20 +187,18 @@ _mkairootfs_img() {
_umount_airootfs _umount_airootfs
install -d -m 0755 -- "${isofs_dir}/${install_dir}/${arch}" install -d -m 0755 -- "${isofs_dir}/${install_dir}/${arch}"
_msg_info "Creating SquashFS image, this may take some time..." _msg_info "Creating SquashFS image, this may take some time..."
_mkairootfs_create_image "${airootfs_dir}.img" _run_mksquashfs "${airootfs_dir}.img"
_msg_info "Done!" _msg_info "Done!"
rm -- "${airootfs_dir}.img" rm -- "${airootfs_dir}.img"
} }
# Makes a SquashFS filesystem from a source directory. # Makes a SquashFS filesystem from a source directory.
_mkairootfs_sfs() { _mkairootfs_squashfs() {
if [[ ! -e "${airootfs_dir}" ]]; then [[ -e "${airootfs_dir}" ]] || _msg_error "The path '${airootfs_dir}' does not exist" 1
_msg_error "The path '${airootfs_dir}' does not exist" 1
fi
install -d -m 0755 -- "${isofs_dir}/${install_dir}/${arch}" install -d -m 0755 -- "${isofs_dir}/${install_dir}/${arch}"
_msg_info "Creating SquashFS image, this may take some time..." _msg_info "Creating SquashFS image, this may take some time..."
_mkairootfs_create_image "${airootfs_dir}" _run_mksquashfs "${airootfs_dir}"
_msg_info "Done!" _msg_info "Done!"
} }
@ -334,17 +285,27 @@ _make_custom_airootfs() {
fi fi
} }
# Packages (airootfs) # Install desired packages to airootfs
_make_packages() { _make_packages() {
_msg_info "Installing packages to '${airootfs_dir}/'..."
if [[ -n "${gpg_key}" ]]; then if [[ -n "${gpg_key}" ]]; then
exec {ARCHISO_GNUPG_FD}<>"${work_dir}/pubkey.gpg" exec {ARCHISO_GNUPG_FD}<>"${work_dir}/pubkey.gpg"
export ARCHISO_GNUPG_FD export ARCHISO_GNUPG_FD
fi fi
_pacman "${pkg_list[@]}"
if [[ "${quiet}" = "y" ]]; then
pacstrap -C "${work_dir}/pacman.conf" -c -G -M -- "${airootfs_dir}" "${pkg_list[@]}" &> /dev/null
else
pacstrap -C "${work_dir}/pacman.conf" -c -G -M -- "${airootfs_dir}" "${pkg_list[@]}"
fi
if [[ -n "${gpg_key}" ]]; then if [[ -n "${gpg_key}" ]]; then
exec {ARCHISO_GNUPG_FD}<&- exec {ARCHISO_GNUPG_FD}<&-
unset ARCHISO_GNUPG_FD unset ARCHISO_GNUPG_FD
fi fi
_msg_info "Done! Packages installed successfully."
} }
# Customize installation (airootfs) # Customize installation (airootfs)
@ -368,8 +329,7 @@ _make_customize_airootfs() {
if [[ -e "${airootfs_dir}/root/customize_airootfs.sh" ]]; then if [[ -e "${airootfs_dir}/root/customize_airootfs.sh" ]]; then
_msg_info "Running customize_airootfs.sh in '${airootfs_dir}' chroot..." _msg_info "Running customize_airootfs.sh in '${airootfs_dir}' chroot..."
_msg_warning "customize_airootfs.sh is deprecated! Support for it will be removed in a future archiso version." _msg_warning "customize_airootfs.sh is deprecated! Support for it will be removed in a future archiso version."
local run_cmd="/root/customize_airootfs.sh" eval -- arch-chroot "${airootfs_dir}" "/root/customize_airootfs.sh"
_chroot_run
rm -- "${airootfs_dir}/root/customize_airootfs.sh" rm -- "${airootfs_dir}/root/customize_airootfs.sh"
_msg_info "Done! customize_airootfs.sh run successfully." _msg_info "Done! customize_airootfs.sh run successfully."
fi fi
@ -379,8 +339,8 @@ _make_customize_airootfs() {
_make_bootmodes() { _make_bootmodes() {
local bootmode local bootmode
for bootmode in "${bootmodes[@]}"; do for bootmode in "${bootmodes[@]}"; do
if typeset -f "_make_boot_${bootmode}" &> /dev/null; then if typeset -f "_make_bootmode_${bootmode}" &> /dev/null; then
_run_once "_make_boot_${bootmode}" _run_once "_make_bootmode_${bootmode}"
else else
_msg_error "${bootmode} is not a valid boot mode" 1 _msg_error "${bootmode} is not a valid boot mode" 1
fi fi
@ -388,7 +348,7 @@ _make_bootmodes() {
} }
# Prepare kernel/initramfs ${install_dir}/boot/ # Prepare kernel/initramfs ${install_dir}/boot/
_make_boot_on_iso() { _make_boot_on_iso9660() {
local ucode_image local ucode_image
_msg_info "Preparing kernel and intramfs for the ISO 9660 file system..." _msg_info "Preparing kernel and intramfs for the ISO 9660 file system..."
install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/${arch}" install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/${arch}"
@ -409,7 +369,7 @@ _make_boot_on_iso() {
} }
# Prepare /${install_dir}/boot/syslinux # Prepare /${install_dir}/boot/syslinux
_make_boot_bios.syslinux.mbr() { _make_bootmode_bios.syslinux.mbr() {
_msg_info "Setting up SYSLINUX for BIOS booting from a disk..." _msg_info "Setting up SYSLINUX for BIOS booting from a disk..."
install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/syslinux" install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/syslinux"
for _cfg in "${profile}/syslinux/"*.cfg; do for _cfg in "${profile}/syslinux/"*.cfg; do
@ -425,7 +385,7 @@ _make_boot_bios.syslinux.mbr() {
install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/lpxelinux.0" "${isofs_dir}/${install_dir}/boot/syslinux/" install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/lpxelinux.0" "${isofs_dir}/${install_dir}/boot/syslinux/"
install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/memdisk" "${isofs_dir}/${install_dir}/boot/syslinux/" install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/memdisk" "${isofs_dir}/${install_dir}/boot/syslinux/"
_run_once _make_boot_on_iso _run_once _make_boot_on_iso9660
if [[ -e "${isofs_dir}/${install_dir}/boot/syslinux/hdt.c32" ]]; then if [[ -e "${isofs_dir}/${install_dir}/boot/syslinux/hdt.c32" ]]; then
install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/syslinux/hdt" install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/syslinux/hdt"
@ -449,7 +409,7 @@ _make_boot_bios.syslinux.mbr() {
} }
# Prepare /isolinux # Prepare /isolinux
_make_boot_bios.syslinux.eltorito() { _make_bootmode_bios.syslinux.eltorito() {
_msg_info "Setting up SYSLINUX for BIOS booting from an optical disc..." _msg_info "Setting up SYSLINUX for BIOS booting from an optical disc..."
install -d -m 0755 -- "${isofs_dir}/isolinux" install -d -m 0755 -- "${isofs_dir}/isolinux"
for _cfg in "${profile}/isolinux/"*".cfg"; do for _cfg in "${profile}/isolinux/"*".cfg"; do
@ -463,13 +423,13 @@ _make_boot_bios.syslinux.eltorito() {
install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/ldlinux.c32" "${isofs_dir}/isolinux/" install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/ldlinux.c32" "${isofs_dir}/isolinux/"
# isolinux.cfg loads syslinux.cfg # isolinux.cfg loads syslinux.cfg
_run_once _make_boot_bios.syslinux.mbr _run_once _make_bootmode_bios.syslinux.mbr
_msg_info "Done! SYSLINUX set up for BIOS booting from an optical disc successfully." _msg_info "Done! SYSLINUX set up for BIOS booting from an optical disc successfully."
} }
# Prepare /EFI on ISO-9660 # Prepare /EFI on ISO-9660
_make_efi() { _make_efi_dir_on_iso9660() {
_msg_info "Preparing an /EFI directory for the ISO 9660 file system..." _msg_info "Preparing an /EFI directory for the ISO 9660 file system..."
install -d -m 0755 -- "${isofs_dir}/EFI/BOOT" install -d -m 0755 -- "${isofs_dir}/EFI/BOOT"
install -m 0644 -- "${airootfs_dir}/usr/lib/systemd/boot/efi/systemd-bootx64.efi" \ install -m 0644 -- "${airootfs_dir}/usr/lib/systemd/boot/efi/systemd-bootx64.efi" \
@ -515,7 +475,7 @@ _make_boot_on_fat() {
} }
# Prepare efiboot.img::/EFI for EFI boot mode # Prepare efiboot.img::/EFI for EFI boot mode
_make_boot_uefi-x64.systemd-boot.esp() { _make_bootmode_uefi-x64.systemd-boot.esp() {
local efiboot_imgsize="0" local efiboot_imgsize="0"
_msg_info "Setting up systemd-boot for UEFI booting..." _msg_info "Setting up systemd-boot for UEFI booting..."
@ -564,36 +524,31 @@ _make_boot_uefi-x64.systemd-boot.esp() {
} }
# Prepare efiboot.img::/EFI for "El Torito" EFI boot mode # Prepare efiboot.img::/EFI for "El Torito" EFI boot mode
_make_boot_uefi-x64.systemd-boot.eltorito() { _make_bootmode_uefi-x64.systemd-boot.eltorito() {
_run_once _make_boot_uefi-x64.systemd-boot.esp _run_once _make_bootmode_uefi-x64.systemd-boot.esp
# Set up /EFI on ISO-9660 # Set up /EFI on ISO-9660 to allow preparing an installation medium by manually copying files
_run_once _make_efi _run_once _make_efi_dir_on_iso9660
} }
# Build airootfs filesystem image # Build airootfs filesystem image
_make_prepare() { _prepare_airootfs_image() {
if [[ "${airootfs_image_type}" == "squashfs" ]]; then # prepare airootfs.sfs for overlayfs usage (default) if typeset -f "_mkairootfs_${airootfs_image_type}" &> /dev/null; then
_run_once _mkairootfs_sfs _run_once "_mkairootfs_${airootfs_image_type}"
elif [[ "${airootfs_image_type}" == "ext4+squashfs" ]]; then # prepare airootfs.sfs for dm-snapshot usage
_run_once _mkairootfs_img
else else
_msg_error "Unsupported image type: '${airootfs_image_type}'" 1 _msg_error "Unsupported image type: '${airootfs_image_type}'" 1
fi fi
_mkchecksum _mkchecksum
if [[ "${gpg_key}" ]]; then [[ -n "${gpg_key}" ]] && _mksignature
_mksignature
fi
} }
# Build ISO # Build ISO
_make_iso() { _build_iso() {
local xorrisofs_options=() local xorrisofs_options=()
[[ -d "${out_dir}" ]] || install -d -- "${out_dir}" [[ -d "${out_dir}" ]] || install -d -- "${out_dir}"
if [[ "${quiet}" == "y" ]]; then [[ "${quiet}" == "y" ]] && xorrisofs_options+=('-quiet')
xorrisofs_options+=('-quiet')
fi
# xorrisofs options for x86 BIOS booting using SYSLINUX # xorrisofs options for x86 BIOS booting using SYSLINUX
# shellcheck disable=SC2076 # shellcheck disable=SC2076
@ -647,9 +602,7 @@ _make_iso() {
# xorrisofs options for X64 UEFI booting using systemd-boot # xorrisofs options for X64 UEFI booting using systemd-boot
# shellcheck disable=SC2076 # shellcheck disable=SC2076
if [[ " ${bootmodes[*]} " =~ ' uefi-x64.systemd-boot.' ]]; then if [[ " ${bootmodes[*]} " =~ ' uefi-x64.systemd-boot.' ]]; then
if [[ ! -f "${work_dir}/efiboot.img" ]]; then [[ -f "${work_dir}/efiboot.img" ]] || _msg_error "The file '${work_dir}/efiboot.img' does not exist." 1
_msg_error "The file '${work_dir}/efiboot.img' does not exist." 1
fi
[[ -e "${isofs_dir}/EFI/archiso" ]] && rm -rf -- "${isofs_dir}/EFI/archiso" [[ -e "${isofs_dir}/EFI/archiso" ]] && rm -rf -- "${isofs_dir}/EFI/archiso"
# systemd-boot in an attached EFI system partition # systemd-boot in an attached EFI system partition
@ -744,31 +697,17 @@ _read_profile() {
# set overrides from mkarchiso option parameters, if present # set overrides from mkarchiso option parameters, if present
_set_overrides() { _set_overrides() {
if [[ -n "$override_iso_label" ]]; then [[ -n "$override_iso_label" ]] && iso_label="$override_iso_label"
iso_label="$override_iso_label" [[ -n "$override_iso_publisher" ]] && iso_publisher="$override_iso_publisher"
fi [[ -n "$override_iso_application" ]] && iso_application="$override_iso_application"
if [[ -n "$override_iso_publisher" ]]; then [[ -n "$override_install_dir" ]] && install_dir="$override_install_dir"
iso_publisher="$override_iso_publisher" [[ -n "$override_pacman_conf" ]] && pacman_conf="$override_pacman_conf"
fi [[ -n "$override_gpg_key" ]] && gpg_key="$override_gpg_key"
if [[ -n "$override_iso_application" ]]; then
iso_application="$override_iso_application"
fi
if [[ -n "$override_install_dir" ]]; then
install_dir="$override_install_dir"
fi
if [[ -n "$override_pacman_conf" ]]; then
pacman_conf="$override_pacman_conf"
fi
if [[ -n "$override_gpg_key" ]]; then
gpg_key="$override_gpg_key"
fi
} }
_export_gpg_publickey() { _export_gpg_publickey() {
if [[ -n "${gpg_key}" ]]; then
gpg --batch --output "${work_dir}/pubkey.gpg" --export "${gpg_key}" gpg --batch --output "${work_dir}/pubkey.gpg" --export "${gpg_key}"
fi
} }
@ -794,26 +733,25 @@ _build_profile() {
printf '%s\n' "$SOURCE_DATE_EPOCH" > "${work_dir}/build_date" printf '%s\n' "$SOURCE_DATE_EPOCH" > "${work_dir}/build_date"
fi fi
_show_config [[ "${quiet}" == "n" ]] && _show_config
_run_once _make_pacman_conf _run_once _make_pacman_conf
_run_once _export_gpg_publickey [[ -n "${gpg_key}" ]] && _run_once _export_gpg_publickey
_run_once _make_custom_airootfs _run_once _make_custom_airootfs
_run_once _make_packages _run_once _make_packages
_run_once _make_customize_airootfs _run_once _make_customize_airootfs
_run_once _make_pkglist _run_once _make_pkglist
_make_bootmodes _make_bootmodes
_run_once _cleanup _run_once _cleanup_airootfs
_run_once _make_prepare _run_once _prepare_airootfs_image
_run_once _make_iso _run_once _build_iso
} }
while getopts 'p:r:C:L:P:A:D:w:o:g:vh?' arg; do while getopts 'p:C:L:P:A:D:w:o:g:vh?' arg; do
case "${arg}" in case "${arg}" in
p) p)
read -r -a opt_pkg_list <<< "${OPTARG}" read -r -a opt_pkg_list <<< "${OPTARG}"
pkg_list+=("${opt_pkg_list[@]}") pkg_list+=("${opt_pkg_list[@]}")
;; ;;
r) run_cmd="${OPTARG}" ;;
C) override_pacman_conf="$(realpath -- "${OPTARG}")" ;; C) override_pacman_conf="$(realpath -- "${OPTARG}")" ;;
L) override_iso_label="${OPTARG}" ;; L) override_iso_label="${OPTARG}" ;;
P) override_iso_publisher="${OPTARG}" ;; P) override_iso_publisher="${OPTARG}" ;;
@ -845,9 +783,6 @@ fi
# get the absolute path representation of the first non-option argument # get the absolute path representation of the first non-option argument
profile="$(realpath -- "${1}")" profile="$(realpath -- "${1}")"
# Set directory path defaults for legacy commands
airootfs_dir="${work_dir}/airootfs"
isofs_dir="${work_dir}/iso"
_read_profile _read_profile
_set_overrides _set_overrides
_build_profile _build_profile