OSDN Git Service

2a8656e7c60504db4ddfdfa38547ca9f927f8a31
[alterlinux/LFBS.git] / build.sh
1 #!/usr/bin/env bash
2
3 # SPDX-License-Identifier: GPL-3.0
4 #
5 # kokkiemouse
6 # Twitter: @kokkiemouse
7 # Email  : kokkiemouse@gmail.com
8 #
9 # lfbs
10 #
11
12 set -e
13 # set -u
14
15 #export LANG=C
16
17 script_path=$(readlink -f "${0%/*}")
18 work_dir="${script_path}/work"
19 channels_dir="${script_path}/channels"
20 nfb_dir="${script_path}/nfb"
21 codename="33"
22 os_name="SereneLinux"
23 iso_name="Fedora"
24 language="ja_JP.UTF-8"
25 channel_name="serene"
26 cache_dir="${script_path}/cache"
27 bootsplash=false
28 arch="x86_64"
29
30 out_dir="${script_path}/out"
31 iso_label="${os_name}_${codename}_${arch}"
32 iso_publisher='Fascode Network <https://fascode.net>'
33 iso_application="${os_name} Live/Rescue CD"
34 iso_version="${codename}-$(date +%Y.%m.%d)"
35 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
36 liveuser_name="fedora"
37 liveuser_password="fedora"
38 liveuser_shell="/usr/bin/zsh"
39
40 #-- language config --#
41
42 # Sets the default locale for the live environment.
43 # You can also place a package list for that locale name and install packages specific to that locale.
44 locale_name="en"
45 locale_gen_name="en_US.UTF-8"
46 locale_version="gl"
47 locale_time="UTC"
48 locale_fullname="global"
49
50 debug=false
51 cache_only=false
52 grub2_standalone_cmd=grub2-mkstandalone
53
54 start_time="$(date +%s)"
55
56 _msg_common() {
57     if [[ "${debug}" = true ]]; then
58         local _current_time
59         local _time
60         _current_time="$(date +%s)"
61         _time="$(("${_current_time}"-"${start_time}"))"
62
63         if [[ "${_time}" -ge 3600 ]]; then
64             echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
65         elif [[ "${_time}" -ge 60 ]]; then
66             echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
67         else
68             echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
69         fi
70     else
71         "${script_path}/echo_color" -t 6 "[LFBS Core]"
72     fi
73 }
74
75 # Show an INFO message
76 # _msg_info <message>
77 _msg_info() {
78     local _msg
79     _msg="${@}"
80     echo "$(_msg_common)  $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
81 }
82
83 # Show an debug message
84 # _msg_debug <message>
85 _msg_debug() {
86     if [[ "${debug}" = true ]]; then
87         local _msg
88         _msg="${@}"
89         echo "$(_msg_common)  $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
90     fi
91 }
92
93 # Show an ERROR message then exit with status
94 # _msg_error <message> <exit code>
95 _msg_error() {
96     local _msg
97     local _error
98     _msg="${1}"
99     _error=${2}
100     echo "$(_msg_common)  $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
101
102     if [[ ! ${_error} = 0 ]]; then
103         exit ${_error}
104     fi
105 }
106
107 # Unmount chroot dir
108 umount_chroot () {
109     local mount
110
111     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
112         if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
113             _msg_info "Unmounting ${mount}"
114             umount -fl "${mount}"
115         fi
116     done
117 }
118
119 # Unmount chroot dir and airootfs
120 umount_chroot_airootfs () {
121     local mount
122
123     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
124         _msg_info "Unmounting ${mount}"
125         umount -fl "${mount}"
126     done
127 }
128 # Helper function to run make_*() only one time.
129 run_once() {
130     local name
131     umount_chroot
132     name="$1"
133
134     if [[ ! -e "${work_dir}/build.${name}" ]]; then
135         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
136         "${1}"
137         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
138         touch "${work_dir}/build.${name}"
139     fi
140 }
141
142 run_cmd() {
143     local mount
144
145     for mount in "dev" "dev/pts" "proc" "sys" ; do
146     #for mount in "dev" "dev/pts" "proc" "sys" ; do
147         mount --bind /${mount} "${work_dir}/airootfs/${mount}"
148     done
149     
150     mkdir -p "${work_dir}/airootfs/run/systemd/resolve/"
151     cp /etc/resolv.conf "${work_dir}/airootfs/run/systemd/resolve/stub-resolv.conf"
152     unshare --fork --pid chroot "${work_dir}/airootfs" "${@}"
153
154     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
155         if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
156             umount -fl "${mount}"
157         fi
158     done
159 }
160
161 _dnf_install() {    
162     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
163     run_cmd dnf -c /dnf_conf install -y ${@}
164 }
165
166 # rm helper
167 # Delete the file if it exists.
168 # For directories, rm -rf is used.
169 # If the file does not exist, skip it.
170 # remove <file> <file> ...
171 remove() {
172     local _list
173     local _file
174     _list=($(echo "$@"))
175
176     for _file in "${_list[@]}"; do
177         _msg_debug "Removeing ${_file}"
178
179         if [[ -f ${_file} ]]; then
180             rm -f "${_file}"
181         elif [[ -d ${_file} ]]; then
182             rm -rf "${_file}"
183         fi
184     done
185 }
186
187 # Show help
188 _usage () {
189     echo "usage ${0} [options] [channel]"
190     echo
191     echo " General options:"
192     echo
193     echo "    -a | --arch <str>      Set architecture"
194     echo "                           Default: ${arch}"
195     echo "    -b | --bootsplash      Enable Plymouth"
196     echo "    -l | --lang <lang>     Specifies the default language for the live environment"
197     echo "                           Default: ${locale_name}"
198     echo "    -m | --mirror <url>    Set apt mirror server."
199     echo "                           Default: ${mirror}"
200     echo "    -o | --out <out_dir>   Set the output directory"
201     echo "                           Default: ${out_dir}"
202     echo "    -w | --work <work_dir> Set the working directory"
203     echo "                           Default: ${work_dir}"
204     echo "    -c | --cache <cache_dir> Set the cache directory"
205     echo "                           Default: ${cache_dir}"
206     echo
207     echo "    -d | --debug           "
208     echo "    -h | --help            This help message and exit"
209     echo
210     echo "You can switch between installed packages, files included in images, etc. by channel."
211     echo
212     echo " Language for each architecture:"
213     for _list in ${script_path}/system/locale-* ; do
214         _arch="${_list#${script_path}/system/locale-}"
215         echo -n "    ${_arch}"
216         for i in $( seq 1 $(( ${blank} - 4 - ${#_arch} )) ); do
217             echo -ne " "
218         done
219         _locale_name_list=$(cat ${_list} | grep -h -v ^'#' | awk '{print $1}')
220         for _lang in ${_locale_name_list[@]};do
221             echo -n "${_lang} "
222         done
223         echo
224     done
225     echo " Channel:"
226     
227     local _channel
228     local channel_list
229     local description
230
231     for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
232         if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
233             channel_list+=( "${_channel}" )
234         fi
235     done
236
237     for _channel in ${channel_list[@]}; do
238         if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
239             description=$(cat "${channels_dir}/${_channel}/description.txt")
240         else
241             description="This channel does not have a description.txt."
242         fi
243
244         echo -ne "    ${_channel}"
245
246         for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
247             echo -ne " "
248         done
249         
250         echo -ne "${description}\n"
251     done
252 }
253
254 dnfstrap() {
255     if [[ ! -d "${cache_dir}" ]]; then
256         mkdir -p "${cache_dir}"
257     fi
258     cp -rf "${script_path}/system/dnfconf.conf" "${work_dir}/airootfs/dnf_conf"
259     if [[ ! -d "${work_dir}/airootfs/dnf_cache" ]]; then
260         mkdir -p "${work_dir}/airootfs/dnf_cache"
261     fi
262     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
263     dnf -c "${work_dir}/airootfs/dnf_conf" --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) install ${@} -y
264     umount -fl "${work_dir}/airootfs/dnf_cache"
265 }
266
267 make_basefs() {
268     _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
269     dnfstrap @Core yamad-repo 
270     _msg_info "${codename} installed successfully!"
271     
272     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
273     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
274     run_cmd dnf -c /dnf_conf update --refresh -y
275     run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
276     # run_cmd apt-get upgrade
277 }
278
279
280 # Parse files
281 parse_files() {
282     #-- ロケールを解析、設定 --#
283     local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
284
285     # 選択されたロケールの設定が描かれた行番号を取得
286     _locale_config_file="${script_path}/system/locale-${arch}"
287     _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
288     _get_locale_line_number() {
289         local _lang _count=0
290         for _lang in ${_locale_name_list[@]}; do
291             _count=$(( _count + 1 ))
292             if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
293         done
294         echo -n "failed"
295     }
296     _locale_line_number="$(_get_locale_line_number)"
297
298     # 不正なロケール名なら終了する
299     [[ "${_locale_line_number}" == "failed" ]] && _msg_error "${locale_name} is not a valid language." "1"
300
301     # ロケール設定ファイルから該当の行を抽出
302     _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
303
304     # 抽出された行に書かれた設定をそれぞれの変数に代入
305     # ここで定義された変数のみがグローバル変数
306     locale_name="${_locale_config_line[0]}"
307     locale_gen_name="${_locale_config_line[1]}"
308     locale_version="${_locale_config_line[2]}"
309     locale_time="${_locale_config_line[3]}"
310     locale_fullname="${_locale_config_line[4]}"
311 }
312
313 prepare_build() {
314     if [[ ${EUID} -ne 0 ]]; then
315         _msg_error "This script must be run as root." 1
316     fi
317     umount_chroot_airootfs
318     # Check codename
319     if [[ -z "$(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename})" ]]; then
320         _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
321     fi
322     if [[ ! -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
323         mkdir -p "${work_dir}/squashfsroot/LiveOS/"
324         mkdir -p "${work_dir}/airootfs/"
325         _msg_info "Make rootfs image..."
326         truncate -s 6G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
327         _msg_info "Format rootfs image..."
328         mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
329     fi    
330     mkdir -p "${out_dir}"
331     _msg_info "Mount rootfs image..."
332     mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
333
334 }
335
336 make_systemd() {
337     _dnf_install dbus-tools
338     run_cmd dbus-uuidgen --ensure=/etc/machine-id
339     if [[ ! -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
340         run_cmd mkdir /var/lib/dbus
341     fi
342     run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
343 }
344 make_repo_packages() {    
345     local  _pkg  _pkglist=($("${script_path}/tools/pkglist.sh" -a "x86_64" -c "${channels_dir}/${channel_name}" -k "${codename}" -l "${locale_name}" $(if [[ "${boot_splash}" = true ]]; then echo -n "-b"; fi) ))
346     # Create a list of packages to be finally installed as packages.list directly under the working directory.
347     echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
348     # Install packages on airootfs
349     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
350     run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
351 }
352 make_dnf_packages() {
353     
354     #local  _pkg  _pkglist=($("${script_path}/tools/pkglist.sh" -a "x86_64" -k "${kernel}" -c "${channel_dir}" -l "${locale_name}" $(if [[ "${boot_splash}" = true ]]; then echo -n "-b"; fi) ))
355     local  _pkg  _pkglist=($("${script_path}/tools/pkglist.sh" -a "x86_64" -c "${channels_dir}/${channel_name}" -l "${locale_name}" $(if [[ "${boot_splash}" = true ]]; then echo -n "-b"; fi) ))
356     # Create a list of packages to be finally installed as packages.list directly under the working directory.
357     echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
358     for _pkg in ${_pkglist[@]}; do
359         echo ${_pkg} >> "${work_dir}/packages.list"
360     done
361
362     # Install packages on airootfs
363     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
364     run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
365 }
366
367 make_cp_airootfs() {
368     local _copy_airootfs
369
370     _copy_airootfs() {
371         local _dir="${1%/}"
372         if [[ -d "${_dir}" ]]; then
373             cp -af "${_dir}"/* "${work_dir}/airootfs"
374         fi
375     }
376     _copy_airootfs "${channels_dir}/share/airootfs"
377     _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
378     _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
379     _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
380 }
381
382 make_config() {
383     # customize_airootfs options
384     # -b                        : Enable boot splash.
385     # -d                        : Enable debug mode.
386     # -g <locale_gen_name>      : Set locale-gen.
387     # -i <inst_dir>             : Set install dir
388     # -k <kernel config line>   : Set kernel name.
389     # -o <os name>              : Set os name.
390     # -p <password>             : Set password.
391     # -s <shell>                : Set user shell.
392     # -t                        : Set plymouth theme.
393     # -u <username>             : Set live user name.
394     # -x                        : Enable bash debug mode.
395     # -r                        : Enable rebuild.
396     # -z <locale_time>          : Set the time zone.
397     # -l <locale_name>          : Set language.
398     #
399     # -j is obsolete in AlterISO3 and cannot be used.
400     # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
401     local _airootfs_script_options _run_script
402     _airootfs_script_options="-p ${liveuser_password} -u ${liveuser_name} -o ${os_name} -s ${liveuser_shell} -a ${arch} -g ${locale_gen_name} -l ${locale_name} -z ${locale_time} "
403     if [[ ${bootsplash} == true ]]; then
404         _airootfs_script_options="${_airootfs_script_options} -b"
405     fi
406     _run_script() {
407         local _file
408         for _file in ${@}; do
409             if [[ -f "${work_dir}/airootfs${_file}" ]]; then run_cmd "${_file}" ${_airootfs_script_options}; fi
410             if [[ -f "${work_dir}/airootfs${_file}" ]]; then chmod 755 "${work_dir}/airootfs${_file}"; fi
411         done
412     }
413
414     _run_script "/root/customize_airootfs.sh" "/root/customize_airootfs_${channel_name}.sh"
415 }
416
417 make_clean() {
418     mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
419     run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
420 }
421
422 make_squashfs() {
423     # prepare
424     remove "${bootfiles_dir}"
425     if [[ -d "${work_dir}/airootfs/dnf_cache" ]]; then
426         rm -rf "${work_dir}/airootfs/dnf_cache"
427     fi
428     mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
429     #generate initrd
430     _msg_info "make initrd..."
431     run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
432     cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
433     mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
434     #cp isolinux
435     cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
436     # make squashfs
437     remove "${work_dir}/airootfs/boot"
438     mkdir "${work_dir}/airootfs/boot"
439     cp ${bootfiles_dir}/boot/vmlinuz ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules)
440     kernelkun=$(run_cmd ls /lib/modules)
441     echo -e "\nkernel-install add ${kernelkun} /boot/vmlinuz-${kernelkun}" >> ${work_dir}/airootfs/usr/share/calamares/final-process
442     umount "${work_dir}/airootfs"
443     _msg_info "e2fsck..."
444     e2fsck -f "${work_dir}/squashfsroot/LiveOS/rootfs.img"
445     _msg_info "Minimize rootfs..."
446     resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
447     _msg_info "Compress rootfs.."
448     mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img" -noappend -no-recovery -comp zstd -Xcompression-level 19 -b 1048576
449     _msg_info "Deleting block image..."
450     rm -rf "${work_dir}/squashfsroot/LiveOS/rootfs.img"
451 }
452
453 make_nfb() {
454     touch "${bootfiles_dir}/fedora_lfbs"
455     # isolinux setup
456     sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g"  > "${bootfiles_dir}/isolinux/isolinux.cfg"
457     #grub
458     if [[ ${bootsplash} = true ]]; then
459         sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" | sed "s|selinux=0|selinux=0 quiet splash|g" > "${bootfiles_dir}/grub/grub.cfg"
460     else
461         sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
462     fi
463     sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" | sed "s|selinux=0|selinux=0 quiet splash|g" > "${bootfiles_dir}/grub/grub.cfg"
464 }
465 make_efi() {
466     # UEFI 32bit (ia32)
467     ${grub2_standalone_cmd} \
468         --format=i386-efi \
469         --output="${bootfiles_dir}/grub/bootia32.efi" \
470         --locales="" \
471         --fonts="" \
472         "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
473     
474     # UEFI 64bit (x64)
475     ${grub2_standalone_cmd} \
476         --format=x86_64-efi \
477         --output="${bootfiles_dir}/grub/bootx64.efi" \
478         --locales="" \
479         --fonts="" \
480         "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
481
482     # create efiboot.img
483     truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
484     mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
485     mkdir "${bootfiles_dir}/mnt"
486     mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
487     mkdir -p "${bootfiles_dir}/mnt/efi/boot"
488     cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
489     cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
490     umount -d "${bootfiles_dir}/mnt"
491     remove "${bootfiles_dir}/mnt"
492 }
493 make_iso() {
494     cd "${bootfiles_dir}"
495
496     # create checksum (using at booting)
497     bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
498
499     # create iso
500     xorriso \
501         -as mkisofs \
502         -iso-level 3 \
503         -full-iso9660-filenames \
504         -volid "${iso_label}" \
505         -appid "${iso_application}" \
506         -publisher "${iso_publisher}" \
507         -preparer "prepared by LFBS" \
508         -b isolinux/isolinux.bin \
509             -no-emul-boot \
510             -boot-load-size 4 \
511             -boot-info-table \
512         -eltorito-alt-boot \
513             -eltorito-platform efi \
514             -eltorito-boot EFI/efiboot.img \
515             -no-emul-boot \
516         -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
517         -isohybrid-gpt-basdat \
518         -eltorito-catalog isolinux/boot.cat \
519         -output "${out_dir}/${iso_filename}" \
520         -graft-points \
521             "." \
522             "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
523             "/EFI/efiboot.img=grub/efiboot.img"
524     
525     cd - > /dev/null
526 }
527
528 make_checksum() {
529     cd "${out_dir}"
530     _msg_info "Creating md5 checksum ..."
531     md5sum "${iso_filename}" > "${iso_filename}.md5"
532
533     _msg_info "Creating sha256 checksum ..."
534     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
535     cd - > /dev/null 2>&1
536     umount_chroot_airootfs
537 }
538
539 # 引数解析()
540 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
541
542 _opt_short="w:l:o:hba:-:m:c:d"
543 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only,bootsplash"
544 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
545
546 if [[ ${?} != 0 ]]; then
547     exit 1
548 fi
549
550 eval set -- "${OPT}"
551
552 while :; do
553     case ${1} in
554         -a | --arch)
555             arch="${2}"
556             shift 2
557             ;;
558         -b | --bootsplash)
559             bootsplash=true
560             shift 1
561             ;;
562         -c | --cache)
563             cache_dir="${2}"
564             shift 2
565             ;;
566         -d | --debug)
567             debug=true
568             shift 1
569             ;;
570         -h | --help)
571             _usage
572             exit 0
573             ;;
574         -m | --mirror)
575             mirror="${2}"
576             shift 2
577             ;;
578         -l | --lang)
579             locale_name="${2}"
580             shift 2
581             ;;
582         -o | --out)
583             out_dir="${2}"
584             shift 2
585             ;;
586         -w | --work)
587             work_dir="${2}"
588             shift 2
589             ;;
590         --cache-only)
591             cache_only=true
592             shift 1
593             ;;
594         --)
595             shift
596             break
597             ;;
598         *)
599             _msg_error "Invalid argument '${1}'"
600             _usage 1
601             ;;
602     esac
603 done
604 if [[ -f /etc/arch-release ]]; then
605     grub2_standalone_cmd=grub-mkstandalone
606 fi
607 bootfiles_dir="${work_dir}/bootfiles"
608 trap  umount_chroot_airootfs 0 2 15
609
610 if [[ -n "${1}" ]]; then
611     channel_name="${1}"
612     if [[ "${channel_name}" = "umount" ]]; then
613         umount_chroot_airootfs
614         exit 0
615     fi
616     if [[ "${channel_name}" = "clean" ]]; then
617         umount_chroot_airootfs
618         _msg_info "deleting work dir..."
619         remove "${work_dir}"
620         exit 0
621     fi
622     check_channel() {
623         local channel_list
624         local i
625         channel_list=()
626         
627         for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
628             if [[ -n "$(ls "${channels_dir}/${_channel}")" ]] && [[ ! "${_channel}" = "share" ]]; then
629                 channel_list+=( "${_channel}" )
630             fi
631         done
632
633         for i in ${channel_list[@]}; do
634             if [[ "${i}" = "${channel_name}" ]]; then
635                 echo -n "true"
636                 return 0
637             fi
638         done
639
640         echo -n "false"
641         return 1
642     }
643
644     if [[ "$(check_channel ${channel_name})" = false ]]; then
645         _msg_error "Invalid channel ${channel_name}"
646         exit 1
647     fi
648 fi
649 iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
650 umount_chroot_airootfs
651 if [[ -d "${work_dir}" ]]; then
652     _msg_info "deleting work dir..."
653     remove "${work_dir}"
654 fi
655
656 prepare_build
657 parse_files
658 run_once make_basefs
659 run_once make_systemd
660 run_once make_repo_packages
661 run_once make_dnf_packages
662 run_once make_cp_airootfs
663 run_once make_config
664 run_once make_clean
665 run_once make_squashfs
666 run_once make_nfb
667 run_once make_efi
668 run_once make_iso
669 run_once make_checksum