OSDN Git Service

fixed login
[alterlinux/LFBS.git] / lfbs
1 #!/usr/bin/env bash
2
3 # SPDX-License-Identifier: GPL-3.0
4 #
5 # kokkiemouse
6 # Twitter: @kokkiemouse
7 # Email  : kokkiemouse@gmail.com
8 #
9 # lfbs
10 #
11
12 set -e
13 # set -u
14
15 export LANG=C
16
17 script_path=$(readlink -f "${0%/*}")
18 work_dir="${script_path}/work"
19 channels_dir="${script_path}/channels"
20 codename="32"
21 os_name="Fedora"
22 iso_name="Fedora"
23
24 arch=x86_64
25
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"
32
33
34 debug=false
35 cache_only=false
36
37
38 start_time="$(date +%s)"
39
40 _msg_common() {
41     if [[ "${debug}" = true ]]; then
42         local _current_time
43         local _time
44         _current_time="$(date +%s)"
45         _time="$(("${_current_time}"-"${start_time}"))"
46
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]")"
51         else
52             echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
53         fi
54     else
55         "${script_path}/echo_color" -t 6 "[LFBS Core]"
56     fi
57 }
58
59 # Show an INFO message
60 # _msg_info <message>
61 _msg_info() {
62     local _msg
63     _msg="${@}"
64     echo "$(_msg_common)  $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
65 }
66
67 # Show an debug message
68 # _msg_debug <message>
69 _msg_debug() {
70     if [[ "${debug}" = true ]]; then
71         local _msg
72         _msg="${@}"
73         echo "$(_msg_common)  $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
74     fi
75 }
76
77 # Show an ERROR message then exit with status
78 # _msg_error <message> <exit code>
79 _msg_error() {
80     local _msg
81     local _error
82     _msg="${1}"
83     _error=${2}
84     echo "$(_msg_common)  $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
85
86     if [[ ! ${_error} = 0 ]]; then
87         exit ${_error}
88     fi
89 }
90
91 # Unmount chroot dir
92 umount_chroot () {
93     local mount
94
95     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
96         _msg_info "Unmounting ${mount}"
97         umount -fl "${mount}"
98     done
99 }
100
101 # Helper function to run make_*() only one time.
102 run_once() {
103     local name
104     umount_chroot
105     name="$1"
106
107     if [[ ! -e "${work_dir}/build.${name}" ]]; then
108         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
109         "${1}"
110         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
111         touch "${work_dir}/build.${name}"
112     fi
113 }
114
115 run_cmd() {
116     local mount
117
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}"
122         else
123             mount --bind /${mount} "${work_dir}/airootfs/${mount}"
124         fi
125     done
126     
127     chroot "${work_dir}/airootfs" "${@}"
128
129     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
130         umount -fl "${mount}"
131     done
132 }
133
134 _dnf_install() {
135     run_cmd dnf install -y ${@}
136 }
137
138 # rm helper
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> ...
143 remove() {
144     local _list
145     local _file
146     _list=($(echo "$@"))
147
148     for _file in "${_list[@]}"; do
149         _msg_debug "Removeing ${_file}"
150
151         if [[ -f ${_file} ]]; then
152             rm -f "${_file}"
153         elif [[ -d ${_file} ]]; then
154             rm -rf "${_file}"
155         fi
156     done
157 }
158
159 # Show help
160 _usage () {
161     echo "usage ${0} [options] [channel]"
162     echo
163     echo " General options:"
164     echo
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}"
175     echo
176     echo "    -d | --debug           "
177     echo "    -h | --help            This help message and exit"
178     echo
179     echo "You can switch between installed packages, files included in images, etc. by channel."
180     echo
181     echo " Channel:"
182     
183     local _channel
184     local channel_list
185     local description
186
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}" )
190         fi
191     done
192
193     for _channel in ${channel_list[@]}; do
194         if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
195             description=$(cat "${channels_dir}/${_channel}/description.txt")
196         else
197             description="This channel does not have a description.txt."
198         fi
199
200         echo -ne "    ${_channel}"
201
202         for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
203             echo -ne " "
204         done
205         
206         echo -ne "${description}\n"
207     done
208 }
209
210
211 prepare_build() {
212     if [[ ${EUID} -ne 0 ]]; then
213         _msg_error "This script must be run as root." 1
214     fi
215
216     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
217     [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
218     umount_chroot
219
220     # Check codename
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})."
223     fi
224
225 }
226
227 make_basefs() {
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!"
233     
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
239 }
240
241
242 prepare_build() {
243     if [[ ${EUID} -ne 0 ]]; then
244         _msg_error "This script must be run as root." 1
245     fi
246
247     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
248     [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
249     umount_chroot
250
251     # Check codename
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})."
254     fi
255
256 }
257
258 make_systemd() {
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
263 }
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'
268
269     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
270         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
271     fi
272
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"
275     fi
276
277     if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
278         run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
279     fi
280
281     remove "${work_dir}/airootfs/dnfpkglist"
282 }
283
284 make_cp_airootfs() {
285     if [ -d ${channels_dir}/share/airootfs ]; then
286         cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
287     fi
288     if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
289         cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
290     fi
291 }
292
293 make_config() {
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
297     fi
298 }
299 # 引数解析()
300 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
301
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} -- "${@}")
305
306 if [[ ${?} != 0 ]]; then
307     exit 1
308 fi
309
310 eval set -- "${OPT}"
311
312 while :; do
313     case ${1} in
314         -a | --arch)
315             if [[ -z ${2} ]]; then
316                 _msg_error "Please specify the architecture."
317                 exit 1
318             else
319                 arch="${2}"
320             fi
321             shift 2
322             ;;
323         -c | --codename)
324             if [[ -z ${2} ]]; then
325                 _msg_error "Please specify the codename."
326                 exit 1
327             else
328                 codename="${2}"
329             fi
330             shift 2
331             ;;
332         -d | --debug)
333             debug=true
334             shift 1
335             ;;
336         -h | --help)
337             _usage
338             exit 0
339             ;;
340         -m | --mirror)
341             if [[ -z ${2} ]]; then
342                 _msg_error "Please specify the mirror server."
343                 exit 1
344             else
345                 mirror="${2}"
346             fi
347
348             shift 2
349             ;;
350         -o | --out)
351             if [[ -z ${2} ]]; then
352                 _msg_error "Please specify the out dir."
353                 exit 1
354             else
355                 out_dir="${2}"
356             fi
357
358             shift 2
359             ;;
360         -w | --work)
361             if [[ -z ${2} ]]; then
362                 _msg_error "Please specify the out dir."
363                 exit 1
364             else
365                 work_dir="${2}"
366             fi
367
368             shift 2
369             ;;
370         --cache-only)
371             cache_only=true
372             shift 1
373             ;;
374         --)
375             shift
376             break
377             ;;
378         *)
379             _msg_error "Invalid argument '${1}'"
380             _usage 1
381             ;;
382     esac
383 done
384
385 bootfiles_dir="${work_dir}/bootfiles"
386 trap  umount_chroot 0 2 15
387
388 if [[ -n "${1}" ]]; then
389     channel_name="${1}"
390
391     check_channel() {
392         local channel_list
393         local i
394         channel_list=()
395
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}" )
399             fi
400         done
401
402         for i in ${channel_list[@]}; do
403             if [[ "${i}" = "${channel_name}" ]]; then
404                 echo -n "true"
405                 return 0
406             fi
407         done
408
409         echo -n "false"
410         return 1
411     }
412
413     if [[ $(check_channel ${channel_name}) = false ]]; then
414         _msg_error "Invalid channel ${channel_name}"
415         exit 1
416     fi
417 fi
418
419
420 prepare_build
421 run_once make_basefs
422 run_once make_systemd
423 run_once make_dnf_packages
424 run_once make_cp_airootfs
425 run_once make_config
426 run_once make_add_user
427 run_once make_clean
428 run_once make_squashfs
429 run_once make_nfb
430 run_once make_efi
431 run_once make_iso
432 run_once make_checksum