OSDN Git Service

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