mirror of
https://github.com/Antynea/grub-btrfs.git
synced 2026-03-04 21:15:02 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3eb7eb7861 | ||
|
|
35703e71c1 | ||
|
|
a1a48d26b7 | ||
|
|
13a7186aaf | ||
|
|
9b73e0ac62 |
@@ -1,2 +1,7 @@
|
||||
[Service]
|
||||
ExecStartPost=/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg
|
||||
# Set the possible paths for `grub-mkconfig`
|
||||
Environment="PATH=/sbin:/bin:/usr/sbin:/usr/bin"
|
||||
# Load environment variables from the configuration
|
||||
EnvironmentFile=/etc/grub.d/41_snapshots-btrfs_config
|
||||
# Regenerate just '/boot/grub/grub-btrfs.cfg' if it exists, else regenerate the whole grub menu
|
||||
ExecStartPost=/bin/bash -c 'if [ -s "/boot/${GRUB_BTRFS_DIRNAME:-grub}/grub-btrfs.cfg" ]; then /etc/grub.d/41_snapshots-btrfs; else ${GRUB_BTRFS_MKCONFIG:-/usr/bin/grub-mkconfig} -o /boot/${GRUB_BTRFS_DIRNAME:-grub}/grub.cfg; fi'
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
# Written by: Antynea
|
||||
# BTC donation address: 1Lbvz244WA8xbpHek9W2Y12cakM6rDe5Rt
|
||||
# Github: https://github.com/Antynea/grub-btrfs
|
||||
# #
|
||||
#
|
||||
# Purpose: Include btrfs snapshots at boot options (grub-menu).
|
||||
#
|
||||
# What this script does:
|
||||
@@ -20,7 +20,7 @@
|
||||
# - Refer 41_snapshots-btrfs_config for the list of available options and their default values.
|
||||
# - Place your configurations to either /etc/grub.d/41_snapshots-btrfs_config or /etc/default/grub.
|
||||
#
|
||||
# - Generate grub.cfg (on Arch Linux use grub-mkconfig -o /boot/grub/grub.cfg)
|
||||
# - Generate grub.cfg (on Arch Linux use grub-mkconfig -o /boot/$grub_directory/grub.cfg)
|
||||
#
|
||||
# - grub-btrfs automatically generates snapshots entries.
|
||||
# - You will see it appear different entries (e.g : Snapshot: [2014-02-12 11:24:37] my snapshot name overkill)
|
||||
@@ -83,8 +83,12 @@ show_snap_found=${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}
|
||||
show_total_snap_found=${GRUB_BTRFS_SHOW_TOTAL_SNAPSHOTS_FOUND:-"true"}
|
||||
## Ignore specific path during run "grub-mkconfig"
|
||||
ignore_specific_path=("${GRUB_BTRFS_IGNORE_SPECIFIC_PATH[@]}")
|
||||
## snapper's config name
|
||||
## Snapper's config name
|
||||
snapper_config=${GRUB_BTRFS_SNAPPER_CONFIG:-"root"}
|
||||
## Override boot partition detection
|
||||
override_boot_partition_detection=${GRUB_BTRFS_OVERRIDE_BOOT_PARTITION_DETECTION:-"false"}
|
||||
## Customize GRUB directory
|
||||
grub_directory=${GRUB_BTRFS_DIRNAME:-"grub"}
|
||||
|
||||
########################
|
||||
### variables script ###
|
||||
@@ -92,14 +96,20 @@ snapper_config=${GRUB_BTRFS_SNAPPER_CONFIG:-"root"}
|
||||
## Internationalization (default : english)
|
||||
export TEXTDOMAIN=grub-btrfs-git
|
||||
export TEXTDOMAINDIR="/usr/share/locale"
|
||||
## hints string
|
||||
hs_boot=$(${grub_probe} --target="hints_string" "/boot" 2>/dev/null)
|
||||
## UUID of the boot partition
|
||||
boot_uuid=$(${grub_probe} --target="fs_uuid" "/boot" 2>/dev/null)
|
||||
## Type filesystem of boot partition
|
||||
## Probe info "Boot partition"
|
||||
# Boot device
|
||||
boot_device=$(${grub_probe} --target=device "/boot")
|
||||
# hints string
|
||||
boot_hs=$(${grub_probe} --device "${boot_device}" --target="hints_string" 2>/dev/null)
|
||||
# UUID of the boot partition
|
||||
boot_uuid=$(${grub_probe} --device "${boot_device}" --target="fs_uuid" 2>/dev/null)
|
||||
# Type filesystem of boot partition
|
||||
boot_fs=$(${grub_probe} --target="fs" "/boot" 2>/dev/null)
|
||||
## UUID of the root partition
|
||||
root_uuid=$(${grub_probe} "/" --target="fs_uuid" 2>/dev/null)
|
||||
## Probe info "Root partition"
|
||||
# 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
|
||||
kernel_parameters="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT"
|
||||
## Mount point location
|
||||
@@ -108,6 +118,16 @@ gbgmp=$(mktemp -d)
|
||||
CLASS="--class snapshots --class gnu-linux --class gnu --class os"
|
||||
## save IFS
|
||||
oldIFS=$IFS
|
||||
## Detect uuid requirement (lvm,btrfs...)
|
||||
check_uuid_required() {
|
||||
if [ "x${root_uuid}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
|
||||
|| ! test -e "/dev/disk/by-uuid/${root_uuid}" \
|
||||
|| ( test -e "${root_device}" && uses_abstraction "${root_device}" lvm ); then
|
||||
LINUX_ROOT_DEVICE=${root_device}
|
||||
else
|
||||
LINUX_ROOT_DEVICE=UUID=${root_uuid}
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
######################
|
||||
@@ -137,10 +157,10 @@ test_btrfs()
|
||||
|
||||
## Create entry
|
||||
entry() {
|
||||
echo "$@" >> "/boot/grub/grub-btrfs.cfg"
|
||||
echo "$@" >> "/boot/$grub_directory/grub-btrfs.cfg"
|
||||
# local arg="$@"
|
||||
# echo "${arg}" >> "/boot/grub/grub-btrfs.cfg"
|
||||
# cat << EOF >> "/boot/grub/grub-btrfs.cfg"
|
||||
# echo "${arg}" >> "/boot/$grub_directory/grub-btrfs.cfg"
|
||||
# cat << EOF >> "/boot/$grub_directory/grub-btrfs.cfg"
|
||||
# ${arg}
|
||||
# EOF
|
||||
}
|
||||
@@ -179,13 +199,13 @@ make_menu_entries()
|
||||
set gfxpayload=keep
|
||||
insmod ${boot_fs}
|
||||
if [ x\$feature_platform_search_hint = xy ]; then
|
||||
search --no-floppy --fs-uuid --set=root ${hs_boot} ${boot_uuid}
|
||||
search --no-floppy --fs-uuid --set=root ${boot_hs} ${boot_uuid}
|
||||
else
|
||||
search --no-floppy --fs-uuid --set=root ${boot_uuid}
|
||||
fi
|
||||
echo 'Loading Snapshot: "${snap_date_time}" "${snap_dir_name}"'
|
||||
echo 'Loading Kernel: "${k}" ...'
|
||||
linux \"${boot_dir_root_grub}/"${k}"\" root=UUID=${root_uuid} rw rootflags=subvol=\""${snap_dir_name}"\" ${kernel_parameters}"
|
||||
linux \"${boot_dir_root_grub}/"${k}"\" root="${LINUX_ROOT_DEVICE}" rw rootflags=subvol=\""${snap_dir_name}"\" ${kernel_parameters}"
|
||||
if [[ -f "${boot_dir}"/"${u}" && "${i}" != "${prefix_i}-${kversion}-${alt_suffix_i}" ]] ; then
|
||||
entry "\
|
||||
echo 'Loading Microcode & Initramfs: "${u}" "${i}" ...'
|
||||
@@ -217,8 +237,8 @@ snapshot_list()
|
||||
# Query info from snapper if it is installed
|
||||
type snapper >/dev/null 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
local snapper_ids=($(snapper -t 0 -c "$snapper_config" list | tail -n +3 | cut -d'|' -f 2))
|
||||
local snapper_types=($(snapper -t 0 -c "$snapper_config" list | tail -n +3 | cut -d'|' -f 1))
|
||||
local snapper_ids=($(snapper -t 0 -c "$snapper_config" list | tail -n +3 | cut -d'|' -f 1))
|
||||
local snapper_types=($(snapper -t 0 -c "$snapper_config" list | tail -n +3 | cut -d'|' -f 2))
|
||||
|
||||
IFS=$'\n'
|
||||
local snapper_descriptions=($(snapper -t 0 -c "$snapper_config" list | tail -n +3 | cut -d'|' -f 7))
|
||||
@@ -482,8 +502,8 @@ printf "###### - Grub-btrfs: Snapshot detection started - ######\n" >&2 ;
|
||||
# if btrfs prog isn't installed, exit
|
||||
test_btrfs
|
||||
# Delete existing config
|
||||
#rm -f --preserve-root "/boot/grub/grub-btrfs.cfg"
|
||||
> "/boot/grub/grub-btrfs.cfg"
|
||||
#rm -f --preserve-root "/boot/$grub_directory/grub-btrfs.cfg"
|
||||
> "/boot/$grub_directory/grub-btrfs.cfg"
|
||||
# Create mount point then mounting
|
||||
[[ ! -d $gbgmp ]] && mkdir -p $gbgmp
|
||||
mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/
|
||||
@@ -491,13 +511,20 @@ mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/
|
||||
count_warning_menuentries=0
|
||||
# Count snapshots
|
||||
count_limit_snap=0
|
||||
# detect uuid requirement
|
||||
check_uuid_required
|
||||
# Detects if /boot is a separate partition
|
||||
if [[ "$root_uuid" != "$boot_uuid" ]]; then
|
||||
printf "# Info: Separate boot partition detected \n" >&2 ;
|
||||
if [[ "$override_boot_partition_detection" == "true" ]]; then
|
||||
printf "# Info: Override boot partition detection : enable \n" >&2 ;
|
||||
boot_separate
|
||||
else
|
||||
printf "# Info: Separate boot partition not detected \n" >&2 ;
|
||||
boot_bounded
|
||||
if [[ "$root_uuid" != "$boot_uuid" ]]; then
|
||||
printf "# Info: Separate boot partition detected \n" >&2 ;
|
||||
boot_separate
|
||||
else
|
||||
printf "# Info: Separate boot partition not detected \n" >&2 ;
|
||||
boot_bounded
|
||||
fi
|
||||
fi
|
||||
# unmounting mount point
|
||||
umount $gbgmp
|
||||
@@ -512,7 +539,7 @@ fi
|
||||
if [[ "${count_limit_snap}" = "0" || -z "${count_limit_snap}" ]]; then
|
||||
print_error "No snapshots found."
|
||||
fi
|
||||
root_grub="$(make_system_path_relative_to_its_root /boot/grub)"
|
||||
root_grub="$(make_system_path_relative_to_its_root /boot/$grub_directory)"
|
||||
# Make a submenu in GRUB (grub.cfg)
|
||||
cat << EOF
|
||||
submenu '${submenuname}' {
|
||||
|
||||
@@ -14,3 +14,6 @@
|
||||
# GRUB_BTRFS_IGNORE_SPECIFIC_PATH=("var/lib/docker") # Ignore specific path during run "grub-mkconfig"
|
||||
# GRUB_BTRFS_SNAPPER_CONFIG="root" # Snapper's config name to use
|
||||
# GRUB_BTRFS_DISABLE="false" # Disable Grub-btrfs
|
||||
# GRUB_BTRFS_DIRNAME=grub # Might be grub2 on some systems ex. /boot/grub2/...
|
||||
# GRUB_BTRFS_OVERRIDE_BOOT_PARTITION_DETECTION="false" # Change to "true" if you have a boot partition in a different subvolume
|
||||
# GRUB_BTRFS_MKCONFIG=grub-mkconfig # Might be 'grub2-mkconfig' on some systems
|
||||
|
||||
12
README.md
12
README.md
@@ -86,6 +86,16 @@ Add this lines to /etc/default/grub:
|
||||
|
||||
(Disable grub-btrfs)
|
||||
|
||||
* GRUB_BTRFS_DIRNAME="grub"
|
||||
|
||||
(Name of the grub folder on /boot/)
|
||||
|
||||
* GRUB_BTRFS_OVERRIDE_BOOT_PARTITION_DETECTION="false"
|
||||
(Change to "true" if you have a boot partition in a different subvolume)
|
||||
|
||||
* GRUB_BTRFS_MKCONFIG=grub-mkconfig
|
||||
|
||||
(Name or path of the 'grub-mkconfig' executable; this is 'grub2-mkconfig' on some systems)
|
||||
|
||||
Generate grub.cfg (on Arch linux use grub-mkconfig -o /boot/grub/grub.cfg )
|
||||
|
||||
@@ -100,6 +110,8 @@ If you would like grub to automatically update when Snapper timeline snapshots a
|
||||
- `/etc/systemd/system/snapper-timeline.service.d/`
|
||||
- `/etc/systemd/system/snapper-cleanup.service.d/`
|
||||
|
||||
Or copy `grub-btrfs.path` and `grub-btrfs.service` to `/etc/systemd/system/`
|
||||
|
||||
Once the configuration files are in place, `systemctl daemon-reload` should be run to reload the units and make the changes active.
|
||||
|
||||
##
|
||||
|
||||
8
grub-btrfs.path
Executable file
8
grub-btrfs.path
Executable file
@@ -0,0 +1,8 @@
|
||||
[Unit]
|
||||
Description=Monitors for new snapshots
|
||||
|
||||
[Path]
|
||||
PathModified=/.snapshots
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
11
grub-btrfs.service
Executable file
11
grub-btrfs.service
Executable file
@@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=Regenerate grub-btrfs.cfg
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
# Set the possible paths for `grub-mkconfig`
|
||||
Environment="PATH=/sbin:/bin:/usr/sbin:/usr/bin"
|
||||
# Load environment variables from the configuration
|
||||
EnvironmentFile=/etc/grub.d/41_snapshots-btrfs_config
|
||||
# Regenerate just '/boot/grub/grub-btrfs.cfg' if it exists and is not empty, else regenerate the whole grub menu
|
||||
ExecStart=/bin/bash -c 'if [ -s "/boot/${GRUB_BTRFS_DIRNAME:-grub}/grub-btrfs.cfg" ]; then /etc/grub.d/41_snapshots-btrfs; else ${GRUB_BTRFS_MKCONFIG:-/usr/bin/grub-mkconfig} -o /boot/${GRUB_BTRFS_DIRNAME:-grub}/grub.cfg; fi'
|
||||
Reference in New Issue
Block a user