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"
26 out_dir="${script_path}/out"
27 iso_label="${os_name}_${codename}_${arch}"
28 iso_publisher='Fascode Network <https://fascode.net>'
29 iso_application="${os_name} Live/Rescue CD"
30 iso_version="${codename}-$(date +%Y.%m.%d)"
31 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
38 start_time="$(date +%s)"
41 if [[ "${debug}" = true ]]; then
44 _current_time="$(date +%s)"
45 _time="$(("${_current_time}"-"${start_time}"))"
47 if [[ "${_time}" -ge 3600 ]]; then
48 echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
49 elif [[ "${_time}" -ge 60 ]]; then
50 echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
52 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
55 "${script_path}/echo_color" -t 6 "[LFBS Core]"
59 # Show an INFO message
64 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
67 # Show an debug message
68 # _msg_debug <message>
70 if [[ "${debug}" = true ]]; then
73 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
77 # Show an ERROR message then exit with status
78 # _msg_error <message> <exit code>
84 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
86 if [[ ! ${_error} = 0 ]]; then
95 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
96 _msg_info "Unmounting ${mount}"
101 # Helper function to run make_*() only one time.
107 if [[ ! -e "${work_dir}/build.${name}" ]]; then
108 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
110 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
111 touch "${work_dir}/build.${name}"
118 for mount in "dev" "dev/pts" "proc" "sys" "etc/resolv.conf"; do
119 #for mount in "dev" "dev/pts" "proc" "sys" ; do
120 if [[ "${mount}" == "etc/resolv.conf" ]]; then
121 cp /etc/resolv.conf "${work_dir}/airootfs/${mount}"
123 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
127 chroot "${work_dir}/airootfs" "${@}"
129 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
130 umount -fl "${mount}"
135 run_cmd dnf install -y ${@}
139 # Delete the file if it exists.
140 # For directories, rm -rf is used.
141 # If the file does not exist, skip it.
142 # remove <file> <file> ...
148 for _file in "${_list[@]}"; do
149 _msg_debug "Removeing ${_file}"
151 if [[ -f ${_file} ]]; then
153 elif [[ -d ${_file} ]]; then
161 echo "usage ${0} [options] [channel]"
163 echo " General options:"
165 echo " -a | --arch <str> Set architecture"
166 echo " Default: ${arch}"
167 echo " -c | --codename <str> Set ubuntu codename"
168 echo " Default: ${codename}"
169 echo " -m | --mirror <url> Set apt mirror server."
170 echo " Default: ${mirror}"
171 echo " -o | --out <out_dir> Set the output directory"
172 echo " Default: ${out_dir}"
173 echo " -w | --work <work_dir> Set the working directory"
174 echo " Default: ${work_dir}"
176 echo " -d | --debug "
177 echo " -h | --help This help message and exit"
179 echo "You can switch between installed packages, files included in images, etc. by channel."
187 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
188 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
189 channel_list+=( "${_channel}" )
193 for _channel in ${channel_list[@]}; do
194 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
195 description=$(cat "${channels_dir}/${_channel}/description.txt")
197 description="This channel does not have a description.txt."
200 echo -ne " ${_channel}"
202 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
206 echo -ne "${description}\n"
212 if [[ ${EUID} -ne 0 ]]; then
213 _msg_error "This script must be run as root." 1
216 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
217 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
221 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
222 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
228 remove "${work_dir}/${codename}"
229 _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
230 mkdir -p "${work_dir}/airootfs"
231 dnf --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) groupinstall core -y
232 _msg_info "${codename} installed successfully!"
234 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
235 run_cmd dnf upgrade -y
236 run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
237 run_cmd dnf clean all
238 # run_cmd apt-get upgrade
243 if [[ ${EUID} -ne 0 ]]; then
244 _msg_error "This script must be run as root." 1
247 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
248 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
252 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
253 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
259 _dnf_install dbus-tools
260 run_cmd dbus-uuidgen --ensure=/etc/machine-id
261 run_cmd mkdir /var/lib/dbus
262 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
264 make_dnf_packages() {
265 remove "${work_dir}/airootfs/dnfpkglist"
266 #_apt_install initramfs-tools
267 # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
269 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
270 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
273 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
274 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
277 if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
278 run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
281 remove "${work_dir}/airootfs/dnfpkglist"
285 if [ -d ${channels_dir}/share/airootfs ]; then
286 cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
288 if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
289 cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
294 run_cmd truncate -s 0 /etc/machine-id
295 if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
296 run_cmd /root/customize_airootfs.sh
300 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
302 _opt_short="w:o:ha:-:m:c:d"
303 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
304 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
306 if [[ ${?} != 0 ]]; then
315 if [[ -z ${2} ]]; then
316 _msg_error "Please specify the architecture."
324 if [[ -z ${2} ]]; then
325 _msg_error "Please specify the codename."
341 if [[ -z ${2} ]]; then
342 _msg_error "Please specify the mirror server."
351 if [[ -z ${2} ]]; then
352 _msg_error "Please specify the out dir."
361 if [[ -z ${2} ]]; then
362 _msg_error "Please specify the out dir."
379 _msg_error "Invalid argument '${1}'"
385 bootfiles_dir="${work_dir}/bootfiles"
386 trap umount_chroot 0 2 15
388 if [[ -n "${1}" ]]; then
396 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
397 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
398 channel_list+=( "${_channel}" )
402 for i in ${channel_list[@]}; do
403 if [[ "${i}" = "${channel_name}" ]]; then
413 if [[ $(check_channel ${channel_name}) = false ]]; then
414 _msg_error "Invalid channel ${channel_name}"
422 run_once make_systemd
423 run_once make_dnf_packages
424 run_once make_cp_airootfs
426 run_once make_add_user
428 run_once make_squashfs
432 run_once make_checksum