mkarchiso: add a -r option to delete the working directory

`-r` will instruct to delete the working directory at the end of a
`mkarchiso` run.

If the specified directory already exists, then it will not be deleted
and instead produce a warning.

Implements #211
This commit is contained in:
nl6720 2023-02-25 11:54:41 +02:00
parent b9cec1e08b
commit b5e7f5afc5
No known key found for this signature in database
GPG Key ID: 5CE88535E188D369
2 changed files with 24 additions and 2 deletions

View File

@ -11,6 +11,7 @@ Added
- Added classes for Memtest86+ and UEFI Shell menuentries. - Added classes for Memtest86+ and UEFI Shell menuentries.
- Add foot-terminfo and wezterm-terminfo packages to releng to support terminal emulators using them. E.g. when - Add foot-terminfo and wezterm-terminfo packages to releng to support terminal emulators using them. E.g. when
installing via SSH. installing via SSH.
- Add a new ``-r`` option to ``mkarchiso`` that deletes the working directly after the build.
Changed Changed
------- -------

View File

@ -39,6 +39,7 @@ pacman_conf=""
packages="" packages=""
bootstrap_packages="" bootstrap_packages=""
pacstrap_dir="" pacstrap_dir=""
declare -i rm_work_dir=0
buildmodes=() buildmodes=()
bootmodes=() bootmodes=()
airootfs_image_type="" airootfs_image_type=""
@ -110,6 +111,7 @@ usage: ${app_name} [options] <profile_dir>
Default: '${out_dir}' Default: '${out_dir}'
-p [package ..] Package(s) to install. -p [package ..] Package(s) to install.
Multiple packages are provided as quoted, space delimited list. Multiple packages are provided as quoted, space delimited list.
-r Delete the working directory at the end.
-v Enable verbose output -v Enable verbose output
-w <work_dir> Set the working directory -w <work_dir> Set the working directory
Default: '${work_dir}' Default: '${work_dir}'
@ -1658,6 +1660,9 @@ _set_overrides() {
elif [[ -z "$quiet" ]]; then elif [[ -z "$quiet" ]]; then
quiet="y" quiet="y"
fi fi
if [[ -v override_rm_work_dir ]]; then
rm_work_dir="$override_rm_work_dir"
fi
# Set variables that do not have overrides # Set variables that do not have overrides
[[ -n "$airootfs_image_type" ]] || airootfs_image_type="squashfs" [[ -n "$airootfs_image_type" ]] || airootfs_image_type="squashfs"
@ -1730,6 +1735,16 @@ _make_pkglist() {
_msg_info "Done!" _msg_info "Done!"
} }
# Create working directory
_make_work_dir() {
if [[ ! -d "${work_dir}" ]]; then
install -d -- "${work_dir}"
elif (( rm_work_dir )); then
rm_work_dir=0
_msg_warning "Working directory removal requested, but '${work_dir}' already exists. It will not be removed!" 0
fi
}
# build the base for an ISO and/or a netboot target # build the base for an ISO and/or a netboot target
_build_iso_base() { _build_iso_base() {
local run_once_mode="base" local run_once_mode="base"
@ -1741,7 +1756,7 @@ _build_iso_base() {
isofs_dir="${work_dir}/iso" isofs_dir="${work_dir}/iso"
# Create working directory # Create working directory
[[ -d "${work_dir}" ]] || install -d -- "${work_dir}" _run_once _make_work_dir
# Write build date to file if it does not exist already # Write build date to file if it does not exist already
[[ -e "${work_dir}/build_date" ]] || printf '%s\n' "$SOURCE_DATE_EPOCH" > "${work_dir}/build_date" [[ -e "${work_dir}/build_date" ]] || printf '%s\n' "$SOURCE_DATE_EPOCH" > "${work_dir}/build_date"
@ -1820,9 +1835,14 @@ _build() {
for buildmode in "${buildmodes[@]}"; do for buildmode in "${buildmodes[@]}"; do
_run_once "_build_buildmode_${buildmode}" _run_once "_build_buildmode_${buildmode}"
done done
if (( rm_work_dir )); then
_msg_info 'Removing the working directory...'
rm -rf -- "${work_dir:?}/"
_msg_info 'Done!'
fi
} }
while getopts 'c:p:C:L:P:A:D:w:m:o:g:G:vh?' arg; do while getopts 'c:p:C:L:P:A:D:w:m:o:g:G:vrh?' arg; do
case "${arg}" in case "${arg}" in
p) read -r -a override_pkg_list <<< "${OPTARG}" ;; p) read -r -a override_pkg_list <<< "${OPTARG}" ;;
C) override_pacman_conf="${OPTARG}" ;; C) override_pacman_conf="${OPTARG}" ;;
@ -1837,6 +1857,7 @@ while getopts 'c:p:C:L:P:A:D:w:m:o:g:G:vh?' arg; do
g) override_gpg_key="${OPTARG}" ;; g) override_gpg_key="${OPTARG}" ;;
G) override_gpg_sender="${OPTARG}" ;; G) override_gpg_sender="${OPTARG}" ;;
v) override_quiet="n" ;; v) override_quiet="n" ;;
r) declare -i override_rm_work_dir=1 ;;
h|?) _usage 0 ;; h|?) _usage 0 ;;
*) *)
_msg_error "Invalid argument '${arg}'" 0 _msg_error "Invalid argument '${arg}'" 0