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"
29 out_dir="${script_path}/out"
30 iso_label="${os_name}_${codename}_${arch}"
31 iso_publisher='Fascode Network <https://fascode.net>'
32 iso_application="${os_name} Live/Rescue CD"
33 iso_version="${codename}-$(date +%Y.%m.%d)"
34 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
36 liveuser_password=fedora
37 liveuser_shell="/usr/bin/zsh"
39 #-- language config --#
41 # Sets the default locale for the live environment.
42 # You can also place a package list for that locale name and install packages specific to that locale.
44 locale_gen_name="en_US.UTF-8"
47 locale_fullname="global"
53 start_time="$(date +%s)"
56 if [[ "${debug}" = true ]]; then
59 _current_time="$(date +%s)"
60 _time="$(("${_current_time}"-"${start_time}"))"
62 if [[ "${_time}" -ge 3600 ]]; then
63 echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
64 elif [[ "${_time}" -ge 60 ]]; then
65 echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
67 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
70 "${script_path}/echo_color" -t 6 "[LFBS Core]"
74 # Show an INFO message
79 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
82 # Show an debug message
83 # _msg_debug <message>
85 if [[ "${debug}" = true ]]; then
88 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
92 # Show an ERROR message then exit with status
93 # _msg_error <message> <exit code>
99 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
101 if [[ ! ${_error} = 0 ]]; then
110 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
111 if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
114 _msg_info "Unmounting ${mount}"
115 umount -fl "${mount}"
120 # Unmount chroot dir and airootfs
121 umount_chroot_airootfs () {
124 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
125 _msg_info "Unmounting ${mount}"
126 umount -fl "${mount}"
129 # Helper function to run make_*() only one time.
135 if [[ ! -e "${work_dir}/build.${name}" ]]; then
136 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
138 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
139 touch "${work_dir}/build.${name}"
146 for mount in "dev" "dev/pts" "proc" "sys" "etc/resolv.conf"; do
147 #for mount in "dev" "dev/pts" "proc" "sys" ; do
148 if [[ "${mount}" == "etc/resolv.conf" ]]; then
149 cp /etc/resolv.conf "${work_dir}/airootfs/${mount}"
151 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
155 chroot "${work_dir}/airootfs" "${@}"
157 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
158 if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
161 umount -fl "${mount}"
167 run_cmd dnf install -y ${@}
171 # Delete the file if it exists.
172 # For directories, rm -rf is used.
173 # If the file does not exist, skip it.
174 # remove <file> <file> ...
180 for _file in "${_list[@]}"; do
181 _msg_debug "Removeing ${_file}"
183 if [[ -f ${_file} ]]; then
185 elif [[ -d ${_file} ]]; then
193 echo "usage ${0} [options] [channel]"
195 echo " General options:"
197 echo " -a | --arch <str> Set architecture"
198 echo " Default: ${arch}"
199 echo " -c | --codename <str> Set ubuntu codename"
200 echo " Default: ${codename}"
201 echo " -l | --lang <lang> Specifies the default language for the live environment"
202 echo " Default: ${locale_name}"
203 echo " -m | --mirror <url> Set apt mirror server."
204 echo " Default: ${mirror}"
205 echo " -o | --out <out_dir> Set the output directory"
206 echo " Default: ${out_dir}"
207 echo " -w | --work <work_dir> Set the working directory"
208 echo " Default: ${work_dir}"
210 echo " -d | --debug "
211 echo " -h | --help This help message and exit"
213 echo "You can switch between installed packages, files included in images, etc. by channel."
215 echo " Language for each architecture:"
216 for _list in ${script_path}/system/locale-* ; do
217 _arch="${_list#${script_path}/system/locale-}"
219 for i in $( seq 1 $(( ${blank} - 4 - ${#_arch} )) ); do
222 _locale_name_list=$(cat ${_list} | grep -h -v ^'#' | awk '{print $1}')
223 for _lang in ${_locale_name_list[@]};do
234 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
235 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
236 channel_list+=( "${_channel}" )
240 for _channel in ${channel_list[@]}; do
241 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
242 description=$(cat "${channels_dir}/${_channel}/description.txt")
244 description="This channel does not have a description.txt."
247 echo -ne " ${_channel}"
249 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
253 echo -ne "${description}\n"
260 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
261 dnf --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) install @Core -y
262 _msg_info "${codename} installed successfully!"
264 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
265 run_cmd dnf update -y
266 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
267 # run_cmd apt-get upgrade
274 local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
276 # 選択されたロケールの設定が描かれた行番号を取得
277 _locale_config_file="${script_path}/system/locale-${arch}"
278 _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
279 _get_locale_line_number() {
281 for _lang in ${_locale_name_list[@]}; do
282 _count=$(( _count + 1 ))
283 if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
287 _locale_line_number="$(_get_locale_line_number)"
290 [[ "${_locale_line_number}" == "failed" ]] && msg_error "${locale_name} is not a valid language." "1"
292 # ロケール設定ファイルから該当の行を抽出
293 _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
295 # 抽出された行に書かれた設定をそれぞれの変数に代入
296 # ここで定義された変数のみがグローバル変数
297 locale_name="${_locale_config_line[0]}"
298 locale_gen_name="${_locale_config_line[1]}"
299 locale_version="${_locale_config_line[2]}"
300 locale_time="${_locale_config_line[3]}"
301 locale_fullname="${_locale_config_line[4]}"
304 if [[ ${EUID} -ne 0 ]]; then
305 _msg_error "This script must be run as root." 1
307 umount_chroot_airootfs
309 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
310 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
312 if [[ -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
315 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
316 mkdir -p "${work_dir}/airootfs/"
317 _msg_info "Make rootfs image..."
318 truncate -s 5G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
319 _msg_info "Format rootfs image..."
320 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
322 if [[ -d "${out_dir}" ]]; then
325 mkdir -p "${out_dir}"
327 _msg_info "Mount rootfs image..."
328 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
333 _dnf_install dbus-tools
334 run_cmd dbus-uuidgen --ensure=/etc/machine-id
335 if [[ -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
338 run_cmd mkdir /var/lib/dbus
340 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
342 make_dnf_packages() {
343 remove "${work_dir}/airootfs/dnfpkglist"
344 #_apt_install initramfs-tools
345 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
347 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
348 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
350 if [[ -f "${channels_dir}/share/packages-${locale_name}.${arch}" ]]; then
351 grep -h -v ^'#' "${channels_dir}/share/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
354 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
355 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
358 if [[ -f "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" ]]; then
359 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
362 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
363 run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
366 remove "${work_dir}/airootfs/dnfpkglist"
374 if [[ -d "${_dir}" ]]; then
375 cp -af "${_dir}"/* "${work_dir}/airootfs"
378 _copy_airootfs "${channels_dir}/share/airootfs"
379 _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
380 _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
381 _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
386 # customize_airootfs options
387 # -b : Enable boot splash.
388 # -d : Enable debug mode.
389 # -g <locale_gen_name> : Set locale-gen.
390 # -i <inst_dir> : Set install dir
391 # -k <kernel config line> : Set kernel name.
392 # -o <os name> : Set os name.
393 # -p <password> : Set password.
394 # -s <shell> : Set user shell.
395 # -t : Set plymouth theme.
396 # -u <username> : Set live user name.
397 # -x : Enable bash debug mode.
398 # -r : Enable rebuild.
399 # -z <locale_time> : Set the time zone.
400 # -l <locale_name> : Set language.
402 # -j is obsolete in AlterISO3 and cannot be used.
403 # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
404 local _airootfs_script_options
405 _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} "
409 for _file in ${@}; do
410 if [[ -f "$_file" ]]; then chmod 755 "${_file}" ;fi
413 chmod_755 "${work_dir}/airootfs/root/customize_airootfs.sh" "${work_dir}/airootfs/root/customize_airootfs_${channel_name}.sh"
415 if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
416 run_cmd /root/customize_airootfs.sh ${_airootfs_script_options}
418 if [ -f ${work_dir}/airootfs/root/customize_airootfs_${channel_name}.sh ]; then
419 run_cmd /root/customize_airootfs_${channel_name}.sh ${_airootfs_script_options}
421 run_cmd ln -sf /usr/share/zoneinfo/${locale_time} /etc/localtime
422 echo "LANG=${locale_gen_name}" > "${work_dir}/airootfs/etc/locale.conf"
423 run_cmd truncate -s 0 /etc/machine-id
424 run_cmd passwd -u -f root
427 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
428 run_cmd dnf clean all
433 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
434 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
436 _msg_info "make initrd..."
437 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
438 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
439 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
441 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
443 rm -rf "${work_dir}/airootfs/boot"
444 umount "${work_dir}/airootfs"
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"
452 touch "${bootfiles_dir}/fedora_lfbs"
454 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
456 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
462 --output="${bootfiles_dir}/grub/bootia32.efi" \
465 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
469 --format=x86_64-efi \
470 --output="${bootfiles_dir}/grub/bootx64.efi" \
473 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
476 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
477 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
478 mkdir "${bootfiles_dir}/mnt"
479 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
480 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
481 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
482 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
483 umount -d "${bootfiles_dir}/mnt"
484 rm -r "${bootfiles_dir}/mnt"
487 cd "${bootfiles_dir}"
489 # create checksum (using at booting)
490 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
496 -full-iso9660-filenames \
497 -volid "${iso_label}" \
498 -appid "${iso_application}" \
499 -publisher "${iso_publisher}" \
500 -preparer "prepared by LFBS" \
501 -b isolinux/isolinux.bin \
506 -eltorito-platform efi \
507 -eltorito-boot EFI/efiboot.img \
509 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
510 -isohybrid-gpt-basdat \
511 -eltorito-catalog isolinux/boot.cat \
512 -output "${out_dir}/${iso_filename}" \
515 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
516 "/EFI/efiboot.img=grub/efiboot.img"
523 _msg_info "Creating md5 checksum ..."
524 md5sum "${iso_filename}" > "${iso_filename}.md5"
526 _msg_info "Creating sha256 checksum ..."
527 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
528 cd - > /dev/null 2>&1
529 umount_chroot_airootfs
533 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
535 _opt_short="w:l:o:ha:-:m:c:d"
536 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only"
537 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
539 if [[ ${?} != 0 ]]; then
548 if [[ -z ${2} ]]; then
549 _msg_error "Please specify the architecture."
557 if [[ -z ${2} ]]; then
558 _msg_error "Please specify the codename."
574 if [[ -z ${2} ]]; then
575 _msg_error "Please specify the mirror server."
588 if [[ -z ${2} ]]; then
589 _msg_error "Please specify the out dir."
598 if [[ -z ${2} ]]; then
599 _msg_error "Please specify the out dir."
616 _msg_error "Invalid argument '${1}'"
622 bootfiles_dir="${work_dir}/bootfiles"
623 trap umount_chroot 0 2 15
625 if [[ -n "${1}" ]]; then
627 if [[ "${channel_name}" = "umount" ]]; then
628 umount_chroot_airootfs
631 if [[ "${channel_name}" = "clean" ]]; then
632 umount_chroot_airootfs
633 _msg_info "deleting work dir..."
642 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
643 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
644 channel_list+=( "${_channel}" )
648 for i in ${channel_list[@]}; do
649 if [[ "${i}" = "${channel_name}" ]]; then
659 if [[ $(check_channel ${channel_name}) = false ]]; then
660 _msg_error "Invalid channel ${channel_name}"
664 iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
665 umount_chroot_airootfs
666 if [[ -d "${work_dir}" ]]; then
667 _msg_info "deleting work dir..."
673 run_once make_systemd
674 run_once make_dnf_packages
675 run_once make_cp_airootfs
678 run_once make_squashfs
682 run_once make_checksum