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
201 if [[ -n $(find ${channels_dir}/*/ppa -type f) ]]; then; then
202 _apt_install software-properties-common
204 for ppa_list in $(find ${channels_dir}/* -type f -name ppa); do
205 for ppa in $(cat $ppa_list); do
206 run_cmd add-apt-repository --yes $ppa
213 _apt_install systemd-sysv
214 run_cmd dbus-uuidgen > /etc/machine-id
215 run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
219 remove "${work_dir}/airootfs/installpkglist"
221 if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
222 grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
224 if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
225 grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
227 if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
228 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
234 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'
237 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
240 cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
243 #run_cmd echo -ne "UTC" > '/etc/timezone'
244 #run_cmd dpkg-reconfigure -f noninteractive tzdata
246 run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
247 run_cmd truncate -s 0 /etc/machine-id
250 make_customize_airootfs() {
251 # Overwrite airootfs with customize_airootfs.
258 if [[ -d "${_dir}" ]]; then
259 cp -af "${_dir}"/* "${work_dir}/airootfs"
264 copy_airootfs "${channels_dir}/share/airootfs"
265 copy_airootfs "${channels_dir}/${channel_name}/airootfs"
267 if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
268 chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
269 run_cmd "/root/customize_airootfs.sh"
274 run_cmd apt-get clean
275 run_cmd apt-get --yes autoremove
276 run_cmd rm -rf "/tmp/* ~/.bash_history"
280 run_cmd update-initramfs -c -k all
281 _apt_install memtest86+
282 [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
283 mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
284 cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
285 cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
286 cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
288 if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
289 wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
292 (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
296 touch "${bootfiles_dir}/ubuntu"
297 cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
301 run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
302 cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
303 sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
304 sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
305 sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
306 sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
307 sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
311 mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
312 printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
316 cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
320 if [[ "${arch}" = "amd64" ]]; then
323 local _arch="${arch}"
326 --format=${_arch}-efi \
327 --output=isolinux/bootx64.efi \
330 "boot/grub/grub.cfg=isolinux/grub.cfg"
333 dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
334 sudo mkfs.vfat efiboot.img && \
335 LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
336 LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
340 --output=isolinux/core.img \
341 --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
342 --modules="linux16 linux normal iso9660 biosdisk search" \
345 "boot/grub/grub.cfg=isolinux/grub.cfg"
346 cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
350 /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
357 -full-iso9660-filenames \
358 -volid "${iso_label}" \
359 -appid "${iso_application}" \
360 -publisher "${iso_publisher}" \
361 -preparer "prepared by LUBS" \
362 -eltorito-boot boot/grub/bios.img \
366 --eltorito-catalog boot/grub/boot.cat \
368 --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
372 -append_partition 2 0xef isolinux/efiboot.img \
373 -output "${out_dir}/${iso_filename}" \
376 /boot/grub/bios.img=isolinux/bios.img \
377 /EFI/efiboot.img=isolinux/efiboot.img
382 _msg_info "Creating md5 checksum ..."
383 md5sum "${iso_filename}" > "${iso_filename}.md5"
385 _msg_info "Creating sha256 checksum ..."
386 sha256sum "${iso_filename}" > "${iso_filename}.sha256"
387 cd - > /dev/null 2>&1
392 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
394 _opt_short="w:o:ha:-:m:c:d"
395 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
396 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
397 if [[ ${?} != 0 ]]; then
404 if [[ -z ${2} ]]; then
405 _msg_error "Please specify the architecture."
413 if [[ -z ${2} ]]; then
414 _msg_error "Please specify the codename."
430 if [[ -z ${2} ]]; then
431 _msg_error "Please specify the mirror server."
439 if [[ -z ${2} ]]; then
440 _msg_error "Please specify the out dir."
448 if [[ -z ${2} ]]; then
449 _msg_error "Please specify the out dir."
461 _msg_error "Invalid argument '${1}'"
467 bootfiles_dir="${work_dir}/bootfiles"
472 run_once make_sourcelist
473 run_once make_systemd
474 run_once make_packages
476 run_once make_customize_airootfs
478 run_once make_bootfiles
479 run_once make_grubcfg
480 run_once make_manifest
481 run_once make_squashfs
482 run_once make_deifnes
483 run_once run_bootfiles make_isolinux
484 run_once run_bootfiles make_md5sum
485 run_once run_bootfiles make_iso
486 run_once make_checksum