OSDN Git Service

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