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 "${script_path}/lubs-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}"
158 if [[ ! -d "${cache_dir}/${codename}" ]]; then
159 _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
160 mkdir -p "${cache_dir}/${codename}"
161 debootstrap --arch=${arch} --include=linux-image-generic --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
162 _msg_info "${codename} installed successfully!"
164 _msg_info "${codename} cache is found."
167 rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
168 _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
169 rsync -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
170 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
171 run_cmd apt-get update
172 # run_cmd apt-get upgrade
176 cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
177 run_cmd apt-get update
179 if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
182 for repo in $(find ${channels_dir}/*/repo -name *.list); do
183 key="$(dirname $repo)/$(basename $repo | sed "s/list/key/")"
185 if [[ -f "$key" ]]; then
186 if $(file $key | grep -q "PGP/GPG key"); then
187 cp $key ${work_dir}/airootfs/$(basename $key)
189 wget -q -O ${work_dir}/airootfs/$(basename $key) $(cat $key)
192 run_cmd apt-key add $(basename $key)
193 rm ${work_dir}/airootfs/$(basename $key)
194 cp $repo ${work_dir}/airootfs/etc/apt/sources.list.d
198 run_cmd apt-get update
206 PPA_FILELIST=("${channels_dir}/${channel_name}/ppa_list.${arch}"
207 _apt_install software-properties-common
208 for _PPA_FILE in ${PPA_FILELIST[@]}; do
209 for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
210 run_cmd add-apt-repository --yes "${_ppa}"
216 _apt_install systemd-sysv
217 run_cmd dbus-uuidgen > /etc/machine-id
218 run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
222 remove "${work_dir}/airootfs/installpkglist"
224 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
225 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
227 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
228 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
230 if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
231 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
237 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'
240 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
243 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
246 #run_cmd echo -ne "UTC" > '/etc/timezone'
247 #run_cmd dpkg-reconfigure -f noninteractive tzdata
249 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
250 run_cmd truncate -s 0 /etc/machine-id
253 make_customize_airootfs() {
254 # Overwrite airootfs with customize_airootfs.
261 if [[ -d "${_dir}" ]]; then
262 cp -af "${_dir}"/* "${work_dir}/airootfs"
267 copy_airootfs "${channels_dir}/share/airootfs"
268 copy_airootfs "${channels_dir}/${channel_name}/airootfs"
270 if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
271 chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
272 run_cmd "/root/customize_airootfs.sh"
277 run_cmd apt-get clean
278 run_cmd apt-get --yes autoremove
279 run_cmd rm -rf "/tmp/* ~/.bash_history"
283 run_cmd update-initramfs -c -k all
284 _apt_install memtest86+
285 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
286 mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
287 cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
288 cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
289 cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
291 if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
292 wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
295 (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
299 touch "${bootfiles_dir}/ubuntu"
300 cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
304 run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
305 cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
306 sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
307 sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
308 sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
309 sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
310 sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
314 mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
315 printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
319 cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
323 if [[ "${arch}" = "amd64" ]]; then
326 local _arch="${arch}"
329 --format=${_arch}-efi \
330 --output=isolinux/bootx64.efi \
333 "boot/grub/grub.cfg=isolinux/grub.cfg"
336 dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
337 sudo mkfs.vfat efiboot.img && \
338 LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
339 LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
343 --output=isolinux/core.img \
344 --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
345 --modules="linux16 linux normal iso9660 biosdisk search" \
348 "boot/grub/grub.cfg=isolinux/grub.cfg"
349 cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
353 /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
360 -full-iso9660-filenames \
361 -volid "${iso_label}" \
362 -appid "${iso_application}" \
363 -publisher "${iso_publisher}" \
364 -preparer "prepared by LUBS" \
365 -eltorito-boot boot/grub/bios.img \
369 --eltorito-catalog boot/grub/boot.cat \
371 --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
375 -append_partition 2 0xef isolinux/efiboot.img \
376 -output "${out_dir}/${iso_filename}" \
379 /boot/grub/bios.img=isolinux/bios.img \
380 /EFI/efiboot.img=isolinux/efiboot.img
385 _msg_info "Creating md5 checksum ..."
386 md5sum "${iso_filename}" > "${iso_filename}.md5"
388 _msg_info "Creating sha256 checksum ..."
389 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
390 cd - > /dev/null 2>&1
395 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
397 _opt_short="w:o:ha:-:m:c:d"
398 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
399 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
400 if [[ ${?} != 0 ]]; then
407 if [[ -z ${2} ]]; then
408 _msg_error "Please specify the architecture."
416 if [[ -z ${2} ]]; then
417 _msg_error "Please specify the codename."
433 if [[ -z ${2} ]]; then
434 _msg_error "Please specify the mirror server."
442 if [[ -z ${2} ]]; then
443 _msg_error "Please specify the out dir."
451 if [[ -z ${2} ]]; then
452 _msg_error "Please specify the out dir."
464 _msg_error "Invalid argument '${1}'"
470 bootfiles_dir="${work_dir}/bootfiles"
475 run_once make_sourcelist
476 run_once make_systemd
477 run_once make_packages
479 run_once make_customize_airootfs
481 run_once make_bootfiles
482 run_once make_grubcfg
483 run_once make_manifest
484 run_once make_squashfs
485 run_once make_deifnes
486 run_once run_bootfiles make_isolinux
487 run_once run_bootfiles make_md5sum
488 run_once run_bootfiles make_iso
489 run_once make_checksum