OSDN Git Service

[change] : Fixed notation of variables
[alterlinux/LUBS.git] / lubs
1 #!/usr/bin/env bash
2
3 set -e
4 # set -u
5
6 export LANG=C
7
8 script_path=$(readlink -f ${0%/*})
9
10 cache_dir="${script_path}/cache"
11 channels_dir="${script_path}/channels"
12 isolinux_dir="${script_path}/isolinux"
13
14 codename="focal"
15 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
16 os_name="Ubuntu"
17 iso_name="ubuntu"
18
19 arch=amd64
20 work_dir="${script_path}/work"
21 out_dir="${script_path}/out"
22 iso_label="${os_name}_${codename}_${arch}"
23 iso_publisher='Fascode Network <https://fascode.net>'
24 iso_application="${os_name} Live/Rescue CD"
25 iso_version="${codename}-$(date +%Y.%m.%d)"
26 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
27
28 channel_name="serene"
29
30 debug=false
31 cache_only=false
32
33
34 start_time="$(date +%s)"
35
36 _msg_common() {
37     if [[ "${debug}" = true ]]; then
38         local _current_time="$(date +%s)"
39         local _time
40
41         _time="$(("${_current_time}"-"${start_time}"))"
42
43         if [[ "${_time}" -ge 3600 ]]; then
44             echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
45         elif [[ "${_time}" -ge 60 ]]; then
46             echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
47         else
48             echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LUBS Core]")"
49         fi
50     else
51         "${script_path}/echo_color" -t 6 "[LUBS Core]"
52     fi
53 }
54
55 # Show an INFO message
56 # _msg_info <message>
57 _msg_info() {
58     local _msg="${@}"
59     echo "$(_msg_common)  $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
60 }
61
62 # Show an debug message
63 # _msg_debug <message>
64 _msg_debug() {
65     if [[ "${debug}" = true ]]; then
66         local _msg="${@}"
67         echo "$(_msg_common)  $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
68     fi
69 }
70
71 # Show an ERROR message then exit with status
72 # _msg_error <message> <exit code>
73 _msg_error() {
74     local _msg="${1}"
75     local _error=${2}
76     echo "$(_msg_common)  $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
77     if [[ ! ${_error} = 0 ]]; then
78         exit ${_error}
79     fi
80 }
81
82 # Unmount chroot dir
83 umount_chroot () {
84     local mount
85     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir})); do
86         _msg_info "Unmounting ${mount}"
87         umount -fl "${mount}"
88     done
89 }
90
91 # Helper function to run make_*() only one time.
92 run_once() {
93     local name
94     umount_chroot
95
96     if [[ "run_bootfiles" == "$1" ]]; then
97         name="$2"
98     else
99         name="$1"
100     fi
101
102     if [[ ! -e "${work_dir}/build.${name}" ]]; then
103         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
104
105         if [[ "run_bootfiles" == "$1" ]]; then
106             "$1" "$2"
107         else
108             "$1"
109         fi
110
111         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
112         touch "${work_dir}/build.${name}"
113     fi
114 }
115
116 run_cmd() {
117     arch-chroot "${work_dir}/airootfs" "${@}"
118 }
119
120 run_bootfiles() {
121     cd "${bootfiles_dir}"
122     "$1"
123     cd - > /dev/null
124 }
125
126 _apt_install() {
127     run_cmd apt-get --no-install-recommends --yes install ${@}
128 }
129
130 # rm helper
131 # Delete the file if it exists.
132 # For directories, rm -rf is used.
133 # If the file does not exist, skip it.
134 # remove <file> <file> ...
135 remove() {
136     local _list
137     local _file
138     _list=($(echo "$@"))
139     for _file in "${_list[@]}"; do
140         _msg_debug "Removeing ${_file}"
141         if [[ -f ${_file} ]]; then
142             rm -f "${_file}"
143         elif [[ -d ${_file} ]]; then
144             rm -rf "${_file}"
145         fi
146     done
147 }
148
149 # Show help
150 _usage () {
151     echo "usage ${0} [options] [channel]"
152     echo
153     echo " General options:"
154     echo
155     echo "    -a | --arch <str>      Set architecture"
156     echo "                           Default: ${arch}"
157     echo "    -c | --codename <str>  Set ubuntu codename"
158     echo "                           Default: ${codename}"
159     echo "    -m | --mirror <url>    Set apt mirror server."
160     echo "                           Default: ${mirror}"
161     echo "    -o | --out <out_dir>   Set the output directory"
162     echo "                           Default: ${out_dir}"
163     echo "    -w | --work <work_dir> Set the working directory"
164     echo "                           Default: ${work_dir}"
165     echo
166     echo "    -d | --debug           "
167     echo "    -h | --help            This help message and exit"
168     echo
169     echo "You can switch between installed packages, files included in images, etc. by channel."
170     echo
171     echo " Channel:"
172     local _channel
173     local channel_list
174     local description
175     for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
176         if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
177             channel_list="${channel_list[@]} ${_channel}"
178         fi
179     done
180     for _channel in ${channel_list[@]}; do
181         if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
182             description=$(cat "${channels_dir}/${_channel}/description.txt")
183         else
184             description="This channel does not have a description.txt."
185         fi
186         echo -ne "    ${_channel}"
187         for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
188             echo -ne " "
189         done
190         echo -ne "${description}\n"
191     done
192 }
193
194
195 prepare_build() {
196     if [[ ${EUID} -ne 0 ]]; then
197         _msg_error "This script must be run as root." 1
198     fi
199     
200     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
201     [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
202
203     local mount
204     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
205         _msg_info "Unmounting ${mount}"
206         umount "${mount}"
207     done
208
209     # Check codename
210     if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
211         _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
212     fi
213
214 }
215
216
217 make_basefs() {
218     local debootstrap_status
219     statusfile="${cache_dir}/${codename}/status"
220
221     debootstrap_status() {
222         if [[ ! -d "$(dirname ${statusfile})" ]]; then
223             mkdir -p "$(dirname ${statusfile})"
224         fi
225         echo "${1}" > "${statusfile}"
226     }
227
228     if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
229         _msg_info "${codename} cache is found."
230     else
231         remove "${cache_dir}/${codename}"
232         debootstrap_status "Running"
233         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}/airootfs'..."
234         mkdir -p "${cache_dir}/${codename}/airootfs"
235         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}/airootfs" ${mirror}
236         _msg_info "${codename} installed successfully!"
237         debootstrap_status "Done"
238     fi
239     if [[ "${cache_only}" = true ]]; then
240         exit 0
241     fi
242     
243     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
244     _msg_info "copy base files from '${cache_dir}/${codename}/airootfs' to '${work_dir}/airootfs'..."
245     rsync  -au "${cache_dir}/${codename}/airootfs/" "${work_dir}/airootfs"
246     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
247     run_cmd apt-get update
248     # run_cmd apt-get upgrade
249 }
250
251 make_sourcelist() {
252     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
253     run_cmd apt-get update
254
255     if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
256         _apt_install gnupg
257
258         for repo in $(find ${channels_dir}/*/repo -name *.list); do
259             key="$(dirname ${repo})/$(basename ${repo} | sed "s/list/key/")"
260             
261             if [[ -f "${key}" ]]; then
262                 if $(file ${key} | grep -q "PGP/GPG key"); then
263                     cp ${key} ${work_dir}/airootfs/$(basename ${key})
264                 else
265                     wget -q -O ${work_dir}/airootfs/$(basename ${key}) $(cat ${key})
266                 fi
267
268                 run_cmd apt-key add $(basename ${key})
269                 rm ${work_dir}/airootfs/$(basename ${key})
270                 cp ${repo} ${work_dir}/airootfs/etc/apt/sources.list.d
271             fi
272         done
273
274         run_cmd apt-get update
275     fi
276     
277     # PPA
278     local PPA_FILELIST
279     local _PPA_FILE
280     local _ppa
281
282     PPA_FILELIST=("${channels_dir}/share/ppa_list.${arch}" "${channels_dir}/${channel_name}/ppa_list.${arch}")
283     _apt_install software-properties-common
284     for _PPA_FILE in ${PPA_FILELIST[@]}; do
285         if [[ -f "${_PPA_FILE}" ]]; then
286             for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
287                 run_cmd add-apt-repository --yes "${_ppa}"
288             done
289         fi
290     done
291 }
292
293 make_systemd() {
294     _apt_install systemd-sysv
295     run_cmd dbus-uuidgen > /etc/machine-id
296     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
297 }
298
299 make_apt_packages() {
300     remove "${work_dir}/airootfs/aptpkglist"
301
302     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
303         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
304     fi
305     if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
306         grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
307     fi
308     if [[ -n "$(echo $(<"${work_dir}/airootfs/aptpkglist"))" ]]; then
309         run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<aptpkglist))'
310     fi
311     remove "${work_dir}/airootfs/aptpkglist"
312 }
313
314 make_snap_packages() {
315     remove "${work_dir}/airootfs/snappkglist"
316
317     if [[ -f "${channels_dir}/share/snap-packages.${arch}" ]]; then
318         grep -h -v ^'#' "${channels_dir}/share/snap-packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/snappkglist"
319     fi
320     if [[ -f "${channels_dir}/${channel_name}/snap-packages.${arch}" ]]; then
321         grep -h -v ^'#' "${channels_dir}/${channel_name}/snap-packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/snappkglist"
322     fi
323     if [[ -n "$(echo $(<"${work_dir}/airootfs/snappkglist"))" ]]; then
324         _apt_install snapd
325         run_cmd env -i bash -c 'snap install $(echo $(<snappkglist))'
326     fi
327     remove "${work_dir}/airootfs/snappkglist"
328 }
329
330 make_config() {
331     # Locales
332     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive LANG=en_US.UTF-8 LC_ALL=C LANGUAGE=en_US.UTF-8 dpkg-reconfigure locales'
333
334     # resolvconf
335     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
336
337     # NetworkManager
338     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
339
340     # Timezone
341     #run_cmd echo -ne "UTC" > '/etc/timezone'
342     #run_cmd dpkg-reconfigure -f noninteractive tzdata
343
344     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
345     run_cmd truncate -s 0 /etc/machine-id
346 }
347
348 make_customize_airootfs() {
349     # Overwrite airootfs with customize_airootfs.
350     local copy_airootfs
351
352     copy_airootfs() {
353         local i 
354         for i in "${@}"; do
355             local _dir="${1%/}"
356             if [[ -d "${_dir}" ]]; then
357                 cp -af "${_dir}"/* "${work_dir}/airootfs"
358             fi
359         done
360     }
361
362     copy_airootfs "${channels_dir}/share/airootfs"
363     copy_airootfs "${channels_dir}/${channel_name}/airootfs"
364
365     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
366         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
367         run_cmd "/root/customize_airootfs.sh"
368     fi
369 }
370
371 make_clean() {
372     run_cmd apt-get clean
373     run_cmd apt-get --yes autoremove
374     run_cmd rm -rf "/tmp/* ~/.bash_history"
375 }
376
377 make_bootfiles() {
378     run_cmd update-initramfs -c -k all
379     _apt_install memtest86+
380     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
381     mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
382     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
383     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
384     cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
385
386     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
387         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
388     fi
389
390     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
391 }
392
393 make_grubcfg() {
394     touch "${bootfiles_dir}/ubuntu"
395     cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
396 }
397
398 make_manifest() {
399     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
400     cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
401     sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
402     sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
403     sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
404     sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
405     sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
406 }
407
408 make_squashfs() {
409     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
410     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
411 }
412
413 make_deifnes() {
414     cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
415 }
416
417 make_isolinux() {
418     if [[ "${arch}" = "amd64" ]]; then
419         local _arch="x86_64"
420     else
421         local _arch="${arch}"
422     fi
423     grub-mkstandalone \
424         --format=${_arch}-efi \
425         --output=isolinux/bootx64.efi \
426         --locales="" \
427         --fonts="" \
428         "boot/grub/grub.cfg=isolinux/grub.cfg"
429     (
430         cd isolinux && \
431         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
432         sudo mkfs.vfat efiboot.img && \
433         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
434         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
435     )
436     grub-mkstandalone \
437         --format=i386-pc \
438         --output=isolinux/core.img \
439         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
440         --modules="linux16 linux normal iso9660 biosdisk search" \
441         --locales="" \
442         --fonts="" \
443         "boot/grub/grub.cfg=isolinux/grub.cfg"
444     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
445 }
446
447 make_md5sum() {
448     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
449 }
450
451 make_iso() {
452     xorriso \
453         -as mkisofs \
454         -iso-level 3 \
455         -full-iso9660-filenames \
456         -volid "${iso_label}" \
457         -appid "${iso_application}" \
458         -publisher "${iso_publisher}" \
459         -preparer "prepared by LUBS" \
460         -eltorito-boot boot/grub/bios.img \
461         -no-emul-boot \
462         -boot-load-size 4 \
463         -boot-info-table \
464         --eltorito-catalog boot/grub/boot.cat \
465         --grub2-boot-info \
466         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
467         -eltorito-alt-boot \
468         -e EFI/efiboot.img \
469         -no-emul-boot \
470         -append_partition 2 0xef isolinux/efiboot.img \
471         -output "${out_dir}/${iso_filename}" \
472         -graft-points \
473             "." \
474             /boot/grub/bios.img=isolinux/bios.img \
475             /EFI/efiboot.img=isolinux/efiboot.img
476 }
477
478 make_checksum() {
479     cd ${out_dir}
480     _msg_info "Creating md5 checksum ..."
481     md5sum "${iso_filename}" > "${iso_filename}.md5"
482
483     _msg_info "Creating sha256 checksum ..."
484     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
485     cd - > /dev/null 2>&1
486 }
487
488
489 # 引数解析()
490 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
491
492 _opt_short="w:o:ha:-:m:c:d"
493 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
494 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
495 if [[ ${?} != 0 ]]; then
496     exit 1
497 fi
498 eval set -- "${OPT}"
499 while :; do
500     case ${1} in
501         -a | --arch)
502             if [[ -z ${2} ]]; then
503                 _msg_error "Please specify the architecture."
504                 exit 1
505             else
506                 arch="${2}"
507             fi
508             shift 2
509             ;;
510         -c | --codename)
511             if [[ -z ${2} ]]; then
512                 _msg_error "Please specify the codename."
513                 exit 1
514             else
515                 codename="${2}"
516             fi
517             shift 2
518             ;;
519         -d | --debug)
520             debug=true
521             shift 1
522             ;;
523         -h | --help)
524             _usage
525             exit 0
526             ;;
527         -m | --mirror)
528             if [[ -z ${2} ]]; then
529                 _msg_error "Please specify the mirror server."
530                 exit 1
531             else
532                 mirror="${2}"
533             fi
534             shift 2
535             ;;
536         -o | --out)
537             if [[ -z ${2} ]]; then
538                 _msg_error "Please specify the out dir."
539                 exit 1
540             fi
541             shift 2
542             ;;
543         -w | --work)
544             if [[ -z ${2} ]]; then
545                 _msg_error "Please specify the out dir."
546                 exit 1
547             else
548                 work_dir="${2}"
549             fi
550             shift 2
551             ;;
552         --cache-only)
553             cache_only=true
554             shift 1
555             ;;
556         --)
557             shift
558             break
559             ;;
560         *)
561             _msg_error "Invalid argument '${1}'"
562             _usage 1
563             ;;
564     esac
565 done
566
567 bootfiles_dir="${work_dir}/bootfiles"
568
569 if [[ -n "${1}" ]]; then
570     channel_name="${1}"
571
572     check_channel() {
573         local channel_list
574         local i
575         channel_list=()
576         for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
577             if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
578                 channel_list="${channel_list[@]} ${_channel}"
579             fi
580         done
581         for i in ${channel_list[@]}; do
582             if [[ "${i}" = "${channel_name}" ]]; then
583                 echo -n "true"
584                 return 0
585             fi
586         done
587         echo -n "false"
588         return 1
589     }
590
591     if [[ $(check_channel ${channel_name}) = false ]]; then
592         _msg_error "Invalid channel ${channel_name}"
593         exit 1
594     fi
595 fi
596
597
598 prepare_build
599 run_once make_basefs
600 run_once make_sourcelist
601 run_once make_systemd
602 run_once make_apt_packages
603 run_once make_snap_packages
604 run_once make_config
605 run_once make_customize_airootfs
606 run_once make_clean
607 run_once make_bootfiles
608 run_once make_grubcfg
609 run_once make_manifest
610 run_once make_squashfs
611 run_once make_deifnes
612 run_once run_bootfiles make_isolinux
613 run_once run_bootfiles make_md5sum
614 run_once run_bootfiles make_iso
615 run_once make_checksum