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 apt-get upgrade
243 if [[ ${EUID} -ne 0 ]]; then
244 _msg_error "This script must be run as root." 1
246 umount_chroot_airootfs
248 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
249 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
251 if [[ -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
254 mkdir -p "${work_dir}/squashfsroot/LiveOS/"
255 mkdir -p "${work_dir}/airootfs/"
256 _msg_info "Make rootfs image..."
257 truncate -s 4G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
258 _msg_info "Format rootfs image..."
259 mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
261 if [[ -d "${out_dir}" ]]; then
264 mkdir -p "${out_dir}"
266 _msg_info "Mount rootfs image..."
267 mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
272 _dnf_install dbus-tools
273 run_cmd dbus-uuidgen --ensure=/etc/machine-id
274 run_cmd mkdir /var/lib/dbus
275 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
277 make_dnf_packages() {
278 remove "${work_dir}/airootfs/dnfpkglist"
279 #_apt_install initramfs-tools
280 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
282 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
283 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
286 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
287 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
290 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
291 run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
294 remove "${work_dir}/airootfs/dnfpkglist"
298 if [ -d ${channels_dir}/share/airootfs ]; then
299 cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
301 if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
302 cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
307 if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
308 run_cmd /root/customize_airootfs.sh
310 run_cmd truncate -s 0 /etc/machine-id
311 run_cmd passwd -u -f root
314 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
315 run_cmd dnf clean all
320 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
321 mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
323 _msg_info "make initrd..."
324 run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
325 cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
326 mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
328 cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
330 rm -rf "${work_dir}/airootfs/boot"
331 umount "${work_dir}/airootfs"
332 _msg_info "Minimize rootfs..."
333 resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
334 _msg_info "Compress rootfs.."
335 mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img"
339 touch "${bootfiles_dir}/fedora_lfbs"
341 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
343 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
349 --output="${bootfiles_dir}/grub/bootia32.efi" \
352 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
356 --format=x86_64-efi \
357 --output="${bootfiles_dir}/grub/bootx64.efi" \
360 "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
363 truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
364 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
365 mkdir "${bootfiles_dir}/mnt"
366 mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
367 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
368 cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
369 cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
370 umount -d "${bootfiles_dir}/mnt"
371 rm -r "${bootfiles_dir}/mnt"
374 cd "${bootfiles_dir}"
376 # create checksum (using at booting)
377 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
383 -full-iso9660-filenames \
384 -volid "${iso_label}" \
385 -appid "${iso_application}" \
386 -publisher "${iso_publisher}" \
387 -preparer "prepared by LFBS" \
388 -eltorito-boot isolinux/isolinux.bin \
393 -eltorito-platform efi \
394 -eltorito-boot EFI/efiboot.img \
396 -isohybrid-gpt-basdat \
397 -eltorito-catalog isolinux/boot.cat \
398 -output "${out_dir}/${iso_filename}" \
401 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
402 "/EFI/efiboot.img=grub/efiboot.img"
407 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
409 _opt_short="w:o:ha:-:m:c:d"
410 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
411 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
413 if [[ ${?} != 0 ]]; then
422 if [[ -z ${2} ]]; then
423 _msg_error "Please specify the architecture."
431 if [[ -z ${2} ]]; then
432 _msg_error "Please specify the codename."
448 if [[ -z ${2} ]]; then
449 _msg_error "Please specify the mirror server."
458 if [[ -z ${2} ]]; then
459 _msg_error "Please specify the out dir."
468 if [[ -z ${2} ]]; then
469 _msg_error "Please specify the out dir."
486 _msg_error "Invalid argument '${1}'"
492 bootfiles_dir="${work_dir}/bootfiles"
493 trap umount_chroot 0 2 15
495 if [[ -n "${1}" ]]; then
503 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
504 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
505 channel_list+=( "${_channel}" )
509 for i in ${channel_list[@]}; do
510 if [[ "${i}" = "${channel_name}" ]]; then
520 if [[ $(check_channel ${channel_name}) = false ]]; then
521 _msg_error "Invalid channel ${channel_name}"
525 umount_chroot_airootfs
529 run_once make_systemd
530 run_once make_dnf_packages
531 run_once make_cp_airootfs
534 run_once make_squashfs
538 run_once make_checksum