From d68fdf5fffd78965187c79c1d8b2b832b67c52b9 Mon Sep 17 00:00:00 2001 From: Kriss Date: Mon, 11 Jan 2021 13:37:40 +0100 Subject: [PATCH] Adds a new config variable "GRUB_BTRFS_ROOTFLAGS" (#133) Script: Improves the rootflags option in the kernel command line: - Remove "rw" flag. - Parse fstab mountoptions and add them to rootflags: Adds functionality to read in snapshot specific mount flags from the respective fstab and uses them in combination with the new config option, while the latter is prioritized. Both places can, but don't need to define mount flags. If either of them doesn't, it will simply not be taken into account. Config file: Adds a new config variable "GRUB_BTRFS_ROOTFLAGS": - The new variable is used to include user-defined rootflags in the kernel command line of generated snapshot bootmenu entries. See config file for more infos. --- 41_snapshots-btrfs | 13 ++++++++++++- config | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/41_snapshots-btrfs b/41_snapshots-btrfs index fe72eb1..ad7b6e5 100755 --- a/41_snapshots-btrfs +++ b/41_snapshots-btrfs @@ -115,6 +115,13 @@ else LINUX_ROOT_DEVICE=UUID=${root_uuid} fi } +## Detect rootflags +detect_rootflags() +{ + local fstabflags=$(grep -oE '^\s*[^#][[:graph:]]+\s+/\s+btrfs\s+[[:graph:]]+' "${gbgmp}/${snap_dir_name}/etc/fstab" \ + | sed -E 's/^.*[[:space:]]([[:graph:]]+)$/\1/;s/,?subvol(id)?=[^,$]+//g;s/^,//') + rootflags="rootflags=${fstabflags:+$fstabflags,}${GRUB_BTRFS_ROOTFLAGS:+$GRUB_BTRFS_ROOTFLAGS,}" +} ### Error Handling print_error() @@ -183,7 +190,7 @@ make_menu_entries() fi echo 'Loading Snapshot: "${snap_date_time}" "${snap_dir_name}"' echo 'Loading Kernel: "${k}" ...' - linux \"${boot_dir_root_grub}/"${k}"\" root="${LINUX_ROOT_DEVICE}" rw ${kernel_parameters} rootflags=subvol=\""${snap_dir_name}"\"" + linux \"${boot_dir_root_grub}/"${k}"\" root="${LINUX_ROOT_DEVICE}" ${kernel_parameters} ${rootflags}subvol=\""${snap_dir_name}"\"" if [[ "${name_microcode}" != "x" ]] ; then entry "\ echo 'Loading Microcode & Initramfs: "${u}" "${i}" ...' @@ -417,6 +424,8 @@ boot_bounded() detect_kernel if [ -z "${list_kernel}" ]; then continue; fi name_kernel=("${list_kernel[@]##*"/"}") + # Detect rootflags + detect_rootflags # Initramfs (Original + custom initramfs) detect_initramfs if [ -z "${list_initramfs}" ]; then continue; fi @@ -472,6 +481,8 @@ boot_separate() snap_dir_name="$(trim "$snap_dir_name")" snap_date_time="$(echo "$item" | cut -d' ' -f1-2)" snap_date_time="$(trim "$snap_date_time")" + # Detect rootflags + detect_rootflags # show snapshot found during run "grub-mkconfig" if [[ "${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}" = "true" ]]; then printf "Found snapshot: %s\n" "$item" >&2 ; diff --git a/config b/config index 1314905..4638b10 100644 --- a/config +++ b/config @@ -54,6 +54,14 @@ # Default: ("") #GRUB_BTRFS_CUSTOM_MICROCODE=("custom-ucode.img" "custom-uc.img "custom_ucode.cpio") +# Comma seperated mount options to be used when booting a snapshot. +# They can be defined here as well as in the "/" line inside the respective snapshots' +# "/etc/fstab" files. Mount options found in both places are combined, and this variable +# takes priority over `fstab` entries. +# NB: Do NOT include "subvol=..." or "subvolid=..." here. +# Default: "" +#GRUB_BTRFS_ROOTFLAGS="space_cache,commit=10,norecovery" + # Ignore specific path during run "grub-mkconfig". # Only exact paths are ignored. # e.g : if `specific path` = @, only `@` snapshot will be ignored.