manjaro-tools/lib/util-iso.sh

504 lines
14 KiB
Bash
Raw Normal View History

2014-12-08 23:50:56 +01:00
#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2017-04-27 12:01:26 +02:00
import ${LIBDIR}/util-chroot.sh
2017-04-19 21:30:38 +02:00
import ${LIBDIR}/util-iso-chroot.sh
2017-05-22 23:30:20 +02:00
import ${LIBDIR}/util-iso-grub.sh
import ${LIBDIR}/util-yaml.sh
import ${LIBDIR}/util-iso-mount.sh
2017-06-08 11:15:37 +02:00
import ${LIBDIR}/util-profile.sh
2016-02-27 00:39:58 +01:00
error_function() {
2016-09-15 23:58:18 +02:00
if [[ -p $logpipe ]]; then
rm "$logpipe"
fi
2017-04-28 10:30:34 +02:00
local func="$1"
2016-09-15 23:58:18 +02:00
# first exit all subshells, then print the error
if (( ! BASH_SUBSHELL )); then
2017-04-28 10:30:34 +02:00
error "A failure occurred in %s()." "$func"
2016-09-15 23:58:18 +02:00
plain "Aborting..."
fi
umount_fs
umount_img
2016-09-15 23:58:18 +02:00
exit 2
2016-02-27 00:39:58 +01:00
}
# $1: function
run_log(){
2016-09-15 23:58:18 +02:00
local func="$1"
local tmpfile=${tmp_dir}/$func.ansi.log logfile=${log_dir}/$(gen_iso_fn).$func.log
logpipe=$(mktemp -u "${tmp_dir}/$func.pipe.XXXXXXXX")
mkfifo "$logpipe"
tee "$tmpfile" < "$logpipe" &
local teepid=$!
$func &> "$logpipe"
wait $teepid
rm "$logpipe"
cat $tmpfile | perl -pe 's/\e\[?.*?[\@-~]//g' > $logfile
rm "$tmpfile"
2016-02-27 00:39:58 +01:00
}
run_safe() {
2016-09-15 23:58:18 +02:00
local restoretrap func="$1"
set -e
set -E
restoretrap=$(trap -p ERR)
trap 'error_function $func' ERR
2016-02-27 00:39:58 +01:00
2016-09-15 23:58:18 +02:00
if ${verbose};then
run_log "$func"
else
"$func"
fi
2016-02-27 00:39:58 +01:00
2016-09-15 23:58:18 +02:00
eval $restoretrap
set +E
set +e
2016-02-27 00:39:58 +01:00
}
2016-06-13 21:48:41 +02:00
trap_exit() {
2016-09-15 23:58:18 +02:00
local sig=$1; shift
error "$@"
umount_fs
2016-09-15 23:58:18 +02:00
trap -- "$sig"
kill "-$sig" "$$"
2016-06-13 21:48:41 +02:00
}
2017-04-19 21:30:38 +02:00
configure_live_image(){
2017-04-28 10:30:34 +02:00
local fs="$1"
2017-04-19 21:30:38 +02:00
msg "Configuring [livefs]"
2017-04-28 10:30:34 +02:00
configure_hosts "$fs"
configure_system "$fs"
configure_services "$fs"
configure_calamares "$fs"
write_live_session_conf "$fs"
2017-04-19 21:30:38 +02:00
msg "Done configuring [livefs]"
}
make_sig () {
2017-04-28 10:30:34 +02:00
local idir="$1" file="$2"
msg2 "Creating signature file..."
2017-04-28 10:30:34 +02:00
cd "$idir"
user_own "$idir"
su ${OWNER} -c "gpg --detach-sign --default-key ${gpgkey} $file.sfs"
chown -R root "$idir"
cd ${OLDPWD}
2016-10-19 16:01:16 +02:00
}
make_checksum(){
2017-04-28 10:30:34 +02:00
local idir="$1" file="$2"
2016-10-19 16:01:16 +02:00
msg2 "Creating md5sum ..."
2017-04-28 10:30:34 +02:00
cd $idir
md5sum $file.sfs > $file.md5
2016-10-19 16:01:16 +02:00
cd ${OLDPWD}
}
2015-05-11 12:47:02 +02:00
# $1: image path
make_sfs() {
2016-10-13 15:24:31 +02:00
local src="$1"
if [[ ! -e "${src}" ]]; then
error "The path %s does not exist" "${src}"
retrun 1
2016-09-15 23:58:18 +02:00
fi
2017-05-21 23:12:31 +02:00
local timer=$(get_timer) dest=${iso_root}/${os_id}/${target_arch}
2016-09-15 23:58:18 +02:00
local name=${1##*/}
2016-10-13 15:24:31 +02:00
local sfs="${dest}/${name}.sfs"
mkdir -p ${dest}
2016-10-13 15:24:31 +02:00
msg "Generating SquashFS image for %s" "${src}"
if [[ -f "${sfs}" ]]; then
local has_changed_dir=$(find ${src} -newer ${sfs})
msg2 "Possible changes for %s ..." "${src}" >> ${tmp_dir}/buildiso.debug
2016-09-15 23:58:18 +02:00
msg2 "%s" "${has_changed_dir}" >> ${tmp_dir}/buildiso.debug
if [[ -n "${has_changed_dir}" ]]; then
2016-10-13 15:24:31 +02:00
msg2 "SquashFS image %s is not up to date, rebuilding..." "${sfs}"
rm "${sfs}"
2016-09-15 23:58:18 +02:00
else
2016-10-13 15:24:31 +02:00
msg2 "SquashFS image %s is up to date, skipping." "${sfs}"
2016-09-15 23:58:18 +02:00
return
fi
fi
if ${persist};then
local size=32G
local mnt="${mnt_dir}/${name}"
msg2 "Creating ext4 image of %s ..." "${size}"
truncate -s ${size} "${src}.img"
local ext4_args=()
${verbose} && ext4_args+=(-q)
ext4_args+=(-O ^has_journal,^resize_inode -E lazy_itable_init=0 -m 0)
mkfs.ext4 ${ext4_args[@]} -F "${src}.img" &>/dev/null
tune2fs -c 0 -i 0 "${src}.img" &> /dev/null
mount_img "${work_dir}/${name}.img" "${mnt}"
msg2 "Copying %s ..." "${src}/"
cp -aT "${src}/" "${mnt}/"
umount_img "${mnt}"
fi
2016-10-13 15:24:31 +02:00
msg2 "Creating SquashFS image, this may take some time..."
local used_kernel=${kernel:5:1} mksfs_args=()
if ${persist};then
mksfs_args+=(${work_dir}/${name}.img)
else
mksfs_args+=(${src})
fi
mksfs_args+=(${sfs} -noappend)
2016-10-19 16:01:16 +02:00
local highcomp="-b 256K -Xbcj x86" comp='xz'
2016-09-15 23:58:18 +02:00
if [[ "${name}" == "mhwdfs" && ${used_kernel} < "4" ]]; then
mksfs_args+=(-comp lz4)
2016-09-15 23:58:18 +02:00
else
2016-10-19 16:01:16 +02:00
mksfs_args+=(-comp ${comp} ${highcomp})
fi
if ${verbose};then
mksquashfs "${mksfs_args[@]}" >/dev/null
else
mksquashfs "${mksfs_args[@]}"
2016-09-15 23:58:18 +02:00
fi
2016-10-19 16:01:16 +02:00
make_checksum "${dest}" "${name}"
2016-10-13 15:24:31 +02:00
${persist} && rm "${src}.img"
2017-02-15 01:18:20 +01:00
if [[ -n ${gpgkey} ]];then
make_sig "${dest}" "${name}"
fi
2016-09-15 23:58:18 +02:00
show_elapsed_time "${FUNCNAME}" "${timer_start}"
2015-05-11 12:47:02 +02:00
}
2016-02-26 16:09:34 +01:00
assemble_iso(){
2016-09-15 23:58:18 +02:00
msg "Creating ISO image..."
2017-05-10 23:11:42 +02:00
local mod_date=$(date -u +%Y-%m-%d-%H-%M-%S-00 | sed -e s/-//g)
2017-03-29 21:48:38 +02:00
2016-09-15 23:58:18 +02:00
xorriso -as mkisofs \
2017-03-29 21:48:38 +02:00
--modification-date=${mod_date} \
--protective-msdos-label \
-volid "${iso_label}" \
2017-05-10 23:11:42 +02:00
-appid "$(get_osname) Live/Rescue CD" \
-publisher "$(get_osname) <$(get_disturl)>" \
2017-03-29 21:48:38 +02:00
-preparer "Prepared by manjaro-tools/${0##*/}" \
2017-03-29 19:03:29 +02:00
-r -graft-points -no-pad \
--sort-weight 0 / \
--sort-weight 1 /boot \
--grub2-mbr ${iso_root}/boot/grub/i386-pc/boot_hybrid.img \
2017-03-29 19:03:29 +02:00
-partition_offset 16 \
-b boot/grub/i386-pc/eltorito.img \
-c boot.catalog \
-no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
-eltorito-alt-boot \
-append_partition 2 0xef ${iso_root}/efi.img \
2017-03-29 19:03:29 +02:00
-e --interval:appended_partition_2:all:: \
-no-emul-boot \
-iso-level 3 \
-o ${iso_dir}/${iso_file} \
${iso_root}/
2017-03-29 20:47:17 +02:00
# arg to add with xorriso-1.4.7
# -iso_mbr_part_type 0x00
}
# Build ISO
make_iso() {
2016-09-15 23:58:18 +02:00
msg "Start [Build ISO]"
touch "${iso_root}/.miso"
for sfs_dir in $(find "${work_dir}" -maxdepth 1 -type d); do
2016-10-13 15:24:31 +02:00
if [[ "${sfs_dir}" != "${work_dir}" ]]; then
make_sfs "${sfs_dir}"
2016-09-15 23:58:18 +02:00
fi
done
msg "Making bootable image"
# Sanity checks
[[ ! -d "${iso_root}" ]] && return 1
2016-09-15 23:58:18 +02:00
if [[ -f "${iso_dir}/${iso_file}" ]]; then
msg2 "Removing existing bootable image..."
rm -rf "${iso_dir}/${iso_file}"
fi
assemble_iso
msg "Done [Build ISO]"
}
2016-02-18 22:02:01 +01:00
gen_iso_fn(){
2016-09-15 23:58:18 +02:00
local vars=() name
2017-05-21 23:12:31 +02:00
vars+=("${os_id}")
if ! ${chrootcfg};then
2016-09-15 23:58:18 +02:00
[[ -n ${profile} ]] && vars+=("${profile}")
fi
[[ ${initsys} == 'openrc' ]] && vars+=("${initsys}")
vars+=("${dist_release}")
vars+=("${target_branch}")
vars+=("${target_arch}")
for n in ${vars[@]};do
name=${name:-}${name:+-}${n}
done
echo $name
2016-02-18 22:02:01 +01:00
}
2017-05-21 22:44:08 +02:00
copy_overlay(){
local src="$1" dest="$2"
if [[ -e "$src" ]];then
msg2 "Copying [%s] ..." "${src##*/}"
cp -LR "$src"/* "$dest"
fi
}
# Base installation (rootfs)
2015-01-13 04:48:04 +01:00
make_image_root() {
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/rootfs.lock ]]; then
msg "Prepare [Base installation] (rootfs)"
local rootfs="${work_dir}/rootfs"
2015-06-29 04:08:45 +02:00
prepare_dir "${rootfs}"
create_chroot "${mkchroot_args[@]}" "${rootfs}" "${packages[@]}"
2015-06-29 04:08:45 +02:00
2017-05-21 22:44:08 +02:00
copy_overlay "${root_overlay}" "${rootfs}"
2016-05-20 19:42:16 +02:00
configure_lsb "${rootfs}"
clean_up_image "${rootfs}"
2017-05-08 18:19:36 +02:00
msg "Done [Base installation] (rootfs)"
2016-09-15 23:58:18 +02:00
fi
2014-12-10 19:06:52 +01:00
}
2016-10-12 12:32:54 +02:00
make_image_desktop() {
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/desktopfs.lock ]]; then
2016-10-11 17:56:23 +02:00
msg "Prepare [Desktop installation] (desktopfs)"
local desktopfs="${work_dir}/desktopfs"
prepare_dir "${desktopfs}"
mount_fs "${desktopfs}" "${work_dir}"
create_chroot "${mkchroot_args[@]}" "${desktopfs}" "${packages[@]}"
2015-06-29 04:08:45 +02:00
2017-05-21 22:44:08 +02:00
copy_overlay "${desktop_overlay}" "${desktopfs}"
umount_fs
clean_up_image "${desktopfs}"
2017-05-08 18:19:36 +02:00
2016-10-11 17:56:23 +02:00
msg "Done [Desktop installation] (desktopfs)"
2016-09-15 23:58:18 +02:00
fi
2016-02-25 15:51:25 +01:00
}
2015-12-18 18:25:13 +01:00
make_image_live() {
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/livefs.lock ]]; then
msg "Prepare [Live installation] (livefs)"
local livefs="${work_dir}/livefs"
prepare_dir "${livefs}"
mount_fs "${livefs}" "${work_dir}" "${desktop_list}"
create_chroot "${mkchroot_args[@]}" "${livefs}" "${packages[@]}"
2015-06-29 04:08:45 +02:00
2017-05-21 22:44:08 +02:00
copy_overlay "${live_overlay}" "${livefs}"
configure_live_image "${livefs}"
2017-06-06 23:29:04 +02:00
pacman -Qr "${livefs}" > ${iso_dir}/$(gen_iso_fn)-pkgs.txt
2016-05-20 19:42:16 +02:00
umount_fs
clean_up_image "${livefs}"
2017-05-08 18:19:36 +02:00
msg "Done [Live installation] (livefs)"
2016-09-15 23:58:18 +02:00
fi
2014-12-15 23:12:04 +01:00
}
make_image_mhwd() {
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/mhwdfs.lock ]]; then
msg "Prepare [drivers repository] (mhwdfs)"
2017-06-08 11:15:37 +02:00
local mhwdfs="${work_dir}/mhwdfs" repo="/opt/pkg"
2017-06-08 11:15:37 +02:00
prepare_dir "${mhwdfs}${repo}"
mount_fs "${mhwdfs}" "${work_dir}" "${desktop_list}"
2015-06-29 04:08:45 +02:00
2017-06-08 11:15:37 +02:00
copy_from_cache "${mhwdfs}" "${repo}" "${packages[@]}"
2015-07-04 12:09:39 +02:00
if [[ -n "${packages_cleanup[@]}" ]]; then
for pkg in ${packages_cleanup[@]}; do
2017-06-08 11:15:37 +02:00
rm ${mhwdfs}${repo}/${pkg}
2016-09-15 23:58:18 +02:00
done
fi
2017-05-08 18:19:36 +02:00
2017-06-08 11:15:37 +02:00
make_repo "${mhwdfs}" "${repo}"
configure_mhwd_drivers "${mhwdfs}" "${repo}"
umount_fs
clean_up_image "${mhwdfs}"
2017-05-08 18:19:36 +02:00
: > ${work_dir}/mhwdfs.lock
msg "Done [drivers repository] (mhwdfs)"
2016-09-15 23:58:18 +02:00
fi
2014-12-08 23:50:56 +01:00
}
2015-01-13 04:48:04 +01:00
make_image_boot() {
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/bootfs.lock ]]; then
msg "Prepare [/iso/boot]"
local boot="${iso_root}/boot"
2017-03-27 18:08:13 +02:00
prepare_dir "${boot}"
2016-10-12 12:32:54 +02:00
cp ${work_dir}/rootfs/boot/vmlinuz* ${boot}/vmlinuz-${target_arch}
2016-10-12 12:32:54 +02:00
local bootfs="${work_dir}/bootfs"
2015-06-25 09:59:35 +02:00
mount_fs "${bootfs}" "${work_dir}" "${desktop_list}"
2015-06-25 09:59:35 +02:00
prepare_initcpio "${bootfs}"
prepare_initramfs "${bootfs}"
2015-06-29 04:08:45 +02:00
cp ${bootfs}/boot/initramfs.img ${boot}/initramfs-${target_arch}.img
prepare_boot_extras "${bootfs}" "${boot}"
2015-06-25 09:59:35 +02:00
umount_fs
2015-06-25 09:59:35 +02:00
rm -R ${bootfs}
2017-05-08 18:19:36 +02:00
: > ${work_dir}/bootfs.lock
msg "Done [/iso/boot]"
2016-09-15 23:58:18 +02:00
fi
2014-12-08 23:50:56 +01:00
}
2017-03-27 18:08:13 +02:00
configure_grub(){
2017-04-28 10:30:34 +02:00
local conf="$1"
2017-05-21 23:12:31 +02:00
local default_args="misobasedir=${os_id} misolabel=${iso_label}" boot_args=('quiet')
2017-03-27 18:08:13 +02:00
[[ ${initsys} == 'systemd' ]] && boot_args+=('systemd.show_status=1')
sed -e "s|@DIST_NAME@|${dist_name}|g" \
-e "s|@ARCH@|${target_arch}|g" \
-e "s|@DEFAULT_ARGS@|${default_args}|g" \
-e "s|@BOOT_ARGS@|${boot_args[*]}|g" \
-e "s|@PROFILE@|${profile}|g" \
2017-04-28 10:30:34 +02:00
-i $conf
2017-03-27 18:08:13 +02:00
}
configure_grub_theme(){
2017-04-28 10:30:34 +02:00
local conf="$1"
2017-05-21 23:12:31 +02:00
sed -e "s|@ISO_NAME@|${os_id}|" -i "$conf"
}
make_grub(){
2017-05-08 18:19:36 +02:00
if [[ ! -e ${work_dir}/grub.lock ]]; then
msg "Prepare [/iso/boot/grub]"
2017-03-27 18:08:13 +02:00
prepare_grub "${work_dir}/rootfs" "${work_dir}/livefs" "${iso_root}"
2017-03-27 18:08:13 +02:00
configure_grub "${iso_root}/boot/grub/kernels.cfg"
configure_grub_theme "${iso_root}/boot/grub/variable.cfg"
2017-03-27 18:08:13 +02:00
2017-05-08 18:19:36 +02:00
: > ${work_dir}/grub.lock
msg "Done [/iso/boot/grub]"
fi
}
check_requirements(){
2017-04-28 10:30:34 +02:00
prepare_dir "${log_dir}"
2017-04-28 10:30:34 +02:00
eval_build_list "${list_dir_iso}" "${build_list_iso}"
[[ -f ${run_dir}/repo_info ]] || die "%s is not a valid iso profiles directory!" "${run_dir}"
2016-09-15 23:58:18 +02:00
local iso_kernel=${kernel:5:1} host_kernel=$(uname -r)
if [[ ${iso_kernel} < "4" ]] \
|| [[ ${host_kernel%%*.} < "4" ]];then
die "The host and iso kernels must be version>=4.0!"
2016-09-15 23:58:18 +02:00
fi
2017-04-28 10:30:34 +02:00
for sig in TERM HUP QUIT; do
trap "trap_exit $sig \"$(gettext "%s signal caught. Exiting...")\" \"$sig\"" "$sig"
done
trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT
trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR
}
2014-12-31 01:02:43 +01:00
compress_images(){
2016-09-15 23:58:18 +02:00
local timer=$(get_timer)
run_safe "make_iso"
user_own "${iso_dir}" "-R"
show_elapsed_time "${FUNCNAME}" "${timer}"
2014-12-31 01:02:43 +01:00
}
2016-02-25 06:40:39 +01:00
prepare_images(){
2016-09-15 23:58:18 +02:00
local timer=$(get_timer)
load_pkgs "${root_list}" "${target_arch}" "${edition}" "${initsys}" "${kernel}"
2016-09-15 23:58:18 +02:00
run_safe "make_image_root"
2017-04-30 09:13:19 +02:00
if [[ -f "${desktop_list}" ]] ; then
load_pkgs "${desktop_list}" "${target_arch}" "${edition}" "${initsys}" "${kernel}"
2016-10-12 12:32:54 +02:00
run_safe "make_image_desktop"
2016-09-15 23:58:18 +02:00
fi
2017-05-10 23:11:42 +02:00
if [[ -f ${live_list} ]]; then
load_pkgs "${live_list}" "${target_arch}" "${edition}" "${initsys}" "${kernel}"
2016-09-15 23:58:18 +02:00
run_safe "make_image_live"
fi
2017-05-10 23:11:42 +02:00
if ! ${netinstall} ; then
load_pkgs "${mhwd_list}" "${target_arch}" "${edition}" "${initsys}" "${kernel}"
2016-09-15 23:58:18 +02:00
run_safe "make_image_mhwd"
fi
run_safe "make_image_boot"
run_safe "make_grub"
2017-03-27 18:08:13 +02:00
2016-09-15 23:58:18 +02:00
show_elapsed_time "${FUNCNAME}" "${timer}"
}
archive_logs(){
local name=$(gen_iso_fn) ext=log.tar.xz src=${tmp_dir}/archives.list
find ${log_dir} -maxdepth 1 -name "$name*.log" -printf "%f\n" > $src
msg2 "Archiving log files [%s] ..." "$name.$ext"
tar -cJf ${log_dir}/$name.$ext -C ${log_dir} -T $src
msg2 "Cleaning log files ..."
find ${log_dir} -maxdepth 1 -name "$name*.log" -delete
}
make_profile(){
2016-09-15 23:58:18 +02:00
msg "Start building [%s]" "${profile}"
if ${clean_first};then
chroot_clean "${chroots_iso}/${profile}/${target_arch}"
2017-03-27 18:08:13 +02:00
local unused_arch='i686'
if [[ ${target_arch} == 'i686' ]];then
2017-06-08 11:15:37 +02:00
unused_arch='x86_64'
fi
if [[ -d "${chroots_iso}/${profile}/${unused_arch}" ]];then
chroot_clean "${chroots_iso}/${profile}/${unused_arch}"
fi
clean_iso_root "${iso_root}"
fi
2017-03-27 18:08:13 +02:00
2016-09-15 23:58:18 +02:00
if ${iso_only}; then
[[ ! -d ${work_dir} ]] && die "Create images: buildiso -p %s -x" "${profile}"
compress_images
${verbose} && archive_logs
exit 1
fi
if ${images_only}; then
prepare_images
${verbose} && archive_logs
warning "Continue compress: buildiso -p %s -zc ..." "${profile}"
exit 1
else
prepare_images
compress_images
${verbose} && archive_logs
fi
reset_profile
msg "Finished building [%s]" "${profile}"
show_elapsed_time "${FUNCNAME}" "${timer_start}"
}
2016-02-25 06:40:39 +01:00
build(){
2017-04-28 10:30:34 +02:00
local prof="$1"
prepare_build "$prof"
2016-09-15 23:58:18 +02:00
make_profile
2016-02-25 06:40:39 +01:00
}