From d1a1812709f1cf4133d2d610df8a9e4ea07f1dbb Mon Sep 17 00:00:00 2001 From: leventbesli Date: Thu, 7 Nov 2024 01:05:26 +0300 Subject: [PATCH] feat: btrfs assistant & grub-btrfs (#789) * Btrfs Assistant(+snapper), grub-btrfs Setup added Adds fedora-btrfs-assistant.sh, updates tab_data and documentation. * updated doc, ta_data and sh * enable grub-btrfsd service added * updated some wording in notices. * updated wording and add prompts for actions * added fix for a possible grub error also some more enhancements and wording improvements. * user action removed reinstall snapper action removed because removing and reinstalling snapper is a very unlikely action. and also snapper can't handle ./snapsots folder in the disk after reinstalling: "creating btrfs subvolume .snapshots failed since it already exists". * firs snapshot order changed home snapshot taken first because it's config can be saved to first root snapshot. * better explanations in user prompts * improved y/n loop * formatting improvements * formatting improvement Co-authored-by: Adam Perkowski * formatting improvement Co-authored-by: Adam Perkowski * Update task list Co-authored-by: Adam Perkowski * removed a inline comment Co-authored-by: Adam Perkowski * removed an unnececary inline comment Co-authored-by: Adam Perkowski * removed an unnececary inline comment Co-authored-by: Adam Perkowski * improved sed -i Co-authored-by: Adam Perkowski * improved sed -i Co-authored-by: Adam Perkowski * improved sed -i Co-authored-by: Adam Perkowski * newlines removed from tab_data.toml Co-authored-by: Nyx * btrfs detection moved to toml Co-authored-by: Nyx * data.preconditions added - escalation tool used * $PACKAGER replaced with dnf * re-added packager Co-authored-by: Adam Perkowski * re-added packager Co-authored-by: Adam Perkowski * usergide updated with docgen * typo fix --------- Co-authored-by: Adam Perkowski Co-authored-by: Nyx --- .../fedora/fedora-btrfs-assistant.sh | 168 ++++++++++++++++++ core/tabs/system-setup/tab_data.toml | 11 ++ docs/userguide.md | 1 + 3 files changed, 180 insertions(+) create mode 100644 core/tabs/system-setup/fedora/fedora-btrfs-assistant.sh diff --git a/core/tabs/system-setup/fedora/fedora-btrfs-assistant.sh b/core/tabs/system-setup/fedora/fedora-btrfs-assistant.sh new file mode 100644 index 000000000..1d5dc2ff1 --- /dev/null +++ b/core/tabs/system-setup/fedora/fedora-btrfs-assistant.sh @@ -0,0 +1,168 @@ +#!/bin/sh -e + +. ../../common-script.sh + +# This script automates the installation and root and home snapshot configuration of Snapper and installs Grub-Btrfs on Fedora. +# Also installs python3-dnf-plugin-snapper package for automatic snapshots after dnf commands. + +# Install Btrfs-Assistant/snapper and dependencies +installBtrfsStack() { + if ! command_exists btrfs-assistant; then + printf "%b\n" "${YELLOW}==========================================${RC}" + printf "%b\n" "${YELLOW}Installing Btrfs Assistant with snapper...${RC}" + printf "%b\n" "${YELLOW}==========================================${RC}" + case "$PACKAGER" in + dnf) + "$ESCALATION_TOOL" "$PACKAGER" install -y btrfs-assistant python3-dnf-plugin-snapper + ;; + *) + printf "%b\n" "${RED}Unsupported package manager: ""$PACKAGER""${RC}" + exit 1 + ;; + esac + else + printf "%b\n" "${GREEN}Btrfs Assistant is already installed.${RC}" + fi +} + +# Create first snapper config for root and home and create new manual snapshots +configureSnapper() { + printf "%b\n" "${YELLOW}===========================================================================${RC}" + printf "%b\n" "${YELLOW}Creating snapper root(/) and /home config and taking the first snapshots...${RC}" + printf "%b\n" "${YELLOW}===========================================================================${RC}" + "$ESCALATION_TOOL" snapper -c home create-config /home && "$ESCALATION_TOOL" snapper -c home create --description "First home Snapshot" + "$ESCALATION_TOOL" snapper -c root create-config / && "$ESCALATION_TOOL" snapper -c root create --description "First root Snapshot" + printf "%b\n" "${YELLOW}Updating timeline settings...${RC}" + # Modifyling default timeline root config + "$ESCALATION_TOOL" sed -i'' ' + s/^TIMELINE_LIMIT_HOURLY="[^"]*"/TIMELINE_LIMIT_HOURLY="1"/; + s/^TIMELINE_LIMIT_DAILY="[^"]*"/TIMELINE_LIMIT_DAILY="2"/; + s/^TIMELINE_LIMIT_WEEKLY="[^"]*"/TIMELINE_LIMIT_WEEKLY="1"/; + s/^TIMELINE_LIMIT_MONTHLY="[^"]*"/TIMELINE_LIMIT_MONTHLY="0"/; + s/^TIMELINE_LIMIT_YEARLY="[^"]*"/TIMELINE_LIMIT_YEARLY="0"/ + ' /etc/snapper/configs/root + # Modifyling default timeline for home config + "$ESCALATION_TOOL" sed -i'' ' + s/^TIMELINE_LIMIT_HOURLY="[^"]*"/TIMELINE_LIMIT_HOURLY="2"/; + s/^TIMELINE_LIMIT_DAILY="[^"]*"/TIMELINE_LIMIT_DAILY="1"/; + s/^TIMELINE_LIMIT_WEEKLY="[^"]*"/TIMELINE_LIMIT_WEEKLY="0"/; + s/^TIMELINE_LIMIT_MONTHLY="[^"]*"/TIMELINE_LIMIT_MONTHLY="1"/; + s/^TIMELINE_LIMIT_YEARLY="[^"]*"/TIMELINE_LIMIT_YEARLY="0"/ + ' /etc/snapper/configs/home + printf "%b\n" "${GREEN}Snapper configs and first snapshots created.${RC}" +} + +# Starting services +serviceStartEnable() { + printf "%b\n" "${YELLOW}==================================================================================${RC}" + printf "%b\n" "${YELLOW}Starting and enabling snapper-timeline.timer and snapper-cleanup.timer services...${RC}" + printf "%b\n" "${YELLOW}==================================================================================${RC}" + "$ESCALATION_TOOL" systemctl enable --now snapper-timeline.timer + "$ESCALATION_TOOL" systemctl enable --now snapper-cleanup.timer + printf "%b\n" "${GREEN}Snapper services started and enabled.${RC}" +} + +# Ask user if they want to install grub-btrfs +askInstallGrubBtrfs() { + printf "%b\n" "${YELLOW}=====================================${RC}" + printf "%b\n" "${YELLOW}(optional) grub-btrfs installation...${RC}" + printf "%b\n" "${YELLOW}=====================================${RC}" + printf "%b\n" "${YELLOW}You can skip installing grub-btrfs and use only Btrfs Assistant GUI or snapper CLI.${RC}" + printf "%b\n" "${CYAN}Notice: grub-btrfs may cause problems with booting into snapshots and other OSes on systems with secure boot/tpm. You will be asked to apply mitigation for this issue in next step.${RC}" + + while true; do + printf "%b" "${YELLOW}Do you want to install grub-btrfs? Press (y) for yes, (n) for no, (f) to apply tpm mitigation to already installed grub-btrfs: ${RC}" + read -r response + case "$response" in + [yY]*) + installGrubBtrfs + break + ;; + [nN]*) + printf "%b\n" "${GREEN}Skipping grub-btrfs installation.${RC}" + break + ;; + [fF]*) + mitigateTpmError + break + ;; + *) + printf "%b\n" "${RED}Invalid input. Please enter 'y' for yes, 'n' for no, or (f) to apply tpm mitigation to already installed grub-btrfs.${RC}" + ;; + esac + done +} + +# Install grub-btrfs +installGrubBtrfs() { + # Check if the grub-btrfs dir exists before attempting to clone into it. + printf "%b\n" "${YELLOW}Downloading grub-btrfs and installing dependencies...${RC}" + if [ -d "$HOME/grub-btrfs" ]; then + rm -rf "$HOME/grub-btrfs" + fi + "$ESCALATION_TOOL" "$PACKAGER" install -y make git inotify-tools + cd "$HOME" && git clone https://github.com/Antynea/grub-btrfs + printf "%b\n" "${YELLOW}Installing grub-btrfs...${RC}" + cd "$HOME/grub-btrfs" + printf "%b\n" "${YELLOW}Modifying grub-btrfs configuration for Fedora...${RC}" + sed -i'' '/#GRUB_BTRFS_SNAPSHOT_KERNEL/a GRUB_BTRFS_SNAPSHOT_KERNEL_PARAMETERS="systemd.volatile=state"' config + sed -i'' '/#GRUB_BTRFS_GRUB_DIRNAME/a GRUB_BTRFS_GRUB_DIRNAME="/boot/grub2"' config + sed -i'' '/#GRUB_BTRFS_MKCONFIG=/a GRUB_BTRFS_MKCONFIG=/sbin/grub2-mkconfig' config + sed -i'' '/#GRUB_BTRFS_SCRIPT_CHECK=/a GRUB_BTRFS_SCRIPT_CHECK=grub2-script-check' config + "$ESCALATION_TOOL" make install + printf "%b\n" "${YELLOW}Updating grub configuration and enabling grub-btrfsd service...${RC}" + "$ESCALATION_TOOL" grub2-mkconfig -o /boot/grub2/grub.cfg && "$ESCALATION_TOOL" systemctl enable --now grub-btrfsd.service + printf "%b\n" "${YELLOW}Cleaning up installation files...${RC}" + cd .. && rm -rf "$HOME/grub-btrfs" + printf "%b\n" "${GREEN}Grub-btrfs installed and service enabled.${RC}" + printf "%b\n" "${CYAN}Notice: To perform a system recovery via grub-btrfs, perform a restore operation with Btrfs Assistant GUI after booting into the snapshot.${RC}" + mitigateTpmError +} + +mitigateTpmError() { + printf "%b\n" "${YELLOW}===============================================${RC}" + printf "%b\n" "${YELLOW}Mitigation for 'tpm.c:150:unknown TPM error'...${RC}" + printf "%b\n" "${YELLOW}===============================================${RC}" + printf "%b\n" "${YELLOW}Some systems with secure boot/tpm may encounter 'tpm.c:150:unknown TPM error' when booting into snapshots.${RC}" + printf "%b\n" "${YELLOW}If you encounter this issue, you can come back later and apply this mitigation or you can apply it now.${RC}" + while true; do + printf "%b\n" "${YELLOW}Do you want to apply the TPM error mitigation? (y/n): ${RC}" + read -r response + case "$response" in + [yY]*) + printf "%b\n" "${YELLOW}Creating /etc/grub.d/02_tpm file...${RC}" + echo '#!/bin/sh' | "$ESCALATION_TOOL" tee /etc/grub.d/02_tpm > /dev/null + echo 'echo "rmmod tpm"' | "$ESCALATION_TOOL" tee -a /etc/grub.d/02_tpm > /dev/null + "$ESCALATION_TOOL" chmod +x /etc/grub.d/02_tpm # makes the file executable + "$ESCALATION_TOOL" grub2-mkconfig -o /boot/grub2/grub.cfg # updates grub config + printf "%b\n" "${GREEN}Mitigation applied and grub config updated.${RC}" + break + ;; + [nN]*) + printf "%b\n" "${GREEN}Skipping TPM error mitigation.${RC}" + break + ;; + *) + printf "%b\n" "${RED}Invalid input. Please enter 'y' for yes or 'n' for no.${RC}" + ;; + esac + done +} + +# Post install information +someNotices() { + printf "%b\n" "${YELLOW}================================NOTICES================================${RC}" + printf "%b\n" "${YELLOW}Notice: You can manage snapshots from GUI with Btrfs Assistant or CLI with snapper.${RC}" + printf "%b\n" "${YELLOW}Notice: You may change (Hourly, daily, weekly, monthly, yearly) timeline settings with Btrfs Assistant GUI.${RC}" + printf "%b\n" "${RED}Notice: If you used the default Fedora disk partitioning during OS installation, the /boot configured as an separate EXT4 partition. Therefore, it cannot be included in root snapshots. Backup separately...${RC}" + printf "%b\n" "${YELLOW}================================NOTICES================================${RC}" + printf "%b\n" "${GREEN}Setup process completed.${RC}" +} + +checkEnv +checkEscalationTool +installBtrfsStack +configureSnapper +serviceStartEnable +askInstallGrubBtrfs +someNotices diff --git a/core/tabs/system-setup/tab_data.toml b/core/tabs/system-setup/tab_data.toml index 2c439cab7..7465ab83a 100644 --- a/core/tabs/system-setup/tab_data.toml +++ b/core/tabs/system-setup/tab_data.toml @@ -71,6 +71,17 @@ description = "Enables Virtualization through dnf" script = "fedora/virtualization.sh" task_list = "I" +[[data.entries]] +name = "Btrfs Assistant, Snapper Config, grub-btrfs" +description = "Installs Btrfs Assistant, Snapper, dnf snapper plugin and takes the first root(/) and /home snapshots. Enables snapper-timeline and snapper-cleanup services. Installs Grub-Btrfs. Notice: To perform a system recovery via grub-btrfs, perform a restore operation with Btrfs Assistant GUI after booting into the snapshot. Notice: If you used the default Fedora disk partitioning during OS installation, the /boot configured as an separate EXT4 partition. Therefore, it cannot be included in root snapshots. Backup separately." +script = "fedora/fedora-btrfs-assistant.sh" +task_list = "I PFM SS" + +[[data.preconditions]] +matches = true +data = "command_exists" +values = [ "btrfs" ] + [[data]] name = "Build Prerequisites" description = "This script is designed to handle the installation of various software dependencies across different Linux distributions" diff --git a/docs/userguide.md b/docs/userguide.md index d845e67f3..261597a78 100644 --- a/docs/userguide.md +++ b/docs/userguide.md @@ -97,6 +97,7 @@ https://github.com/ChrisTitusTech/dwm-titus - **RPM Fusion**: RPM Fusion provides software that the Fedora Project or Red Hat doesn't want to ship. That software is provided as precompiled RPMs for all current Fedora versions and current Red Hat Enterprise Linux or clones versions; you can use the RPM Fusion repositories with tools like yum and PackageKit. For more information visit: https://rpmfusion.org/ - **Upgrade to a New Fedora Release**: Upgrades system to the next Fedora release - **Virtualization**: Enables Virtualization through dnf +- **Btrfs Assistant, Snapper Config, grub-btrfs**: Installs Btrfs Assistant, Snapper, dnf snapper plugin and takes the first root(/) and /home snapshots. Enables snapper-timeline and snapper-cleanup services. Installs Grub-Btrfs. Notice: To perform a system recovery via grub-btrfs, perform a restore operation with Btrfs Assistant GUI after booting into the snapshot. Notice: If you used the default Fedora disk partitioning during OS installation, the /boot configured as an separate EXT4 partition. Therefore, it cannot be included in root snapshots. Backup separately. - **Build Prerequisites**: This script is designed to handle the installation of various software dependencies across different Linux distributions - **Full System Cleanup**: This script is designed to remove unnecessary packages, clean old cache files, remove temporary files, and to empty the trash. - **Full System Update**: This command updates your system to the latest packages available for your distro