set -e
# set -u
-export LANG=C
+#export LANG=C
script_path=$(readlink -f "${0%/*}")
-cache_dir="${script_path}/cache"
-
work_dir="${script_path}/work"
channels_dir="${script_path}/channels"
+nfb_dir="${script_path}/nfb"
codename="32"
os_name="Fedora"
iso_name="Fedora"
+language="ja_JP.UTF-8"
+channel_name="lxde"
+cache_dir="${script_path}/cache"
-arch=amd64
+arch="x86_64"
out_dir="${script_path}/out"
iso_label="${os_name}_${codename}_${arch}"
iso_application="${os_name} Live/Rescue CD"
iso_version="${codename}-$(date +%Y.%m.%d)"
iso_filename="${iso_name}-${iso_version}-${arch}.iso"
+liveuser_name="fedora"
+liveuser_password="fedora"
+liveuser_shell="/usr/bin/zsh"
-username="liveuser"
-usershell="/bin/bash"
+#-- language config --#
+
+# Sets the default locale for the live environment.
+# You can also place a package list for that locale name and install packages specific to that locale.
+locale_name="en"
+locale_gen_name="en_US.UTF-8"
+locale_version="gl"
+locale_time="UTC"
+locale_fullname="global"
debug=false
cache_only=false
_time="$(("${_current_time}"-"${start_time}"))"
if [[ "${_time}" -ge 3600 ]]; then
- echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
+ echo "[$(date -d @${_time} +%H:%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
elif [[ "${_time}" -ge 60 ]]; then
- echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LUBS Core]")"
+ echo "[00:$(date -d @${_time} +%M.%S)]$("${script_path}/echo_color" -t 6 "[LFBS Core]")"
else
- echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LUBS Core]")"
+ echo "[00:00.$(date -d @${_time} +%S)] $("${script_path}/echo_color" -t 6 "[LFBS Core]")"
fi
else
- "${script_path}/echo_color" -t 6 "[LUBS Core]"
+ "${script_path}/echo_color" -t 6 "[LFBS Core]"
fi
}
local mount
for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
+ if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
+ _msg_info "Unmounting ${mount}"
+ umount -fl "${mount}"
+ fi
+ done
+}
+
+# Unmount chroot dir and airootfs
+umount_chroot_airootfs () {
+ local mount
+
+ for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
_msg_info "Unmounting ${mount}"
umount -fl "${mount}"
done
}
-
# Helper function to run make_*() only one time.
run_once() {
local name
run_cmd() {
local mount
- #for mount in "dev" "dev/pts" "proc" "sys" "run/systemd/resolve/stub-resolv.conf"; do
- for mount in "dev" "dev/pts" "proc" "sys" ; do
- #if [[ "${mount}" == "run/systemd/resolve/stub-resolv.conf" ]]; then
- # mount --bind /etc/resolv.conf "${work_dir}/airootfs/${mount}"
- #else
+ for mount in "dev" "dev/pts" "proc" "sys" "etc/resolv.conf"; do
+ #for mount in "dev" "dev/pts" "proc" "sys" ; do
+ if [[ "${mount}" == "etc/resolv.conf" ]]; then
+ cp /etc/resolv.conf "${work_dir}/airootfs/${mount}"
+ else
mount --bind /${mount} "${work_dir}/airootfs/${mount}"
- #fi
+ fi
done
- chroot "${work_dir}/airootfs" "${@}"
+ unshare --fork --pid chroot "${work_dir}/airootfs" "${@}"
for mount in $(mount | awk '{print $3}' | grep "$(realpath "${work_dir}")" | sort -r); do
- umount -fl "${mount}"
+ if [[ ! "${mount}" == "${work_dir}/airootfs" ]]; then
+ umount -fl "${mount}"
+ fi
done
}
echo
echo " -a | --arch <str> Set architecture"
echo " Default: ${arch}"
- echo " -c | --codename <str> Set ubuntu codename"
- echo " Default: ${codename}"
+ echo " -l | --lang <lang> Specifies the default language for the live environment"
+ echo " Default: ${locale_name}"
echo " -m | --mirror <url> Set apt mirror server."
echo " Default: ${mirror}"
echo " -o | --out <out_dir> Set the output directory"
echo " Default: ${out_dir}"
echo " -w | --work <work_dir> Set the working directory"
echo " Default: ${work_dir}"
+ echo " -c | --cache <cache_dir> Set the cache directory"
+ echo " Default: ${cache_dir}"
echo
echo " -d | --debug "
echo " -h | --help This help message and exit"
echo
echo "You can switch between installed packages, files included in images, etc. by channel."
echo
+ echo " Language for each architecture:"
+ for _list in ${script_path}/system/locale-* ; do
+ _arch="${_list#${script_path}/system/locale-}"
+ echo -n " ${_arch}"
+ for i in $( seq 1 $(( ${blank} - 4 - ${#_arch} )) ); do
+ echo -ne " "
+ done
+ _locale_name_list=$(cat ${_list} | grep -h -v ^'#' | awk '{print $1}')
+ for _lang in ${_locale_name_list[@]};do
+ echo -n "${_lang} "
+ done
+ echo
+ done
echo " Channel:"
local _channel
done
}
+dnfstrap() {
+ if [[ ! -d "${cache_dir}" ]]; then
+ mkdir -p "${cache_dir}"
+ fi
+ cp -rf "${script_path}/system/dnfconf.conf" "${work_dir}/dnf_conf"
+ mount --bind "${cache_dir}" "${work_dir}/airootfs/dnf_cache"
+ dnf -c "${work_dir}/dnf_conf" --installroot="${work_dir}/airootfs" $(${script_path}/system/repository-json-parser.py ${script_path}/system/repository.json) install ${@} -y
+}
+
+
+make_basefs() {
+ _msg_info "Installing Fedora to '${work_dir}/airootfs'..."
+ dnfstrap @Core
+ _msg_info "${codename} installed successfully!"
+
+ echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' > "${work_dir}/airootfs/etc/bash.bashrc"
+ run_cmd dnf update -y
+ run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
+ # run_cmd apt-get upgrade
+}
+
+
+# Parse files
+parse_files() {
+ #-- ロケールを解析、設定 --#
+ local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
+
+ # 選択されたロケールの設定が描かれた行番号を取得
+ _locale_config_file="${script_path}/system/locale-${arch}"
+ _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
+ _get_locale_line_number() {
+ local _lang _count=0
+ for _lang in ${_locale_name_list[@]}; do
+ _count=$(( _count + 1 ))
+ if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
+ done
+ echo -n "failed"
+ }
+ _locale_line_number="$(_get_locale_line_number)"
+
+ # 不正なロケール名なら終了する
+ [[ "${_locale_line_number}" == "failed" ]] && msg_error "${locale_name} is not a valid language." "1"
+
+ # ロケール設定ファイルから該当の行を抽出
+ _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
+
+ # 抽出された行に書かれた設定をそれぞれの変数に代入
+ # ここで定義された変数のみがグローバル変数
+ locale_name="${_locale_config_line[0]}"
+ locale_gen_name="${_locale_config_line[1]}"
+ locale_version="${_locale_config_line[2]}"
+ locale_time="${_locale_config_line[3]}"
+ locale_fullname="${_locale_config_line[4]}"
+}
prepare_build() {
if [[ ${EUID} -ne 0 ]]; then
_msg_error "This script must be run as root." 1
fi
-
- [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
- [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
- umount_chroot
-
+ umount_chroot_airootfs
# Check codename
- if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
+ if [[ -z "$(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename})" ]]; then
_msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
fi
+ if [[ ! -d "${work_dir}/squashfsroot/LiveOS/" ]]; then
+ mkdir -p "${work_dir}/squashfsroot/LiveOS/"
+ mkdir -p "${work_dir}/airootfs/"
+ _msg_info "Make rootfs image..."
+ truncate -s 5G "${work_dir}/squashfsroot/LiveOS/rootfs.img"
+ _msg_info "Format rootfs image..."
+ mkfs.ext4 -F "${work_dir}/squashfsroot/LiveOS/rootfs.img"
+ fi
+ mkdir -p "${out_dir}"
+ _msg_info "Mount rootfs image..."
+ mount -o loop,rw,sync "${work_dir}/squashfsroot/LiveOS/rootfs.img" "${work_dir}/airootfs"
}
-make_basefs() {
- local dnf_status
- statusfile="${cache_dir}/${codename}/status"
+make_systemd() {
+ _dnf_install dbus-tools
+ run_cmd dbus-uuidgen --ensure=/etc/machine-id
+ if [[ ! -d "${work_dir}/airootfs/var/lib/dbus" ]]; then
+ run_cmd mkdir /var/lib/dbus
+ fi
+ run_cmd ln -sf /etc/machine-id /var/lib/dbus/machine-id
+}
+make_dnf_packages() {
+ remove "${work_dir}/airootfs/dnfpkglist"
+ #_apt_install initramfs-tools
+ # run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --yes'
- dnf_status() {
- if [[ ! -d "$(dirname ${statusfile})" ]]; then
- mkdir -p "$(dirname ${statusfile})"
+ if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
+ grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
+ fi
+ if [[ -f "${channels_dir}/share/packages-${locale_name}.${arch}" ]]; then
+ grep -h -v ^'#' "${channels_dir}/share/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
+ fi
+
+ if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
+ grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
+ fi
+
+ if [[ -f "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" ]]; then
+ grep -h -v ^'#' "${channels_dir}/${channel_name}/packages-${locale_name}.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/dnfpkglist"
+ fi
+
+ if [[ -s "${work_dir}/airootfs/dnfpkglist" ]]; then
+ run_cmd env -i bash -c 'dnf -y --nogpgcheck install $(echo $(<dnfpkglist))'
+ fi
+
+ remove "${work_dir}/airootfs/dnfpkglist"
+}
+
+make_cp_airootfs() {
+ local _copy_airootfs
+
+ _copy_airootfs() {
+ local _dir="${1%/}"
+ if [[ -d "${_dir}" ]]; then
+ cp -af "${_dir}"/* "${work_dir}/airootfs"
fi
- echo "${1}" > "${statusfile}"
}
+ _copy_airootfs "${channels_dir}/share/airootfs"
+ _copy_airootfs "${channels_dir}/share/airootfs.${locale_name}"
+ _copy_airootfs "${channels_dir}/${channel_name}/airootfs"
+ _copy_airootfs "${channels_dir}/${channel_name}/airootfs.${locale_name}"
+}
- if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
- _msg_info "${codename} cache is found."
- else
- remove "${cache_dir}/${codename}"
- dnf_status "Running"
- _msg_info "Installing Fedora to '${cache_dir}/${codename}/airootfs'..."
- mkdir -p "${cache_dir}/${codename}/airootfs"
- dnf install dnf --releasever=${codename} --installroot="${cache_dir}/${codename}/airootfs" -y
- _msg_info "${codename} installed successfully!"
- dnf_status "Done"
- fi
+make_config() {
+ # customize_airootfs options
+ # -b : Enable boot splash.
+ # -d : Enable debug mode.
+ # -g <locale_gen_name> : Set locale-gen.
+ # -i <inst_dir> : Set install dir
+ # -k <kernel config line> : Set kernel name.
+ # -o <os name> : Set os name.
+ # -p <password> : Set password.
+ # -s <shell> : Set user shell.
+ # -t : Set plymouth theme.
+ # -u <username> : Set live user name.
+ # -x : Enable bash debug mode.
+ # -r : Enable rebuild.
+ # -z <locale_time> : Set the time zone.
+ # -l <locale_name> : Set language.
+ #
+ # -j is obsolete in AlterISO3 and cannot be used.
+ # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
+ local _airootfs_script_options _run_script
+ _airootfs_script_options="-p ${liveuser_password} -u ${liveuser_name} -o ${os_name} -s ${liveuser_shell} -a ${arch} -g ${locale_gen_name} -l ${locale_name} -z ${locale_time} "
+
+ _run_script() {
+ local _file
+ for _file in ${@}; do
+ if [[ -f "${work_dir}/airootfs${_file}" ]]; then run_cmd "${_file}" ${_airootfs_script_options}; fi
+ if [[ -f "${work_dir}/airootfs${_file}" ]]; then chmod 755 "${work_dir}/airootfs${_file}"; fi
+ done
+ }
- if [[ "${cache_only}" = true ]]; then
+ _run_script "/root/customize_airootfs.sh" "/root/customize_airootfs_${channel_name}.sh"
+}
+
+make_clean() {
+ run_cmd dnf -y remove $(run_cmd dnf repoquery --installonly --latest-limit=-1 -q)
+ run_cmd dnf clean all
+}
+
+make_squashfs() {
+ # prepare
+ remove "${bootfiles_dir}"
+ mkdir -p "${bootfiles_dir}"/{grub,LiveOS,boot,isolinux}
+ #generate initrd
+ _msg_info "make initrd..."
+ run_cmd dracut --xz --add "dmsquash-live convertfs pollcdrom" --omit plymouth --no-hostonly --no-early-microcode /boot/initrd0 `run_cmd ls /lib/modules`
+ cp ${work_dir}/airootfs/boot/vmlinuz-$(run_cmd ls /lib/modules) ${bootfiles_dir}/boot/vmlinuz
+ mv ${work_dir}/airootfs/boot/initrd0 ${bootfiles_dir}/boot/initrd
+ #cp isolinux
+ cp "${nfb_dir}"/isolinux/* "${bootfiles_dir}/isolinux/"
+ # make squashfs
+ remove "${work_dir}/airootfs/boot"
+ umount "${work_dir}/airootfs"
+ _msg_info "Minimize rootfs..."
+ resize2fs -M "${work_dir}/squashfsroot/LiveOS/rootfs.img"
+ _msg_info "Compress rootfs.."
+ mksquashfs "${work_dir}/squashfsroot/" "${bootfiles_dir}/LiveOS/squashfs.img"
+}
+
+make_nfb() {
+ touch "${bootfiles_dir}/fedora_lfbs"
+ # isolinux setup
+ sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/isolinux.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/isolinux/isolinux.cfg"
+ #grub
+ sed "s|%OS_NAME%|${os_name}|g" "${nfb_dir}/grub.cfg" | sed "s|%CD_LABEL%|${iso_label}|g" > "${bootfiles_dir}/grub/grub.cfg"
+}
+make_efi() {
+ # UEFI 32bit (ia32)
+ grub-mkstandalone \
+ --format=i386-efi \
+ --output="${bootfiles_dir}/grub/bootia32.efi" \
+ --locales="" \
+ --fonts="" \
+ "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
+
+ # UEFI 64bit (x64)
+ grub-mkstandalone \
+ --format=x86_64-efi \
+ --output="${bootfiles_dir}/grub/bootx64.efi" \
+ --locales="" \
+ --fonts="" \
+ "boot/grub/grub.cfg=${bootfiles_dir}/grub/grub.cfg"
+
+ # create efiboot.img
+ truncate -s 200M "${bootfiles_dir}/grub/efiboot.img"
+ mkfs.fat -F 16 -f 1 -r 112 "${bootfiles_dir}/grub/efiboot.img"
+ mkdir "${bootfiles_dir}/mnt"
+ mount "${bootfiles_dir}/grub/efiboot.img" "${bootfiles_dir}/mnt"
+ mkdir -p "${bootfiles_dir}/mnt/efi/boot"
+ cp "${bootfiles_dir}/grub/bootia32.efi" "${bootfiles_dir}/mnt/efi/boot"
+ cp "${bootfiles_dir}/grub/bootx64.efi" "${bootfiles_dir}/mnt/efi/boot"
+ umount -d "${bootfiles_dir}/mnt"
+ remove "${bootfiles_dir}/mnt"
+}
+make_iso() {
+ cd "${bootfiles_dir}"
+
+ # create checksum (using at booting)
+ bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
+
+ # create iso
+ xorriso \
+ -as mkisofs \
+ -iso-level 3 \
+ -full-iso9660-filenames \
+ -volid "${iso_label}" \
+ -appid "${iso_application}" \
+ -publisher "${iso_publisher}" \
+ -preparer "prepared by LFBS" \
+ -b isolinux/isolinux.bin \
+ -no-emul-boot \
+ -boot-load-size 4 \
+ -boot-info-table \
+ -eltorito-alt-boot \
+ -eltorito-platform efi \
+ -eltorito-boot EFI/efiboot.img \
+ -no-emul-boot \
+ -isohybrid-mbr "${bootfiles_dir}/isolinux/isohdpfx.bin" \
+ -isohybrid-gpt-basdat \
+ -eltorito-catalog isolinux/boot.cat \
+ -output "${out_dir}/${iso_filename}" \
+ -graft-points \
+ "." \
+ "/isolinux/isolinux.bin=isolinux/isolinux.bin" \
+ "/EFI/efiboot.img=grub/efiboot.img"
+
+ cd - > /dev/null
+}
+
+make_checksum() {
+ cd "${out_dir}"
+ _msg_info "Creating md5 checksum ..."
+ md5sum "${iso_filename}" > "${iso_filename}.md5"
+
+ _msg_info "Creating sha256 checksum ..."
+ sha256sum "${iso_filename}" > "${iso_filename}.sha256"
+ cd - > /dev/null 2>&1
+ umount_chroot_airootfs
+}
+
+# 引数解析()
+# 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
+
+_opt_short="w:l:o:ha:-:m:c:d"
+_opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only"
+OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
+
+if [[ ${?} != 0 ]]; then
+ exit 1
+fi
+
+eval set -- "${OPT}"
+
+while :; do
+ case ${1} in
+ -a | --arch)
+ arch="${2}"
+ shift 2
+ ;;
+ -c | --cache)
+ cache_dir="${2}"
+ shift 2
+ ;;
+ -d | --debug)
+ debug=true
+ shift 1
+ ;;
+ -h | --help)
+ _usage
+ exit 0
+ ;;
+ -m | --mirror)
+ mirror="${2}"
+ shift 2
+ ;;
+ -l | --lang)
+ locale_name="${2}"
+ shift 2
+ ;;
+ -o | --out)
+ out_dir="${2}"
+ shift 2
+ ;;
+ -w | --work)
+ work_dir="${2}"
+ shift 2
+ ;;
+ --cache-only)
+ cache_only=true
+ shift 1
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ _msg_error "Invalid argument '${1}'"
+ _usage 1
+ ;;
+ esac
+done
+
+bootfiles_dir="${work_dir}/bootfiles"
+trap umount_chroot_airootfs 0 2 15
+
+if [[ -n "${1}" ]]; then
+ channel_name="${1}"
+ if [[ "${channel_name}" = "umount" ]]; then
+ umount_chroot_airootfs
exit 0
fi
+ if [[ "${channel_name}" = "clean" ]]; then
+ umount_chroot_airootfs
+ _msg_info "deleting work dir..."
+ remove "${work_dir}"
+ exit 0
+ fi
+ check_channel() {
+ local channel_list
+ local i
+ channel_list=()
+
+ for _channel in $(ls -l "${channels_dir}" | awk '$1 ~ /d/ {print $9 }'); do
+ if [[ -n "$(ls "${channels_dir}/${_channel}")" ]] && [[ ! "${_channel}" = "share" ]]; then
+ channel_list+=( "${_channel}" )
+ fi
+ done
- rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
- _msg_info "copy base files from '${cache_dir}/${codename}/airootfs' to '${work_dir}/airootfs'..."
- rsync -au "${cache_dir}/${codename}/airootfs/" "${work_dir}/airootfs"
- echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
- run_cmd dnf upgrade -y
- # run_cmd apt-get upgrade
-}
-make_basefs
-umount_chroot
\ No newline at end of file
+ for i in ${channel_list[@]}; do
+ if [[ "${i}" = "${channel_name}" ]]; then
+ echo -n "true"
+ return 0
+ fi
+ done
+
+ echo -n "false"
+ return 1
+ }
+
+ if [[ "$(check_channel ${channel_name})" = false ]]; then
+ _msg_error "Invalid channel ${channel_name}"
+ exit 1
+ fi
+fi
+iso_filename="${iso_name}-${codename}-${channel_name}-${locale_name}-$(date +%Y.%m.%d)-${arch}.iso"
+umount_chroot_airootfs
+if [[ -d "${work_dir}" ]]; then
+ _msg_info "deleting work dir..."
+ remove "${work_dir}"
+fi
+
+prepare_build
+parse_files
+run_once make_basefs
+run_once make_systemd
+run_once make_dnf_packages
+run_once make_cp_airootfs
+run_once make_config
+run_once make_clean
+run_once make_squashfs
+run_once make_nfb
+run_once make_efi
+run_once make_iso
+run_once make_checksum