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 cp /etc/resolv.conf "${work_dir}/airootfs/etc/resolv.conf"
146 unshare --fork --pid chroot "${work_dir}/airootfs" "${@}"
148 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
149 if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
150 umount -fl "${mount}"
156 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
157 run_cmd dnf -c /dnf_conf install -y ${@}
161 # Delete the file if it exists.
162 # For directories, rm -rf is used.
163 # If the file does not exist, skip it.
164 # remove <file> <file> ...
166 local _list=($(echo "$@"))
169 for _file in "${_list[@]}"; do
170 _msg_debug "Removeing ${_file}"
172 if [[ -f ${_file} ]]; then
174 elif [[ -d ${_file} ]]; then
180 # Usage: echo_blank <number>
181 # 指定されたぶんの半角空白文字を出力します
184 for _local in $(seq 1 "${1}"); do echo -ne " "; done
189 echo "usage ${0} [options] [channel]"
191 echo " General options:"
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 <dir> Set the output directory"
201 echo " Default: ${out_dir}"
202 echo " -w | --work <dir> Set the working directory"
203 echo " Default: ${work_dir}"
204 echo " -c | --cache <dir> Set the cache directory"
205 echo " Default: ${cache_dir}"
207 echo " -d | --debug Enable debug messages"
208 echo " -x | --bash-debug Enable bash debug mode(set -xv)"
209 echo " -h | --help This help message and exit"
211 echo "You can switch between installed packages, files included in images, etc. by channel."
214 local blank="23" _arch _list _dirname _channel
216 echo " Language for each architecture:"
217 for _list in ${script_path}/system/locale-* ; do
218 _arch="${_list#${script_path}/system/locale-}"
220 echo_blank "$(( ${blank} - ${#_arch} ))"
221 "${script_path}/tools/locale.sh" -a "${_arch}" show
224 echo -e "\n Channel:"
225 local _channel channel_list description
226 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
227 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
228 channel_list+=( "${_channel}" )
231 for _channel in ${channel_list[@]}; do
232 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
233 description=$(cat "${channels_dir}/${_channel}/description.txt")
235 description="This channel does not have a description.txt."
237 echo -ne " ${_channel}"
238 echo_blank "$(( ${blank} - ${#_channel} ))"
239 echo -ne "${description}\n"
244 if [[ ! -d "${cache_dir}" ]]; then
245 mkdir -p "${cache_dir}"
247 cp -rf "${script_path}/system/dnfconf.conf" "${work_dir}/airootfs/dnf_conf"
248 if [[ ! -d "${work_dir}/airootfs/dnf_cache" ]]; then
249 mkdir -p "${work_dir}/airootfs/dnf_cache"
251 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
252 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
253 umount -fl "${work_dir}/airootfs/dnf_cache"
257 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
258 dnfstrap @Core yamad-repo
259 _msg_info "${codename} installed successfully!"
261 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
262 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
263 run_cmd dnf -c /dnf_conf update --refresh -y
264 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
265 # run_cmd apt-get upgrade
271 eval $(bash "${script_path}/tools/locale.sh" -s -a "${arch}" get "${locale_name}")
275 if [[ ${EUID} -ne 0 ]]; then
276 _msg_error "This script must be run as root." 1
278 umount_chroot_airootfs
280 if [[ -z "$(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename})" ]]; then
281 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
283 if [[ ! -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
284 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
285 mkdir -p "${work_dir}/airootfs/"
286 _msg_info "Make rootfs image..."
287 truncate -s 32G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
288 _msg_info "Format rootfs image..."
289 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
291 mkdir -p "${out_dir}"
292 _msg_info "Mount rootfs image..."
293 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
298 _dnf_install dbus-tools
299 run_cmd dbus-uuidgen --ensure=/etc/machine-id
300 if [[ ! -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
301 run_cmd mkdir /var/lib/dbus
303 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
305 make_repo_packages() {
306 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) ))
307 if [ -n "${_pkglist[*]}" ]; then
308 # Create a list of packages to be finally installed as packages.list directly under the working directory.
309 echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
310 # Install packages on airootfs
311 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
312 run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
315 make_dnf_packages() {
317 #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) ))
318 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) ))
319 # Create a list of packages to be finally installed as packages.list directly under the working directory.
320 echo -e "# The list of packages that is installed in live cd.\n#\n\n" > "${work_dir}/packages.list"
321 for _pkg in ${_pkglist[@]}; do
322 echo ${_pkg} >> "${work_dir}/packages.list"
325 # Install packages on airootfs
326 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
327 run_cmd dnf -y --nogpgcheck -c /dnf_conf install ${_pkglist[*]}
335 if [[ -d "${_dir}" ]]; then
336 cp -af "${_dir}"/* "${work_dir}/airootfs"
339 _copy_airootfs "${channels_dir}/share/airootfs"
340 _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
341 _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
342 _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
346 # customize_airootfs options
347 # -b : Enable boot splash.
348 # -d : Enable debug mode.
349 # -g <locale_gen_name> : Set locale-gen.
350 # -i <inst_dir> : Set install dir
351 # -k <kernel config line> : Set kernel name.
352 # -o <os name> : Set os name.
353 # -p <password> : Set password.
354 # -s <shell> : Set user shell.
355 # -t : Set plymouth theme.
356 # -u <username> : Set live user name.
357 # -x : Enable bash debug mode.
358 # -z <locale_time> : Set the time zone.
359 # -l <locale_name> : Set language.
361 # -j is obsolete in AlterISO3 and cannot be used.
362 # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
363 local _airootfs_script_options _run_script
364 _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} "
365 if [[ ${bootsplash} == true ]]; then
366 _airootfs_script_options="${_airootfs_script_options} -b"
370 for _file in ${@}; do
371 if [[ -f "${work_dir}/airootfs${_file}" ]]; then run_cmd "${_file}" ${_airootfs_script_options}; fi
372 if [[ -f "${work_dir}/airootfs${_file}" ]]; then chmod 755 "${work_dir}/airootfs${_file}"; fi
376 _run_script "/root/customize_airootfs.sh" "/root/customize_airootfs_${channel_name}.sh"
380 mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
381 run_cmd dnf -c /dnf_conf -y remove $(run_cmd dnf -c /dnf_conf repoquery --installonly --latest-limit=-1 -q)
386 remove "${bootfiles_dir}"
387 if [[ -d "${work_dir}/airootfs/dnf_cache" ]]; then
388 rm -rf "${work_dir}/airootfs/dnf_cache"
390 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
392 _msg_info "make initrd..."
393 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
394 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
395 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
397 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
399 remove "${work_dir}/airootfs/boot"
400 mkdir "${work_dir}/airootfs/boot"
401 cp ${bootfiles_dir}/boot/vmlinuz ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules)
402 kernelkun=$(run_cmd ls /lib/modules)
403 echo -e "\nkernel-install add ${kernelkun} /boot/vmlinuz-${kernelkun}" >> ${work_dir}/airootfs/usr/share/calamares/final-process
404 umount "${work_dir}/airootfs"
405 # _msg_info "e2fsck..."
406 # e2fsck -f "${work_dir}/squashfsroot/LiveOS/rootfs.img"
407 # _msg_info "Minimize rootfs..."
408 # resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
409 _msg_info "Compress rootfs.."
410 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img" -noappend -no-recovery -comp zstd -Xcompression-level 19 -b 1048576
411 _msg_info "Deleting block image..."
412 rm -rf "${work_dir}/squashfsroot/LiveOS/rootfs.img"
416 touch "${bootfiles_dir}/fedora_lfbs"
418 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
420 if [[ ${bootsplash} = true ]]; then
421 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"
423 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
425 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"
429 ${grub2_standalone_cmd} \
431 --output="${bootfiles_dir}/grub/bootia32.efi" \
434 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
437 ${grub2_standalone_cmd} \
438 --format=x86_64-efi \
439 --output="${bootfiles_dir}/grub/bootx64.efi" \
442 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
445 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
446 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
447 mkdir "${bootfiles_dir}/mnt"
448 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
449 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
450 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
451 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
452 umount -d "${bootfiles_dir}/mnt"
453 remove "${bootfiles_dir}/mnt"
456 cd "${bootfiles_dir}"
458 # create checksum (using at booting)
459 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
465 -full-iso9660-filenames \
466 -volid "${iso_label}" \
467 -appid "${iso_application}" \
468 -publisher "${iso_publisher}" \
469 -preparer "prepared by LFBS" \
470 -b isolinux/isolinux.bin \
475 -eltorito-platform efi \
476 -eltorito-boot EFI/efiboot.img \
478 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
479 -isohybrid-gpt-basdat \
480 -eltorito-catalog isolinux/boot.cat \
481 -output "${out_dir}/${iso_filename}" \
484 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
485 "/EFI/efiboot.img=grub/efiboot.img"
492 _msg_info "Creating md5 checksum ..."
493 md5sum "${iso_filename}" > "${iso_filename}.md5"
495 _msg_info "Creating sha256 checksum ..."
496 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
497 cd - > /dev/null 2>&1
498 umount_chroot_airootfs
501 # 引数解析 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
502 _opt_short="w:l:o:hba:-:m:c:dx"
503 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only,bootsplash,bash-debug"
504 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
506 if [[ ${?} != 0 ]]; then
563 _msg_error "Invalid argument '${1}'"
568 if [[ -f "/etc/arch-release" ]]; then
569 grub2_standalone_cmd=grub-mkstandalone
571 bootfiles_dir="${work_dir}/bootfiles"
572 trap 'umount_chroot_airootfs' 0 2 15
574 if [[ -n "${1}" ]]; then
576 if [[ "${channel_name}" = "umount" ]]; then
577 umount_chroot_airootfs
580 if [[ "${channel_name}" = "clean" ]]; then
581 umount_chroot_airootfs
582 _msg_info "deleting work dir..."
591 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
592 if [[ -n "$(ls "${channels_dir}/${_channel}")" ]] && [[ ! "${_channel}" = "share" ]]; then
593 channel_list+=( "${_channel}" )
597 for i in ${channel_list[@]}; do
598 if [[ "${i}" = "${channel_name}" ]]; then
608 if [[ "$(check_channel ${channel_name})" = false ]]; then
609 _msg_error "Invalid channel ${channel_name}"
613 iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
614 umount_chroot_airootfs
615 if [[ -d "${work_dir}" ]]; then
616 _msg_info "deleting work dir..."
623 run_once make_systemd
624 run_once make_repo_packages
625 run_once make_dnf_packages
626 run_once make_cp_airootfs
629 run_once make_squashfs
633 run_once make_checksum