2 # SPDX-License-Identifier: GPL-3.0
6 # Email : m.k419sabuaka@gmail.com
10 # Email : hayao@fascode.net
12 # (c) 2019-2020 Fascode Network.
22 script_path=$(readlink -f "${0%/*}")
24 cache_dir="${script_path}/cache"
25 channels_dir="${script_path}/channels"
26 nfb_dir="${script_path}/nfb"
29 mirror="http://linux.yz.yamagata-u.ac.jp/ubuntu/"
34 work_dir="${script_path}/work"
35 out_dir="${script_path}/out"
36 iso_label="${os_name}_${codename}_${arch}"
37 iso_publisher='Fascode Network <https://fascode.net>'
38 iso_application="${os_name} Live/Rescue CD"
39 iso_version="${codename}-$(date +%Y.%m.%d)"
50 start_time="$(date +%s)"
53 if [[ "${debug}" = true ]]; then
56 _current_time="$(date +%s)"
57 _time="$(("${_current_time}"-"${start_time}"))"
59 if [[ "${_time}" -ge 3600 ]]; then
60 echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
61 elif [[ "${_time}" -ge 60 ]]; then
62 echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
64 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LUBS Core]")"
67 "${script_path}/echo_color" -t 6 "[LUBS Core]"
71 # Show an INFO message
76 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
79 # Show an debug message
80 # _msg_debug <message>
82 if [[ "${debug}" = true ]]; then
85 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
89 # Show an ERROR message then exit with status
90 # _msg_error <message> <exit code>
96 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
98 if [[ ! ${_error} = 0 ]]; then
107 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
108 _msg_info "Unmounting ${mount}"
109 umount -fl "${mount}"
113 # Helper function to run make_*() only one time.
119 if [[ ! -e "${work_dir}/build.${name}" ]]; then
120 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
122 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
123 touch "${work_dir}/build.${name}"
130 for mount in "dev" "dev/pts" "proc" "sys" "run/systemd/resolve/stub-resolv.conf"; do
131 if [[ "${mount}" == "run/systemd/resolve/stub-resolv.conf" ]]; then
132 mount --bind /etc/resolv.conf "${work_dir}/airootfs/${mount}"
134 mount --bind /${mount} "${work_dir}/airootfs/${mount}"
138 chroot "${work_dir}/airootfs" "${@}"
140 for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
141 umount -fl "${mount}"
146 run_cmd apt-get --no-install-recommends --yes install ${@}
150 # Delete the file if it exists.
151 # For directories, rm -rf is used.
152 # If the file does not exist, skip it.
153 # remove <file> <file> ...
159 for _file in "${_list[@]}"; do
160 _msg_debug "Removeing ${_file}"
162 if [[ -f ${_file} ]]; then
164 elif [[ -d ${_file} ]]; then
172 echo "usage ${0} [options] [channel]"
174 echo " General options:"
176 echo " -a | --arch <str> Set architecture"
177 echo " Default: ${arch}"
178 echo " -c | --codename <str> Set ubuntu codename"
179 echo " Default: ${codename}"
180 echo " -m | --mirror <url> Set apt mirror server."
181 echo " Default: ${mirror}"
182 echo " -o | --out <out_dir> Set the output directory"
183 echo " Default: ${out_dir}"
184 echo " -w | --work <work_dir> Set the working directory"
185 echo " Default: ${work_dir}"
187 echo " -d | --debug "
188 echo " -h | --help This help message and exit"
190 echo "You can switch between installed packages, files included in images, etc. by channel."
198 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
199 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
200 channel_list+=( "${_channel}" )
204 for _channel in ${channel_list[@]}; do
205 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
206 description=$(cat "${channels_dir}/${_channel}/description.txt")
208 description="This channel does not have a description.txt."
211 echo -ne " ${_channel}"
213 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
217 echo -ne "${description}\n"
223 if [[ ${EUID} -ne 0 ]]; then
224 msg_warn "This script must be run as root." >&2
225 msg_warn "Re-run 'sudo ${0} ${@}'"
230 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
231 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
234 # Load channel config
237 [[ -f "${1}" ]] && source "${1}"
239 _load_config "${channels_dir}/${channel_name}/config.any"
240 _load_config "${channels_dir}/${channel_name}/config.${arch}"
243 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
244 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
248 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
254 local debootstrap_status
255 statusfile="${cache_dir}/${codename}/status"
257 debootstrap_status() {
258 if [[ ! -d "$(dirname ${statusfile})" ]]; then
259 mkdir -p "$(dirname ${statusfile})"
261 echo "${1}" > "${statusfile}"
264 if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
265 _msg_info "${codename} cache is found."
267 remove "${cache_dir}/${codename}"
268 debootstrap_status "Running"
269 _msg_info "Installing Ubuntu to '${cache_dir}/${codename}/airootfs'..."
270 mkdir -p "${cache_dir}/${codename}/airootfs"
271 debootstrap --arch="${arch}" --verbose --merged-usr "${codename}" "${cache_dir}/${codename}/airootfs" "${mirror}"
272 _msg_info "${codename} installed successfully!"
273 debootstrap_status "Done"
276 if [[ "${cache_only}" = true ]]; then
280 rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
281 _msg_info "copy base files from '${cache_dir}/${codename}/airootfs' to '${work_dir}/airootfs'..."
282 rsync -au "${cache_dir}/${codename}/airootfs/" "${work_dir}/airootfs"
283 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
284 run_cmd apt-get update
285 # run_cmd apt-get upgrade
289 cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
290 sed -i "s@http://archive.ubuntu.com/ubuntu/@${mirror}@g" ${work_dir}/airootfs/etc/apt/sources.list
291 run_cmd apt-get update
293 if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
296 for repo in $(find ${channels_dir}/*/repo -name '*.list'); do
297 key="$(dirname ${repo})/$(basename ${repo} | sed "s/list/key/")"
299 if [[ -f "${key}" ]]; then
300 if file ${key} | grep -q "PGP/GPG key"; then
301 cp "${key}" "${work_dir}/airootfs/$(basename ${key})"
303 wget -q -O "${work_dir}/airootfs/$(basename ${key})" "$(cat ${key})"
306 run_cmd apt-key add "$(basename ${key})"
307 rm "${work_dir}/airootfs/$(basename ${key})"
308 cp "${repo}" "${work_dir}/airootfs/etc/apt/sources.list.d"
311 run_cmd apt-get update
318 PPA_FILELIST=("${channels_dir}/share/ppa_list.${arch}" "${channels_dir}/${channel_name}/ppa_list.${arch}")
319 _apt_install software-properties-common
321 for _PPA_FILE in ${PPA_FILELIST[@]}; do
322 if [[ -f "${_PPA_FILE}" ]]; then
323 for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
324 run_cmd add-apt-repository --yes "${_ppa}"
331 _apt_install systemd-sysv
332 run_cmd dbus-uuidgen --ensure=/etc/machine-id
333 run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
336 make_apt_packages() {
337 remove "${work_dir}/airootfs/aptpkglist"
338 _apt_install initramfs-tools
339 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
341 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
342 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
345 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
346 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
349 if [[ -s "${work_dir}/airootfs/aptpkglist" ]]; then
350 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<aptpkglist))'
353 remove "${work_dir}/airootfs/aptpkglist"
358 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'
361 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
364 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
367 #run_cmd echo -ne "UTC" > '/etc/timezone'
368 #run_cmd dpkg-reconfigure -f noninteractive tzdata
370 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
371 run_cmd truncate -s 0 /etc/machine-id
375 sed "s|%USERNAME%|${username}|g;s|%OS_NAME%|${os_name}|g" "${nfb_dir}/casper.conf" > "${work_dir}/airootfs/etc/casper.conf"
376 chmod 755 "${work_dir}"/airootfs/usr/share/initramfs-tools/scripts/casper-bottom/*adduser "${work_dir}"/airootfs/usr/share/initramfs-tools/scripts/casper-bottom/*autologin
377 run_cmd env -i bash -c 'mkinitramfs -o /boot/initrd.img-`uname -r` `uname -r`'
381 sed -i "s@${mirror}@http://archive.ubuntu.com/ubuntu/@g" ${work_dir}/airootfs/etc/apt/sources.list
382 run_cmd apt-get update
383 run_cmd apt-get clean
384 run_cmd apt-get --yes autoremove
385 run_cmd rm -rf "/tmp/* ~/.bash_history"
390 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
391 mkdir -p "${bootfiles_dir}"/{casper,isolinux,install}
394 mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
399 touch "${bootfiles_dir}/ubuntu"
400 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/README.diskdefines" > "${bootfiles_dir}/README.diskdefines"
403 cp "${work_dir}/airootfs/boot/vmlinuz" "${bootfiles_dir}/casper/vmlinuz"
404 cp "${work_dir}/airootfs/boot/initrd.img" "${bootfiles_dir}/casper/initrd"
405 run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' > ${bootfiles_dir}/casper/filesystem.manifest
406 cp -v "${bootfiles_dir}/casper/filesystem.manifest" "${bootfiles_dir}/casper/filesystem.manifest-desktop"
407 sed -i '/casper/d;/discover/d;/laptop-detect/d;/os-prober/d;/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
408 printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > "${bootfiles_dir}/casper/filesystem.size"
411 cp "${work_dir}"/airootfs/usr/lib/syslinux/modules/bios/*.c32 "${bootfiles_dir}/isolinux/"
412 cp "${work_dir}/airootfs/usr/lib/ISOLINUX/isolinux.bin" "${bootfiles_dir}/isolinux/"
413 cp "${work_dir}/airootfs/usr/lib/ISOLINUX/isohdpfx.bin" "${bootfiles_dir}/isolinux/"
414 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" > "${bootfiles_dir}/isolinux/grub.cfg"
415 sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" > "${bootfiles_dir}/isolinux/isolinux.cfg"
418 _apt_install memtest86+
419 cp "${work_dir}/airootfs/boot/memtest86+.bin" "${bootfiles_dir}/install/memtest86+"
422 if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
423 wget -O "${cache_dir}/memtest86-usb.zip" "https://www.memtest86.com/downloads/memtest86-usb.zip"
424 bash -c "(unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${cache_dir}/memtest86)"
427 cp "${cache_dir}/memtest86" "${bootfiles_dir}/install/memtest86"
434 --output="${bootfiles_dir}/isolinux/bootia32.efi" \
437 "boot/grub/grub.cfg=${bootfiles_dir}/isolinux/grub.cfg"
441 --format=x86_64-efi \
442 --output="${bootfiles_dir}/isolinux/bootx64.efi" \
445 "boot/grub/grub.cfg=${bootfiles_dir}/isolinux/grub.cfg"
448 truncate -s 50M "${bootfiles_dir}/isolinux/efiboot.img"
449 mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/isolinux/efiboot.img"
450 mount "${bootfiles_dir}/isolinux/efiboot.img" "${bootfiles_dir}/mnt"
451 mkdir -p "${bootfiles_dir}/mnt/efi/boot"
452 cp "${bootfiles_dir}/isolinux/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot/"
453 cp "${bootfiles_dir}/isolinux/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot/"
454 umount -d "${bootfiles_dir}/mnt"
455 rm -r "${bootfiles_dir}/mnt"
459 cd "${bootfiles_dir}"
461 # create checksum (using at booting)
462 bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
468 -full-iso9660-filenames \
469 -volid "${iso_label}" \
470 -appid "${iso_application}" \
471 -publisher "${iso_publisher}" \
472 -preparer "prepared by LUBS" \
473 -eltorito-boot isolinux/isolinux.bin \
478 -eltorito-platform efi \
479 -eltorito-boot EFI/efiboot.img \
481 -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
482 -isohybrid-gpt-basdat \
483 -eltorito-catalog isolinux/boot.cat \
484 -output "${out_dir}/${iso_filename}" \
487 "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
488 "/EFI/efiboot.img=isolinux/efiboot.img"
495 _msg_info "Creating md5 checksum ..."
496 md5sum "${iso_filename}" > "${iso_filename}.md5"
498 _msg_info "Creating sha256 checksum ..."
499 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
500 cd - > /dev/null 2>&1
505 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
507 _opt_short="w:o:ha:-:m:c:d"
508 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work,cache-only"
509 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
511 if [[ ${?} != 0 ]]; then
520 if [[ -z ${2} ]]; then
521 _msg_error "Please specify the architecture."
529 if [[ -z ${2} ]]; then
530 _msg_error "Please specify the codename."
546 if [[ -z ${2} ]]; then
547 _msg_error "Please specify the mirror server."
556 if [[ -z ${2} ]]; then
557 _msg_error "Please specify the out dir."
566 if [[ -z ${2} ]]; then
567 _msg_error "Please specify the out dir."
584 _msg_error "Invalid argument '${1}'"
590 bootfiles_dir="${work_dir}/bootfiles"
591 trap umount_chroot 0 2 15
593 if [[ -n "${1}" ]]; then
601 for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
602 if [[ -n $(ls "${channels_dir}/${_channel}") ]] && [[ ! "${_channel}" = "share" ]]; then
603 channel_list+=( "${_channel}" )
607 for i in ${channel_list[@]}; do
608 if [[ "${i}" = "${channel_name}" ]]; then
618 if [[ $(check_channel ${channel_name}) = false ]]; then
619 _msg_error "Invalid channel ${channel_name}"
627 run_once make_sourcelist
628 run_once make_systemd
629 run_once make_apt_packages
631 run_once make_add_user
633 run_once make_squashfs
637 run_once make_checksum