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 if (( "${_time}" >= 3600 )); then
65 echo -n "[$(date -d @${_time} +%H:%M.%S)] "
66 #elif [[ "${_time}" -ge 60 ]]; then
67 elif (( "${_time}" >= 60 )); then
68 echo -n "[00:$(date -d @${_time} +%M.%S)] "
70 echo -n "[00:00.$(date -d @${_time} +%S)] "
75 # Show an INFO message
79 "${script_path}/tools/msg.sh" -a "LFBS Core" -l "Info:" -s "6" info "${@}"
82 # Show an debug message
83 # _msg_debug <message>
86 if [[ "${debug}" = true ]]; then
87 "${script_path}/tools/msg.sh" -a "LFBS Core" -l "Debug:" -s "6" debug "${@}"
91 # Show an ERROR message then exit with status
92 # _msg_error <message> <exit code>
95 "${script_path}/tools/msg.sh" -a "LFBS Core" -l "Error:" -s "6" error "${@}"
96 if [[ -n "${2:-}" ]]; then
105 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
106 if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
107 _msg_info "Unmounting ${mount}"
108 umount -fl "${mount}"
113 # Unmount chroot dir and airootfs
114 umount_chroot_airootfs () {
117 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
118 _msg_info "Unmounting ${mount}"
119 umount -fl "${mount}"
122 # Helper function to run make_*() only one time.
128 if [[ ! -e "${work_dir}/build.${name}" ]]; then
129 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
131 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
132 touch "${work_dir}/build.${name}"
139 for mount in "dev" "dev/pts" "proc" "sys" ; do
140 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
143 mkdir -p "${work_dir}/airootfs/run/systemd/resolve/"
144 cp /etc/resolv.conf "${work_dir}/airootfs/run/systemd/resolve/stub-resolv.conf"
145 unshare --fork --pid chroot "${work_dir}/airootfs" "${@}"
147 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
148 if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
149 umount -fl "${mount}"
155 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
156 run_cmd dnf -c /dnf_conf install -y ${@}
160 # Delete the file if it exists.
161 # For directories, rm -rf is used.
162 # If the file does not exist, skip it.
163 # remove <file> <file> ...
165 local _list=($(echo "$@"))
168 for _file in "${_list[@]}"; do
169 _msg_debug "Removeing ${_file}"
171 if [[ -f ${_file} ]]; then
173 elif [[ -d ${_file} ]]; then
179 # Usage: echo_blank <number>
180 # 指定されたぶんの半角空白文字を出力します
183 for _local in $(seq 1 "${1}"); do echo -ne " "; done
188 echo "usage ${0} [options] [channel]"
190 echo " General options:"
192 echo " -a | --arch <str> Set architecture"
193 echo " Default: ${arch}"
194 echo " -b | --bootsplash Enable Plymouth"
195 echo " -l | --lang <lang> Specifies the default language for the live environment"
196 echo " Default: ${locale_name}"
197 echo " -m | --mirror <url> Set apt mirror server."
198 echo " Default: ${mirror}"
199 echo " -o | --out <dir> Set the output directory"
200 echo " Default: ${out_dir}"
201 echo " -w | --work <dir> Set the working directory"
202 echo " Default: ${work_dir}"
203 echo " -c | --cache <dir> Set the cache directory"
204 echo " Default: ${cache_dir}"
206 echo " -d | --debug Enable debug messages"
207 echo " -x | --bash-debug Enable bash debug mode(set -xv)"
208 echo " -h | --help This help message and exit"
210 echo "You can switch between installed packages, files included in images, etc. by channel."
213 local blank="23" _arch _list _dirname _channel
215 echo " Language for each architecture:"
216 for _list in ${script_path}/system/locale-* ; do
217 _arch="${_list#${script_path}/system/locale-}"
219 echo_blank "$(( ${blank} - ${#_arch} ))"
220 "${script_path}/tools/locale.sh" -a "${_arch}" show
223 echo -e "\n Channel:"
224 local _channel channel_list description
225 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
226 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
227 channel_list+=( "${_channel}" )
230 for _channel in ${channel_list[@]}; do
231 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
232 description=$(cat "${channels_dir}/${_channel}/description.txt")
234 description="This channel does not have a description.txt."
236 echo -ne " ${_channel}"
237 echo_blank "$(( ${blank} - ${#_channel} ))"
238 echo -ne "${description}\n"
243 if [[ ! -d "${cache_dir}" ]]; then
244 mkdir -p "${cache_dir}"
246 cp -rf "${script_path}/system/dnfconf.conf" "${work_dir}/airootfs/dnf_conf"
247 if [[ ! -d "${work_dir}/airootfs/dnf_cache" ]]; then
248 mkdir -p "${work_dir}/airootfs/dnf_cache"
250 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
251 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
252 umount -fl "${work_dir}/airootfs/dnf_cache"
256 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
257 dnfstrap @Core yamad-repo
258 _msg_info "${codename} installed successfully!"
260 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
261 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
262 run_cmd dnf -c /dnf_conf update --refresh -y
263 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
264 # run_cmd apt-get upgrade
270 eval $(bash "${script_path}/tools/locale.sh" -s -a "${arch}" get "${locale_name}")
274 if [[ ${EUID} -ne 0 ]]; then
275 _msg_error "This script must be run as root." 1
277 umount_chroot_airootfs
279 if [[ -z "$(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename})" ]]; then
280 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
282 if [[ ! -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
283 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
284 mkdir -p "${work_dir}/airootfs/"
285 _msg_info "Make rootfs image..."
286 truncate -s 6G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
287 _msg_info "Format rootfs image..."
288 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
290 mkdir -p "${out_dir}"
291 _msg_info "Mount rootfs image..."
292 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
297 _dnf_install dbus-tools
298 run_cmd dbus-uuidgen --ensure=/etc/machine-id
299 if [[ ! -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
300 run_cmd mkdir /var/lib/dbus
302 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
304 make_repo_packages() {
305 local _pkg _pkglist=($("${script_path}/tools/pkglist-repo.sh" -a "x86_64" -c "${channels_dir}/${channel_name}" -k "${codename}" -l "${locale_name}" $(if [[ "${bootsplash}" = true ]]; then echo -n "-b"; fi) ))
306 # Create a list of packages to be finally installed as packages.list directly under the working directory.
307 echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
308 # Install packages on airootfs
309 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
310 run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
312 make_dnf_packages() {
314 #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) ))
315 local _pkg _pkglist=($("${script_path}/tools/pkglist.sh" -a "x86_64" -c "${channels_dir}/${channel_name}" -l "${locale_name}" $(if [[ "${bootsplash}" = true ]]; then echo -n "-b"; fi) ))
316 # Create a list of packages to be finally installed as packages.list directly under the working directory.
317 echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
318 for _pkg in ${_pkglist[@]}; do
319 echo ${_pkg} >> "${work_dir}/packages.list"
322 # Install packages on airootfs
323 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
324 run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
332 if [[ -d "${_dir}" ]]; then
333 cp -af "${_dir}"/* "${work_dir}/airootfs"
336 _copy_airootfs "${channels_dir}/share/airootfs"
337 _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
338 _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
339 _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
343 # customize_airootfs options
344 # -b : Enable boot splash.
345 # -d : Enable debug mode.
346 # -g <locale_gen_name> : Set locale-gen.
347 # -i <inst_dir> : Set install dir
348 # -k <kernel config line> : Set kernel name.
349 # -o <os name> : Set os name.
350 # -p <password> : Set password.
351 # -s <shell> : Set user shell.
352 # -t : Set plymouth theme.
353 # -u <username> : Set live user name.
354 # -x : Enable bash debug mode.
355 # -z <locale_time> : Set the time zone.
356 # -l <locale_name> : Set language.
358 # -j is obsolete in AlterISO3 and cannot be used.
359 # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
360 local _airootfs_script_options _run_script
361 _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} "
362 if [[ ${bootsplash} == true ]]; then
363 _airootfs_script_options="${_airootfs_script_options} -b"
367 for _file in ${@}; do
368 if [[ -f "${work_dir}/airootfs${_file}" ]]; then run_cmd "${_file}" ${_airootfs_script_options}; fi
369 if [[ -f "${work_dir}/airootfs${_file}" ]]; then chmod 755 "${work_dir}/airootfs${_file}"; fi
373 _run_script "/root/customize_airootfs.sh" "/root/customize_airootfs_${channel_name}.sh"
377 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
378 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
383 remove "${bootfiles_dir}"
384 if [[ -d "${work_dir}/airootfs/dnf_cache" ]]; then
385 rm -rf "${work_dir}/airootfs/dnf_cache"
387 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
389 _msg_info "make initrd..."
390 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
391 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
392 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
394 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
396 remove "${work_dir}/airootfs/boot"
397 mkdir "${work_dir}/airootfs/boot"
398 cp ${bootfiles_dir}/boot/vmlinuz ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules)
399 kernelkun=$(run_cmd ls /lib/modules)
400 echo -e "\nkernel-install add ${kernelkun} /boot/vmlinuz-${kernelkun}" >> ${work_dir}/airootfs/usr/share/calamares/final-process
401 umount "${work_dir}/airootfs"
402 _msg_info "e2fsck..."
403 e2fsck -f "${work_dir}/squashfsroot/LiveOS/rootfs.img"
404 _msg_info "Minimize rootfs..."
405 resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
406 _msg_info "Compress rootfs.."
407 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img" -noappend -no-recovery -comp zstd -Xcompression-level 19 -b 1048576
408 _msg_info "Deleting block image..."
409 rm -rf "${work_dir}/squashfsroot/LiveOS/rootfs.img"
413 touch "${bootfiles_dir}/fedora_lfbs"
415 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
417 if [[ ${bootsplash} = true ]]; then
418 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"
420 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
422 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"
426 ${grub2_standalone_cmd} \
428 --output="${bootfiles_dir}/grub/bootia32.efi" \
431 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
434 ${grub2_standalone_cmd} \
435 --format=x86_64-efi \
436 --output="${bootfiles_dir}/grub/bootx64.efi" \
439 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
442 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
443 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
444 mkdir "${bootfiles_dir}/mnt"
445 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
446 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
447 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
448 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
449 umount -d "${bootfiles_dir}/mnt"
450 remove "${bootfiles_dir}/mnt"
453 cd "${bootfiles_dir}"
455 # create checksum (using at booting)
456 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
462 -full-iso9660-filenames \
463 -volid "${iso_label}" \
464 -appid "${iso_application}" \
465 -publisher "${iso_publisher}" \
466 -preparer "prepared by LFBS" \
467 -b isolinux/isolinux.bin \
472 -eltorito-platform efi \
473 -eltorito-boot EFI/efiboot.img \
475 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
476 -isohybrid-gpt-basdat \
477 -eltorito-catalog isolinux/boot.cat \
478 -output "${out_dir}/${iso_filename}" \
481 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
482 "/EFI/efiboot.img=grub/efiboot.img"
489 _msg_info "Creating md5 checksum ..."
490 md5sum "${iso_filename}" > "${iso_filename}.md5"
492 _msg_info "Creating sha256 checksum ..."
493 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
494 cd - > /dev/null 2>&1
495 umount_chroot_airootfs
498 # 引数解析 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
499 _opt_short="w:l:o:hba:-:m:c:dx"
500 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only,bootsplash,bash-debug"
501 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
503 if [[ ${?} != 0 ]]; then
560 _msg_error "Invalid argument '${1}'"
565 if [[ -f "/etc/arch-release" ]]; then
566 grub2_standalone_cmd=grub-mkstandalone
568 bootfiles_dir="${work_dir}/bootfiles"
569 trap 'umount_chroot_airootfs' 0 2 15
571 if [[ -n "${1}" ]]; then
573 if [[ "${channel_name}" = "umount" ]]; then
574 umount_chroot_airootfs
577 if [[ "${channel_name}" = "clean" ]]; then
578 umount_chroot_airootfs
579 _msg_info "deleting work dir..."
588 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
589 if [[ -n "$(ls "${channels_dir}/${_channel}")" ]] && [[ ! "${_channel}" = "share" ]]; then
590 channel_list+=( "${_channel}" )
594 for i in ${channel_list[@]}; do
595 if [[ "${i}" = "${channel_name}" ]]; then
605 if [[ "$(check_channel ${channel_name})" = false ]]; then
606 _msg_error "Invalid channel ${channel_name}"
610 iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
611 umount_chroot_airootfs
612 if [[ -d "${work_dir}" ]]; then
613 _msg_info "deleting work dir..."
620 run_once make_systemd
621 run_once make_repo_packages
622 run_once make_dnf_packages
623 run_once make_cp_airootfs
626 run_once make_squashfs
630 run_once make_checksum