OSDN Git Service

[fix] : Fixed mount
[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     run_cmd dbus-uuidgen > /etc/machine-id
303     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
304 }
305
306 make_apt_packages() {
307     remove "${work_dir}/airootfs/aptpkglist"
308
309     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
310         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
311     fi
312     if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
313         grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
314     fi
315     if [[ -n "$(echo $(<"${work_dir}/airootfs/aptpkglist"))" ]]; then
316         run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<aptpkglist))'
317     fi
318     remove "${work_dir}/airootfs/aptpkglist"
319 }
320
321 make_snap_packages() {
322     remove "${work_dir}/airootfs/snappkglist"
323
324     if [[ -f "${channels_dir}/share/snap-packages.${arch}" ]]; then
325         grep -h -v ^'#' "${channels_dir}/share/snap-packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/snappkglist"
326     fi
327     if [[ -f "${channels_dir}/${channel_name}/snap-packages.${arch}" ]]; then
328         grep -h -v ^'#' "${channels_dir}/${channel_name}/snap-packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/snappkglist"
329     fi
330     if [[ -n "$(echo $(<"${work_dir}/airootfs/snappkglist"))" ]]; then
331         _apt_install snapd
332         run_cmd env -i bash -c 'snap install $(echo $(<snappkglist))'
333     fi
334     remove "${work_dir}/airootfs/snappkglist"
335 }
336
337 make_config() {
338     # Locales
339     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'
340
341     # resolvconf
342     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
343
344     # NetworkManager
345     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
346
347     # Timezone
348     #run_cmd echo -ne "UTC" > '/etc/timezone'
349     #run_cmd dpkg-reconfigure -f noninteractive tzdata
350
351     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
352     run_cmd truncate -s 0 /etc/machine-id
353 }
354
355 make_customize_airootfs() {
356     #-- Overwrite airootfs files --#
357     local copy_airootfs
358     copy_airootfs() {
359         local _dir="${1%/}"
360         if [[ -d "${_dir}" ]]; then
361             cp -af "${_dir}"/* "${work_dir}/airootfs"
362         fi
363     }
364     copy_airootfs "${channels_dir}/share/airootfs"
365     copy_airootfs "${channels_dir}/${channel_name}/airootfs"
366
367
368     #-- Run scripts --#
369     local _customize_siript_options="-a ${arch} -u '${username}' -p '${password}' -s '${usershell}'"
370     if ${bash_debug}; then
371         _customize_siript_options="$_customize_siript_options -x"
372     elif ${debug}; then
373         _customize_siript_options="$_customize_siript_options -d"
374     fi
375
376     # run_customize_script <script path from lubs> <script path in chroot>
377     local run_customize_script
378     run_customize_script() {
379         if [[ -f "${1}" ]]; then
380             chmod 755 "${1}"
381             run_cmd "${2} ${_customize_siript_options}"
382         fi
383     }
384     run_customize_script "${work_dir}/airootfs/root/customize_airootfs.sh" "/root/customize_airootfs.sh"
385     run_customize_script "${work_dir}/airootfs/root/customize_airootfs_${channel_name}.sh"  "/root/customize_airootfs_${channel_name}.sh"
386 }
387
388 make_clean() {
389     run_cmd apt-get clean
390     run_cmd apt-get --yes autoremove
391     run_cmd rm -rf "/tmp/* ~/.bash_history"
392 }
393
394 make_bootfiles() {
395     run_cmd env -i bash -c 'update-initramfs -c -k all'
396     _apt_install memtest86+
397     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
398     mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
399     cp "${work_dir}/airootfs/boot/vmlinuz" "${bootfiles_dir}/casper/vmlinuz"
400     cp "${work_dir}/airootfs/boot/initrd.img" "${bootfiles_dir}/casper/initrd"
401     cp "${work_dir}/airootfs/boot/memtest86+.bin" "${bootfiles_dir}/install/memtest86+"
402
403     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
404         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
405     fi
406
407     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
408 }
409
410 make_grubcfg() {
411     touch "${bootfiles_dir}/ubuntu"
412     cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
413 }
414
415 make_manifest() {
416     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
417     cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
418     sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
419     sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
420     sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
421     sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
422     sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
423 }
424
425 make_squashfs() {
426     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
427     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
428 }
429
430 make_deifnes() {
431     cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
432 }
433
434 make_isolinux() {
435     if [[ "${arch}" = "amd64" ]]; then
436         local _arch="x86_64"
437     else
438         local _arch="${arch}"
439     fi
440     grub-mkstandalone \
441         --format=${_arch}-efi \
442         --output=isolinux/bootx64.efi \
443         --locales="" \
444         --fonts="" \
445         "boot/grub/grub.cfg=isolinux/grub.cfg"
446     (
447         cd isolinux && \
448         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
449         sudo mkfs.vfat efiboot.img && \
450         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
451         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
452     )
453     grub-mkstandalone \
454         --format=i386-pc \
455         --output=isolinux/core.img \
456         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
457         --modules="linux16 linux normal iso9660 biosdisk search" \
458         --locales="" \
459         --fonts="" \
460         --themes="" \
461         "boot/grub/grub.cfg=isolinux/grub.cfg"
462     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
463 }
464
465 make_md5sum() {
466     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
467 }
468
469 make_iso() {
470     xorriso \
471         -as mkisofs \
472         -iso-level 3 \
473         -full-iso9660-filenames \
474         -volid "${iso_label}" \
475         -appid "${iso_application}" \
476         -publisher "${iso_publisher}" \
477         -preparer "prepared by LUBS" \
478         -eltorito-boot boot/grub/bios.img \
479         -no-emul-boot \
480         -boot-load-size 4 \
481         -boot-info-table \
482         --eltorito-catalog boot/grub/boot.cat \
483         --grub2-boot-info \
484         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
485         -eltorito-alt-boot \
486         -e EFI/efiboot.img \
487         -no-emul-boot \
488         -append_partition 2 0xef isolinux/efiboot.img \
489         -output "${out_dir}/${iso_filename}" \
490         -graft-points \
491             "." \
492             /boot/grub/bios.img=isolinux/bios.img \
493             /EFI/efiboot.img=isolinux/efiboot.img
494 }
495
496 make_checksum() {
497     cd ${out_dir}
498     _msg_info "Creating md5 checksum ..."
499     md5sum "${iso_filename}" > "${iso_filename}.md5"
500
501     _msg_info "Creating sha256 checksum ..."
502     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
503     cd - > /dev/null 2>&1
504 }
505
506
507 # 引数解析()
508 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
509
510 _opt_short="w:o:ha:-:m:c:d"
511 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
512 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
513 if [[ ${?} != 0 ]]; then
514     exit 1
515 fi
516 eval set -- "${OPT}"
517 while :; do
518     case ${1} in
519         -a | --arch)
520             if [[ -z ${2} ]]; then
521                 _msg_error "Please specify the architecture."
522                 exit 1
523             else
524                 arch="${2}"
525             fi
526             shift 2
527             ;;
528         -c | --codename)
529             if [[ -z ${2} ]]; then
530                 _msg_error "Please specify the codename."
531                 exit 1
532             else
533                 codename="${2}"
534             fi
535             shift 2
536             ;;
537         -d | --debug)
538             debug=true
539             shift 1
540             ;;
541         -h | --help)
542             _usage
543             exit 0
544             ;;
545         -m | --mirror)
546             if [[ -z ${2} ]]; then
547                 _msg_error "Please specify the mirror server."
548                 exit 1
549             else
550                 mirror="${2}"
551             fi
552             shift 2
553             ;;
554         -o | --out)
555             if [[ -z ${2} ]]; then
556                 _msg_error "Please specify the out dir."
557                 exit 1
558             fi
559             shift 2
560             ;;
561         -w | --work)
562             if [[ -z ${2} ]]; then
563                 _msg_error "Please specify the out dir."
564                 exit 1
565             else
566                 work_dir="${2}"
567             fi
568             shift 2
569             ;;
570         --cache-only)
571             cache_only=true
572             shift 1
573             ;;
574         --)
575             shift
576             break
577             ;;
578         *)
579             _msg_error "Invalid argument '${1}'"
580             _usage 1
581             ;;
582     esac
583 done
584
585 bootfiles_dir="${work_dir}/bootfiles"
586 trap  umount_chroot 0 2 15
587
588 if [[ -n "${1}" ]]; then
589     channel_name="${1}"
590
591     check_channel() {
592         local channel_list
593         local i
594         channel_list=()
595         for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
596             if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
597                 channel_list="${channel_list[@]} ${_channel}"
598             fi
599         done
600         for i in ${channel_list[@]}; do
601             if [[ "${i}" = "${channel_name}" ]]; then
602                 echo -n "true"
603                 return 0
604             fi
605         done
606         echo -n "false"
607         return 1
608     }
609
610     if [[ $(check_channel ${channel_name}) = false ]]; then
611         _msg_error "Invalid channel ${channel_name}"
612         exit 1
613     fi
614 fi
615
616
617 prepare_build
618 run_once make_basefs
619 run_once make_sourcelist
620 run_once make_systemd
621 run_once make_apt_packages
622 #run_once make_snap_packages
623 run_once make_config
624 run_once make_customize_airootfs
625 run_once make_clean
626 run_once make_bootfiles
627 run_once make_grubcfg
628 run_once make_manifest
629 run_once make_squashfs
630 run_once make_deifnes
631 run_once run_bootfiles make_isolinux
632 run_once run_bootfiles make_md5sum
633 run_once run_bootfiles make_iso
634 run_once make_checksum