8 script_path=$(readlink -f ${0%/*})
9 channels_dir="${script_path}/channels"
10 isolinux_dir="${script_path}/isolinux"
13 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
18 work_dir="${script_path}/work"
19 out_dir="${script_path}/out"
20 iso_label="${os_name}_${codename}_${arch}"
21 iso_publisher='Fascode Network <https://fascode.net>'
22 iso_application="${os_name} Live/Rescue CD"
23 iso_version="${codename}-$(date +%Y.%m.%d)"
24 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
31 # Show an INFO message
35 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 32 " INFO:" "/#" -t 0 "${_msg}"
38 # Show an debug message
39 # _msg_debug <message>
41 if [[ "${debug}" = true ]]; then
43 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 35 "Debug:" "/#" -t 0 "${_msg}"
47 # Show an ERROR message then exit with status
48 # _msg_error <message> <exit code>
52 "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 31 "ERROR:" "/#" -t 0 "${_msg}" >&2
53 if [[ ! ${_error} = 0 ]]; then
58 # Helper function to run make_*() only one time.
62 if [[ "run_bootfiles" == "$1" ]]; then
68 if [[ ! -e "${work_dir}/build.${name}" ]]; then
69 _msg_info "$(echo $name | sed "s@_@ @g") is starting."
71 if [[ "run_bootfiles" == "$1" ]]; then
77 _msg_info "$(echo $name | sed "s@_@ @g") was done!"
78 touch "${work_dir}/build.${name}"
83 "${script_path}/lubs-chroot" "${work_dir}/airootfs" "${@}"
93 run_cmd apt-get --no-install-recommends --yes install ${@}
97 # Delete the file if it exists.
98 # For directories, rm -rf is used.
99 # If the file does not exist, skip it.
100 # remove <file> <file> ...
105 for _file in "${_list[@]}"; do
106 _msg_debug "Removeing ${_file}"
107 if [[ -f ${_file} ]]; then
109 elif [[ -d ${_file} ]]; then
117 echo "usage ${0} [options] [channel]"
119 echo " General options:"
121 echo " -a | --arch <str> Set architecture"
122 echo " Default: ${arch}"
123 echo " -c | --codename <str> Set ubuntu codename"
124 echo " Default: ${codename}"
125 echo " -m | --mirror <url> Set apt mirror server."
126 echo " Default: ${mirror}"
127 echo " -o | --out <out_dir> Set the output directory"
128 echo " Default: ${out_dir}"
129 echo " -w | --work <work_dir> Set the working directory"
130 echo " Default: ${work_dir}"
132 echo " -d | --debug "
133 echo " -h | --help This help message and exit"
139 if [[ ${EUID} -ne 0 ]]; then
140 _msg_error "This script must be run as root." 1
143 [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
144 [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
147 for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
148 _msg_info "Unmounting ${mount}"
156 if [[ ! -d "${cache_dir}/${codename}" ]]; then
157 _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
158 mkdir -p "${cache_dir}/${codename}"
159 debootstrap --arch=${arch} --include=linux-image-generic --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
160 _msg_info "${codename} installed successfully!"
162 _msg_info "${codename} cache is found."
165 rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
166 _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
167 rsync -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
168 echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
169 run_cmd apt-get update
170 # run_cmd apt-get upgrade
174 cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
175 run_cmd apt-get update
177 if [[ -f "${channels_dir}/${channel_name}/${channel_name}.list" ]] && [[ -f "${channels_dir}/${channel_name}/${channel_name}-list_key" ]]; then
178 source ${channels_dir}/${channel_name}/${channel_name}-list_key
180 if [[ ! -f "${cache_dir}/${channel_name}.key" ]]; then
181 wget -q -O ${cache_dir}/${channel_name}.key "${url}"
185 cp ${cache_dir}/${channel_name}.key ${work_dir}/airootfs/${channel_name}.key
186 run_cmd apt-key add ${channel_name}.key
187 cp ${channels_dir}/${channel_name}/${channel_name}.list ${work_dir}/airootfs/etc/apt/sources.list.d
188 run_cmd apt-get update
193 _apt_install systemd-sysv
194 run_cmd dbus-uuidgen > /etc/machine-id
195 run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
199 remove "${work_dir}/airootfs/installpkglist"
201 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
202 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
204 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
205 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
207 if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
208 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
214 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'
217 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
220 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
223 #run_cmd echo -ne "UTC" > '/etc/timezone'
224 #run_cmd dpkg-reconfigure -f noninteractive tzdata
226 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
227 run_cmd truncate -s 0 /etc/machine-id
230 make_customize_airootfs() {
231 # Overwrite airootfs with customize_airootfs.
238 if [[ -d "${_dir}" ]]; then
239 cp -af "${_dir}"/* "${work_dir}/airootfs"
244 copy_airootfs "${channels_dir}/share/airootfs"
245 copy_airootfs "${channels_dir}/${channel_name}/airootfs"
247 if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
248 chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
249 run_cmd "/root/customize_airootfs.sh"
254 run_cmd apt-get clean
255 run_cmd apt-get --yes autoremove
256 run_cmd rm -rf "/tmp/* ~/.bash_history"
260 run_cmd update-initramfs -c -k all
261 _apt_install memtest86+
262 mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
263 cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
264 cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
265 cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
267 if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
268 wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
271 (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
275 touch "${bootfiles_dir}/ubuntu"
276 cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
280 run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
281 cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
282 sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
283 sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
284 sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
285 sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
286 sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
290 mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
291 printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
295 cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
299 if [[ "${arch}" = "amd64" ]]; then
302 local _arch="${arch}"
305 --format=${_arch}-efi \
306 --output=isolinux/bootx64.efi \
309 "boot/grub/grub.cfg=isolinux/grub.cfg"
312 dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
313 sudo mkfs.vfat efiboot.img && \
314 LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
315 LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
319 --output=isolinux/core.img \
320 --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
321 --modules="linux16 linux normal iso9660 biosdisk search" \
324 "boot/grub/grub.cfg=isolinux/grub.cfg"
325 cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
329 /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
336 -full-iso9660-filenames \
337 -volid "${iso_label}" \
338 -appid "${iso_application}" \
339 -publisher "${iso_publisher}" \
340 -preparer "prepared by LUBS" \
341 -eltorito-boot boot/grub/bios.img \
345 --eltorito-catalog boot/grub/boot.cat \
347 --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
351 -append_partition 2 0xef isolinux/efiboot.img \
352 -output "${out_dir}/${iso_filename}" \
355 /boot/grub/bios.img=isolinux/bios.img \
356 /EFI/efiboot.img=isolinux/efiboot.img
361 _msg_info "Creating md5 checksum ..."
362 md5sum "${iso_filename}" > "${iso_filename}.md5"
364 _msg_info "Creating sha256 checksum ..."
365 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
366 cd - > /dev/null 2>&1
371 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
373 _opt_short="w:o:ha:-:m:c:d"
374 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
375 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
376 if [[ ${?} != 0 ]]; then
383 if [[ -z ${2} ]]; then
384 _msg_error "Please specify the architecture."
392 if [[ -z ${2} ]]; then
393 _msg_error "Please specify the codename."
409 if [[ -z ${2} ]]; then
410 _msg_error "Please specify the mirror server."
418 if [[ -z ${2} ]]; then
419 _msg_error "Please specify the out dir."
427 if [[ -z ${2} ]]; then
428 _msg_error "Please specify the out dir."
440 _msg_error "Invalid argument '${1}'"
446 cache_dir="${work_dir}/cache"
447 bootfiles_dir="${work_dir}/bootfiles"
452 run_once make_sourcelist
453 run_once make_systemd
454 run_once make_packages
456 run_once make_customize_airootfs
458 run_once make_bootfiles
459 run_once make_grubcfg
460 run_once make_manifest
461 run_once make_squashfs
462 run_once make_deifnes
463 run_once run_bootfiles make_isolinux
464 run_once run_bootfiles make_md5sum
465 run_once run_bootfiles make_iso
466 run_once make_checksum