OSDN Git Service

[update] : Use an array as an argument to customize_airootfs
[alterlinux/alterlinux.git] / build.sh
1 #!/usr/bin/env bash
2 #
3 # Yamada Hayao
4 # Twitter: @Hayao0819
5 # Email  : hayao@fascode.net
6 #
7 # (c) 2019-2021 Fascode Network.
8 #
9 # build.sh
10 #
11 # The main script that runs the build
12 #
13
14 set -Eeu
15
16 # Internal config
17 # Do not change these values.
18 script_path="$( cd -P "$( dirname "$(readlink -f "${0}")" )" && pwd )"
19 defaultconfig="${script_path}/default.conf"
20 tools_dir="${script_path}/tools"
21 module_dir="${script_path}/modules"
22 customized_username=false
23 customized_password=false
24 customized_kernel=false
25 customized_logpath=false
26 pkglist_args=()
27 makepkg_script_args=()
28 modules=()
29 DEFAULT_ARGUMENT=""
30 ARGUMENT=("${@}")
31 alteriso_version="3.1"
32 norepopkg=()
33
34 # Load config file
35 [[ ! -f "${defaultconfig}" ]] && "${tools_dir}/msg.sh" -a 'build.sh' error "${defaultconfig} was not found." && exit 1
36 for config in "${defaultconfig}" "${script_path}/custom.conf"; do
37     [[ -f "${config}" ]] && source "${config}"
38 done
39
40 umask 0022
41
42 # Message common function
43 # msg_common [type] [-n] [string]
44 msg_common(){
45     local _msg_opts=("-a" "build.sh") _type="${1}"
46     shift 1
47     [[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
48     [[ "${msgdebug}" = true ]] && _msg_opts+=("-x")
49     [[ "${nocolor}"  = true ]] && _msg_opts+=("-n")
50     _msg_opts+=("${_type}" "${@}")
51     "${tools_dir}/msg.sh" "${_msg_opts[@]}"
52 }
53
54 # Show an INFO message
55 # ${1}: message string
56 msg_info() { msg_common info "${@}"; }
57
58 # Show an Warning message
59 # ${1}: message string
60 msg_warn() { msg_common warn "${@}"; }
61
62 # Show an debug message
63 # ${1}: message string
64 msg_debug() { 
65     [[ "${debug}" = true ]] && msg_common debug "${@}" || return 0
66 }
67
68 # Show an ERROR message then exit with status
69 # ${1}: message string
70 # ${2}: exit code number (with 0 does not exit)
71 msg_error() {
72     msg_common error "${1}"
73     [[ -n "${2:-""}" ]] && exit "${2}" || return 0
74 }
75
76
77 # Usage: getclm <number>
78 # 標準入力から値を受けとり、引数で指定された列を抽出します。
79 getclm() { cut -d " " -f "${1}"; }
80
81 # Usage: echo_blank <number>
82 # 指定されたぶんの半角空白文字を出力します
83 echo_blank(){ yes " " 2> /dev/null  | head -n "${1}" | tr -d "\n"; }
84
85 _usage () {
86     echo "usage ${0} [options] [channel]"
87     echo
88     echo "A channel is a profile of AlterISO settings."
89     echo
90     echo " General options:"
91     echo "    -b | --boot-splash           Enable boot splash"
92     echo "    -e | --cleanup | --cleaning  Enable post-build cleaning"
93     echo "    -r |  --tarball               Build rootfs in tar.xz format"
94     echo "    -h | --help                  This help message and exit"
95     echo
96     echo "    -a | --arch <arch>           Set iso architecture"
97     echo "    -c | --comp-type <comp_type> Set SquashFS compression type (gzip, lzma, lzo, xz, zstd)"
98     echo "    -g | --gpgkey <key>          Set gpg key"
99     echo "    -l | --lang <lang>           Specifies the default language for the live environment"
100     echo "    -k | --kernel <kernel>       Set special kernel type. See below for available kernels"
101     echo "    -o | --out <out_dir>         Set the output directory"
102     echo "    -p | --password <password>   Set a live user password"
103     echo "    -t | --comp-opts <options>   Set compressor-specific options."
104     echo "    -u | --user <username>       Set user name"
105     echo "    -w | --work <work_dir>       Set the working directory"
106     echo
107
108     local blank="29" _arch _dirname _type _output _first
109     for _type in "locale" "kernel"; do
110         echo " ${_type} for each architecture:"
111         for _arch in $(find "${script_path}/system/" -maxdepth 1 -mindepth 1 -name "${_type}-*" -print0 | xargs -I{} -0 basename {} | sed "s|${_type}-||g"); do
112             echo "    ${_arch}$(echo_blank "$(( "${blank}" - "${#_arch}" ))")$("${tools_dir}/${_type}.sh" -a "${_arch}" show)"
113         done
114         echo
115     done
116
117     echo " Channel:"
118     for _dirname in $(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" -d -b -n --line show | sed "s|.add$||g"); do
119         readarray -t _output < <("${tools_dir}/channel.sh" --version "${alteriso_version}" --nocheck desc "${_dirname}")
120         _first=true
121         echo -n "    ${_dirname}"
122         for _out in "${_output[@]}"; do
123             "${_first}" && echo -e "    $(echo_blank "$(( "${blank}" - 4 - "${#_dirname}" ))")${_out}" || echo -e "    $(echo_blank "$(( "${blank}" + 5 - "${#_dirname}" ))")${_out}"
124             _first=false
125         done
126     done
127
128     echo
129     echo " Debug options: Please use at your own risk."
130     echo "    -d | --debug                 Enable debug messages"
131     echo "    -x | --bash-debug            Enable bash debug mode(set -xv)"
132     echo "         --channellist           Output the channel list and exit"
133     echo "         --config                Load additional config file"
134     echo "         --gitversion            Add Git commit hash to image file version"
135     echo "         --logpath <file>        Set log file path (use with --log)"
136     echo "         --[no]log               (No) log ;re-run script with tee"
137     echo "         --msgdebug              Enables output debugging"
138     echo "         --noaur                 Ignore aur packages (Use only for debugging)"
139     echo "         --nocolor               No output colored output"
140     echo "         --[no]confirm           (No) check the settings before building"
141     echo "         --nochkver              No check the version of the channel"
142     echo "         --nodebug               Disable all debug messages"
143     echo "         --noefi                 No efi boot (Use only for debugging)"
144     echo "         --noloopmod             No check and load kernel module automatically"
145     echo "         --nodepend              No check package dependencies before building"
146     echo "         --noiso                 No build iso image (Use with --tarball)"
147     echo "         --nosigcheck            No pacman signature check"
148     echo "         --pacman-debug          Enable pacman debug mode"
149     echo "         --normwork              No remove working dir"
150     echo "         --nopkgbuild            Ignore PKGBUILD (Use only for debugging)"
151     echo "         --tar-type <comp_type>  Set compression type (gzip, lzma, lzo, xz, zstd)"
152     echo "         --tar-opts <option>     Set tar command argument (Use with --tarball)"
153     echo "         --add-module <module>   Load additional modules (Separated by \",\")"
154     echo
155     echo " Many packages are installed from AUR, so specifying --noaur can cause problems."
156     echo
157     [[ -n "${1:-}" ]] && exit "${1}"
158 }
159
160 # Unmount helper Usage: _umount <target>
161 _umount() { mountpoint -q "${1}" && umount -lf "${1}"; return 0; }
162
163 # Mount helper Usage: _mount <source> <target>
164 _mount() { ! mountpoint -q "${2}" && [[ -f "${1}" ]] && [[ -d "${2}" ]] && mount "${1}" "${2}"; return 0; }
165
166 # Unmount work dir
167 umount_work () {
168     local _args=("${build_dir}")
169     [[ "${debug}" = true ]] && _args=("-d" "${_args[@]}")
170     [[ "${nocolor}" = true ]] && _args+=("--nocolor")
171     "${tools_dir}/umount.sh" "${_args[@]}"
172 }
173
174 # Mount airootfs on "${airootfs_dir}"
175 mount_airootfs () {
176     mkdir -p "${airootfs_dir}"
177     _mount "${airootfs_dir}.img" "${airootfs_dir}"
178 }
179
180
181 # Helper function to run make_*() only one time.
182 run_once() {
183     if [[ ! -e "${lockfile_dir}/build.${1}" ]]; then
184         msg_debug "Running ${1} ..."
185         mount_airootfs
186         eval "${@}"
187         mkdir -p "${lockfile_dir}"; touch "${lockfile_dir}/build.${1}"
188         umount_work
189     else
190         msg_debug "Skipped because ${1} has already been executed."
191     fi
192 }
193
194 # Show message when file is removed
195 # remove <file> <file> ...
196 remove() {
197     local _file
198     for _file in "${@}"; do msg_debug "Removing ${_file}"; rm -rf "${_file}"; done
199 }
200
201 # 強制終了時にアンマウント
202 umount_trap() {
203     local _status="${?}"
204     umount_work
205     msg_error "It was killed by the user.\nThe process may not have completed successfully."
206     exit "${_status}"
207 }
208
209 # 設定ファイルを読み込む
210 # load_config [file1] [file2] ...
211 load_config() {
212     local _file
213     for _file in "${@}"; do [[ -f "${_file}" ]] && source "${_file}" && msg_debug "The settings have been overwritten by the ${_file}"; done
214     return 0
215 }
216
217 # Display channel list
218 show_channel_list() {
219     local _args=("-v" "${alteriso_version}" show)
220     [[ "${nochkver}" = true ]] && _args+=("-n")
221     bash "${tools_dir}/channel.sh" "${_args[@]}"
222 }
223
224 # Execute command for each module. It will be executed with {} replaced with the module name.
225 # for_module <command>
226 for_module(){ local module && for module in "${modules[@]}"; do eval "${@//"{}"/${module}}"; done; }
227
228 # pacstrapを実行
229 _pacstrap(){
230     msg_info "Installing packages to ${airootfs_dir}/'..."
231     local _args=("-c" "-G" "-M" "--" "${airootfs_dir}" "${@}")
232     [[ "${pacman_debug}" = true ]] && _args+=(--debug)
233     pacstrap -C "${build_dir}/pacman.conf" "${_args[@]}"
234     msg_info "Packages installed successfully!"
235 }
236
237 # chroot環境でpacmanコマンドを実行
238 # /etc/alteriso-pacman.confを準備してコマンドを実行します
239 _run_with_pacmanconf(){
240     sed "s|^CacheDir     =|#CacheDir    =|g" "${build_dir}/pacman.conf" > "${airootfs_dir}/etc/alteriso-pacman.conf"
241     eval -- "${@}"
242     remove "${airootfs_dir}/etc/alteriso-pacman.conf"
243 }
244
245 # コマンドをchrootで実行する
246 _chroot_run() {
247     msg_debug "Run command in chroot\nCommand: ${*}"
248     eval -- arch-chroot "${airootfs_dir}" "${@}"
249 }
250
251 _cleanup_common () {
252     msg_info "Cleaning up what we can on airootfs..."
253
254     # Delete pacman database sync cache files (*.tar.gz)
255     [[ -d "${airootfs_dir}/var/lib/pacman" ]] && find "${airootfs_dir}/var/lib/pacman" -maxdepth 1 -type f -delete
256
257     # Delete pacman database sync cache
258     [[ -d "${airootfs_dir}/var/lib/pacman/sync" ]] && find "${airootfs_dir}/var/lib/pacman/sync" -delete
259
260     # Delete pacman package cache
261     [[ -d "${airootfs_dir}/var/cache/pacman/pkg" ]] && find "${airootfs_dir}/var/cache/pacman/pkg" -type f -delete
262
263     # Delete all log files, keeps empty dirs.
264     [[ -d "${airootfs_dir}/var/log" ]] && find "${airootfs_dir}/var/log" -type f -delete
265
266     # Delete all temporary files and dirs
267     [[ -d "${airootfs_dir}/var/tmp" ]] && find "${airootfs_dir}/var/tmp" -mindepth 1 -delete
268
269     # Delete package pacman related files.
270     find "${build_dir}" \( -name '*.pacnew' -o -name '*.pacsave' -o -name '*.pacorig' \) -delete
271
272     # Delete all cache file
273     [[ -d "${airootfs_dir}/var/cache" ]] && find "${airootfs_dir}/var/cache" -mindepth 1 -delete
274
275     # Create an empty /etc/machine-id
276     printf '' > "${airootfs_dir}/etc/machine-id"
277
278     msg_info "Done!"
279 }
280
281 _cleanup_airootfs(){
282     _cleanup_common
283     # Delete all files in /boot
284     [[ -d "${airootfs_dir}/boot" ]] && find "${airootfs_dir}/boot" -mindepth 1 -delete
285 }
286
287 _mkchecksum() {
288     msg_info "Creating md5 checksum ..."
289     echo "$(md5sum "${1}" | getclm 1) $(basename "${1}")" > "${1}.md5"
290     msg_info "Creating sha256 checksum ..."
291     echo "$(sha256sum "${1}" | getclm 1) $(basename "${1}")" > "${1}.sha256"
292 }
293
294 # Check the value of a variable that can only be set to true or false.
295 check_bool() {
296     local _value _variable
297     for _variable in "${@}"; do
298         msg_debug -n "Checking ${_variable}..."
299         eval ": \${${_variable}:=''}"
300         _value="$(eval echo "\$${_variable}")"
301         if [[ ! -v "${1}" ]] || [[ "${_value}"  = "" ]]; then
302             [[ "${debug}" = true ]] && echo ; msg_error "The variable name ${_variable} is empty." "1"
303         elif [[ ! "${_value}" = "true" ]] && [[ ! "${_value}" = "false" ]]; then
304             [[ "${debug}" = true ]] && echo ; msg_error "The variable name ${_variable} is not of bool type (${_variable} = ${_value})" "1"
305         elif [[ "${debug}" = true ]]; then
306             echo -e " ${_value}"
307         fi
308     done
309 }
310
311 _run_cleansh(){
312     bash "$([[ "${bash_debug}" = true ]] && echo -n "-x" || echo -n "+x")" "${tools_dir}/clean.sh" -o -w "$(realpath "${build_dir}")" "$([[ "${debug}" = true ]] && printf "%s" "-d")" "$([[ "${noconfirm}" = true ]] && printf "%s" "-n")" "$([[ "${nocolor}" = true ]] && printf "%s" "--nocolor")"
313 }
314
315
316 # Check the build environment and create a directory.
317 prepare_env() {
318     # Check packages
319     if [[ "${nodepend}" = false ]]; then
320         local _check_failed=false _pkg _result=0
321         msg_info "Checking dependencies ..."
322         ! pacman -Qq pyalpm > /dev/null 2>&1 && msg_error "pyalpm is not installed." 1
323         for _pkg in "${dependence[@]}"; do
324             eval "${tools_dir}/package.py" "${_pkg}" "$( [[ "${debug}" = false ]] && echo "> /dev/null")" || _result="${?}"
325             if (( _result == 3 )) || (( _result == 4 )); then
326                 _check_failed=true
327             fi
328             _result=0
329         done
330         [[ "${_check_failed}" = true ]] && exit 1
331     fi
332
333     # Load loop kernel module
334     if [[ "${noloopmod}" = false ]]; then
335         [[ ! -d "/usr/lib/modules/$(uname -r)" ]] && msg_error "The currently running kernel module could not be found.\nProbably the system kernel has been updated.\nReboot your system to run the latest kernel." "1"
336         lsmod | getclm 1 | grep -qx "loop" || modprobe loop
337     fi
338
339     # Check work dir
340     if [[ "${normwork}" = false ]]; then
341         msg_info "Deleting the contents of ${build_dir}..."
342         _run_cleansh
343     fi
344
345     # Set gpg key
346     if [[ -n "${gpg_key}" ]]; then
347         gpg --batch --output "${work_dir}/pubkey.gpg" --export "${gpg_key}"
348         exec {ARCHISO_GNUPG_FD}<>"${build_dir}/pubkey.gpg"
349         export ARCHISO_GNUPG_FD
350     fi
351
352     # 強制終了時に作業ディレクトリを削除する
353     local _trap_remove_work
354     _trap_remove_work() {
355         local status="${?}"
356         [[ "${normwork}" = false ]] && echo && _run_cleansh
357         exit "${status}"
358     }
359     trap '_trap_remove_work' HUP INT QUIT TERM
360
361     return 0
362 }
363
364 # Error message
365 error_exit_trap(){
366     local _exit="${?}" _line="${1}" && shift 1
367     msg_error "An exception error occurred in the function"
368     msg_error "Exit Code: ${_exit}\nLine: ${_line}\nArgument: ${ARGUMENT[*]}"
369     exit "${_exit}"
370 }
371
372 # Show settings.
373 show_settings() {
374     if [[ "${boot_splash}" = true ]]; then
375         msg_info "Boot splash is enabled."
376         msg_info "Theme is used ${theme_name}."
377     fi
378     msg_info "Language is ${locale_fullname}."
379     msg_info "Use the ${kernel} kernel."
380     msg_info "Live username is ${username}."
381     msg_info "Live user password is ${password}."
382     msg_info "The compression method of squashfs is ${sfs_comp}."
383     msg_info "Use the ${channel_name%.add} channel."
384     msg_info "Build with architecture ${arch}."
385     (( "${#additional_exclude_pkg[@]}" != 0 )) && msg_info "Excluded packages: ${additional_exclude_pkg[*]}"
386     if [[ "${noconfirm}" = false ]]; then
387         echo -e "\nPress Enter to continue or Ctrl + C to cancel."
388         read -r
389     fi
390     trap HUP INT QUIT TERM
391     trap 'umount_trap' HUP INT QUIT TERM
392     trap 'error_exit_trap $LINENO' ERR
393
394     return 0
395 }
396
397
398 # Preparation for build
399 prepare_build() {
400     # Debug mode
401     [[ "${bash_debug}" = true ]] && set -x -v
402
403     # Show alteriso version
404     [[ -n "${gitrev-""}" ]] && msg_debug "The version of alteriso is ${gitrev}"
405
406     # Load configs
407     load_config "${channel_dir}/config.any" "${channel_dir}/config.${arch}"
408
409     # Additional modules
410     modules+=("${additional_modules[@]}")
411
412     # Legacy mode
413     if [[ "$(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" ver "${channel_name}")" = "3.0" ]]; then
414         msg_warn "The module cannot be used because it works with Alter ISO3.0 compatibility."
415         modules=("legacy")
416         [[ "${include_extra-"unset"}" = true ]] && modules=("legacy-extra")
417     fi
418
419     # Load presets
420     local _modules=() module_check
421     for_module '[[ -f "${preset_dir}/{}" ]] && readarray -t -O "${#_modules[@]}" _modules < <(grep -h -v ^'#' "${preset_dir}/{}") || _modules+=("{}")'
422     modules=("${_modules[@]}")
423     unset _modules
424
425     # Check modules
426     module_check(){
427         msg_debug "Checking ${1} module ..."
428         bash "${tools_dir}/module.sh" check "${1}" || msg_error "Module ${1} is not available." "1";
429     }
430     readarray -t modules < <(printf "%s\n" "${modules[@]}" | awk '!a[$0]++')
431     for_module "module_check {}"
432
433     # Load modules
434     for_module load_config "${module_dir}/{}/config.any" "${module_dir}/{}/config.${arch}"
435     msg_debug "Loaded modules: ${modules[*]}"
436     ! printf "%s\n" "${modules[@]}" | grep -x "share" >/dev/null 2>&1 && msg_warn "The share module is not loaded."
437
438     # Set kernel
439     [[ "${customized_kernel}" = false ]] && kernel="${defaultkernel}"
440
441     # Parse files
442     eval "$(bash "${tools_dir}/locale.sh" -s -a "${arch}" get "${locale_name}")"
443     eval "$(bash "${tools_dir}/kernel.sh" -s -c "${channel_name}" -a "${arch}" get "${kernel}")"
444
445     # Set username and password
446     [[ "${customized_username}" = false ]] && username="${defaultusername}"
447     [[ "${customized_password}" = false ]] && password="${defaultpassword}"
448
449     # gitversion
450     [[ "${gitversion}" = true ]] && iso_version="${iso_version}-${gitrev}"
451
452     # Generate tar file name
453     tar_ext=""
454     case "${tar_comp}" in
455         "gzip" ) tar_ext="gz"                        ;;
456         "zstd" ) tar_ext="zst"                       ;;
457         "xz" | "lzo" | "lzma") tar_ext="${tar_comp}" ;;
458     esac
459
460     # Generate iso file name
461     local _channel_name="${channel_name%.add}-${locale_version}" 
462     iso_filename="${iso_name}-${_channel_name}-${iso_version}-${arch}.iso"
463     tar_filename="${iso_filename%.iso}.tar.${tar_ext}"
464     [[ "${nochname}" = true ]] && iso_filename="${iso_name}-${iso_version}-${arch}.iso"
465     msg_debug "Iso filename is ${iso_filename}"
466
467     # check bool
468     check_bool boot_splash cleaning noconfirm nodepend customized_username customized_password noloopmod nochname tarball noiso noaur customized_syslinux norescue_entry debug bash_debug nocolor msgdebug noefi nosigcheck gitversion
469
470     # Check architecture for each channel
471     local _exit=0
472     bash "${tools_dir}/channel.sh" --version "${alteriso_version}" -a "${arch}" -n -b check "${channel_name}" || _exit="${?}"
473     ( (( "${_exit}" != 0 )) && (( "${_exit}" != 1 )) ) && msg_error "${channel_name} channel does not support current architecture (${arch})." "1"
474
475     # Run with tee
476     if [[ ! "${logging}" = false ]]; then
477         [[ "${customized_logpath}" = false ]] && logging="${out_dir}/${iso_filename%.iso}.log"
478         mkdir -p "$(dirname "${logging}")" && touch "${logging}"
479         msg_warn "Re-run sudo ${0} ${ARGUMENT[*]} --nodepend --nolog --nocolor 2>&1 | tee ${logging}"
480         sudo "${0}" "${ARGUMENT[@]}" --nolog --nocolor --nodepend 2>&1 | tee "${logging}"
481         exit "${?}"
482     fi
483
484     # Set argument of pkglist.sh
485     pkglist_args=("-a" "${arch}" "-k" "${kernel}" "-c" "${channel_dir}" "-l" "${locale_name}" --line)
486     [[ "${boot_splash}"              = true ]] && pkglist_args+=("-b")
487     [[ "${debug}"                    = true ]] && pkglist_args+=("-d")
488     [[ "${memtest86}"                = true ]] && pkglist_args+=("-m")
489     [[ "${nocolor}"                  = true ]] && pkglist_args+=("--nocolor")
490     (( "${#additional_exclude_pkg[@]}" >= 1 )) && pkglist_args+=("-e" "${additional_exclude_pkg[*]}")
491     pkglist_args+=("${modules[@]}")
492
493     # Set argument of aur.sh and pkgbuild.sh
494     [[ "${bash_debug}"   = true ]] && makepkg_script_args+=("-x")
495     [[ "${pacman_debug}" = true ]] && makepkg_script_args+=("-d")
496
497     return 0
498 }
499
500
501 # Setup custom pacman.conf with current cache directories.
502 make_pacman_conf() {
503     # Pacman configuration file used only when building
504     # If there is pacman.conf for each channel, use that for building
505     local _pacman_conf _pacman_conf_list=("${script_path}/pacman-${arch}.conf" "${channel_dir}/pacman-${arch}.conf" "${script_path}/system/pacman-${arch}.conf")
506     for _pacman_conf in "${_pacman_conf_list[@]}"; do
507         if [[ -f "${_pacman_conf}" ]]; then
508             build_pacman_conf="${_pacman_conf}"
509             break
510         fi
511     done
512
513     msg_debug "Use ${build_pacman_conf}"
514     sed -r "s|^#?\\s*CacheDir.+|CacheDir     = ${cache_dir}|g" "${build_pacman_conf}" > "${build_dir}/pacman.conf"
515
516     [[ "${nosigcheck}" = true ]] && sed -ir "s|^s*SigLevel.+|SigLevel = Never|g" "${build_pacman_conf}"
517
518     [[ -n "$(find "${cache_dir}" -maxdepth 1 -name '*.pkg.tar.*' 2> /dev/null)" ]] && msg_info "Use cached package files in ${cache_dir}"
519
520     # Share any architecture packages
521     #while read -r _pkg; do
522     #    if [[ ! -f "${cache_dir}/$(basename "${_pkg}")" ]]; then
523     #        ln -s "${_pkg}" "${cache_dir}"
524     #    fi
525     #done < <(find "${cache_dir}/../" -type d -name "$(basename "${cache_dir}")" -prune -o -type f -name "*-any.pkg.tar.*" -printf "%p\n")
526
527     return 0
528 }
529
530 # Base installation (airootfs)
531 make_basefs() {
532     msg_info "Creating ext4 image of 32GiB..."
533     truncate -s 32G -- "${airootfs_dir}.img"
534     mkfs.ext4 -O '^has_journal,^resize_inode' -E 'lazy_itable_init=0' -m 0 -F -- "${airootfs_dir}.img" 32G
535     tune2fs -c "0" -i "0" "${airootfs_dir}.img"
536     msg_info "Done!"
537
538     msg_info "Mounting ${airootfs_dir}.img on ${airootfs_dir}"
539     mount_airootfs
540     msg_info "Done!"
541     return 0
542 }
543
544 # Additional packages (airootfs)
545 make_packages_repo() {
546     msg_debug "pkglist.sh ${pkglist_args[*]}"
547     readarray -t _pkglist_install < <("${tools_dir}/pkglist.sh" "${pkglist_args[@]}")
548
549     # Package check
550     #readarray -t _pkglist < <("${tools_dir}/pkglist.sh" "${pkglist_args[@]}")
551     #readarray -t repopkgs < <(pacman-conf -c "${build_pacman_conf}" -l | xargs -I{} pacman -Sql --config "${build_pacman_conf}" --color=never {} && pacman -Sg)
552     #local _pkg
553     #for _pkg in "${_pkglist[@]}"; do
554     #    msg_info "Checking ${_pkg}..."
555     #    if printf "%s\n" "${repopkgs[@]}" | grep -qx "${_pkg}"; then
556     #        _pkglist_install+=("${_pkg}")
557     #    else
558     #        msg_info "${_pkg} was not found. Install it with yay from AUR"
559     #        norepopkg+=("${_pkg}")
560     #    fi
561     #done
562
563     # Create a list of packages to be finally installed as packages.list directly under the working directory.
564     echo -e "# The list of packages that is installed in live cd.\n#\n" > "${build_dir}/packages.list"
565     printf "%s\n" "${_pkglist_install[@]}" >> "${build_dir}/packages.list"
566
567     # Install packages on airootfs
568     _pacstrap "${_pkglist_install[@]}"
569
570     return 0
571 }
572
573 make_packages_aur() {
574     readarray -t _pkglist_aur < <("${tools_dir}/pkglist.sh" --aur "${pkglist_args[@]}")
575     _pkglist_aur=("${_pkglist_aur[@]}" "${norepopkg[@]}")
576
577     # Create a list of packages to be finally installed as packages.list directly under the working directory.
578     echo -e "\n# AUR packages.\n#\n" >> "${build_dir}/packages.list"
579     printf "%s\n" "${_pkglist_aur[@]}" >> "${build_dir}/packages.list"
580
581     # prepare for yay
582     cp -rf --preserve=mode "${script_path}/system/aur.sh" "${airootfs_dir}/root/aur.sh"
583     _pacstrap --asdeps --needed "go" # --asdepsをつけているのでaur.shで削除される --neededをつけているので明示的にインストールされている場合削除されない
584
585     # Run aur script
586     _run_with_pacmanconf _chroot_run "bash" "/root/aur.sh" "${makepkg_script_args[@]}" "${_pkglist_aur[@]}"
587
588     # Remove script
589     remove "${airootfs_dir}/root/aur.sh"
590
591     return 0
592 }
593
594 make_pkgbuild() {
595     # Get PKGBUILD List
596     local _pkgbuild_dirs=("${channel_dir}/pkgbuild.any" "${channel_dir}/pkgbuild.${arch}")
597     for_module '_pkgbuild_dirs+=("${module_dir}/{}/pkgbuild.any" "${module_dir}/{}/pkgbuild.${arch}")'
598
599     # Copy PKGBUILD to work
600     mkdir -p "${airootfs_dir}/pkgbuilds/"
601     for _dir in $(find "${_pkgbuild_dirs[@]}" -type f -name "PKGBUILD" -print0 2>/dev/null | xargs -0 -I{} realpath {} | xargs -I{} dirname {}); do
602         msg_info "Find $(basename "${_dir}")"
603         cp -r "${_dir}" "${airootfs_dir}/pkgbuilds/"
604     done
605     
606     # copy buold script
607     cp -rf --preserve=mode "${script_path}/system/pkgbuild.sh" "${airootfs_dir}/root/pkgbuild.sh"
608
609     # Run build script
610     _run_with_pacmanconf _chroot_run "bash" "/root/pkgbuild.sh" "${makepkg_script_args[@]}" "/pkgbuilds"
611
612     # Remove script
613     remove "${airootfs_dir}/root/pkgbuild.sh"
614
615     return 0
616 }
617
618 # Customize installation (airootfs)
619 make_customize_airootfs() {
620     # Overwrite airootfs with customize_airootfs.
621     local _airootfs _airootfs_script_options _script _script_list _airootfs_list=() _main_script
622
623     for_module '_airootfs_list+=("${module_dir}/{}/airootfs.any" "${module_dir}/{}/airootfs.${arch}")'
624     _airootfs_list+=("${channel_dir}/airootfs.any" "${channel_dir}/airootfs.${arch}")
625
626     for _airootfs in "${_airootfs_list[@]}";do
627         if [[ -d "${_airootfs}" ]]; then
628             msg_debug "Copying airootfs ${_airootfs} ..."
629             cp -af "${_airootfs}"/* "${airootfs_dir}"
630         fi
631     done
632
633     # Replace /etc/mkinitcpio.conf if Plymouth is enabled.
634     if [[ "${boot_splash}" = true ]]; then
635         cp -f "${script_path}/mkinitcpio/mkinitcpio-plymouth.conf" "${airootfs_dir}/etc/mkinitcpio.conf"
636     else
637         cp -f "${script_path}/mkinitcpio/mkinitcpio.conf" "${airootfs_dir}/etc/mkinitcpio.conf"
638     fi
639     
640     # customize_airootfs options
641     # -b                        : Enable boot splash.
642     # -d                        : Enable debug mode.
643     # -g <locale_gen_name>      : Set locale-gen.
644     # -i <inst_dir>             : Set install dir
645     # -k <kernel config line>   : Set kernel name.
646     # -o <os name>              : Set os name.
647     # -p <password>             : Set password.
648     # -s <shell>                : Set user shell.
649     # -t                        : Set plymouth theme.
650     # -u <username>             : Set live user name.
651     # -x                        : Enable bash debug mode.
652     # -z <locale_time>          : Set the time zone.
653     # -l <locale_name>          : Set language.
654     #
655     # -j is obsolete in AlterISO3 and cannot be used.
656     # -r is obsolete due to the removal of rebuild.
657     # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
658
659     # Generate options of customize_airootfs.sh.
660     _airootfs_script_options=(-p "${password}" -k "${kernel} ${kernel_filename} ${kernel_mkinitcpio_profile}" -u "${username}" -o "${os_name}" -i "${install_dir}" -s "${usershell}" -a "${arch}" -g "${locale_gen_name}" -l "${locale_name}" -z "${locale_time}" -t "${theme_name}")
661     [[ "${boot_splash}" = true ]] && _airootfs_script_options+=("-b")
662     [[ "${debug}" = true       ]] && _airootfs_script_options+=("-d")
663     [[ "${bash_debug}" = true  ]] && _airootfs_script_options+=("-x")
664
665     _main_script="root/customize_airootfs.sh"
666
667     _script_list=(
668         "${airootfs_dir}/root/customize_airootfs_${channel_name}.sh"
669         "${airootfs_dir}/root/customize_airootfs_${channel_name%.add}.sh"
670     )
671
672     for_module '_script_list+=("${airootfs_dir}/root/customize_airootfs_{}.sh")'
673
674     # Create script
675     for _script in "${_script_list[@]}"; do
676         if [[ -f "${_script}" ]]; then
677             (echo && cat "${_script}")  >> "${airootfs_dir}/${_main_script}"
678             remove "${_script}"
679         else
680             msg_debug "${_script} was not found."
681         fi
682     done
683
684     chmod 755 "${airootfs_dir}/${_main_script}"
685     cp "${airootfs_dir}/${_main_script}" "${build_dir}/$(basename "${_main_script}")"
686     _chroot_run "${_main_script}" "${_airootfs_script_options[@]}"
687     remove "${airootfs_dir}/${_main_script}"
688
689     # /root permission https://github.com/archlinux/archiso/commit/d39e2ba41bf556674501062742190c29ee11cd59
690     chmod -f 750 "${airootfs_dir}/root"
691
692     return 0
693 }
694
695 # Copy mkinitcpio archiso hooks and build initramfs (airootfs)
696 make_setup_mkinitcpio() {
697     local _hook
698     mkdir -p "${airootfs_dir}/etc/initcpio/hooks" "${airootfs_dir}/etc/initcpio/install"
699
700     for _hook in "archiso" "archiso_shutdown" "archiso_pxe_common" "archiso_pxe_nbd" "archiso_pxe_http" "archiso_pxe_nfs" "archiso_loop_mnt"; do
701         cp "${script_path}/system/initcpio/hooks/${_hook}" "${airootfs_dir}/etc/initcpio/hooks"
702         cp "${script_path}/system/initcpio/install/${_hook}" "${airootfs_dir}/etc/initcpio/install"
703     done
704
705     sed -i "s|/usr/lib/initcpio/|/etc/initcpio/|g" "${airootfs_dir}/etc/initcpio/install/archiso_shutdown"
706     cp "${script_path}/system/initcpio/install/archiso_kms" "${airootfs_dir}/etc/initcpio/install"
707     cp "${script_path}/system/initcpio/archiso_shutdown" "${airootfs_dir}/etc/initcpio"
708     if [[ "${boot_splash}" = true ]]; then
709         cp "${script_path}/mkinitcpio/mkinitcpio-archiso-plymouth.conf" "${airootfs_dir}/etc/mkinitcpio-archiso.conf"
710     else
711         cp "${script_path}/mkinitcpio/mkinitcpio-archiso.conf" "${airootfs_dir}/etc/mkinitcpio-archiso.conf"
712     fi
713     if [[ "${gpg_key}" ]]; then
714       gpg --export "${gpg_key}" >"${build_dir}/gpgkey"
715       exec 17<>"${build_dir}/gpgkey"
716     fi
717
718     _chroot_run "mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/${kernel_filename} -g /boot/archiso.img"
719
720     [[ "${gpg_key}" ]] && exec 17<&-
721     
722     return 0
723 }
724
725 # Prepare kernel/initramfs ${install_dir}/boot/
726 make_boot() {
727     mkdir -p "${isofs_dir}/${install_dir}/boot/${arch}"
728     cp "${airootfs_dir}/boot/archiso.img" "${isofs_dir}/${install_dir}/boot/${arch}/archiso.img"
729     cp "${airootfs_dir}/boot/${kernel_filename}" "${isofs_dir}/${install_dir}/boot/${arch}/${kernel_filename}"
730
731     return 0
732 }
733
734 # Add other aditional/extra files to ${install_dir}/boot/
735 make_boot_extra() {
736     if [[ -e "${airootfs_dir}/boot/memtest86+/memtest.bin" ]]; then
737         install -m 0644 -- "${airootfs_dir}/boot/memtest86+/memtest.bin" "${isofs_dir}/${install_dir}/boot/memtest"
738         install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/licenses/memtest86+/"
739         install -m 0644 -- "${airootfs_dir}/usr/share/licenses/common/GPL2/license.txt" "${isofs_dir}/${install_dir}/boot/licenses/memtest86+/"
740     fi
741
742     local _ucode_image
743     msg_info "Preparing microcode for the ISO 9660 file system..."
744
745     for _ucode_image in {intel-uc.img,intel-ucode.img,amd-uc.img,amd-ucode.img,early_ucode.cpio,microcode.cpio}; do
746         if [[ -e "${airootfs_dir}/boot/${_ucode_image}" ]]; then
747             msg_info "Installimg ${_ucode_image} ..."
748             install -m 0644 -- "${airootfs_dir}/boot/${_ucode_image}" "${isofs_dir}/${install_dir}/boot/"
749             if [[ -e "${airootfs_dir}/usr/share/licenses/${_ucode_image%.*}/" ]]; then
750                 install -d -m 0755 -- "${isofs_dir}/${install_dir}/boot/licenses/${_ucode_image%.*}/"
751                 install -m 0644 -- "${airootfs_dir}/usr/share/licenses/${_ucode_image%.*}/"* "${isofs_dir}/${install_dir}/boot/licenses/${_ucode_image%.*}/"
752             fi
753         fi
754     done
755     msg_info "Done!"
756
757     return 0
758 }
759
760 # Prepare /${install_dir}/boot/syslinux
761 make_syslinux() {
762     mkdir -p "${isofs_dir}/syslinux"
763
764     # 一時ディレクトリに設定ファイルをコピー
765     mkdir -p "${build_dir}/syslinux/"
766     cp -a "${script_path}/syslinux/"* "${build_dir}/syslinux/"
767     if [[ -d "${channel_dir}/syslinux" ]] && [[ "${customized_syslinux}" = true ]]; then
768         cp -af "${channel_dir}/syslinux"* "${build_dir}/syslinux/"
769     fi
770
771     # copy all syslinux config to work dir
772     for _cfg in "${build_dir}/syslinux/"*.cfg; do
773         sed "s|%ARCHISO_LABEL%|${iso_label}|g;
774              s|%OS_NAME%|${os_name}|g;
775              s|%KERNEL_FILENAME%|${kernel_filename}|g;
776              s|%ARCH%|${arch}|g;
777              s|%INSTALL_DIR%|${install_dir}|g" "${_cfg}" > "${isofs_dir}/syslinux/${_cfg##*/}"
778     done
779
780     # Replace the SYSLINUX configuration file with or without boot splash.
781     local _use_config_name _no_use_config_name _pxe_or_sys
782     if [[ "${boot_splash}" = true ]]; then
783         _use_config_name=splash
784         _no_use_config_name=nosplash
785     else
786         _use_config_name=nosplash
787         _no_use_config_name=splash
788     fi
789     for _pxe_or_sys in "sys" "pxe"; do
790         remove "${isofs_dir}/syslinux/archiso_${_pxe_or_sys}_${_no_use_config_name}.cfg"
791         mv "${isofs_dir}/syslinux/archiso_${_pxe_or_sys}_${_use_config_name}.cfg" "${isofs_dir}/syslinux/archiso_${_pxe_or_sys}.cfg"
792     done
793
794     # Set syslinux wallpaper
795     if [[ -f "${channel_dir}/splash.png" ]]; then
796         cp "${channel_dir}/splash.png" "${isofs_dir}/syslinux"
797     else
798         cp "${script_path}/syslinux/splash.png" "${isofs_dir}/syslinux"
799     fi
800
801     # remove config
802     local _remove_config
803     function _remove_config() {
804         remove "${isofs_dir}/syslinux/${1}"
805         sed -i "s|$(grep "${1}" "${isofs_dir}/syslinux/archiso_sys_load.cfg")||g" "${isofs_dir}/syslinux/archiso_sys_load.cfg" 
806     }
807
808     [[ "${norescue_entry}" = true  ]] && _remove_config archiso_sys_rescue.cfg
809     [[ "${memtest86}"      = false ]] && _remove_config memtest86.cfg
810
811     # copy files
812     cp "${airootfs_dir}/usr/lib/syslinux/bios/"*.c32 "${isofs_dir}/syslinux"
813     cp "${airootfs_dir}/usr/lib/syslinux/bios/lpxelinux.0" "${isofs_dir}/syslinux"
814     cp "${airootfs_dir}/usr/lib/syslinux/bios/memdisk" "${isofs_dir}/syslinux"
815
816
817     if [[ -e "${isofs_dir}/syslinux/hdt.c32" ]]; then
818         install -d -m 0755 -- "${isofs_dir}/syslinux/hdt"
819         if [[ -e "${airootfs_dir}/usr/share/hwdata/pci.ids" ]]; then
820             gzip -c -9 "${airootfs_dir}/usr/share/hwdata/pci.ids" > "${isofs_dir}/syslinux/hdt/pciids.gz"
821         fi
822         find "${airootfs_dir}/usr/lib/modules" -name 'modules.alias' -print -exec gzip -c -9 '{}' ';' -quit > "${isofs_dir}/syslinux/hdt/modalias.gz"
823     fi
824
825     return 0
826 }
827
828 # Prepare /isolinux
829 make_isolinux() {
830     install -d -m 0755 -- "${isofs_dir}/syslinux"
831     sed "s|%INSTALL_DIR%|${install_dir}|g" "${script_path}/system/isolinux.cfg" > "${isofs_dir}/syslinux/isolinux.cfg"
832     install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/isolinux.bin" "${isofs_dir}/syslinux/"
833     install -m 0644 -- "${airootfs_dir}/usr/lib/syslinux/bios/isohdpfx.bin" "${isofs_dir}/syslinux/"
834
835     return 0
836 }
837
838 # Prepare /EFI
839 make_efi() {
840     local _bootfile _use_config_name="nosplash" _efi_config_list=() _efi_config
841     [[ "${boot_splash}" = true ]] && _use_config_name="splash"
842     _bootfile="$(basename "$(ls "${airootfs_dir}/usr/lib/systemd/boot/efi/systemd-boot"*".efi" )")"
843
844     install -d -m 0755 -- "${isofs_dir}/EFI/boot"
845     install -m 0644 -- "${airootfs_dir}/usr/lib/systemd/boot/efi/${_bootfile}" "${isofs_dir}/EFI/boot/${_bootfile#systemd-}"
846
847     install -d -m 0755 -- "${isofs_dir}/loader/entries"
848     sed "s|%ARCH%|${arch}|g;" "${script_path}/efiboot/${_use_config_name}/loader.conf" > "${isofs_dir}/loader/loader.conf"
849
850     readarray -t _efi_config_list < <(find "${script_path}/efiboot/${_use_config_name}/" -mindepth 1 -maxdepth 1 -type f -name "archiso-usb*.conf" -printf "%f\n" | grep -v "rescue")
851     [[ "${norescue_entry}" = false ]] && readarray -t _efi_config_list < <(find "${script_path}/efiboot/${_use_config_name}/" -mindepth 1 -maxdepth 1 -type f  -name "archiso-usb*.conf" -printf "%f\n")
852
853     for _efi_config in "${_efi_config_list[@]}"; do
854         sed "s|%ARCHISO_LABEL%|${iso_label}|g;
855             s|%OS_NAME%|${os_name}|g;
856             s|%KERNEL_FILENAME%|${kernel_filename}|g;
857             s|%ARCH%|${arch}|g;
858             s|%INSTALL_DIR%|${install_dir}|g" \
859         "${script_path}/efiboot/${_use_config_name}/${_efi_config}" > "${isofs_dir}/loader/entries/$(basename "${_efi_config}" | sed "s|usb|${arch}|g")"
860     done
861
862     # edk2-shell based UEFI shell
863     local _efi_shell_arch
864     if [[ -d "${airootfs_dir}/usr/share/edk2-shell" ]]; then
865         for _efi_shell_arch in $(find "${airootfs_dir}/usr/share/edk2-shell" -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 -I{} basename {}); do
866             if [[ -f "${airootfs_dir}/usr/share/edk2-shell/${_efi_shell_arch}/Shell_Full.efi" ]]; then
867                 cp "${airootfs_dir}/usr/share/edk2-shell/${_efi_shell_arch}/Shell_Full.efi" "${isofs_dir}/EFI/shell_${_efi_shell_arch}.efi"
868             elif [[ -f "${airootfs_dir}/usr/share/edk2-shell/${_efi_shell_arch}/Shell.efi" ]]; then
869                 cp "${airootfs_dir}/usr/share/edk2-shell/${_efi_shell_arch}/Shell.efi" "${isofs_dir}/EFI/shell_${_efi_shell_arch}.efi"
870             else
871                 continue
872             fi
873             echo -e "title  UEFI Shell ${_efi_shell_arch}\nefi    /EFI/shell_${_efi_shell_arch}.efi" > "${isofs_dir}/loader/entries/uefi-shell-${_efi_shell_arch}.conf"
874         done
875     fi
876
877     return 0
878 }
879
880 # Prepare efiboot.img::/EFI for "El Torito" EFI boot mode
881 make_efiboot() {
882     truncate -s 128M "${build_dir}/efiboot.img"
883     mkfs.fat -n ARCHISO_EFI "${build_dir}/efiboot.img"
884
885     mkdir -p "${build_dir}/efiboot"
886     mount "${build_dir}/efiboot.img" "${build_dir}/efiboot"
887
888     mkdir -p "${build_dir}/efiboot/EFI/alteriso/${arch}" "${build_dir}/efiboot/EFI/boot" "${build_dir}/efiboot/loader/entries"
889     cp "${isofs_dir}/${install_dir}/boot/${arch}/${kernel_filename}" "${build_dir}/efiboot/EFI/alteriso/${arch}/${kernel_filename}.efi"
890     cp "${isofs_dir}/${install_dir}/boot/${arch}/archiso.img" "${build_dir}/efiboot/EFI/alteriso/${arch}/archiso.img"
891
892     local _ucode_image _efi_config _use_config_name="nosplash" _bootfile
893     for _ucode_image in "${airootfs_dir}/boot/"{intel-uc.img,intel-ucode.img,amd-uc.img,amd-ucode.img,early_ucode.cpio,microcode.cpio}; do
894         [[ -e "${_ucode_image}" ]] && cp "${_ucode_image}" "${build_dir}/efiboot/EFI/alteriso/"
895     done
896
897     cp "${airootfs_dir}/usr/share/efitools/efi/HashTool.efi" "${build_dir}/efiboot/EFI/boot/"
898
899     _bootfile="$(basename "$(ls "${airootfs_dir}/usr/lib/systemd/boot/efi/systemd-boot"*".efi" )")"
900     cp "${airootfs_dir}/usr/lib/systemd/boot/efi/${_bootfile}" "${build_dir}/efiboot/EFI/boot/${_bootfile#systemd-}"
901
902     [[ "${boot_splash}" = true ]] && _use_config_name="splash"
903     sed "s|%ARCH%|${arch}|g;" "${script_path}/efiboot/${_use_config_name}/loader.conf" > "${build_dir}/efiboot/loader/loader.conf"
904
905     find "${isofs_dir}/loader/entries/" -maxdepth 1 -mindepth 1 -name "uefi-shell*" -type f -printf "%p\0" | xargs -0 -I{} cp {} "${build_dir}/efiboot/loader/entries/"
906
907     readarray -t _efi_config_list < <(find "${script_path}/efiboot/${_use_config_name}/" -mindepth 1 -maxdepth 1 -type f -name "archiso-cd*.conf" -printf "%f\n" | grep -v "rescue")
908     [[ "${norescue_entry}" = false ]] && readarray -t _efi_config_list < <(find "${script_path}/efiboot/${_use_config_name}/" -mindepth 1 -maxdepth 1 -type f  -name "archiso-cd*.conf" -printf "%f\n")
909
910     for _efi_config in "${_efi_config_list[@]}"; do
911         sed "s|%ARCHISO_LABEL%|${iso_label}|g;
912             s|%OS_NAME%|${os_name}|g;
913             s|%KERNEL_FILENAME%|${kernel_filename}|g;
914             s|%ARCH%|${arch}|g;
915             s|%INSTALL_DIR%|${install_dir}|g" \
916         "${script_path}/efiboot/${_use_config_name}/${_efi_config}" > "${build_dir}/efiboot/loader/entries/$(basename "${_efi_config}" | sed "s|cd|${arch}|g")"
917     done
918
919     find "${isofs_dir}/EFI" -maxdepth 1 -mindepth 1 -name "shell*.efi" -printf "%p\0" | xargs -0 -I{} cp {} "${build_dir}/efiboot/EFI/"
920     umount -d "${build_dir}/efiboot"
921
922     return 0
923 }
924
925 # Compress tarball
926 make_tarball() {
927     # backup airootfs.img for tarball
928     msg_debug "Tarball filename is ${tar_filename}"
929     msg_info "Copying airootfs.img ..."
930     cp "${airootfs_dir}.img" "${airootfs_dir}.img.org"
931
932     # Run script
933     mount_airootfs
934     [[ -f "${airootfs_dir}/root/optimize_for_tarball.sh" ]] && _chroot_run "bash /root/optimize_for_tarball.sh -u ${username}"
935     _cleanup_common
936     _chroot_run "mkinitcpio -P"
937     remove "${airootfs_dir}/root/optimize_for_tarball.sh"
938
939     # make
940     tar_comp_opt+=("--${tar_comp}")
941     mkdir -p "${out_dir}"
942     msg_info "Creating tarball..."
943     cd -- "${airootfs_dir}"
944     msg_debug "Run tar -c -v -p -f \"${out_dir}/${tar_filename}\" ${tar_comp_opt[*]} ./*"
945     tar -c -v -p -f "${out_dir}/${tar_filename}" "${tar_comp_opt[@]}" ./*
946     cd -- "${OLDPWD}"
947
948     # checksum
949     _mkchecksum "${out_dir}/${tar_filename}"
950     msg_info "Done! | $(ls -sh "${out_dir}/${tar_filename}")"
951
952     remove "${airootfs_dir}.img"
953     mv "${airootfs_dir}.img.org" "${airootfs_dir}.img"
954
955     [[ "${noiso}" = true ]] && msg_info "The password for the live user and root is ${password}."
956     
957     return 0
958 }
959
960
961 # Build airootfs filesystem image
962 make_prepare() {
963     mount_airootfs
964
965     # Create packages list
966     msg_info "Creating a list of installed packages on live-enviroment..."
967     pacman-key --init
968     pacman -Q --sysroot "${airootfs_dir}" | tee "${isofs_dir}/${install_dir}/pkglist.${arch}.txt" "${build_dir}/packages-full.list" > /dev/null
969
970     # Cleanup
971     remove "${airootfs_dir}/root/optimize_for_tarball.sh"
972     _cleanup_airootfs
973
974     # Create squashfs
975     mkdir -p -- "${isofs_dir}/${install_dir}/${arch}"
976     msg_info "Creating SquashFS image, this may take some time..."
977     mksquashfs "${airootfs_dir}" "${build_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend -comp "${sfs_comp}" "${sfs_comp_opt[@]}"
978
979     # Create checksum
980     msg_info "Creating checksum file for self-test..."
981     echo "$(sha512sum "${isofs_dir}/${install_dir}/${arch}/airootfs.sfs" | getclm 1) airootfs.sfs" > "${isofs_dir}/${install_dir}/${arch}/airootfs.sha512"
982     msg_info "Done!"
983
984     # Sign with gpg
985     if [[ -v gpg_key ]] && (( "${#gpg_key}" != 0 )); then
986         msg_info "Creating signature file ($gpg_key) ..."
987         cd -- "${isofs_dir}/${install_dir}/${arch}"
988         gpg --detach-sign --default-key "${gpg_key}" "airootfs.sfs"
989         cd -- "${OLDPWD}"
990         msg_info "Done!"
991     fi
992
993     umount_work
994
995     [[ "${cleaning}" = true ]] && remove "${airootfs_dir}" "${airootfs_dir}.img"
996
997     return 0
998 }
999
1000 make_alteriso_info(){
1001     # iso version info
1002     if [[ "${include_info}" = true ]]; then
1003         local _info_file="${isofs_dir}/alteriso-info" _version="${iso_version}"
1004         remove "${_info_file}"; touch "${_info_file}"
1005         [[ -d "${script_path}/.git" ]] && [[ "${gitversion}" = false ]] && _version="${iso_version}-${gitrev}"
1006         "${tools_dir}/alteriso-info.sh" -a "${arch}" -b "${boot_splash}" -c "${channel_name%.add}" -d "${iso_publisher}" -k "${kernel}" -o "${os_name}" -p "${password}" -u "${username}" -v "${_version}" > "${_info_file}"
1007     fi
1008
1009     return 0
1010 }
1011
1012 # Add files to the root of isofs
1013 make_overisofs() {
1014     local _over_isofs_list _isofs
1015     _over_isofs_list=("${channel_dir}/over_isofs.any""${channel_dir}/over_isofs.${arch}")
1016     for_module '_over_isofs_list+=("${module_dir}/{}/over_isofs.any""${module_dir}/{}/over_isofs.${arch}")'
1017     for _isofs in "${_over_isofs_list[@]}"; do
1018         [[ -d "${_isofs}" ]] && cp -af "${_isofs}"/* "${isofs_dir}"
1019     done
1020
1021     return 0
1022 }
1023
1024 # Build ISO
1025 make_iso() {
1026     local _iso_efi_boot_args=()
1027     # If exists, add an EFI "El Torito" boot image (FAT filesystem) to ISO-9660 image.
1028     [[ -f "${build_dir}/efiboot.img" ]] && _iso_efi_boot_args=(-append_partition 2 C12A7328-F81F-11D2-BA4B-00A0C93EC93B "${build_dir}/efiboot.img" -appended_part_as_gpt -eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot -isohybrid-gpt-basdat)
1029
1030     mkdir -p -- "${out_dir}"
1031     msg_info "Creating ISO image..."
1032     xorriso -as mkisofs \
1033         -iso-level 3 \
1034         -full-iso9660-filenames \
1035         -joliet \
1036         -joliet-long \
1037         -rational-rock \
1038         -volid "${iso_label}" \
1039         -appid "${iso_application}" \
1040         -publisher "${iso_publisher}" \
1041         -preparer "prepared by AlterISO" \
1042         -eltorito-boot syslinux/isolinux.bin \
1043         -eltorito-catalog syslinux/boot.cat \
1044         -no-emul-boot -boot-load-size 4 -boot-info-table \
1045         -isohybrid-mbr "${build_dir}/iso/syslinux/isohdpfx.bin" \
1046         "${_iso_efi_boot_args[@]}" \
1047         -output "${out_dir}/${iso_filename}" \
1048         "${build_dir}/iso/"
1049     _mkchecksum "${out_dir}/${iso_filename}"
1050     msg_info "Done! | $(ls -sh -- "${out_dir}/${iso_filename}")"
1051
1052     msg_info "The password for the live user and root is ${password}."
1053
1054     return 0
1055 }
1056
1057
1058 # Parse options
1059 ARGUMENT=("${DEFAULT_ARGUMENT[@]}" "${@}")
1060 OPTS=("a:" "b" "c:" "d" "e" "g:" "h" "j" "k:" "l:" "o:" "p:" "r" "t:" "u:" "w:" "x")
1061 OPTL=("arch:" "boot-splash" "comp-type:" "debug" "cleaning" "cleanup" "gpgkey:" "help" "lang:" "japanese" "kernel:" "out:" "password:" "comp-opts:" "user:" "work:" "bash-debug" "nocolor" "noconfirm" "nodepend" "gitversion" "msgdebug" "noloopmod" "tarball" "noiso" "noaur" "nochkver" "channellist" "config:" "noefi" "nodebug" "nosigcheck" "normwork" "log" "logpath:" "nolog" "nopkgbuild" "pacman-debug" "confirm" "tar-type:" "tar-opts:" "add-module:")
1062 OPT="$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" --  "${ARGUMENT[@]}")" || exit 1
1063
1064 eval set -- "${OPT}"
1065 msg_debug "Argument: ${OPT}"
1066 unset OPT OPTS OPTL DEFAULT_ARGUMENT
1067
1068 while true; do
1069     case "${1}" in
1070         -a | --arch)
1071             arch="${2}"
1072             shift 2
1073             ;;
1074         -b | --boot-splash)
1075             boot_splash=true
1076             shift 1
1077             ;;
1078         -c | --comp-type)
1079             case "${2}" in
1080                 "gzip" | "lzma" | "lzo" | "lz4" | "xz" | "zstd") sfs_comp="${2}" ;;
1081                 *) msg_error "Invaild compressors '${2}'" '1' ;;
1082             esac
1083             shift 2
1084             ;;
1085         -d | --debug)
1086             debug=true
1087             shift 1
1088             ;;
1089         -e | --cleaning | --cleanup)
1090             cleaning=true
1091             shift 1
1092             ;;
1093         -g | --gpgkey)
1094             gpg_key="${2}"
1095             shift 2
1096             ;;
1097         -h | --help)
1098             _usage 0
1099             ;;
1100         -j | --japanese)
1101             msg_error "This option is obsolete in AlterISO 3. To use Japanese, use \"-l ja\"." "1"
1102             ;;
1103         -k | --kernel)
1104             customized_kernel=true
1105             kernel="${2}"
1106             shift 2
1107             ;;
1108         -l | --lang)
1109             locale_name="${2}"
1110             shift 2
1111             ;;
1112         -o | --out)
1113             out_dir="${2}"
1114             shift 2
1115             ;;
1116         -p | --password)
1117             customized_password=true
1118             password="${2}"
1119             shift 2
1120             ;;
1121         -r | --tarball)
1122             tarball=true
1123             shift 1
1124             ;;
1125         -t | --comp-opts)
1126             if [[ "${2}" = "reset" ]]; then
1127                 sfs_comp_opt=()
1128             else
1129                 IFS=" " read -r -a sfs_comp_opt <<< "${2}"
1130             fi
1131             shift 2
1132             ;;
1133         -u | --user)
1134             customized_username=true
1135             username="$(echo -n "${2}" | sed 's/ //g' | tr '[:upper:]' '[:lower:]')"
1136             shift 2
1137             ;;
1138         -w | --work)
1139             work_dir="${2}"
1140             shift 2
1141             ;;
1142         -x | --bash-debug)
1143             debug=true
1144             bash_debug=true
1145             shift 1
1146             ;;
1147         --noconfirm)
1148             noconfirm=true
1149             shift 1
1150             ;;
1151         --confirm)
1152             noconfirm=false
1153             shift 1
1154             ;;
1155         --nodepend)
1156             nodepend=true
1157             shift 1
1158             ;;
1159         --nocolor)
1160             nocolor=true
1161             shift 1
1162             ;;
1163         --gitversion)
1164             if [[ -d "${script_path}/.git" ]]; then
1165                 gitversion=true
1166             else
1167                 msg_error "There is no git directory. You need to use git clone to use this feature." "1"
1168             fi
1169             shift 1
1170             ;;
1171         --msgdebug)
1172             msgdebug=true;
1173             shift 1
1174             ;;
1175         --noloopmod)
1176             noloopmod=true
1177             shift 1
1178             ;;
1179         --noiso)
1180             noiso=true
1181             shift 1
1182             ;;
1183         --noaur)
1184             noaur=true
1185             shift 1
1186             ;;
1187         --nochkver)
1188             nochkver=true
1189             shift 1
1190             ;;
1191         --nodebug)
1192             debug=false
1193             msgdebug=false
1194             bash_debug=false
1195             shift 1
1196             ;;
1197         --noefi)
1198             noefi=true
1199             shift 1
1200             ;;
1201         --channellist)
1202             show_channel_list
1203             exit 0
1204             ;;
1205         --config)
1206             source "${2}"
1207             shift 2
1208             ;;
1209         --pacman-debug)
1210             pacman_debug=true
1211             shift 1
1212             ;;
1213         --nosigcheck)
1214             nosigcheck=true
1215             shift 1
1216             ;;
1217         --normwork)
1218             normwork=true
1219             shift 1
1220             ;;
1221         --log)
1222             logging=true
1223             shift 1
1224             ;;
1225         --logpath)
1226             logging="${2}"
1227             customized_logpath=true
1228             shift 2
1229             ;;
1230         --nolog)
1231             logging=false
1232             shift 1
1233             ;;
1234         --nopkgbuild)
1235             nopkgbuild=true
1236             shift 1
1237             ;;
1238         --tar-type)
1239             case "${2}" in
1240                 "gzip" | "lzma" | "lzo" | "lz4" | "xz" | "zstd") tar_comp="${2}" ;;
1241                 *) msg_error "Invaild compressors '${2}'" '1' ;;
1242             esac
1243             shift 2
1244             ;;
1245         --tar-opts)
1246             IFS=" " read -r -a tar_comp_opt <<< "${2}"
1247             shift 2
1248             ;;
1249         --add-module)
1250             readarray -t -O "${#additional_modules[@]}" additional_modules < <(echo "${2}" | tr "," "\n")
1251             msg_debug "Added modules: ${additional_modules[*]}"
1252             shift 2
1253             ;;
1254         --)
1255             shift
1256             break
1257             ;;
1258         *)
1259             msg_error "Argument exception error '${1}'"
1260             msg_error "Please report this error to the developer." 1
1261             ;;
1262     esac
1263 done
1264
1265 # Check root.
1266 if (( ! "${EUID}" == 0 )); then
1267     msg_warn "This script must be run as root." >&2
1268     msg_warn "Re-run 'sudo ${0} ${ARGUMENT[*]}'"
1269     sudo "${0}" "${ARGUMENT[@]}"
1270     exit "${?}"
1271 fi
1272
1273 # Show config message
1274 msg_debug "Use the default configuration file (${defaultconfig})."
1275 [[ -f "${script_path}/custom.conf" ]] && msg_debug "The default settings have been overridden by custom.conf"
1276
1277 # Debug mode
1278 [[ "${bash_debug}" = true ]] && set -x -v
1279
1280 # Check for a valid channel name
1281 if [[ -n "${1+SET}" ]]; then
1282     case "$(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" -n check "${1}"; printf "%d" "${?}")" in
1283         "2")
1284             msg_error "Invalid channel ${1}" "1"
1285             ;;
1286         "1")
1287             channel_dir="${1}"
1288             channel_name="$(basename "${1%/}")"
1289             ;;
1290         "0")
1291             channel_dir="${script_path}/channels/${1}"
1292             channel_name="${1}"
1293             ;;
1294     esac
1295 else
1296     channel_dir="${script_path}/channels/${channel_name}"
1297 fi
1298
1299 # Set vars
1300 build_dir="${work_dir}/build/${arch}" cache_dir="${work_dir}/cache/${arch}" airootfs_dir="${build_dir}/airootfs" isofs_dir="${build_dir}/iso" lockfile_dir="${build_dir}/lockfile" gitrev="$(cd "${script_path}"; git rev-parse --short HEAD)" preset_dir="${script_path}/presets"
1301
1302 # Create dir
1303 for _dir in build_dir cache_dir airootfs_dir isofs_dir lockfile_dir out_dir; do
1304     mkdir -p "$(eval "echo \$${_dir}")"
1305     msg_debug "${_dir} is $(realpath "$(eval "echo \$${_dir}")")"
1306     eval "${_dir}=\"$(realpath "$(eval "echo \$${_dir}")")\""
1307 done
1308
1309
1310 # Set for special channels
1311 if [[ -d "${channel_dir}.add" ]]; then
1312     channel_name="${1}"
1313     channel_dir="${channel_dir}.add"
1314 elif [[ "${channel_name}" = "clean" ]]; then
1315    _run_cleansh
1316     exit 0
1317 fi
1318
1319 # Check channel version
1320 msg_debug "channel path is ${channel_dir}"
1321 if [[ ! "$(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" ver "${channel_name}" | cut -d "." -f 1)" = "$(echo "${alteriso_version}" | cut -d "." -f 1)" ]] && [[ "${nochkver}" = false ]]; then
1322     msg_error "This channel does not support Alter ISO 3."
1323     if [[ -d "${script_path}/.git" ]]; then
1324         msg_error "Please run \"git checkout alteriso-2\"" "1"
1325     else
1326         msg_error "Please download old version here.\nhttps://github.com/FascodeNet/alterlinux/releases" "1"
1327     fi
1328 fi
1329
1330 set -eu
1331
1332 prepare_env
1333 prepare_build
1334 show_settings
1335 run_once make_pacman_conf
1336 run_once make_basefs # Mount airootfs
1337 run_once make_packages_repo
1338 [[ "${noaur}" = false ]] && run_once make_packages_aur
1339 [[ "${nopkgbuild}" = false ]] && run_once make_pkgbuild
1340 run_once make_customize_airootfs
1341 run_once make_setup_mkinitcpio
1342 [[ "${tarball}" = true ]] && run_once make_tarball
1343 if [[ "${noiso}" = false ]]; then
1344     run_once make_syslinux
1345     run_once make_isolinux
1346     run_once make_boot
1347     run_once make_boot_extra
1348     if [[ "${noefi}" = false ]]; then
1349         run_once make_efi
1350         run_once make_efiboot
1351     fi
1352     run_once make_alteriso_info
1353     run_once make_prepare
1354     run_once make_overisofs
1355     run_once make_iso
1356 fi
1357
1358 [[ "${cleaning}" = true ]] && _run_cleansh
1359
1360 exit 0