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"
28 out_dir="${script_path}/out"
29 iso_label="${os_name}_${codename}_${arch}"
30 iso_publisher='Fascode Network <https://fascode.net>'
31 iso_application="${os_name} Live/Rescue CD"
32 iso_version="${codename}-$(date +%Y.%m.%d)"
33 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
35 liveuser_password=fedora
36 liveuser_shell=/usr/bin/zsh
38 #-- language config --#
40 # Sets the default locale for the live environment.
41 # You can also place a package list for that locale name and install packages specific to that locale.
43 locale_gen_name="en_US.UTF-8"
46 locale_fullname="global"
52 start_time="$(date +%s)"
55 if [[ "${debug}" = true ]]; then
58 _current_time="$(date +%s)"
59 _time="$(("${_current_time}"-"${start_time}"))"
61 if [[ "${_time}" -ge 3600 ]]; then
62 echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
63 elif [[ "${_time}" -ge 60 ]]; then
64 echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
66 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
69 "${script_path}/echo_color" -t 6 "[LFBS Core]"
73 # Show an INFO message
78 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
81 # Show an debug message
82 # _msg_debug <message>
84 if [[ "${debug}" = true ]]; then
87 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
91 # Show an ERROR message then exit with status
92 # _msg_error <message> <exit code>
98 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
100 if [[ ! ${_error} = 0 ]]; then
109 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
110 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 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
160 umount -fl "${mount}"
166 run_cmd dnf install -y ${@}
170 # Delete the file if it exists.
171 # For directories, rm -rf is used.
172 # If the file does not exist, skip it.
173 # remove <file> <file> ...
179 for _file in "${_list[@]}"; do
180 _msg_debug "Removeing ${_file}"
182 if [[ -f ${_file} ]]; then
184 elif [[ -d ${_file} ]]; then
192 echo "usage ${0} [options] [channel]"
194 echo " General options:"
196 echo " -a | --arch <str> Set architecture"
197 echo " Default: ${arch}"
198 echo " -c | --codename <str> Set ubuntu codename"
199 echo " Default: ${codename}"
200 echo " -l | --lang <lang> Specifies the default language for the live environment"
201 echo " Default: ${locale_name}"
202 echo " -m | --mirror <url> Set apt mirror server."
203 echo " Default: ${mirror}"
204 echo " -o | --out <out_dir> Set the output directory"
205 echo " Default: ${out_dir}"
206 echo " -w | --work <work_dir> Set the working directory"
207 echo " Default: ${work_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"
259 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
260 dnf --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) install @Core -y
261 _msg_info "${codename} installed successfully!"
263 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
264 run_cmd dnf update -y
265 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
266 # run_cmd apt-get upgrade
273 local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
275 # 選択されたロケールの設定が描かれた行番号を取得
276 _locale_config_file="${script_path}/system/locale-${arch}"
277 _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
278 _get_locale_line_number() {
280 for _lang in ${_locale_name_list[@]}; do
281 _count=$(( _count + 1 ))
282 if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
286 _locale_line_number="$(_get_locale_line_number)"
289 [[ "${_locale_line_number}" == "failed" ]] && msg_error "${locale_name} is not a valid language." "1"
291 # ロケール設定ファイルから該当の行を抽出
292 _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
294 # 抽出された行に書かれた設定をそれぞれの変数に代入
295 # ここで定義された変数のみがグローバル変数
296 locale_name="${_locale_config_line[0]}"
297 locale_gen_name="${_locale_config_line[1]}"
298 locale_version="${_locale_config_line[2]}"
299 locale_time="${_locale_config_line[3]}"
300 locale_fullname="${_locale_config_line[4]}"
303 if [[ ${EUID} -ne 0 ]]; then
304 _msg_error "This script must be run as root." 1
306 umount_chroot_airootfs
308 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
309 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
311 if [[ -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
314 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
315 mkdir -p "${work_dir}/airootfs/"
316 _msg_info "Make rootfs image..."
317 truncate -s 5G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
318 _msg_info "Format rootfs image..."
319 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
321 if [[ -d "${out_dir}" ]]; then
324 mkdir -p "${out_dir}"
326 _msg_info "Mount rootfs image..."
327 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
332 _dnf_install dbus-tools
333 run_cmd dbus-uuidgen --ensure=/etc/machine-id
334 if [[ -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
337 run_cmd mkdir /var/lib/dbus
339 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
341 make_dnf_packages() {
342 remove "${work_dir}/airootfs/dnfpkglist"
343 #_apt_install initramfs-tools
344 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
346 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
347 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
349 if [[ -f "${channels_dir}/share/packages-${locale_name}.${arch}" ]]; then
350 grep -h -v ^'#' "${channels_dir}/share/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
353 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
354 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
357 if [[ -f "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" ]]; then
358 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
361 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
362 run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
365 remove "${work_dir}/airootfs/dnfpkglist"
369 if [ -d ${channels_dir}/share/airootfs ]; then
370 cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
372 if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
373 cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
379 # customize_airootfs options
380 # -b : Enable boot splash.
381 # -d : Enable debug mode.
382 # -g <locale_gen_name> : Set locale-gen.
383 # -i <inst_dir> : Set install dir
384 # -k <kernel config line> : Set kernel name.
385 # -o <os name> : Set os name.
386 # -p <password> : Set password.
387 # -s <shell> : Set user shell.
388 # -t : Set plymouth theme.
389 # -u <username> : Set live user name.
390 # -x : Enable bash debug mode.
391 # -r : Enable rebuild.
392 # -z <locale_time> : Set the time zone.
393 # -l <locale_name> : Set language.
395 # -j is obsolete in AlterISO3 and cannot be used.
396 # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
397 local _airootfs_script_options
398 _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}' "
402 for _file in ${@}; do
403 if [[ -f "$_file" ]]; then chmod 755 "${_file}" ;fi
406 chmod_755 "${work_dir}/airootfs/root/customize_airootfs.sh" "${work_dir}/airootfs/root/customize_airootfs_${channel_name}.sh"
408 if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
409 run_cmd /root/customize_airootfs.sh _airootfs_script_options
411 if [ -f ${work_dir}/airootfs/root/customize_airootfs_${channel_name}.sh ]; then
412 run_cmd /root/customize_airootfs_${channel_name}.sh _airootfs_script_options
414 run_cmd truncate -s 0 /etc/machine-id
415 run_cmd passwd -u -f root
418 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
419 run_cmd dnf clean all
424 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
425 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
427 _msg_info "make initrd..."
428 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
429 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
430 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
432 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
434 rm -rf "${work_dir}/airootfs/boot"
435 umount "${work_dir}/airootfs"
436 _msg_info "Minimize rootfs..."
437 resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
438 _msg_info "Compress rootfs.."
439 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img"
443 touch "${bootfiles_dir}/fedora_lfbs"
445 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
447 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
453 --output="${bootfiles_dir}/grub/bootia32.efi" \
456 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
460 --format=x86_64-efi \
461 --output="${bootfiles_dir}/grub/bootx64.efi" \
464 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
467 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
468 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
469 mkdir "${bootfiles_dir}/mnt"
470 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
471 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
472 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
473 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
474 umount -d "${bootfiles_dir}/mnt"
475 rm -r "${bootfiles_dir}/mnt"
478 cd "${bootfiles_dir}"
480 # create checksum (using at booting)
481 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
487 -full-iso9660-filenames \
488 -volid "${iso_label}" \
489 -appid "${iso_application}" \
490 -publisher "${iso_publisher}" \
491 -preparer "prepared by LFBS" \
492 -b isolinux/isolinux.bin \
497 -eltorito-platform efi \
498 -eltorito-boot EFI/efiboot.img \
500 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
501 -isohybrid-gpt-basdat \
502 -eltorito-catalog isolinux/boot.cat \
503 -output "${out_dir}/${iso_filename}" \
506 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
507 "/EFI/efiboot.img=grub/efiboot.img"
514 _msg_info "Creating md5 checksum ..."
515 md5sum "${iso_filename}" > "${iso_filename}.md5"
517 _msg_info "Creating sha256 checksum ..."
518 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
519 cd - > /dev/null 2>&1
520 umount_chroot_airootfs
524 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
526 _opt_short="w:l:o:ha:-:m:c:d"
527 _opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only"
528 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
530 if [[ ${?} != 0 ]]; then
539 if [[ -z ${2} ]]; then
540 _msg_error "Please specify the architecture."
548 if [[ -z ${2} ]]; then
549 _msg_error "Please specify the codename."
565 if [[ -z ${2} ]]; then
566 _msg_error "Please specify the mirror server."
579 if [[ -z ${2} ]]; then
580 _msg_error "Please specify the out dir."
589 if [[ -z ${2} ]]; then
590 _msg_error "Please specify the out dir."
607 _msg_error "Invalid argument '${1}'"
613 bootfiles_dir="${work_dir}/bootfiles"
614 trap umount_chroot 0 2 15
616 if [[ -n "${1}" ]]; then
618 if [[ "${channel_name}" = "umount" ]]; then
619 umount_chroot_airootfs
622 if [[ "${channel_name}" = "clean" ]]; then
623 umount_chroot_airootfs
624 _msg_info "deleting work dir..."
633 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
634 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
635 channel_list+=( "${_channel}" )
639 for i in ${channel_list[@]}; do
640 if [[ "${i}" = "${channel_name}" ]]; then
650 if [[ $(check_channel ${channel_name}) = false ]]; then
651 _msg_error "Invalid channel ${channel_name}"
656 iso_filename="${iso_name}-${codename}-${channel_name}-$(date +%Y.%m.%d)-${arch}.iso"
657 umount_chroot_airootfs
662 run_once make_systemd
663 run_once make_dnf_packages
664 run_once make_cp_airootfs
667 run_once make_squashfs
671 run_once make_checksum