diff --git a/bin/basestrap.in b/bin/basestrap.in index 358a10a..85ed6b0 100644 --- a/bin/basestrap.in +++ b/bin/basestrap.in @@ -19,6 +19,7 @@ LIBDIR='@libdir@' import ${LIBDIR}/util.sh import ${LIBDIR}/util-mount.sh +import ${LIBDIR}/util-chroot.sh newroot=/mnt diff --git a/bin/buildpkg.in b/bin/buildpkg.in index 49a2382..744700a 100644 --- a/bin/buildpkg.in +++ b/bin/buildpkg.in @@ -20,6 +20,7 @@ SYSCONFDIR='@sysconfdir@' [[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh import ${LIBDIR}/util.sh +import ${LIBDIR}/util-chroot.sh import ${LIBDIR}/util-pkg.sh import ${LIBDIR}/util-pkg-chroot.sh diff --git a/bin/buildtree.in b/bin/buildtree.in index abc0e25..32363a3 100644 --- a/bin/buildtree.in +++ b/bin/buildtree.in @@ -16,7 +16,7 @@ SYSCONFDIR='@sysconfdir@' [[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh import ${LIBDIR}/util.sh -import ${LIBDIR}/util-pkgtree.sh +import ${LIBDIR}/util-pkg-tree.sh display_settings(){ show_version diff --git a/bin/mkchroot.in b/bin/mkchroot.in index 22528d3..827fe7f 100644 --- a/bin/mkchroot.in +++ b/bin/mkchroot.in @@ -16,6 +16,7 @@ LIBDIR='@libdir@' [[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh import ${LIBDIR}/util.sh +import ${LIBDIR}/util-chroot.sh working_dir='' files=() diff --git a/bin/mkchrootpkg.in b/bin/mkchrootpkg.in index 8d1580a..279cd2c 100644 --- a/bin/mkchrootpkg.in +++ b/bin/mkchrootpkg.in @@ -16,6 +16,7 @@ LIBDIR='@libdir@' [[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh import ${LIBDIR}/util.sh +import ${LIBDIR}/util-chroot.sh shopt -s nullglob diff --git a/lib/util-chroot.sh b/lib/util-chroot.sh index 531be8a..e45406f 100644 --- a/lib/util-chroot.sh +++ b/lib/util-chroot.sh @@ -8,6 +8,16 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. +copy_mirrorlist(){ + cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/" +} + +copy_keyring(){ + if [[ -d /etc/pacman.d/gnupg ]] && [[ ! -d $1/etc/pacman.d/gnupg ]]; then + cp -a /etc/pacman.d/gnupg "$1/etc/pacman.d/" + fi +} + create_min_fs(){ msg "Creating install root at %s" "$1" mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc} @@ -15,15 +25,6 @@ create_min_fs(){ mkdir -m 0555 -p $1/{sys,proc} } -check_root() { - (( EUID == 0 )) && return - if type -P sudo >/dev/null; then - exec sudo -- "${orig_argv[@]}" - else - exec su root -c "$(printf ' %q' "${orig_argv[@]}")" - fi -} - is_btrfs() { [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]] } @@ -42,3 +43,26 @@ subvolume_delete_recursive() { return 0 } + +# $1: chroot +# kill_chroot_process(){ +# # enable to have more debug info +# #msg "machine-id (etc): $(cat $1/etc/machine-id)" +# #[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)" +# #msg "running processes: " +# #lsof | grep $1 +# +# local prefix="$1" flink pid name +# for root_dir in /proc/*/root; do +# flink=$(readlink $root_dir) +# if [ "x$flink" != "x" ]; then +# if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then +# # this process is in the chroot... +# pid=$(basename $(dirname "$root_dir")) +# name=$(ps -p $pid -o comm=) +# info "Killing chroot process: %s (%s)" "$name" "$pid" +# kill -9 "$pid" +# fi +# fi +# done +# } diff --git a/lib/util-iso-chroot.sh b/lib/util-iso-chroot.sh index 704a9b9..99e8cf2 100644 --- a/lib/util-iso-chroot.sh +++ b/lib/util-iso-chroot.sh @@ -9,10 +9,230 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. +copy_overlay(){ + if [[ -e $1 ]];then + msg2 "Copying [%s] ..." "${1##*/}" + if [[ -L $1 ]];then + cp -a --no-preserve=ownership $1/* $2 + else + cp -LR $1/* $2 + fi + fi +} + +add_svc_rc(){ + if [[ -f $1/etc/init.d/$2 ]];then + msg2 "Setting %s ..." "$2" + chroot $1 rc-update add $2 default &>/dev/null + fi +} + +add_svc_sd(){ + if [[ -f $1/etc/systemd/system/$2.service ]] || \ + [[ -f $1/usr/lib/systemd/system/$2.service ]];then + msg2 "Setting %s ..." "$2" + chroot $1 systemctl enable $2 &>/dev/null + fi +} + +set_xdm(){ + if [[ -f $1/etc/conf.d/xdm ]];then + local conf='DISPLAYMANAGER="'${displaymanager}'"' + sed -i -e "s|^.*DISPLAYMANAGER=.*|${conf}|" $1/etc/conf.d/xdm + fi +} + +configure_mhwd_drivers(){ + local path=$1${mhwd_repo}/ \ + drv_path=$1/var/lib/mhwd/db/pci/graphic_drivers + info "Configuring mwwd db ..." + if [ -z "$(ls $path | grep catalyst-utils 2> /dev/null)" ]; then + msg2 "Disabling Catalyst driver" + mkdir -p $drv_path/catalyst/ + touch $drv_path/catalyst/MHWDCONFIG + fi + if [ -z "$(ls $path | grep nvidia-utils 2> /dev/null)" ]; then + msg2 "Disabling Nvidia driver" + mkdir -p $drv_path/nvidia/ + touch $drv_path/nvidia/MHWDCONFIG + msg2 "Disabling Nvidia Bumblebee driver" + mkdir -p $drv_path/hybrid-intel-nvidia-bumblebee/ + touch $drv_path/hybrid-intel-nvidia-bumblebee/MHWDCONFIG + fi + if [ -z "$(ls $path | grep nvidia-304xx-utils 2> /dev/null)" ]; then + msg2 "Disabling Nvidia 304xx driver" + mkdir -p $drv_path/nvidia-304xx/ + touch $drv_path/nvidia-304xx/MHWDCONFIG + fi + if [ -z "$(ls $path | grep nvidia-340xx-utils 2> /dev/null)" ]; then + msg2 "Disabling Nvidia 340xx driver" + mkdir -p $drv_path/nvidia-340xx/ + touch $drv_path/nvidia-340xx/MHWDCONFIG + fi + if [ -z "$(ls $path | grep xf86-video-amdgpu 2> /dev/null)" ]; then + msg2 "Disabling AMD gpu driver" + mkdir -p $drv_path/xf86-video-amdgpu/ + touch $drv_path/xf86-video-amdgpu/MHWDCONFIG + fi +} + configure_hosts(){ sed -e "s|localhost.localdomain|localhost.localdomain ${hostname}|" -i $1/etc/hosts } +configure_lsb(){ + if [ -e $1/etc/lsb-release ] ; then + msg2 "Configuring lsb-release" + sed -i -e "s/^.*DISTRIB_RELEASE.*/DISTRIB_RELEASE=${dist_release}/" $1/etc/lsb-release + sed -i -e "s/^.*DISTRIB_CODENAME.*/DISTRIB_CODENAME=${dist_codename}/" $1/etc/lsb-release + fi +} + +configure_logind(){ + msg2 "Configuring logind ..." + local conf=$1/etc/$2/logind.conf + sed -i 's/#\(HandleSuspendKey=\)suspend/\1ignore/' "$conf" + sed -i 's/#\(HandleLidSwitch=\)suspend/\1ignore/' "$conf" + sed -i 's/#\(HandleHibernateKey=\)hibernate/\1ignore/' "$conf" +} + +configure_journald(){ + msg2 "Configuring journald ..." + local conf=$1/etc/systemd/journald.conf + sed -i 's/#\(Storage=\)auto/\1volatile/' "$conf" +} + +configure_services(){ + info "Configuring [%s]" "${initsys}" + case ${initsys} in + 'openrc') + for svc in ${enable_openrc[@]}; do + [[ $svc == "xdm" ]] && set_xdm "$1" + add_svc_rc "$1" "$svc" + done + for svc in ${enable_openrc_live[@]}; do + add_svc_rc "$1" "$svc" + done + ;; + 'systemd') + for svc in ${enable_systemd[@]}; do + add_svc_sd "$1" "$svc" + done + for svc in ${enable_systemd_live[@]}; do + add_svc_sd "$1" "$svc" + done + ;; + esac + info "Done configuring [%s]" "${initsys}" +} + +write_live_session_conf(){ + local path=$1${SYSCONFDIR} + [[ ! -d $path ]] && mkdir -p $path + local conf=$path/live.conf + msg2 "Writing %s" "${conf##*/}" + echo '# live session configuration' > ${conf} + echo '' >> ${conf} + echo '# autologin' >> ${conf} + echo "autologin=${autologin}" >> ${conf} + echo '' >> ${conf} + echo '# login shell' >> ${conf} + echo "login_shell=${login_shell}" >> ${conf} + echo '' >> ${conf} + echo '# live username' >> ${conf} + echo "username=${username}" >> ${conf} + echo '' >> ${conf} + echo '# live password' >> ${conf} + echo "password=${password}" >> ${conf} + echo '' >> ${conf} + echo '# live group membership' >> ${conf} + echo "addgroups='${addgroups}'" >> ${conf} + if [[ -n ${smb_workgroup} ]];then + echo '' >> ${conf} + echo '# samba workgroup' >> ${conf} + echo "smb_workgroup=${smb_workgroup}" >> ${conf} + fi +} + +configure_system(){ + case ${initsys} in + 'systemd') + configure_logind "$1" "systemd" + configure_journald "$1" + + # Prevent some services to be started in the livecd + echo 'File created by manjaro-tools. See systemd-update-done.service(8).' \ + | tee "${path}/etc/.updated" >"${path}/var/.updated" + + msg2 "Disable systemd-gpt-auto-generator" + ln -sf /dev/null "${path}/usr/lib/systemd/system-generators/systemd-gpt-auto-generator" + ;; + 'openrc') + configure_logind "$1" "elogind" +# local hn='hostname="'${hostname}'"' +# sed -i -e "s|^.*hostname=.*|${hn}|" $1/etc/conf.d/hostname + ;; + esac + echo ${hostname} > $1/etc/hostname +} + +make_repo(){ + repo-add $1${mhwd_repo}/mhwd.db.tar.gz $1${mhwd_repo}/*pkg*z +} + +clean_iso_root(){ + msg2 "Deleting isoroot [%s] ..." "${1##*/}" + rm -rf --one-file-system "$1" +} + +clean_up_image(){ + msg2 "Cleaning [%s]" "${1##*/}" + + local path + if [[ ${1##*/} == 'mhwdfs' ]];then + path=$1/var + if [[ -d $path ]];then + find "$path" -mindepth 0 -delete &> /dev/null + fi + path=$1/etc + if [[ -d $path ]];then + find "$path" -mindepth 0 -delete &> /dev/null + fi + else + [[ -f "$1/etc/locale.gen.bak" ]] && mv "$1/etc/locale.gen.bak" "$1/etc/locale.gen" + [[ -f "$1/etc/locale.conf.bak" ]] && mv "$1/etc/locale.conf.bak" "$1/etc/locale.conf" + path=$1/boot + if [[ -d "$path" ]]; then + find "$path" -name 'initramfs*.img' -delete &> /dev/null + fi + path=$1/var/lib/pacman/sync + if [[ -d $path ]];then + find "$path" -type f -delete &> /dev/null + fi + path=$1/var/cache/pacman/pkg + if [[ -d $path ]]; then + find "$path" -type f -delete &> /dev/null + fi + path=$1/var/log + if [[ -d $path ]]; then + find "$path" -type f -delete &> /dev/null + fi + path=$1/var/tmp + if [[ -d $path ]];then + find "$path" -mindepth 1 -delete &> /dev/null + fi + path=$1/tmp + if [[ -d $path ]];then + find "$path" -mindepth 1 -delete &> /dev/null + fi + fi + find "$1" -name *.pacnew -name *.pacsave -name *.pacorig -delete + file=$1/boot/grub/grub.cfg + if [[ -f "$file" ]]; then + rm $file + fi +} + copy_from_cache(){ local list="${tmp_dir}"/mhwd-cache.list chroot-run \ @@ -57,3 +277,90 @@ chroot_clean(){ exec 9>&- rm -rf --one-file-system "$1" } + +prepare_initcpio(){ + msg2 "Copying initcpio ..." + cp /etc/initcpio/hooks/miso* $1/etc/initcpio/hooks + cp /etc/initcpio/install/miso* $1/etc/initcpio/install + cp /etc/initcpio/miso_shutdown $1/etc/initcpio +} + +prepare_initramfs(){ + cp ${DATADIR}/mkinitcpio.conf $1/etc/mkinitcpio-${iso_name}.conf + local _kernver=$(cat $1/usr/lib/modules/*/version) + if [[ -n ${gpgkey} ]]; then + su ${OWNER} -c "gpg --export ${gpgkey} >${MT_USERCONFDIR}/gpgkey" + exec 17<>${MT_USERCONFDIR}/gpgkey + fi + MISO_GNUPG_FD=${gpgkey:+17} chroot-run $1 \ + /usr/bin/mkinitcpio -k ${_kernver} \ + -c /etc/mkinitcpio-${iso_name}.conf \ + -g /boot/initramfs.img + + if [[ -n ${gpgkey} ]]; then + exec 17<&- + fi + if [[ -f ${MT_USERCONFDIR}/gpgkey ]]; then + rm ${MT_USERCONFDIR}/gpgkey + fi +} + +prepare_boot_extras(){ + cp $1/boot/intel-ucode.img $2/intel_ucode.img + cp $1/usr/share/licenses/intel-ucode/LICENSE $2/intel_ucode.LICENSE + cp $1/boot/memtest86+/memtest.bin $2/memtest + cp $1/usr/share/licenses/common/GPL2/license.txt $2/memtest.COPYING +} + +prepare_grub(){ + local platform=i386-pc img='core.img' grub=$3/boot/grub efi=$3/efi/boot \ + lib=$1/usr/lib/grub prefix=/boot/grub theme=$2/usr/share/grub data=$1/usr/share/grub + + prepare_dir ${grub}/${platform} + + cp ${theme}/cfg/*.cfg ${grub} + + cp ${lib}/${platform}/* ${grub}/${platform} + + msg2 "Building %s ..." "${img}" + + grub-mkimage -d ${grub}/${platform} -o ${grub}/${platform}/${img} -O ${platform} -p ${prefix} biosdisk iso9660 + + cat ${grub}/${platform}/cdboot.img ${grub}/${platform}/${img} > ${grub}/${platform}/eltorito.img + + case ${target_arch} in + 'i686') + platform=i386-efi + img=bootia32.efi + ;; + 'x86_64') + platform=x86_64-efi + img=bootx64.efi + ;; + esac + + prepare_dir ${efi} + prepare_dir ${grub}/${platform} + + cp ${lib}/${platform}/* ${grub}/${platform} + + msg2 "Building %s ..." "${img}" + + grub-mkimage -d ${grub}/${platform} -o ${efi}/${img} -O ${platform} -p ${prefix} iso9660 + + prepare_dir ${grub}/themes + cp -r ${theme}/themes/${iso_name}-live ${grub}/themes/ + cp ${data}/unicode.pf2 ${grub} + cp -r ${theme}/{locales,tz} ${grub} + + local size=4M mnt="${mnt_dir}/efiboot" efi_img="$3/efi.img" + msg2 "Creating fat image of %s ..." "${size}" + truncate -s ${size} "${efi_img}" + mkfs.fat -n MISO_EFI "${efi_img}" &>/dev/null + prepare_dir "${mnt}" + mount_img "${efi_img}" "${mnt}" + prepare_dir ${mnt}/efi/boot + msg2 "Building %s ..." "${img}" + grub-mkimage -d ${grub}/${platform} -o ${mnt}/efi/boot/${img} -O ${platform} -p ${prefix} iso9660 + umount_img "${mnt}" +} diff --git a/lib/util-iso.sh b/lib/util-iso.sh index 72ad012..14500cf 100644 --- a/lib/util-iso.sh +++ b/lib/util-iso.sh @@ -9,8 +9,7 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -import ${LIBDIR}/util-iso-image.sh -import ${LIBDIR}/util-iso-boot.sh +import ${LIBDIR}/util-iso-chroot.sh import ${LIBDIR}/util-yaml.sh error_function() { @@ -68,6 +67,40 @@ trap_exit() { kill "-$sig" "$$" } +configure_thus(){ + msg2 "Configuring Thus ..." + source "$1/etc/mkinitcpio.d/${kernel}.preset" + local conf="$1/etc/thus.conf" + echo "[distribution]" > "$conf" + echo "DISTRIBUTION_NAME = \"${dist_name} Linux\"" >> "$conf" + echo "DISTRIBUTION_VERSION = \"${dist_release}\"" >> "$conf" + echo "SHORT_NAME = \"${dist_name}\"" >> "$conf" + echo "[install]" >> "$conf" + echo "LIVE_MEDIA_SOURCE = \"/run/miso/bootmnt/${iso_name}/${target_arch}/rootfs.sfs\"" >> "$conf" + echo "LIVE_MEDIA_DESKTOP = \"/run/miso/bootmnt/${iso_name}/${target_arch}/desktopfs.sfs\"" >> "$conf" + echo "LIVE_MEDIA_TYPE = \"squashfs\"" >> "$conf" + echo "LIVE_USER_NAME = \"${username}\"" >> "$conf" + echo "KERNEL = \"${kernel}\"" >> "$conf" + echo "VMLINUZ = \"$(echo ${ALL_kver} | sed s'|/boot/||')\"" >> "$conf" + echo "INITRAMFS = \"$(echo ${default_image} | sed s'|/boot/||')\"" >> "$conf" + echo "FALLBACK = \"$(echo ${fallback_image} | sed s'|/boot/||')\"" >> "$conf" + + if [[ -f $1/usr/share/applications/thus.desktop && -f $1/usr/bin/kdesu ]];then + sed -i -e 's|sudo|kdesu|g' $1/usr/share/applications/thus.desktop + fi +} + +configure_live_image(){ + msg "Configuring [livefs]" + configure_hosts "$1" + configure_system "$1" + configure_services "$1" + configure_calamares "$1" + [[ ${edition} == "sonar" ]] && configure_thus "$1" + write_live_session_conf "$1" + msg "Done configuring [livefs]" +} + make_sig () { msg2 "Creating signature file..." cd "$1" diff --git a/lib/util-pkgtree.sh b/lib/util-pkg-tree.sh similarity index 100% rename from lib/util-pkgtree.sh rename to lib/util-pkg-tree.sh diff --git a/lib/util-pkg.sh b/lib/util-pkg.sh index 39b5ff4..89a633c 100644 --- a/lib/util-pkg.sh +++ b/lib/util-pkg.sh @@ -9,6 +9,15 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. +in_array() { + local needle=$1; shift + local item + for item in "$@"; do + [[ $item = $needle ]] && return 0 # Found + done + return 1 # Not Found +} + # $1: sofile # $2: soarch process_sofile() { diff --git a/lib/util.sh b/lib/util.sh index 8c04053..2cf6e8b 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -110,15 +110,6 @@ eval_build_list(){ esac" } -in_array() { - local needle=$1; shift - local item - for item in "$@"; do - [[ $item = $needle ]] && return 0 # Found - done - return 1 # Not Found -} - get_timer(){ echo $(date +%s) } @@ -132,16 +123,6 @@ show_elapsed_time(){ info "Time %s: %s minutes" "$1" "$(elapsed_time $2)" } -copy_mirrorlist(){ - cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/" -} - -copy_keyring(){ - if [[ -d /etc/pacman.d/gnupg ]] && [[ ! -d $1/etc/pacman.d/gnupg ]]; then - cp -a /etc/pacman.d/gnupg "$1/etc/pacman.d/" - fi -} - load_vars() { local var @@ -663,13 +644,6 @@ show_config(){ fi } -create_min_fs(){ - msg "Creating install root at %s" "$1" - mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc} - mkdir -m 1777 -p $1/tmp - mkdir -m 0555 -p $1/{sys,proc} -} - is_valid_init(){ case $1 in 'openrc'|'systemd') return 0 ;; @@ -716,45 +690,3 @@ check_root() { exec su root -c "$(printf ' %q' "${orig_argv[@]}")" fi } - -is_btrfs() { - [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]] -} - -subvolume_delete_recursive() { - local subvol - - is_btrfs "$1" || return 0 - - while IFS= read -d $'\0' -r subvol; do - if ! btrfs subvolume delete "$subvol" &>/dev/null; then - error "Unable to delete subvolume %s" "$subvol" - return 1 - fi - done < <(find "$1" -xdev -depth -inum 256 -print0) - - return 0 -} - -# $1: chroot -# kill_chroot_process(){ -# # enable to have more debug info -# #msg "machine-id (etc): $(cat $1/etc/machine-id)" -# #[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)" -# #msg "running processes: " -# #lsof | grep $1 -# -# local prefix="$1" flink pid name -# for root_dir in /proc/*/root; do -# flink=$(readlink $root_dir) -# if [ "x$flink" != "x" ]; then -# if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then -# # this process is in the chroot... -# pid=$(basename $(dirname "$root_dir")) -# name=$(ps -p $pid -o comm=) -# info "Killing chroot process: %s (%s)" "$name" "$pid" -# kill -9 "$pid" -# fi -# fi -# done -# }