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"
33 # Show an INFO message
37 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 32 " INFO:" "/#" -t 0 "${_msg}"
40 # Show an debug message
41 # _msg_debug <message>
43 if [[ "${debug}" = true ]]; then
45 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 35 "Debug:" "/#" -t 0 "${_msg}"
49 # Show an ERROR message then exit with status
50 # _msg_error <message> <exit code>
54 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 31 "ERROR:" "/#" -t 0 "${_msg}" >&2
55 if [[ ! ${_error} = 0 ]]; then
60 # Helper function to run make_*() only one time.
64 if [[ "run_bootfiles" == "$1" ]]; then
70 if [[ ! -e "${work_dir}/build.${name}" ]]; then
71 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
73 if [[ "run_bootfiles" == "$1" ]]; then
79 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
80 touch "${work_dir}/build.${name}"
85 arch-chroot "${work_dir}/airootfs" "${@}"
95 run_cmd apt-get --no-install-recommends --yes install ${@}
99 # Delete the file if it exists.
100 # For directories, rm -rf is used.
101 # If the file does not exist, skip it.
102 # remove <file> <file> ...
107 for _file in "${_list[@]}"; do
108 _msg_debug "Removeing ${_file}"
109 if [[ -f ${_file} ]]; then
111 elif [[ -d ${_file} ]]; then
119 echo "usage ${0} [options] [channel]"
121 echo " General options:"
123 echo " -a | --arch <str> Set architecture"
124 echo " Default: ${arch}"
125 echo " -c | --codename <str> Set ubuntu codename"
126 echo " Default: ${codename}"
127 echo " -m | --mirror <url> Set apt mirror server."
128 echo " Default: ${mirror}"
129 echo " -o | --out <out_dir> Set the output directory"
130 echo " Default: ${out_dir}"
131 echo " -w | --work <work_dir> Set the working directory"
132 echo " Default: ${work_dir}"
134 echo " -d | --debug "
135 echo " -h | --help This help message and exit"
141 if [[ ${EUID} -ne 0 ]]; then
142 _msg_error "This script must be run as root." 1
145 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
146 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
149 for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
150 _msg_info "Unmounting ${mount}"
155 if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
156 _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
163 local debootstrap_status
164 statusfile="${cache_dir}/${codename}/status"
166 debootstrap_status() {
167 if [[ ! -d "$(dirname ${statusfile})" ]]; then
168 mkdir -p "$(dirname ${statusfile})"
170 echo "${1}" > "${statusfile}"
173 if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
174 _msg_info "${codename} cache is found."
176 remove "${cache_dir}/${codename}"
177 debootstrap_status "Running"
178 _msg_info "Installing Ubuntu to '${cache_dir}/${codename}/airootfs'..."
179 mkdir -p "${cache_dir}/${codename}/airootfs"
180 debootstrap --arch=${arch} --include=linux-image-generic --verbose --merged-usr "${codename}" "${cache_dir}/${codename}/airootfs" ${mirror}
181 _msg_info "${codename} installed successfully!"
182 debootstrap_status "Done"
185 rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
186 _msg_info "copy base files from '${cache_dir}/${codename}/airootfs' to '${work_dir}/airootfs'..."
187 rsync -au "${cache_dir}/${codename}/airootfs/" "${work_dir}/airootfs"
188 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
189 run_cmd apt-get update
190 # run_cmd apt-get upgrade
194 cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
195 run_cmd apt-get update
197 if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
200 for repo in $(find ${channels_dir}/*/repo -name *.list); do
201 key="$(dirname $repo)/$(basename $repo | sed "s/list/key/")"
203 if [[ -f "$key" ]]; then
204 if $(file $key | grep -q "PGP/GPG key"); then
205 cp $key ${work_dir}/airootfs/$(basename $key)
207 wget -q -O ${work_dir}/airootfs/$(basename $key) $(cat $key)
210 run_cmd apt-key add $(basename $key)
211 rm ${work_dir}/airootfs/$(basename $key)
212 cp $repo ${work_dir}/airootfs/etc/apt/sources.list.d
216 run_cmd apt-get update
224 PPA_FILELIST=("${channels_dir}/share/ppa_list.${arch}" "${channels_dir}/${channel_name}/ppa_list.${arch}")
225 _apt_install software-properties-common
226 for _PPA_FILE in ${PPA_FILELIST[@]}; do
227 if [[ -f "${_PPA_FILE}" ]]; then
228 for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
229 run_cmd add-apt-repository --yes "${_ppa}"
236 _apt_install systemd-sysv
237 run_cmd dbus-uuidgen > /etc/machine-id
238 run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
242 remove "${work_dir}/airootfs/installpkglist"
244 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
245 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
247 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
248 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
250 if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
251 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
257 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'
260 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
263 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
266 #run_cmd echo -ne "UTC" > '/etc/timezone'
267 #run_cmd dpkg-reconfigure -f noninteractive tzdata
269 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
270 run_cmd truncate -s 0 /etc/machine-id
273 make_customize_airootfs() {
274 # Overwrite airootfs with customize_airootfs.
281 if [[ -d "${_dir}" ]]; then
282 cp -af "${_dir}"/* "${work_dir}/airootfs"
287 copy_airootfs "${channels_dir}/share/airootfs"
288 copy_airootfs "${channels_dir}/${channel_name}/airootfs"
290 if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
291 chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
292 run_cmd "/root/customize_airootfs.sh"
297 run_cmd apt-get clean
298 run_cmd apt-get --yes autoremove
299 run_cmd rm -rf "/tmp/* ~/.bash_history"
303 run_cmd update-initramfs -c -k all
304 _apt_install memtest86+
305 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
306 mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
307 cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
308 cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
309 cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
311 if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
312 wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
315 (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
319 touch "${bootfiles_dir}/ubuntu"
320 cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
324 run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
325 cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
326 sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
327 sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
328 sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
329 sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
330 sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
334 mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
335 printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
339 cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
343 if [[ "${arch}" = "amd64" ]]; then
346 local _arch="${arch}"
349 --format=${_arch}-efi \
350 --output=isolinux/bootx64.efi \
353 "boot/grub/grub.cfg=isolinux/grub.cfg"
356 dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
357 sudo mkfs.vfat efiboot.img && \
358 LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
359 LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
363 --output=isolinux/core.img \
364 --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
365 --modules="linux16 linux normal iso9660 biosdisk search" \
368 "boot/grub/grub.cfg=isolinux/grub.cfg"
369 cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
373 /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
380 -full-iso9660-filenames \
381 -volid "${iso_label}" \
382 -appid "${iso_application}" \
383 -publisher "${iso_publisher}" \
384 -preparer "prepared by LUBS" \
385 -eltorito-boot boot/grub/bios.img \
389 --eltorito-catalog boot/grub/boot.cat \
391 --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
395 -append_partition 2 0xef isolinux/efiboot.img \
396 -output "${out_dir}/${iso_filename}" \
399 /boot/grub/bios.img=isolinux/bios.img \
400 /EFI/efiboot.img=isolinux/efiboot.img
405 _msg_info "Creating md5 checksum ..."
406 md5sum "${iso_filename}" > "${iso_filename}.md5"
408 _msg_info "Creating sha256 checksum ..."
409 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
410 cd - > /dev/null 2>&1
415 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
417 _opt_short="w:o:ha:-:m:c:d"
418 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
419 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
420 if [[ ${?} != 0 ]]; then
427 if [[ -z ${2} ]]; then
428 _msg_error "Please specify the architecture."
436 if [[ -z ${2} ]]; then
437 _msg_error "Please specify the codename."
453 if [[ -z ${2} ]]; then
454 _msg_error "Please specify the mirror server."
462 if [[ -z ${2} ]]; then
463 _msg_error "Please specify the out dir."
471 if [[ -z ${2} ]]; then
472 _msg_error "Please specify the out dir."
484 _msg_error "Invalid argument '${1}'"
490 bootfiles_dir="${work_dir}/bootfiles"
495 run_once make_sourcelist
496 run_once make_systemd
497 run_once make_packages
499 run_once make_customize_airootfs
501 run_once make_bootfiles
502 run_once make_grubcfg
503 run_once make_manifest
504 run_once make_squashfs
505 run_once make_deifnes
506 run_once run_bootfiles make_isolinux
507 run_once run_bootfiles make_md5sum
508 run_once run_bootfiles make_iso
509 run_once make_checksum