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"
27 out_dir="${script_path}/out"
28 iso_label="${os_name}_${codename}_${arch}"
29 iso_publisher='Fascode Network <https://fascode.net>'
30 iso_application="${os_name} Live/Rescue CD"
31 iso_version="${codename}-$(date +%Y.%m.%d)"
32 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
39 start_time="$(date +%s)"
42 if [[ "${debug}" = true ]]; then
45 _current_time="$(date +%s)"
46 _time="$(("${_current_time}"-"${start_time}"))"
48 if [[ "${_time}" -ge 3600 ]]; then
49 echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
50 elif [[ "${_time}" -ge 60 ]]; then
51 echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
53 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
56 "${script_path}/echo_color" -t 6 "[LFBS Core]"
60 # Show an INFO message
65 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
68 # Show an debug message
69 # _msg_debug <message>
71 if [[ "${debug}" = true ]]; then
74 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
78 # Show an ERROR message then exit with status
79 # _msg_error <message> <exit code>
85 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
87 if [[ ! ${_error} = 0 ]]; then
96 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
97 if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
100 _msg_info "Unmounting ${mount}"
101 umount -fl "${mount}"
106 # Unmount chroot dir and airootfs
107 umount_chroot_airootfs () {
110 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
111 _msg_info "Unmounting ${mount}"
112 umount -fl "${mount}"
115 # Helper function to run make_*() only one time.
121 if [[ ! -e "${work_dir}/build.${name}" ]]; then
122 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
124 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
125 touch "${work_dir}/build.${name}"
132 for mount in "dev" "dev/pts" "proc" "sys" "etc/resolv.conf"; do
133 #for mount in "dev" "dev/pts" "proc" "sys" ; do
134 if [[ "${mount}" == "etc/resolv.conf" ]]; then
135 cp /etc/resolv.conf "${work_dir}/airootfs/${mount}"
137 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
141 chroot "${work_dir}/airootfs" "${@}"
143 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
144 if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
147 umount -fl "${mount}"
153 run_cmd dnf install -y ${@}
157 # Delete the file if it exists.
158 # For directories, rm -rf is used.
159 # If the file does not exist, skip it.
160 # remove <file> <file> ...
166 for _file in "${_list[@]}"; do
167 _msg_debug "Removeing ${_file}"
169 if [[ -f ${_file} ]]; then
171 elif [[ -d ${_file} ]]; then
179 echo "usage ${0} [options] [channel]"
181 echo " General options:"
183 echo " -a | --arch <str> Set architecture"
184 echo " Default: ${arch}"
185 echo " -c | --codename <str> Set ubuntu codename"
186 echo " Default: ${codename}"
187 echo " -m | --mirror <url> Set apt mirror server."
188 echo " Default: ${mirror}"
189 echo " -o | --out <out_dir> Set the output directory"
190 echo " Default: ${out_dir}"
191 echo " -w | --work <work_dir> Set the working directory"
192 echo " Default: ${work_dir}"
194 echo " -d | --debug "
195 echo " -h | --help This help message and exit"
197 echo "You can switch between installed packages, files included in images, etc. by channel."
205 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
206 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
207 channel_list+=( "${_channel}" )
211 for _channel in ${channel_list[@]}; do
212 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
213 description=$(cat "${channels_dir}/${_channel}/description.txt")
215 description="This channel does not have a description.txt."
218 echo -ne " ${_channel}"
220 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
224 echo -ne "${description}\n"
231 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
232 dnf --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) install @Core -y
233 _msg_info "${codename} installed successfully!"
235 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
236 run_cmd dnf update -y
237 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
238 run_cmd dnf clean all
239 # run_cmd apt-get upgrade
244 if [[ ${EUID} -ne 0 ]]; then
245 _msg_error "This script must be run as root." 1
247 umount_chroot_airootfs
249 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
250 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
252 if [[ -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
255 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
256 mkdir -p "${work_dir}/airootfs/"
257 _msg_info "Make rootfs image..."
258 truncate -s 4G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
259 _msg_info "Format rootfs image..."
260 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
262 if [[ -d "${out_dir}" ]]; then
265 mkdir -p "${out_dir}"
267 _msg_info "Mount rootfs image..."
268 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
273 _dnf_install dbus-tools
274 run_cmd dbus-uuidgen --ensure=/etc/machine-id
275 run_cmd mkdir /var/lib/dbus
276 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
278 make_dnf_packages() {
279 remove "${work_dir}/airootfs/dnfpkglist"
280 #_apt_install initramfs-tools
281 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
283 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
284 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
287 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
288 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
291 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
292 run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
295 remove "${work_dir}/airootfs/dnfpkglist"
299 if [ -d ${channels_dir}/share/airootfs ]; then
300 cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
302 if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
303 cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
308 if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
309 run_cmd /root/customize_airootfs.sh
311 run_cmd truncate -s 0 /etc/machine-id
312 run_cmd passwd -u -f root
315 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
316 run_cmd dnf clean all
321 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
322 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
324 _msg_info "make initrd..."
325 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
326 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
327 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
329 cp "${work_dir}"/airootfs/usr/lib/syslinux/modules/bios/*.c32 "${bootfiles_dir}/isolinux/"
330 cp "${work_dir}/airootfs/usr/lib/ISOLINUX/isolinux.bin" "${bootfiles_dir}/isolinux/"
331 cp "${work_dir}/airootfs/usr/lib/ISOLINUX/isohdpfx.bin" "${bootfiles_dir}/isolinux/"
333 rm -rf "${work_dir}/airootfs/boot"
334 umount "${work_dir}/airootfs"
335 _msg_info "Minimize rootfs..."
336 resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
337 _msg_info "Compress rootfs.."
338 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img"
342 touch "${bootfiles_dir}/fedora_lfbs"
344 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
346 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
352 --output="${bootfiles_dir}/grub/bootia32.efi" \
355 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
359 --format=x86_64-efi \
360 --output="${bootfiles_dir}/grub/bootx64.efi" \
363 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
366 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
367 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
368 mkdir "${bootfiles_dir}/mnt"
369 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
370 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
371 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
372 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
373 umount -d "${bootfiles_dir}/mnt"
374 rm -r "${bootfiles_dir}/mnt"
377 cd "${bootfiles_dir}"
379 # create checksum (using at booting)
380 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
386 -full-iso9660-filenames \
387 -volid "${iso_label}" \
388 -appid "${iso_application}" \
389 -publisher "${iso_publisher}" \
390 -preparer "prepared by LFBS" \
391 -eltorito-boot isolinux/isolinux.bin \
396 -eltorito-platform efi \
397 -eltorito-boot EFI/efiboot.img \
399 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
400 -isohybrid-gpt-basdat \
401 -eltorito-catalog isolinux/boot.cat \
402 -output "${out_dir}/${iso_filename}" \
405 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
406 "/EFI/efiboot.img=grub/efiboot.img"
411 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
413 _opt_short="w:o:ha:-:m:c:d"
414 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
415 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
417 if [[ ${?} != 0 ]]; then
426 if [[ -z ${2} ]]; then
427 _msg_error "Please specify the architecture."
435 if [[ -z ${2} ]]; then
436 _msg_error "Please specify the codename."
452 if [[ -z ${2} ]]; then
453 _msg_error "Please specify the mirror server."
462 if [[ -z ${2} ]]; then
463 _msg_error "Please specify the out dir."
472 if [[ -z ${2} ]]; then
473 _msg_error "Please specify the out dir."
490 _msg_error "Invalid argument '${1}'"
496 bootfiles_dir="${work_dir}/bootfiles"
497 trap umount_chroot 0 2 15
499 if [[ -n "${1}" ]]; then
507 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
508 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
509 channel_list+=( "${_channel}" )
513 for i in ${channel_list[@]}; do
514 if [[ "${i}" = "${channel_name}" ]]; then
524 if [[ $(check_channel ${channel_name}) = false ]]; then
525 _msg_error "Invalid channel ${channel_name}"
529 umount_chroot_airootfs
533 run_once make_systemd
534 run_once make_dnf_packages
535 run_once make_cp_airootfs
538 run_once make_squashfs
542 run_once make_checksum