Deactivate the script as soon as possible

* With these changes, the script will be disabled as soon as possible, if :
  * "GRUB_BTRFS_DISABLE" If this variable is set to "true"
  * "btrfs-progs isn't installed" This package is required to retrieve information from the btrfs filesystem.
  * "grub-mkconfig_lib couldn't be found" This library is required because the script depends on it.
  * "Root filesystem isn't btrfs" grub-btrfs currently checks only the btrfs snapshots present on the root partition.
This commit is contained in:
Antynea
2021-09-22 15:49:02 +02:00
committed by GitHub
parent 863107588c
commit 14bf041ba6
+30 -41
View File
@@ -41,12 +41,23 @@ sysconfdir="/etc"
grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config" grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"
[[ -f "$grub_btrfs_config" ]] && . "$grub_btrfs_config" [[ -f "$grub_btrfs_config" ]] && . "$grub_btrfs_config"
. "$datarootdir/grub/grub-mkconfig_lib" [[ -f "${sysconfdir}/default/grub" ]] && . "${sysconfdir}/default/grub"
. "${sysconfdir}/default/grub"
## Exit the script, if:
disable_script() {
# Disable Grub-btrfs is set to true (default=false)
[[ "${GRUB_BTRFS_DISABLE:-"false"}" == "true" ]] && return 1
# btrfs-progs isn't installed
if ! type btrfs >/dev/null 2>&1; then return 1; fi
# grub-mkconfig_lib couldn't be found
[[ -f "$datarootdir/grub/grub-mkconfig_lib" ]] && . "$datarootdir/grub/grub-mkconfig_lib" || return 1
# Root filesystem isn't btrfs
root_fs=$(${grub_probe} --target="fs" / 2>/dev/null)
[[ "$root_fs" != "btrfs" ]] && return 1
return 0
}
disable_script
### Variables in /etc/default/grub-btrfs/config
## Disable Grub-btrfs (default=active)
[[ "${GRUB_BTRFS_DISABLE:-"false"}" == "true" ]] && exit 0
## Submenu name ## Submenu name
distro=$(awk -F "=" '/^NAME=/ {gsub(/"/, "", $2); print $2}' /etc/os-release) distro=$(awk -F "=" '/^NAME=/ {gsub(/"/, "", $2); print $2}' /etc/os-release)
submenuname=${GRUB_BTRFS_SUBMENUNAME:-"${distro:-Linux} snapshots"} submenuname=${GRUB_BTRFS_SUBMENUNAME:-"${distro:-Linux} snapshots"}
@@ -73,23 +84,16 @@ if [ -n "${GRUB_BTRFS_PROTECTION_AUTHORIZED_USERS}" ] ; then
protection_authorized_users="--users ${GRUB_BTRFS_PROTECTION_AUTHORIZED_USERS} " protection_authorized_users="--users ${GRUB_BTRFS_PROTECTION_AUTHORIZED_USERS} "
fi fi
### variables script ## Probe informations of Root and Boot devices
## Probe info "Boot partition" # Probe info "Root partition"
# Boot device root_device=$(${grub_probe} --target=device /) # Root device
boot_device=$(${grub_probe} --target=device ${boot_directory}) root_uuid=$(${grub_probe} --device ${root_device} --target="fs_uuid" 2>/dev/null) # UUID of the root device
# hints string # Probe info "Boot partition"
boot_hs=$(${grub_probe} --device ${boot_device} --target="hints_string" 2>/dev/null) boot_device=$(${grub_probe} --target=device ${boot_directory}) # Boot device
# UUID of the boot partition boot_uuid=$(${grub_probe} --device ${boot_device} --target="fs_uuid" 2>/dev/null) # UUID of the boot device
boot_uuid=$(${grub_probe} --device ${boot_device} --target="fs_uuid" 2>/dev/null) boot_hs=$(${grub_probe} --device ${boot_device} --target="hints_string" 2>/dev/null) # hints string
# Type filesystem of boot partition boot_fs=$(${grub_probe} --device ${boot_device} --target="fs" 2>/dev/null) # Type filesystem of boot device
boot_fs=$(${grub_probe} --device ${boot_device} --target="fs" 2>/dev/null)
## Probe info "Root partition"
# Type filesystem of root partition
root_fs=$(${grub_probe} --target="fs" / 2>/dev/null)
# Root device
root_device=$(${grub_probe} --target=device /)
# UUID of the root partition
root_uuid=$(${grub_probe} --device ${root_device} --target="fs_uuid" 2>/dev/null)
## Parameters passed to the kernel ## Parameters passed to the kernel
kernel_parameters="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT" kernel_parameters="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT"
## Mount point location ## Mount point location
@@ -116,7 +120,7 @@ detect_rootflags()
rootflags="rootflags=${fstabflags:+$fstabflags,}${GRUB_BTRFS_ROOTFLAGS:+$GRUB_BTRFS_ROOTFLAGS,}" rootflags="rootflags=${fstabflags:+$fstabflags,}${GRUB_BTRFS_ROOTFLAGS:+$GRUB_BTRFS_ROOTFLAGS,}"
} }
### Error Handling ## Error Handling
print_error() print_error()
{ {
local arg="$@" local arg="$@"
@@ -125,18 +129,6 @@ print_error()
exit 0 exit 0
} }
test_btrfs()
{
[[ "$root_fs" != "btrfs" ]] && print_error "Root partition isn't a btrfs filesystem.\nThis script only supports snapshots of the btrfs filesystem."
set +e
type btrfs >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
print_error "Unable to retrieve info from btrfs filesystem, make sure you have btrfs-progs on your system."
fi
set -e
}
### Script
## Create entry ## Create entry
entry() entry()
{ {
@@ -170,7 +162,7 @@ make_menu_entries()
entry " entry "
menuentry '"${k}" & "${i}"' ${CLASS} "\$menuentry_id_option" 'gnulinux-snapshots-$boot_uuid' {" menuentry '"${k}" & "${i}"' ${CLASS} "\$menuentry_id_option" 'gnulinux-snapshots-$boot_uuid' {"
fi fi
entry "\ entry "\
if [ x\$feature_all_video_module = xy ]; then if [ x\$feature_all_video_module = xy ]; then
insmod all_video insmod all_video
fi fi
@@ -205,7 +197,7 @@ make_menu_entries()
entry " entry "
menuentry '"${k}"' ${CLASS} "\$menuentry_id_option" 'gnulinux-snapshots-$boot_uuid' {" menuentry '"${k}"' ${CLASS} "\$menuentry_id_option" 'gnulinux-snapshots-$boot_uuid' {"
fi fi
entry "\ entry "\
if [ x\$feature_all_video_module = xy ]; then if [ x\$feature_all_video_module = xy ]; then
insmod all_video insmod all_video
fi fi
@@ -489,6 +481,7 @@ boot_bounded()
done done
IFS=$oldIFS IFS=$oldIFS
} }
boot_separate() boot_separate()
{ {
boot_dir="${boot_directory}" boot_dir="${boot_directory}"
@@ -537,10 +530,7 @@ boot_separate()
IFS=$oldIFS IFS=$oldIFS
} }
### Start
printf "Detecting snapshots ...\n" >&2 ; printf "Detecting snapshots ...\n" >&2 ;
# Only support btrfs snapshots
test_btrfs
# Delete existing config # Delete existing config
#rm -f --preserve-root "$grub_directory/grub-btrfs.cfg" #rm -f --preserve-root "$grub_directory/grub-btrfs.cfg"
> "$grub_directory/grub-btrfs.cfg" > "$grub_directory/grub-btrfs.cfg"
@@ -585,4 +575,3 @@ submenu '${submenuname}' ${protection_authorized_users}${unrestricted_access_sub
configfile "\${prefix}/grub-btrfs.cfg" configfile "\${prefix}/grub-btrfs.cfg"
} }
EOF EOF
### End