8 script_path=$(readlink -f ${0%/*})
10 cache_dir="${script_path}/cache"
11 channels_dir="${script_path}/channels"
12 isolinux_dir="${script_path}/isolinux"
15 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
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"
34 start_time="$(date +%s)"
37 if [[ "${debug}" = true ]]; then
38 local _current_time="$(date +%s)"
41 _time="$(("${_current_time}"-"${start_time}"))"
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]")"
48 echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LUBS Core]")"
51 "${script_path}/echo_color" -t 6 "[LUBS Core]"
55 # Show an INFO message
59 echo "$(_msg_common) $("${script_path}/echo_color" -t 2 "Info:") ${_msg}"
62 # Show an debug message
63 # _msg_debug <message>
65 if [[ "${debug}" = true ]]; then
67 echo "$(_msg_common) $("${script_path}/echo_color" -t 3 "Debug:") ${_msg}"
71 # Show an ERROR message then exit with status
72 # _msg_error <message> <exit code>
76 echo "$(_msg_common) $("${script_path}/echo_color" -t 1 "Error:") ${_msg}"
77 if [[ ! ${_error} = 0 ]]; then
85 for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir})); do
86 _msg_info "Unmounting ${mount}"
91 # Helper function to run make_*() only one time.
96 if [[ "run_bootfiles" == "$1" ]]; then
102 if [[ ! -e "${work_dir}/build.${name}" ]]; then
103 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
105 if [[ "run_bootfiles" == "$1" ]]; then
111 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
112 touch "${work_dir}/build.${name}"
117 arch-chroot "${work_dir}/airootfs" "${@}"
121 cd "${bootfiles_dir}"
127 run_cmd apt-get --no-install-recommends --yes install ${@}
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> ...
139 for _file in "${_list[@]}"; do
140 _msg_debug "Removeing ${_file}"
141 if [[ -f ${_file} ]]; then
143 elif [[ -d ${_file} ]]; then
151 echo "usage ${0} [options] [channel]"
153 echo " General options:"
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}"
166 echo " -d | --debug "
167 echo " -h | --help This help message and exit"
169 echo "You can switch between installed packages, files included in images, etc. by channel."
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}"
180 for _channel in ${channel_list[@]}; do
181 if [[ -f "${channels_dir}/${_channel}/description.txt" ]]; then
182 description=$(cat "${channels_dir}/${_channel}/description.txt")
184 description="This channel does not have a description.txt."
186 echo -ne " ${_channel}"
187 for i in $( seq 1 $(( 23 - ${#_channel} )) ); do
190 echo -ne "${description}\n"
196 if [[ ${EUID} -ne 0 ]]; then
197 _msg_error "This script must be run as root." 1
200 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
201 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
204 for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
205 _msg_info "Unmounting ${mount}"
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})."
218 local debootstrap_status
219 statusfile="${cache_dir}/${codename}/status"
221 debootstrap_status() {
222 if [[ ! -d "$(dirname ${statusfile})" ]]; then
223 mkdir -p "$(dirname ${statusfile})"
225 echo "${1}" > "${statusfile}"
228 if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
229 _msg_info "${codename} cache is found."
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"
239 if [[ "${cache_only}" = true ]]; then
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
252 cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
253 run_cmd apt-get update
255 if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
258 for repo in $(find ${channels_dir}/*/repo -name *.list); do
259 key="$(dirname ${repo})/$(basename ${repo} | sed "s/list/key/")"
261 if [[ -f "${key}" ]]; then
262 if $(file ${key} | grep -q "PGP/GPG key"); then
263 cp ${key} ${work_dir}/airootfs/$(basename ${key})
265 wget -q -O ${work_dir}/airootfs/$(basename ${key}) $(cat ${key})
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
274 run_cmd apt-get update
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}"
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
299 make_apt_packages() {
300 remove "${work_dir}/airootfs/aptpkglist"
302 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
303 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/aptpkglist"
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"
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))'
311 remove "${work_dir}/airootfs/aptpkglist"
314 make_snap_packages() {
315 remove "${work_dir}/airootfs/snappkglist"
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"
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"
323 if [[ -n "$(echo $(<"${work_dir}/airootfs/snappkglist"))" ]]; then
325 run_cmd env -i bash -c 'snap install $(echo $(<snappkglist))'
327 remove "${work_dir}/airootfs/snappkglist"
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'
335 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
338 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
341 #run_cmd echo -ne "UTC" > '/etc/timezone'
342 #run_cmd dpkg-reconfigure -f noninteractive tzdata
344 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
345 run_cmd truncate -s 0 /etc/machine-id
348 make_customize_airootfs() {
349 # Overwrite airootfs with customize_airootfs.
356 if [[ -d "${_dir}" ]]; then
357 cp -af "${_dir}"/* "${work_dir}/airootfs"
362 copy_airootfs "${channels_dir}/share/airootfs"
363 copy_airootfs "${channels_dir}/${channel_name}/airootfs"
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"
372 run_cmd apt-get clean
373 run_cmd apt-get --yes autoremove
374 run_cmd rm -rf "/tmp/* ~/.bash_history"
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+
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
390 (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
394 touch "${bootfiles_dir}/ubuntu"
395 cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
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"
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
414 cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
418 if [[ "${arch}" = "amd64" ]]; then
421 local _arch="${arch}"
424 --format=${_arch}-efi \
425 --output=isolinux/bootx64.efi \
428 "boot/grub/grub.cfg=isolinux/grub.cfg"
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/
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" \
443 "boot/grub/grub.cfg=isolinux/grub.cfg"
444 cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
448 /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
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 \
464 --eltorito-catalog boot/grub/boot.cat \
466 --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
470 -append_partition 2 0xef isolinux/efiboot.img \
471 -output "${out_dir}/${iso_filename}" \
474 /boot/grub/bios.img=isolinux/bios.img \
475 /EFI/efiboot.img=isolinux/efiboot.img
480 _msg_info "Creating md5 checksum ..."
481 md5sum "${iso_filename}" > "${iso_filename}.md5"
483 _msg_info "Creating sha256 checksum ..."
484 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
485 cd - > /dev/null 2>&1
490 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
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
502 if [[ -z ${2} ]]; then
503 _msg_error "Please specify the architecture."
511 if [[ -z ${2} ]]; then
512 _msg_error "Please specify the codename."
528 if [[ -z ${2} ]]; then
529 _msg_error "Please specify the mirror server."
537 if [[ -z ${2} ]]; then
538 _msg_error "Please specify the out dir."
544 if [[ -z ${2} ]]; then
545 _msg_error "Please specify the out dir."
561 _msg_error "Invalid argument '${1}'"
567 bootfiles_dir="${work_dir}/bootfiles"
569 if [[ -n "${1}" ]]; then
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}"
581 for i in ${channel_list[@]}; do
582 if [[ "${i}" = "${channel_name}" ]]; then
591 if [[ $(check_channel ${channel_name}) = false ]]; then
592 _msg_error "Invalid channel ${channel_name}"
600 run_once make_sourcelist
601 run_once make_systemd
602 run_once make_apt_packages
603 run_once make_snap_packages
605 run_once make_customize_airootfs
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