3 # SPDX-License-Identifier: GPL-3.0
6 # Twitter: @kokkiemouse
7 # Email : kokkiemouse@gmail.com
17 script_path=$(readlink -f "${0%/*}")
18 work_dir="${script_path}/work"
19 channels_dir="${script_path}/channels"
20 nfb_dir="${script_path}/nfb"
24 language="ja_JP.UTF-8"
26 cache_dir="${script_path}/cache"
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"
40 #-- language config --#
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.
45 locale_gen_name="en_US.UTF-8"
48 locale_fullname="global"
52 grub2_standalone_cmd=grub2-mkstandalone
54 start_time="$(date +%s)"
57 if [[ "${debug}" = true ]]; then
60 _current_time="$(date +%s)"
61 _time="$(("${_current_time}"-"${start_time}"))"
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]")"
68 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
71 "${script_path}/echo_color" -t 6 "[LFBS Core]"
75 # Show an INFO message
80 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
83 # Show an debug message
84 # _msg_debug <message>
86 if [[ "${debug}" = true ]]; then
89 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
93 # Show an ERROR message then exit with status
94 # _msg_error <message> <exit code>
100 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
102 if [[ ! ${_error} = 0 ]]; then
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}"
119 # Unmount chroot dir and airootfs
120 umount_chroot_airootfs () {
123 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
124 _msg_info "Unmounting ${mount}"
125 umount -fl "${mount}"
128 # Helper function to run make_*() only one time.
134 if [[ ! -e "${work_dir}/build.${name}" ]]; then
135 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
137 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
138 touch "${work_dir}/build.${name}"
145 for mount in "dev" "dev/pts" "proc" "sys" "etc/resolv.conf"; do
146 #for mount in "dev" "dev/pts" "proc" "sys" ; do
147 if [[ "${mount}" == "etc/resolv.conf" ]]; then
148 cp /etc/resolv.conf "${work_dir}/airootfs/${mount}"
150 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
154 unshare --fork --pid chroot "${work_dir}/airootfs" "${@}"
156 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
157 if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
158 umount -fl "${mount}"
164 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
165 run_cmd dnf -c /dnf_conf install -y ${@}
169 # Delete the file if it exists.
170 # For directories, rm -rf is used.
171 # If the file does not exist, skip it.
172 # remove <file> <file> ...
178 for _file in "${_list[@]}"; do
179 _msg_debug "Removeing ${_file}"
181 if [[ -f ${_file} ]]; then
183 elif [[ -d ${_file} ]]; then
191 echo "usage ${0} [options] [channel]"
193 echo " General options:"
195 echo " -a | --arch <str> Set architecture"
196 echo " Default: ${arch}"
197 echo " -b | --bootsplash Enable Plymouth"
198 echo " -l | --lang <lang> Specifies the default language for the live environment"
199 echo " Default: ${locale_name}"
200 echo " -m | --mirror <url> Set apt mirror server."
201 echo " Default: ${mirror}"
202 echo " -o | --out <out_dir> Set the output directory"
203 echo " Default: ${out_dir}"
204 echo " -w | --work <work_dir> Set the working directory"
205 echo " Default: ${work_dir}"
206 echo " -c | --cache <cache_dir> Set the cache directory"
207 echo " Default: ${cache_dir}"
209 echo " -d | --debug "
210 echo " -h | --help This help message and exit"
212 echo "You can switch between installed packages, files included in images, etc. by channel."
214 echo " Language for each architecture:"
215 for _list in ${script_path}/system/locale-* ; do
216 _arch="${_list#${script_path}/system/locale-}"
218 for i in $( seq 1 $(( ${blank} - 4 - ${#_arch} )) ); do
221 _locale_name_list=$(cat ${_list} | grep -h -v ^'#' | awk '{print $1}')
222 for _lang in ${_locale_name_list[@]};do
233 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
234 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
235 channel_list+=( "${_channel}" )
239 for _channel in ${channel_list[@]}; do
240 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
241 description=$(cat "${channels_dir}/${_channel}/description.txt")
243 description="This channel does not have a description.txt."
246 echo -ne " ${_channel}"
248 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
252 echo -ne "${description}\n"
257 if [[ ! -d "${cache_dir}" ]]; then
258 mkdir -p "${cache_dir}"
260 cp -rf "${script_path}/system/dnfconf.conf" "${work_dir}/airootfs/dnf_conf"
261 if [[ ! -d "${work_dir}/airootfs/dnf_cache" ]]; then
262 mkdir -p "${work_dir}/airootfs/dnf_cache"
264 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
265 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
266 umount -fl "${work_dir}/airootfs/dnf_cache"
270 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
271 dnfstrap @Core yamad-repo
272 _msg_info "${codename} installed successfully!"
274 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
275 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
276 run_cmd dnf -c /dnf_conf update -y
277 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
278 # run_cmd apt-get upgrade
285 local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
287 # 選択されたロケールの設定が描かれた行番号を取得
288 _locale_config_file="${script_path}/system/locale-${arch}"
289 _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
290 _get_locale_line_number() {
292 for _lang in ${_locale_name_list[@]}; do
293 _count=$(( _count + 1 ))
294 if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
298 _locale_line_number="$(_get_locale_line_number)"
301 [[ "${_locale_line_number}" == "failed" ]] && _msg_error "${locale_name} is not a valid language." "1"
303 # ロケール設定ファイルから該当の行を抽出
304 _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
306 # 抽出された行に書かれた設定をそれぞれの変数に代入
307 # ここで定義された変数のみがグローバル変数
308 locale_name="${_locale_config_line[0]}"
309 locale_gen_name="${_locale_config_line[1]}"
310 locale_version="${_locale_config_line[2]}"
311 locale_time="${_locale_config_line[3]}"
312 locale_fullname="${_locale_config_line[4]}"
316 if [[ ${EUID} -ne 0 ]]; then
317 _msg_error "This script must be run as root." 1
319 umount_chroot_airootfs
321 if [[ -z "$(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename})" ]]; then
322 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
324 if [[ ! -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
325 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
326 mkdir -p "${work_dir}/airootfs/"
327 _msg_info "Make rootfs image..."
328 truncate -s 6G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
329 _msg_info "Format rootfs image..."
330 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
332 mkdir -p "${out_dir}"
333 _msg_info "Mount rootfs image..."
334 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
339 _dnf_install dbus-tools
340 run_cmd dbus-uuidgen --ensure=/etc/machine-id
341 if [[ ! -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
342 run_cmd mkdir /var/lib/dbus
344 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
346 make_dnf_packages() {
347 remove "${work_dir}/airootfs/dnfpkglist"
348 #_apt_install initramfs-tools
349 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
351 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
352 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
354 if [[ -f "${channels_dir}/share/packages-${locale_name}.${arch}" ]]; then
355 grep -h -v ^'#' "${channels_dir}/share/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
358 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
359 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
362 if [[ -f "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" ]]; then
363 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
365 if [[ ${bootsplash} == true ]]; then
366 if [[ -f "${channels_dir}/share/packages_plymouth.${arch}" ]]; then
367 grep -h -v ^'#' "${channels_dir}/share/packages_plymouth.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
370 if [[ -f "${channels_dir}/${channel_name}/packages_plymouth.${arch}" ]]; then
371 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages_plymouth.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
374 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
375 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
376 run_cmd env -i bash -c 'dnf -y --nogpgcheck -c /dnf_conf install $(echo $(<dnfpkglist))'
379 remove "${work_dir}/airootfs/dnfpkglist"
387 if [[ -d "${_dir}" ]]; then
388 cp -af "${_dir}"/* "${work_dir}/airootfs"
391 _copy_airootfs "${channels_dir}/share/airootfs"
392 _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
393 _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
394 _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
398 # customize_airootfs options
399 # -b : Enable boot splash.
400 # -d : Enable debug mode.
401 # -g <locale_gen_name> : Set locale-gen.
402 # -i <inst_dir> : Set install dir
403 # -k <kernel config line> : Set kernel name.
404 # -o <os name> : Set os name.
405 # -p <password> : Set password.
406 # -s <shell> : Set user shell.
407 # -t : Set plymouth theme.
408 # -u <username> : Set live user name.
409 # -x : Enable bash debug mode.
410 # -r : Enable rebuild.
411 # -z <locale_time> : Set the time zone.
412 # -l <locale_name> : Set language.
414 # -j is obsolete in AlterISO3 and cannot be used.
415 # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
416 local _airootfs_script_options _run_script
417 _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} "
418 if [[ ${bootsplash} == true ]]; then
419 _airootfs_script_options="${_airootfs_script_options} -b"
423 for _file in ${@}; do
424 if [[ -f "${work_dir}/airootfs${_file}" ]]; then run_cmd "${_file}" ${_airootfs_script_options}; fi
425 if [[ -f "${work_dir}/airootfs${_file}" ]]; then chmod 755 "${work_dir}/airootfs${_file}"; fi
429 _run_script "/root/customize_airootfs.sh" "/root/customize_airootfs_${channel_name}.sh"
433 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
434 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
439 remove "${bootfiles_dir}"
440 if [[ -d "${work_dir}/airootfs/dnf_cache" ]]; then
441 rm -rf "${work_dir}/airootfs/dnf_cache"
443 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
445 _msg_info "make initrd..."
446 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
447 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
448 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
450 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
452 remove "${work_dir}/airootfs/boot"
453 mkdir "${work_dir}/airootfs/boot"
454 cp ${bootfiles_dir}/boot/vmlinuz ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules)
455 kernelkun=$(run_cmd ls /lib/modules)
456 echo -e "\nkernel-install add ${kernelkun} /boot/vmlinuz-${kernelkun}" >> ${work_dir}/airootfs/usr/share/calamares/final-process
457 umount "${work_dir}/airootfs"
458 _msg_info "Minimize rootfs..."
459 resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
460 _msg_info "Compress rootfs.."
461 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img" -noappend -no-recovery -comp zstd -Xcompression-level 19 -b 1048576
462 _msg_info "Deleting block image..."
463 rm -rf "${work_dir}/squashfsroot/LiveOS/rootfs.img"
467 touch "${bootfiles_dir}/fedora_lfbs"
469 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
471 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
475 ${grub2_standalone_cmd} \
477 --output="${bootfiles_dir}/grub/bootia32.efi" \
480 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
483 ${grub2_standalone_cmd} \
484 --format=x86_64-efi \
485 --output="${bootfiles_dir}/grub/bootx64.efi" \
488 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
491 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
492 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
493 mkdir "${bootfiles_dir}/mnt"
494 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
495 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
496 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
497 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
498 umount -d "${bootfiles_dir}/mnt"
499 remove "${bootfiles_dir}/mnt"
502 cd "${bootfiles_dir}"
504 # create checksum (using at booting)
505 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
511 -full-iso9660-filenames \
512 -volid "${iso_label}" \
513 -appid "${iso_application}" \
514 -publisher "${iso_publisher}" \
515 -preparer "prepared by LFBS" \
516 -b isolinux/isolinux.bin \
521 -eltorito-platform efi \
522 -eltorito-boot EFI/efiboot.img \
524 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
525 -isohybrid-gpt-basdat \
526 -eltorito-catalog isolinux/boot.cat \
527 -output "${out_dir}/${iso_filename}" \
530 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
531 "/EFI/efiboot.img=grub/efiboot.img"
538 _msg_info "Creating md5 checksum ..."
539 md5sum "${iso_filename}" > "${iso_filename}.md5"
541 _msg_info "Creating sha256 checksum ..."
542 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
543 cd - > /dev/null 2>&1
544 umount_chroot_airootfs
548 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
550 _opt_short="w:l:o:hba:-:m:c:d"
551 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only,bootsplash"
552 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
554 if [[ ${?} != 0 ]]; then
607 _msg_error "Invalid argument '${1}'"
612 if [[ -f /etc/arch-release ]]; then
613 grub2_standalone_cmd=grub-mkstandalone
615 bootfiles_dir="${work_dir}/bootfiles"
616 trap umount_chroot_airootfs 0 2 15
618 if [[ -n "${1}" ]]; then
620 if [[ "${channel_name}" = "umount" ]]; then
621 umount_chroot_airootfs
624 if [[ "${channel_name}" = "clean" ]]; then
625 umount_chroot_airootfs
626 _msg_info "deleting work dir..."
635 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
636 if [[ -n "$(ls "${channels_dir}/${_channel}")" ]] && [[ ! "${_channel}" = "share" ]]; then
637 channel_list+=( "${_channel}" )
641 for i in ${channel_list[@]}; do
642 if [[ "${i}" = "${channel_name}" ]]; then
652 if [[ "$(check_channel ${channel_name})" = false ]]; then
653 _msg_error "Invalid channel ${channel_name}"
657 iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
658 umount_chroot_airootfs
659 if [[ -d "${work_dir}" ]]; then
660 _msg_info "deleting work dir..."
667 run_once make_systemd
668 run_once make_dnf_packages
669 run_once make_cp_airootfs
672 run_once make_squashfs
676 run_once make_checksum