OSDN Git Service

added i3wm
[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 nfb_dir="${script_path}/nfb"
21 codename="32"
22 os_name="Fedora"
23 iso_name="Fedora"
24
25 arch=x86_64
26
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"
33
34
35 debug=false
36 cache_only=false
37
38
39 start_time="$(date +%s)"
40
41 _msg_common() {
42     if [[ "${debug}" = true ]]; then
43         local _current_time
44         local _time
45         _current_time="$(date +%s)"
46         _time="$(("${_current_time}"-"${start_time}"))"
47
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]")"
52         else
53             echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
54         fi
55     else
56         "${script_path}/echo_color" -t 6 "[LFBS Core]"
57     fi
58 }
59
60 # Show an INFO message
61 # _msg_info <message>
62 _msg_info() {
63     local _msg
64     _msg="${@}"
65     echo "$(_msg_common)  $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
66 }
67
68 # Show an debug message
69 # _msg_debug <message>
70 _msg_debug() {
71     if [[ "${debug}" = true ]]; then
72         local _msg
73         _msg="${@}"
74         echo "$(_msg_common)  $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
75     fi
76 }
77
78 # Show an ERROR message then exit with status
79 # _msg_error <message> <exit code>
80 _msg_error() {
81     local _msg
82     local _error
83     _msg="${1}"
84     _error=${2}
85     echo "$(_msg_common)  $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
86
87     if [[ ! ${_error} = 0 ]]; then
88         exit ${_error}
89     fi
90 }
91
92 # Unmount chroot dir
93 umount_chroot () {
94     local mount
95
96     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
97         if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
98             :
99         else
100             _msg_info "Unmounting ${mount}"
101             umount -fl "${mount}"
102         fi
103     done
104 }
105
106 # Unmount chroot dir and airootfs
107 umount_chroot_airootfs () {
108     local mount
109
110     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
111         _msg_info "Unmounting ${mount}"
112         umount -fl "${mount}"
113     done
114 }
115 # Helper function to run make_*() only one time.
116 run_once() {
117     local name
118     umount_chroot
119     name="$1"
120
121     if [[ ! -e "${work_dir}/build.${name}" ]]; then
122         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
123         "${1}"
124         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
125         touch "${work_dir}/build.${name}"
126     fi
127 }
128
129 run_cmd() {
130     local mount
131
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}"
136         else
137             mount --bind /${mount} "${work_dir}/airootfs/${mount}"
138         fi
139     done
140     
141     chroot "${work_dir}/airootfs" "${@}"
142
143     for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
144         if [[ "${mount}" == "${work_dir}/airootfs" ]]; then
145             :
146         else
147             umount -fl "${mount}"
148         fi
149     done
150 }
151
152 _dnf_install() {
153     run_cmd dnf install -y ${@}
154 }
155
156 # rm helper
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> ...
161 remove() {
162     local _list
163     local _file
164     _list=($(echo "$@"))
165
166     for _file in "${_list[@]}"; do
167         _msg_debug "Removeing ${_file}"
168
169         if [[ -f ${_file} ]]; then
170             rm -f "${_file}"
171         elif [[ -d ${_file} ]]; then
172             rm -rf "${_file}"
173         fi
174     done
175 }
176
177 # Show help
178 _usage () {
179     echo "usage ${0} [options] [channel]"
180     echo
181     echo " General options:"
182     echo
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}"
193     echo
194     echo "    -d | --debug           "
195     echo "    -h | --help            This help message and exit"
196     echo
197     echo "You can switch between installed packages, files included in images, etc. by channel."
198     echo
199     echo " Channel:"
200     
201     local _channel
202     local channel_list
203     local description
204
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}" )
208         fi
209     done
210
211     for _channel in ${channel_list[@]}; do
212         if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
213             description=$(cat "${channels_dir}/${_channel}/description.txt")
214         else
215             description="This channel does not have a description.txt."
216         fi
217
218         echo -ne "    ${_channel}"
219
220         for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
221             echo -ne " "
222         done
223         
224         echo -ne "${description}\n"
225     done
226 }
227
228
229
230 make_basefs() {
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!"
234     
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
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     umount_chroot_airootfs
247     # Check codename
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})."
250     fi
251     if [[ -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
252         :
253     else
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"
260     fi    
261     if [[ -d "${out_dir}" ]]; then
262         :
263     else
264         mkdir -p "${out_dir}"
265     fi
266     _msg_info "Mount rootfs image..."
267     mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
268
269 }
270
271 make_systemd() {
272     _dnf_install dbus-tools
273     run_cmd dbus-uuidgen --ensure=/etc/machine-id
274     if [[ -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
275         :
276     else
277         run_cmd mkdir /var/lib/dbus
278     fi
279     run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
280 }
281 make_dnf_packages() {
282     remove "${work_dir}/airootfs/dnfpkglist"
283     #_apt_install initramfs-tools
284     # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
285
286     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
287         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
288     fi
289
290     if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
291         grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
292     fi
293
294     if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
295         run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
296     fi
297
298     remove "${work_dir}/airootfs/dnfpkglist"
299 }
300
301 make_cp_airootfs() {
302     if [ -d ${channels_dir}/share/airootfs ]; then
303         cp -rf ${channels_dir}/share/airootfs/ ${work_dir}
304     fi
305     if [ -d ${channels_dir}/${channel_name}/airootfs ]; then
306         cp -rf ${channels_dir}/${channel_name}/airootfs/ ${work_dir}
307     fi
308 }
309
310 make_config() {
311     if [ -f ${work_dir}/airootfs/root/customize_airootfs.sh ]; then
312         run_cmd /root/customize_airootfs.sh
313     fi
314     run_cmd truncate -s 0 /etc/machine-id
315     run_cmd passwd -u -f root
316 }
317 make_clean() {
318     run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
319     run_cmd dnf clean all
320 }
321
322 make_squashfs() {
323     # prepare
324     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
325     mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
326     #generate initrd
327     _msg_info "make initrd..."
328     run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
329     cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
330     mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
331     #cp isolinux
332     cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
333     # make squashfs
334     rm -rf "${work_dir}/airootfs/boot"
335     umount "${work_dir}/airootfs"
336     _msg_info "Minimize rootfs..."
337     resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
338     _msg_info "Compress rootfs.."
339     mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img"
340 }
341
342 make_nfb() {
343     touch "${bootfiles_dir}/fedora_lfbs"
344     # isolinux setup
345     sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g"  > "${bootfiles_dir}/isolinux/isolinux.cfg"
346     #grub
347     sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
348 }
349 make_efi() {
350     # UEFI 32bit (ia32)
351     grub-mkstandalone \
352         --format=i386-efi \
353         --output="${bootfiles_dir}/grub/bootia32.efi" \
354         --locales="" \
355         --fonts="" \
356         "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
357     
358     # UEFI 64bit (x64)
359     grub-mkstandalone \
360         --format=x86_64-efi \
361         --output="${bootfiles_dir}/grub/bootx64.efi" \
362         --locales="" \
363         --fonts="" \
364         "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
365
366     # create efiboot.img
367     truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
368     mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
369     mkdir "${bootfiles_dir}/mnt"
370     mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
371     mkdir -p "${bootfiles_dir}/mnt/efi/boot"
372     cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
373     cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
374     umount -d "${bootfiles_dir}/mnt"
375     rm -r "${bootfiles_dir}/mnt"
376 }
377 make_iso() {
378     cd "${bootfiles_dir}"
379
380     # create checksum (using at booting)
381     bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
382
383     # create iso
384     xorriso \
385         -as mkisofs \
386         -iso-level 3 \
387         -full-iso9660-filenames \
388         -volid "${iso_label}" \
389         -appid "${iso_application}" \
390         -publisher "${iso_publisher}" \
391         -preparer "prepared by LFBS" \
392         -b isolinux/isolinux.bin \
393             -no-emul-boot \
394             -boot-load-size 4 \
395             -boot-info-table \
396         -eltorito-alt-boot \
397             -eltorito-platform efi \
398             -eltorito-boot EFI/efiboot.img \
399             -no-emul-boot \
400         -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
401         -isohybrid-gpt-basdat \
402         -eltorito-catalog isolinux/boot.cat \
403         -output "${out_dir}/${iso_filename}" \
404         -graft-points \
405             "." \
406             "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
407             "/EFI/efiboot.img=grub/efiboot.img"
408     
409     cd - > /dev/null
410 }
411
412 make_checksum() {
413     cd "${out_dir}"
414     _msg_info "Creating md5 checksum ..."
415     md5sum "${iso_filename}" > "${iso_filename}.md5"
416
417     _msg_info "Creating sha256 checksum ..."
418     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
419     cd - > /dev/null 2>&1
420     umount_chroot_airootfs
421 }
422
423 # 引数解析()
424 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
425
426 _opt_short="w:o:ha:-:m:c:d"
427 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
428 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
429
430 if [[ ${?} != 0 ]]; then
431     exit 1
432 fi
433
434 eval set -- "${OPT}"
435
436 while :; do
437     case ${1} in
438         -a | --arch)
439             if [[ -z ${2} ]]; then
440                 _msg_error "Please specify the architecture."
441                 exit 1
442             else
443                 arch="${2}"
444             fi
445             shift 2
446             ;;
447         -c | --codename)
448             if [[ -z ${2} ]]; then
449                 _msg_error "Please specify the codename."
450                 exit 1
451             else
452                 codename="${2}"
453             fi
454             shift 2
455             ;;
456         -d | --debug)
457             debug=true
458             shift 1
459             ;;
460         -h | --help)
461             _usage
462             exit 0
463             ;;
464         -m | --mirror)
465             if [[ -z ${2} ]]; then
466                 _msg_error "Please specify the mirror server."
467                 exit 1
468             else
469                 mirror="${2}"
470             fi
471
472             shift 2
473             ;;
474         -o | --out)
475             if [[ -z ${2} ]]; then
476                 _msg_error "Please specify the out dir."
477                 exit 1
478             else
479                 out_dir="${2}"
480             fi
481
482             shift 2
483             ;;
484         -w | --work)
485             if [[ -z ${2} ]]; then
486                 _msg_error "Please specify the out dir."
487                 exit 1
488             else
489                 work_dir="${2}"
490             fi
491
492             shift 2
493             ;;
494         --cache-only)
495             cache_only=true
496             shift 1
497             ;;
498         --)
499             shift
500             break
501             ;;
502         *)
503             _msg_error "Invalid argument '${1}'"
504             _usage 1
505             ;;
506     esac
507 done
508
509 bootfiles_dir="${work_dir}/bootfiles"
510 trap  umount_chroot 0 2 15
511
512 if [[ -n "${1}" ]]; then
513     channel_name="${1}"
514     if [[ "${channel_name}" = "umount" ]]; then
515         umount_chroot_airootfs
516         exit 0
517     fi
518     if [[ "${channel_name}" = "clean" ]]; then
519         umount_chroot_airootfs
520         _msg_info "deleting work dir..."
521         rm -rf "${work_dir}"
522         exit 0
523     fi
524     check_channel() {
525         local channel_list
526         local i
527         channel_list=()
528         
529         for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
530             if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
531                 channel_list+=( "${_channel}" )
532             fi
533         done
534
535         for i in ${channel_list[@]}; do
536             if [[ "${i}" = "${channel_name}" ]]; then
537                 echo -n "true"
538                 return 0
539             fi
540         done
541
542         echo -n "false"
543         return 1
544     }
545
546     if [[ $(check_channel ${channel_name}) = false ]]; then
547         _msg_error "Invalid channel ${channel_name}"
548         exit 1
549     fi
550 fi
551
552 iso_filename="${iso_name}-${codename}-${channel_name}-$(date +%Y.%m.%d)-${arch}.iso"
553 umount_chroot_airootfs
554
555 prepare_build
556 run_once make_basefs
557 run_once make_systemd
558 run_once make_dnf_packages
559 run_once make_cp_airootfs
560 run_once make_config
561 run_once make_clean
562 run_once make_squashfs
563 run_once make_nfb
564 run_once make_efi
565 run_once make_iso
566 run_once make_checksum