OSDN Git Service

added squashfs
[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     if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
295         run_cmd /root/customize_airootfs.sh
296     fi
297     run_cmd truncate -s 0 /etc/machine-id
298 }
299 make_clean() {
300     run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
301     run_cmd dnf clean all
302 }
303
304 make_squashfs() {
305     # prepare
306     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
307     mkdir -p "${bootfiles_dir}"/{isolinux,fedora,boot}
308
309     #generate initrd
310     run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
311     cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
312     mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
313     # make squashfs
314     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/fedora/airootfs.sfs"
315 }
316
317 # 引数解析()
318 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
319
320 _opt_short="w:o:ha:-:m:c:d"
321 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
322 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
323
324 if [[ ${?} != 0 ]]; then
325     exit 1
326 fi
327
328 eval set -- "${OPT}"
329
330 while :; do
331     case ${1} in
332         -a | --arch)
333             if [[ -z ${2} ]]; then
334                 _msg_error "Please specify the architecture."
335                 exit 1
336             else
337                 arch="${2}"
338             fi
339             shift 2
340             ;;
341         -c | --codename)
342             if [[ -z ${2} ]]; then
343                 _msg_error "Please specify the codename."
344                 exit 1
345             else
346                 codename="${2}"
347             fi
348             shift 2
349             ;;
350         -d | --debug)
351             debug=true
352             shift 1
353             ;;
354         -h | --help)
355             _usage
356             exit 0
357             ;;
358         -m | --mirror)
359             if [[ -z ${2} ]]; then
360                 _msg_error "Please specify the mirror server."
361                 exit 1
362             else
363                 mirror="${2}"
364             fi
365
366             shift 2
367             ;;
368         -o | --out)
369             if [[ -z ${2} ]]; then
370                 _msg_error "Please specify the out dir."
371                 exit 1
372             else
373                 out_dir="${2}"
374             fi
375
376             shift 2
377             ;;
378         -w | --work)
379             if [[ -z ${2} ]]; then
380                 _msg_error "Please specify the out dir."
381                 exit 1
382             else
383                 work_dir="${2}"
384             fi
385
386             shift 2
387             ;;
388         --cache-only)
389             cache_only=true
390             shift 1
391             ;;
392         --)
393             shift
394             break
395             ;;
396         *)
397             _msg_error "Invalid argument '${1}'"
398             _usage 1
399             ;;
400     esac
401 done
402
403 bootfiles_dir="${work_dir}/bootfiles"
404 trap  umount_chroot 0 2 15
405
406 if [[ -n "${1}" ]]; then
407     channel_name="${1}"
408
409     check_channel() {
410         local channel_list
411         local i
412         channel_list=()
413
414         for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
415             if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
416                 channel_list+=( "${_channel}" )
417             fi
418         done
419
420         for i in ${channel_list[@]}; do
421             if [[ "${i}" = "${channel_name}" ]]; then
422                 echo -n "true"
423                 return 0
424             fi
425         done
426
427         echo -n "false"
428         return 1
429     }
430
431     if [[ $(check_channel ${channel_name}) = false ]]; then
432         _msg_error "Invalid channel ${channel_name}"
433         exit 1
434     fi
435 fi
436
437
438 prepare_build
439 run_once make_basefs
440 run_once make_systemd
441 run_once make_dnf_packages
442 run_once make_cp_airootfs
443 run_once make_config
444 run_once make_clean
445 run_once make_squashfs
446 run_once make_nfb
447 run_once make_efi
448 run_once make_iso
449 run_once make_checksum