diff --git a/blfsSetup.sh b/blfsSetup.sh index 610e4a7..e71371c 100755 --- a/blfsSetup.sh +++ b/blfsSetup.sh @@ -1,15 +1,27 @@ #!/bin/bash -# Check if the lfs mount point folder is specified as the first argument -if [ -z "$1" ] -then - echo "Please specify the lfs mount point folder as the first argument!" >&2 - exit 1 -fi +_build_blfs() +{ + # First include the needed external script files + local current_dir="$(dirname "$0")" + source $current_dir/libs/func_general.sh -# Run lfsSetup and reboot if success otherwise power off in 5 minutes -if ! ./lfsSetup.sh "$1" "$2" ; then - sudo shutdown --poweroff +5 -else + # Run lfsSetup + ./lfsSetup.sh "$1" "$2" || return 1 + + # Setup autologin + local dir_lfs="$(realpath "$1")" + _setup_autologin "$dir_lfs" || return 1 + + # Create new blfs config + _create_blfs_config "$dir_lfs" || return 1 + + # Create autologin script to run blfs build after reboot + _create_blfs_builder_script "$dir_lfs" || return 1 + + # Reboot system sudo systemctl reboot -fi \ No newline at end of file +} + +# Shutdown on failure +_build_blfs "$1" "$2" || sudo shutdown --poweroff +5 \ No newline at end of file diff --git a/lfsSetup.sh b/lfsSetup.sh index 897b6f2..72a459e 100755 --- a/lfsSetup.sh +++ b/lfsSetup.sh @@ -1,130 +1,60 @@ #!/bin/bash -# Script that setups LFS installer -# Check if the lfs mount point folder is specified as the first argument -if [ -z "$1" ] ; then - echo "Please specify the lfs mount point folder as the first argument!" >&2 - exit 1 -fi +# Function that setups and builds an LFS system +# First parameter is mandatory and it is the new LFS system's root mount point +_build_lfs() +{ + # Check if the lfs mount point folder is specified as the first argument + if [ -z "$1" ] ; then + echo "Please specify the lfs mount point folder as the first argument!" >&2 + return 1 + fi + local dir_lfs="$(realpath "$1")" -# Create folder structure and get LFS and BLFS sources -source lib/func_general.sh -_create_folders_and_get_sources "$1" || return 1 + # First include the needed external script files + local current_dir="$(dirname "$0")" + source $current_dir/libs/func_general.sh + source $current_dir/libs/func_kernel.sh + source $current_dir/libs/func_network.sh + source $current_dir/libs/func_fstab.sh + source $current_dir/libs/func_grub.sh -# Patch the LFS book's packages.ent with latest kernel -source libs/func_kernel.sh -_update_lfs_with_latest_kernel $1 || return 1 + # Create folder structure and get LFS and BLFS sources + _create_folders_and_get_sources "$dir_lfs" || return 1 -# Check for the kernel config and try to create it based on a previous one if not found -_create_kernel_config_if_needed "$1" || return 1 + # Patch the LFS book's packages.ent with latest kernel + _update_lfs_with_latest_kernel $dir_lfs || return 1 -# Check for possible needed firmwares defined in kernel config -_check_and_copy_needed_firmwares || return 1 + # Check for the kernel config and try to create it based on a previous one if not found + _create_kernel_config_if_needed "$dir_lfs" || return 1 -# Patch jhalfs sources -_patch_jhalfs_sources "$1" "$2" || return 1 + # Check for possible needed firmwares defined in kernel config + _check_and_copy_needed_firmwares "$dir_lfs" || return 1 -# Enter to setup folder and start installer -cd "$1/setup" || return 1 -yes "yes" | ./jhalfs run -if [[ $? -gt 0 ]] ; then return 1; fi + # Patch jhalfs sources + _patch_jhalfs_sources "$dir_lfs" "$2" || return 1 -# Patch network script -_patch_network_scripts "$1" || return 1 + # Enter to setup folder and start installer + cd "$dir_lfs/setup" || return 1 + yes "yes" | ./jhalfs run + if [[ $? -gt 0 ]] ; then return 1; fi -# Patch fstab script -source libs/func_fstab.sh -_patch_fstab $1 || return 1 + # Patch network script + _patch_network_scripts "$dir_lfs" || return 1 -# Patch LFS kernel script to keep build folder and add new user -_patch_kernel_script "$1" || return 1 + # Patch fstab script + _patch_fstab $dir_lfs || return 1 -# Patch grub script -_patch_grub_script "$1" || return 1 + # Patch LFS kernel script to keep build folder and add new user + _patch_kernel_script "$dir_lfs" || return 1 -# Enter to jhalfs folder and start the build -cd "$DIR_JHALFS" -make + # Patch grub script + _patch_grub_script "$dir_lfs" || return 1 -# Patch the sudoers file -sudo sed -i "/^root ALL=(ALL:ALL) ALL/a pkr ALL=(ALL:ALL) NOPASSWD: ALL" "$1/etc/sudoers" + # Enter to jhalfs folder and start the build + cd "$dir_lfs/jhalfs" || return 1 + make || return 1 +} -# Move blfs folder to pkr home folder -sudo mv -v "$1/blfs_root" "$1/home/pkr/" -sudo chown -hR pkr:pkr "$1/home/pkr/blfs_root" -sudo chown -hR pkr:pkr "$1/var/lib/jhalfs" -sudo sed -i "s|/blfs_root/packdesc.dtd|/home/pkr/blfs_root/packdesc.dtd|g" "$1/var/lib/jhalfs/BLFS/instpkg.xml" - -# Create autologin script to run blfs build after reboot -local dir_autologin="$1/etc/systemd/system/getty@tty1.service.d" -sudo mkdir -pv "$dir_autologin" -printf "[Service]\nType=simple\nExecStart=\nExecStart=-/sbin/agetty --autologin pkr %%I 38400 linux\n" | sudo tee "$dir_autologin/override.conf" > /dev/null - -cat > "$1/home/pkr/.profile" << EOF -#!/bin/bash -cd "/home/pkr/blfs_root/blfs-xml" -git reset --hard -git clean -xfd -cd "/home/pkr/blfs_root/lfs-xml" -git reset --hard -git clean -xfd -cd "/home/pkr/blfs_root" -make update -. gen_pkg_book.sh <<< yes -cd work -../gen-makefile.sh -make -sudo rm -rfv /etc/systemd/system/getty@tty1.service.d -rm -fv /home/pkr/.profile -sudo systemctl poweroff -EOF - -# Create new blfs config -local dir_blfscfg="$1/home/pkr/blfs_root/configuration" -if [ -f "$dir_blfscfg" ] ; then - sudo rm -fv "$dir_blfscfg" -fi - -cat > "$dir_blfscfg" << EOF -CONFIG_pciutils=y -CONFIG_twm=y -CONFIG_xinit=y -CONFIG_xorg-evdev-driver=y -CONFIG_xorg-libinput-driver=y -CONFIG_xwayland=y -CONFIG_sddm=y -CONFIG_openbox=y -CONFIG_lxqt-menu-data=y -CONFIG_lxqt-panel=y -CONFIG_lxqt-post-install=y -CONFIG_lxqt-pre-install=y -CONFIG_lxqt-runner=y -CONFIG_lxqt-session=y -CONFIG_lxqt-sudo=y -CONFIG_lxqt-themes=y -CONFIG_pcmanfm-qt=y -CONFIG_lxqt-notificationd=y -CONFIG_pavucontrol-qt=y -CONFIG_qterminal=y -CONFIG_firefox=y - -# Build settings -MS_sendmail=y -MAIL_SERVER="sendmail" -DEPLVL_2=y -optDependency=2 -LANGUAGE="hu_HU.UTF-8" -SUDO=y -DEL_LA_FILES=y - -# Build Layout -SRC_ARCHIVE="/sources" -BUILD_ROOT="/sources" -BUILD_SUBDIRS=y - -# Optimization -JOBS=0 -CFG_CFLAGS=" -O3 -pipe -march=native " -CFG_CXXFLAGS=" -O3 -pipe -march=native " -CFG_LDFLAGS="EMPTY" -EOF +_build_lfs "$1" "$2" || exit 1 +_finalize_lfs_build "$1" diff --git a/libs/func_general.sh b/libs/func_general.sh index 5bbef83..2008385 100644 --- a/libs/func_general.sh +++ b/libs/func_general.sh @@ -19,6 +19,15 @@ _create_folders_and_get_sources() echo Done! fi + # Check if jhalfs folder is already exist and delete it if yes + local dir_blfs="$1/blfs_root" + if [ -d "$dir_blfs" ] + then + echo Deleting old blfs_root folder: "$dir_blfs" + sudo rm -rf "$dir_blfs" || { echo "Failed to delete folder: $dir_blfs" >&2; return 1; } + echo Done! + fi + # Now create the jhalfs folder and book-source sub-folder owned by the current user local dir_book="$dir_jhalfs/book-source" local u="$(id -un)" # current user @@ -32,9 +41,9 @@ _create_folders_and_get_sources() git clone https://git.linuxfromscratch.org/lfs.git "$dir_book" || return 1 # Now create the blfs_root folder and blfs-xml sub-folder owned by the current user - local dir_blfs_book="$1/blfs_root/blfs-xml" + local dir_blfs_book="$dir_blfs/blfs-xml" echo Creating blfs_root folder... - sudo install -v -o "$u" -g root -m 1777 -d "$1/blfs_root" || return 1 + sudo install -v -o "$u" -g root -m 1777 -d "$dir_blfs" || return 1 sudo install -v -o "$u" -g "$g" -m 1777 -d "$dir_blfs_book" || return 1 # Clone the BLFS book repository to blfs-xml folder @@ -94,7 +103,7 @@ _patch_jhalfs_sources() sed -i -E "\@BUILDDIR=\"xxx\"@s@xxx@$1@g" "$file_cfg" && sed -i -E "/^FSTAB=/s/^/#/g" "$file_cfg" && sed -i -E "\@FSTAB=\"xxx\"@s@xxx@/home/$u/fstab@g" "$file_cfg" && - sed -i -E "\@CONFIG=\"xxx\"@s@xxx@/home/$g/config-$latest_kernel_ver@g" "$file_cfg" && + sed -i -E "\@CONFIG=\"xxx\"@s@xxx@/home/$u/config-$latest_kernel_ver@g" "$file_cfg" && sed -i -E "\@KEYMAP=\"xxx\"@s@xxx@$(localectl | grep Keymap | awk -F' ' '{printf $NF}')@g" "$file_cfg" if [[ $? -gt 0 ]] ; then echo "Failed to patch jhalfs configuration file." >&2 @@ -117,4 +126,135 @@ _patch_jhalfs_sources() # Patch master.sh to run also the grub config related script sed -i '/^ .*10\*grub/s/^/#/g' "$dir_setup/LFS/master.sh" +} + + +# Final adjustments to the newly created LFS system +_finalize_lfs_build() +{ + # Check parameter + local dir_lfs="$(realpath "$1")" + if [ ! -d "$dir_lfs" ] ; then + echo "Invalid folder: $dir_lfs" + return 1 + fi + + # Patch the sudoers file + sudo sed -i "/^root ALL=(ALL:ALL) ALL/a pkr ALL=(ALL:ALL) NOPASSWD: ALL" "$dir_lfs/etc/sudoers" && + + # Move blfs folder to pkr home folder + sudo mv -v "$dir_lfs/blfs_root" "$dir_lfs/home/pkr/" && + sudo chown -hR pkr:pkr "$dir_lfs/home/pkr/blfs_root" && + sudo chown -hR pkr:pkr "$dir_lfs/var/lib/jhalfs" && + sudo sed -i "s|/blfs_root/packdesc.dtd|/home/pkr/blfs_root/packdesc.dtd|g" "$dir_lfs/var/lib/jhalfs/BLFS/instpkg.xml" +} + + +# Create autologin script to run blfs build after reboot +_setup_autologin() +{ + # Check parameter + local dir_lfs="$(realpath "$1")" + if [ ! -d "$dir_lfs" ] ; then + echo "Invalid folder: $dir_lfs" + return 1 + fi + + local dir_autologin="$dir_lfs/etc/systemd/system/getty@tty1.service.d" + sudo mkdir -pv "$dir_autologin" && + printf "[Service]\nType=simple\nExecStart=\nExecStart=-/sbin/agetty --autologin pkr %%I 38400 linux\n" | sudo tee "$dir_autologin/override.conf" > /dev/null +} + + +# Creates the blfs configuration file with the packages to build. +# The only parameter should be the new LFS system's root folder. +_create_blfs_config() +{ + # Check parameter + local dir_lfs="$(realpath "$1")" + if [ ! -d "$dir_lfs" ] ; then + echo "Invalid folder: $dir_lfs" + return 1 + fi + + # Create new blfs config + local dir_blfscfg="$dir_lfs/home/pkr/blfs_root/configuration" + if [ -f "$dir_blfscfg" ] ; then + sudo rm -fv "$dir_blfscfg" + fi + + cat > "$dir_blfscfg" << EOF +CONFIG_pciutils=y +CONFIG_twm=y +CONFIG_xinit=y +CONFIG_xorg-evdev-driver=y +CONFIG_xorg-libinput-driver=y +CONFIG_xwayland=y +CONFIG_sddm=y +CONFIG_openbox=y +CONFIG_lxqt-menu-data=y +CONFIG_lxqt-panel=y +CONFIG_lxqt-post-install=y +CONFIG_lxqt-pre-install=y +CONFIG_lxqt-runner=y +CONFIG_lxqt-session=y +CONFIG_lxqt-sudo=y +CONFIG_lxqt-themes=y +CONFIG_pcmanfm-qt=y +CONFIG_lxqt-notificationd=y +CONFIG_pavucontrol-qt=y +CONFIG_qterminal=y +CONFIG_firefox=y + +# Build settings +MS_sendmail=y +MAIL_SERVER="sendmail" +DEPLVL_2=y +optDependency=2 +LANGUAGE="hu_HU.UTF-8" +SUDO=y +DEL_LA_FILES=y + +# Build Layout +SRC_ARCHIVE="/sources" +BUILD_ROOT="/sources" +BUILD_SUBDIRS=y + +# Optimization +JOBS=0 +CFG_CFLAGS=" -O3 -pipe -march=native " +CFG_CXXFLAGS=" -O3 -pipe -march=native " +CFG_LDFLAGS="EMPTY" +EOF +} + + +# Creates script that auto builds the blfs system after successfull lfs build +_create_blfs_builder_script() +{ + # Check parameter + local dir_lfs="$(realpath "$1")" + if [ ! -d "$dir_lfs" ] ; then + echo "Invalid folder: $dir_lfs" + return 1 + fi + + cat > "$dir_lfs/home/pkr/.profile" << EOF +#!/bin/bash +cd "/home/pkr/blfs_root/blfs-xml" +git reset --hard +git clean -xfd +cd "/home/pkr/blfs_root/lfs-xml" +git reset --hard +git clean -xfd +cd "/home/pkr/blfs_root" +make update +. gen_pkg_book.sh <<< yes +cd work +../gen-makefile.sh +make +sudo rm -rfv /etc/systemd/system/getty@tty1.service.d +rm -fv /home/pkr/.profile +sudo systemctl poweroff +EOF } \ No newline at end of file diff --git a/libs/func_kernel.sh b/libs/func_kernel.sh index 4b108c2..5ab8ec7 100644 --- a/libs/func_kernel.sh +++ b/libs/func_kernel.sh @@ -20,17 +20,17 @@ _patch_packages_ent_kernel_version() { # Check parameters local number_regex='^[0-9]+$' - if ! [ "$1" =~ $number_regex ] ; then + if ! [[ "$1" =~ $number_regex ]] ; then echo "Invalid major version: $1" >&2 return 1 fi - if ! [ "$2" =~ $number_regex ] ; then + if ! [[ "$2" =~ $number_regex ]] ; then echo "Invalid minor version: $2" >&2 return 1 fi - if ! [ "$3" == "-" ] && ! [ "$3" =~ $number_regex ] ; then + if ! [ "$3" == "-" ] && ! [[ "$3" =~ $number_regex ]] ; then echo "Invalid patch version: $3" >&2 return 1 fi @@ -42,18 +42,19 @@ _patch_packages_ent_kernel_version() return 1 fi - sed -i -E "s@(<\!ENTITY linux-major-version \"+)(.+\">)@\1$1\">@" "$5" - sed -i -E "s@(<\!ENTITY linux-minor-version \"+)(.+\">)@\1$2\">@" "$5" + sed -i -E "s@(<\!ENTITY linux-major-version \"+)(.+\">)@\1$1\">@" "$5" && + sed -i -E "s@(<\!ENTITY linux-minor-version \"+)(.+\">)@\1$2\">@" "$5" && sed -i -E "s@(<\!ENTITY linux-md5 \"+)(.+\">)@\1$4\">@" "$5" + if [[ $? -gt 0 ]] ; then return 1; fi if [ "-" == "$3" ] ; then - sed -i -E '//g;}' "$5" - sed -i -E '/linux-minor-version;">-->/{s///g;}' "$5" + sed -i -E '//g;}' "$5" && + sed -i -E '/linux-minor-version;">-->/{s///g;}' "$5" && sed -i -E '//g;}' "$5" else - sed -i -E '///g;}' "$5" - sed -i -E "s@(<\!ENTITY linux-patch-version \"+)(.+\">)@\1$3\">@" "$5" - sed -i -E '/linux-patch-version;">-->/{s///g;}' "$5" + sed -i -E '///g;}' "$5" && + sed -i -E "s@(<\!ENTITY linux-patch-version \"+)(.+\">)@\1$3\">@" "$5" && + sed -i -E '/linux-patch-version;">-->/{s///g;}' "$5" && sed -i -E '//g;}' "$5" fi } @@ -231,6 +232,12 @@ _create_kernel_config_if_needed() # Check for possible needed firmwares defined in kernel config and copy them for the new LFS system _check_and_copy_needed_firmwares() { + # Check parameter + if [ ! -d "$1" ] ; then + echo "Invalid or missing folder: $1" >&2 + return 1 + fi + local latest_kernel_ver= latest_kernel_ver=$(_get_latest_kernel_version) if [[ $? -gt 0 ]] ; then return 1; fi diff --git a/libs/func_network.sh b/libs/func_network.sh index c690f3a..4a1b47e 100644 --- a/libs/func_network.sh +++ b/libs/func_network.sh @@ -5,7 +5,7 @@ _patch_network_scripts() { # Patch network script local dir_commands="$1/jhalfs/lfs-commands" - net_script=$(find "$dir_commands" -type f -iname "*-network") + local net_script=$(find "$dir_commands" -type f -iname "*-network") if [ ! -f "$net_script" ] ; then echo "Failed to find the network script file." >&2 return 1