OSDN Git Service

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