#!/bin/bash set -e -u export LANG=C app_name=${0##*/} arch=$(uname -m) pkg_list="" run_cmd="" quiet="y" pacman_conf="/etc/pacman.conf" export iso_label="ARCH_$(date +%Y%m)" iso_publisher="Parabola GNU/Linux-libre " iso_application="Parabola GNU/Linux-libre Live/Rescue CD" install_dir="arch" work_dir="work" out_dir="out" # Show an INFO message # $1: message string _msg_info() { local _msg="${1}" echo "[mkarchiso] INFO: ${_msg}" } # Show an ERROR message then exit with status # $1: message string # $2: exit code number (with 0 does not exit) _msg_error() { local _msg="${1}" local _error=${2} echo echo "[mkarchiso] ERROR: ${_msg}" echo if [[ ${_error} -gt 0 ]]; then exit ${_error} fi } # Show space usage similar to df, but better formatted. # $1: mount-point or mounted device. _show_space_usage () { local _where="${1}" local _fs _total _used _avail _pct_u=0 _mnt read _fs _total _used _avail _pct_u _mnt < <(df -m "${_where}" | tail -1) &> /dev/null _msg_info "Total: ${_total} MiB (100%) | Used: ${_used} MiB (${_pct_u}) | Avail: ${_avail} MiB ($((100 - ${_pct_u%\%}))%)" } _chroot_mount () { mount -t devtmpfs dev "${work_dir}/root-image/dev" mount -t devpts devpts "${work_dir}/root-image/dev/pts" mount -t tmpfs devshm "${work_dir}/root-image/dev/shm" mount -t proc proc "${work_dir}/root-image/proc" mount -t tmpfs run "${work_dir}/root-image/run" mount -t sysfs sys "${work_dir}/root-image/sys" mount -t tmpfs tmp "${work_dir}/root-image/tmp" trap '_chroot_umount' EXIT HUP INT TERM } _chroot_umount () { umount "${work_dir}/root-image/tmp" umount "${work_dir}/root-image/sys" umount "${work_dir}/root-image/run" umount "${work_dir}/root-image/proc" umount "${work_dir}/root-image/dev/shm" umount "${work_dir}/root-image/dev/pts" umount "${work_dir}/root-image/dev" trap - EXIT HUP INT TERM } _chroot_init() { if [[ ! -d ${work_dir}/root-image/var/cache/pacman ]]; then mkdir -p ${work_dir}/root-image/{dev,proc,run,sys,tmp,var/lib/pacman} _pacman "base" _pacman "syslinux" fi } _chroot_run() { _chroot_mount eval chroot ${work_dir}/root-image "${run_cmd}" _chroot_umount } # Mount a filesystem (trap signals in case of error for unmounting it # $1: source image # $2: mount-point _mount_fs() { local _src="${1}" local _dst="${2}" trap "_umount_fs ${_src}" EXIT HUP INT TERM mkdir -p "${_dst}" _msg_info "Mounting '${_src}' on '${_dst}'" mount "${_src}" "${_dst}" _show_space_usage "${_dst}" } # Unmount a filesystem (and untrap signals) # $1: mount-point or device/image _umount_fs() { local _dst="${1}" _show_space_usage "${_dst}" _msg_info "Unmounting '${_dst}'" umount "${_dst}" rmdir "${_dst}" trap - EXIT HUP INT TERM } # Compare if a file/directory (source) is newer than other file (target) # $1: source file/directory # $2: target file # return: 0 if target does not exists or if target is older than source. # 1 if target is newer than source _is_directory_changed() { local _src="${1}" local _dst="${2}" if [ -e "${_dst}" ]; then if [[ $(find ${_src} -newer ${_dst} | wc -l) -gt 0 ]]; then _msg_info "Target '${_dst}' is older than '${_src}', updating." rm -f "${_dst}" return 0 else _msg_info "Target '${_dst}' is up to date with '${_src}', skipping." return 1 fi else _msg_info "Target '${_dst}' does not exist, making it from '${_src}'" return 0 fi } # Show help usage, with an exit status. # $1: exit status number. _usage () { echo "usage ${app_name} [options] command " echo " general options:" echo " -p PACKAGE(S) Package(s) to install, can be used multiple times" echo " -r Run inside root-image" echo " -C Config file for pacman." echo " Default: '${pacman_conf}'" echo " -L