Compare commits

..

3 Commits
3.1 ... 3.3

Author SHA1 Message Date
Crafter6432
13a7186aaf Add systemd.path as option to monitor for new Snapshots (#53)
* Use systemd.path to regenerate grub-btrfs.cfg

* Added option GRUB_BTRFS_DIRNAME

* Updated README
2018-06-30 14:46:30 +02:00
Antynea
9b73e0ac62 Check uuid requirement. (#52)
* detect uuid requirement

add new function to detect uuid requirement

* Reduce generation time

New function doesn't need to be called at each generation of a menu entry
2018-06-11 15:52:00 +02:00
Maxim Baz
b17e7bd6a1 Add a dedicated config file (#49) 2018-04-20 18:08:37 +02:00
5 changed files with 100 additions and 72 deletions

View File

@@ -2,68 +2,39 @@
# #
# #
######################################################################################################################################################################### #########################################################################################################################################################################
# Written by: Antynea # # Written by: Antynea
# BTC donation address: 1Lbvz244WA8xbpHek9W2Y12cakM6rDe5Rt # # BTC donation address: 1Lbvz244WA8xbpHek9W2Y12cakM6rDe5Rt
# Github: https://github.com/Antynea/grub-btrfs # # Github: https://github.com/Antynea/grub-btrfs
# # #
# Purpose: Include btrfs snapshots at boot options (grub-menu). # # Purpose: Include btrfs snapshots at boot options (grub-menu).
# # #
# What this script does: # # What this script does:
# Simple rollback using snapshots you made previously. # # Simple rollback using snapshots you made previously.
# - Automatically List snapshots existing on root partition (btrfs). # # - Automatically List snapshots existing on root partition (btrfs).
# - Automatically Detect if "/boot" is in separate partition. # # - Automatically Detect if "/boot" is in separate partition.
# - Automatically Detect kernel, initramfs and intel microcode in "/boot" directory on snapshots. # # - Automatically Detect kernel, initramfs and intel microcode in "/boot" directory on snapshots.
# - Automatically Create corresponding "menuentry" in grub.cfg , which ensures a very easy rollback. # # - Automatically Create corresponding "menuentry" in grub.cfg , which ensures a very easy rollback.
# - Automatically detect snapper and use snapper's snapshot description if available. # # - Automatically detect snapper and use snapper's snapshot description if available.
# # #
# How to customize it: # # How to customize it:
# - Add this lines to /etc/default/grub: # # - 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.
# * GRUB_BTRFS_SUBMENUNAME="Arch Linux snapshots" # #
# (Name appearing in the Grub menu.) # # - Generate grub.cfg (on Arch Linux use grub-mkconfig -o /boot/$grub_directory/grub.cfg)
# * GRUB_BTRFS_PREFIXENTRY="Snapshot:" # #
# (Add a name ahead your snapshots entries in the Grub menu.) # # - grub-btrfs automatically generates snapshots entries.
# * GRUB_BTRFS_DISPLAY_PATH_SNAPSHOT="true" # # - You will see it appear different entries (e.g : Snapshot: [2014-02-12 11:24:37] my snapshot name overkill)
# (Show full path snapshot or only name in the Grub menu) # #
# * GRUB_BTRFS_TITLE_FORMAT="p/d/n" # # Automatically update grub
# (Custom title, shows/hides p"prefix" d"date" n"name" in the Grub menu, separator "/", custom order available) # # If you would like grub to automatically update when Snapper timeline snapshots and cleanups occur, simply install 10-update_grub.conf in the following locations:
# * GRUB_BTRFS_LIMIT="50" # # - /etc/systemd/system/snapper-timeline.service.d/
# (Limit the number of snapshots populated in the GRUB menu.) # # - /etc/systemd/system/snapper-cleanup.service.d/
# * GRUB_BTRFS_SUBVOLUME_SORT="descending" # # Once the configuration files are in place, systemctl daemon-reload should be run to reload the units and make the changes active.
# (Sort the found subvolumes by newest first ("descending") or oldest first ("ascending"). # #
# If "ascending" is chosen then the $GRUB_BTRFS_LIMIT oldest subvolumes will populate the menu.) # # Special thanks for assistance and contributions:
# * GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND="true" # # - My friends
# (Show snapshots found during run "grub-mkconfig") # # - All contributors on Github
# * GRUB_BTRFS_SHOW_TOTAL_SNAPSHOTS_FOUND="true" # #
# (Show Total of snapshots found during run "grub-mkconfig") #
# * GRUB_BTRFS_NKERNEL=("vmlinuz-linux") #
# (Use only if you have custom kernel name.) #
# * GRUB_BTRFS_NINIT=("initramfs-linux.img" "initramfs-linux-fallback.img") #
# (Use only if you have custom initramfs name.) #
# * GRUB_BTRFS_INTEL_UCODE=("intel-ucode.img") #
# (Use only if you have custom intel-ucode.) #
# * 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 (default=active) #
# #
# - Generate grub.cfg (on Arch Linux use grub-mkconfig -o /boot/grub/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) #
# #
# Automatically update grub #
# If you would like grub to automatically update when Snapper timeline snapshots and cleanups occur, simply install 10-update_grub.conf in the following locations: #
# - /etc/systemd/system/snapper-timeline.service.d/ #
# - /etc/systemd/system/snapper-cleanup.service.d/ #
# Once the configuration files are in place, systemctl daemon-reload should be run to reload the units and make the changes active. #
# #
# Special thanks for assistance and contributions: #
# - My friends #
# - All contributors on Github #
# #
######################################################################################################################################################################### #########################################################################################################################################################################
set -e set -e
@@ -72,8 +43,10 @@ prefix="/usr"
exec_prefix="/usr" exec_prefix="/usr"
datarootdir="/usr/share" datarootdir="/usr/share"
sysconfdir="/etc" sysconfdir="/etc"
grub_btrfs_config="${sysconfdir}/grub.d/41_snapshots-btrfs_config"
. "${sysconfdir}/default/grub" . "${sysconfdir}/default/grub"
[[ -f "$grub_btrfs_config" ]] && . "$grub_btrfs_config"
. "$datarootdir/grub/grub-mkconfig_lib" . "$datarootdir/grub/grub-mkconfig_lib"
###################################### ######################################
@@ -113,6 +86,8 @@ ignore_specific_path=("${GRUB_BTRFS_IGNORE_SPECIFIC_PATH[@]}")
## snapper's config name ## snapper's config name
snapper_config=${GRUB_BTRFS_SNAPPER_CONFIG:-"root"} snapper_config=${GRUB_BTRFS_SNAPPER_CONFIG:-"root"}
grub_directory=${GRUB_BTRFS_DIRNAME:-"grub"}
######################## ########################
### variables script ### ### variables script ###
######################## ########################
@@ -135,6 +110,16 @@ gbgmp=$(mktemp -d)
CLASS="--class snapshots --class gnu-linux --class gnu --class os" CLASS="--class snapshots --class gnu-linux --class gnu --class os"
## save IFS ## save IFS
oldIFS=$IFS oldIFS=$IFS
## Detect uuid requirement (lvm,btrfs...)
check_uuid_required() {
if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
|| ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
|| ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
LINUX_ROOT_DEVICE=${GRUB_DEVICE}
else
LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi
}
###################### ######################
@@ -164,10 +149,10 @@ test_btrfs()
## Create entry ## Create entry
entry() { entry() {
echo "$@" >> "/boot/grub/grub-btrfs.cfg" echo "$@" >> "/boot/$grub_directory/grub-btrfs.cfg"
# local arg="$@" # local arg="$@"
# echo "${arg}" >> "/boot/grub/grub-btrfs.cfg" # echo "${arg}" >> "/boot/$grub_directory/grub-btrfs.cfg"
# cat << EOF >> "/boot/grub/grub-btrfs.cfg" # cat << EOF >> "/boot/$grub_directory/grub-btrfs.cfg"
# ${arg} # ${arg}
# EOF # EOF
} }
@@ -212,7 +197,7 @@ make_menu_entries()
fi fi
echo 'Loading Snapshot: "${snap_date_time}" "${snap_dir_name}"' echo 'Loading Snapshot: "${snap_date_time}" "${snap_dir_name}"'
echo 'Loading Kernel: "${k}" ...' 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 if [[ -f "${boot_dir}"/"${u}" && "${i}" != "${prefix_i}-${kversion}-${alt_suffix_i}" ]] ; then
entry "\ entry "\
echo 'Loading Microcode & Initramfs: "${u}" "${i}" ...' echo 'Loading Microcode & Initramfs: "${u}" "${i}" ...'
@@ -421,7 +406,7 @@ boot_bounded()
snap_dir_name="$(trim "$snap_dir_name")" snap_dir_name="$(trim "$snap_dir_name")"
snap_date_time="$(echo "$item" | cut -d' ' -f1-2)" snap_date_time="$(echo "$item" | cut -d' ' -f1-2)"
snap_date_time="$(trim "$snap_date_time")" snap_date_time="$(trim "$snap_date_time")"
boot_dir="$gbgmp/$snap_dir_name/boot" boot_dir="$gbgmp/$snap_dir_name/boot"
# Kernel (Original + custom kernel) # Kernel (Original + custom kernel)
detect_kernel detect_kernel
@@ -464,12 +449,12 @@ boot_separate()
detect_kernel detect_kernel
if [ -z "${list_kernel}" ]; then print_error "Kernels not found."; fi if [ -z "${list_kernel}" ]; then print_error "Kernels not found."; fi
name_kernel=("${list_kernel[@]##*"/"}") name_kernel=("${list_kernel[@]##*"/"}")
# Initramfs (Original + custom initramfs) # Initramfs (Original + custom initramfs)
detect_initramfs detect_initramfs
if [ -z "${list_initramfs}" ]; then print_error "Initramfs not found."; fi if [ -z "${list_initramfs}" ]; then print_error "Initramfs not found."; fi
name_initramfs=("${list_initramfs[@]##*"/"}") name_initramfs=("${list_initramfs[@]##*"/"}")
# microcode (auto-detect + custom microcode) # microcode (auto-detect + custom microcode)
detect_microcode detect_microcode
name_microcode=("${list_ucode[@]##*"/"}") name_microcode=("${list_ucode[@]##*"/"}")
@@ -509,8 +494,8 @@ printf "###### - Grub-btrfs: Snapshot detection started - ######\n" >&2 ;
# if btrfs prog isn't installed, exit # if btrfs prog isn't installed, exit
test_btrfs test_btrfs
# Delete existing config # Delete existing config
#rm -f --preserve-root "/boot/grub/grub-btrfs.cfg" #rm -f --preserve-root "/boot/$grub_directory/grub-btrfs.cfg"
> "/boot/grub/grub-btrfs.cfg" > "/boot/$grub_directory/grub-btrfs.cfg"
# Create mount point then mounting # Create mount point then mounting
[[ ! -d $gbgmp ]] && mkdir -p $gbgmp [[ ! -d $gbgmp ]] && mkdir -p $gbgmp
mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/ mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/
@@ -518,6 +503,8 @@ mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/
count_warning_menuentries=0 count_warning_menuentries=0
# Count snapshots # Count snapshots
count_limit_snap=0 count_limit_snap=0
# detect uuid requirement
check_uuid_required
# Detects if /boot is a separate partition # Detects if /boot is a separate partition
if [[ "$root_uuid" != "$boot_uuid" ]]; then if [[ "$root_uuid" != "$boot_uuid" ]]; then
printf "# Info: Separate boot partition detected \n" >&2 ; printf "# Info: Separate boot partition detected \n" >&2 ;
@@ -539,7 +526,7 @@ fi
if [[ "${count_limit_snap}" = "0" || -z "${count_limit_snap}" ]]; then if [[ "${count_limit_snap}" = "0" || -z "${count_limit_snap}" ]]; then
print_error "No snapshots found." print_error "No snapshots found."
fi 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) # Make a submenu in GRUB (grub.cfg)
cat << EOF cat << EOF
submenu '${submenuname}' { submenu '${submenuname}' {

17
41_snapshots-btrfs_config Normal file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# GRUB_BTRFS_SUBMENUNAME="Arch Linux snapshots" # Name appearing in the Grub menu
# GRUB_BTRFS_PREFIXENTRY="Snapshot:" # Add a name ahead your snapshots entries in the Grub menu
# GRUB_BTRFS_DISPLAY_PATH_SNAPSHOT="true" # Show full path snapshot or only name in the Grub menu
# GRUB_BTRFS_TITLE_FORMAT="p/d/n" # Custom title, shows/hides p"prefix" d"date" n"name" in the Grub menu, separator "/", custom order available
# GRUB_BTRFS_LIMIT="50" # Limit the number of snapshots populated in the GRUB menu
# GRUB_BTRFS_SUBVOLUME_SORT="descending" # Sort the found subvolumes by newest first ("descending") or oldest first ("ascending") and show $GRUB_BTRFS_LIMIT first entries.
# GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND="true" # Show snapshots found during run "grub-mkconfig"
# GRUB_BTRFS_SHOW_TOTAL_SNAPSHOTS_FOUND="true" # Show Total of snapshots found during run "grub-mkconfig"
# GRUB_BTRFS_NKERNEL=("vmlinuz-linux") # Use only if you have custom kernel name
# GRUB_BTRFS_NINIT=("initramfs-linux.img" "initramfs-linux-fallback.img") # Use only if you have custom initramfs name
# GRUB_BTRFS_INTEL_UCODE=("intel-ucode.img") # Use only if you have custom intel-ucode
# 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/...

View File

@@ -86,6 +86,10 @@ Add this lines to /etc/default/grub:
(Disable grub-btrfs) (Disable grub-btrfs)
* GRUB_BTRFS_DIRNAME="grub"
(Name of the grub folder on /boot/)
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/grub.cfg )
@@ -100,6 +104,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-timeline.service.d/`
- `/etc/systemd/system/snapper-cleanup.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. 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
View File

@@ -0,0 +1,8 @@
[Unit]
Description=Monitors for new snapshots
[Path]
PathModified=/.snapshots
[Install]
WantedBy=multi-user.target

10
grub-btrfs.service Executable file
View File

@@ -0,0 +1,10 @@
[Unit]
Description=Regenerate grub-btrfs.cfg
[Service]
Type=oneshot
#Inital setup
EnvironmentFile=/etc/grub.d/41_snapshots-btrfs_config
ExecStartPre=-/bin/bash -c '[ ! -f "/boot/${GRUB_BTRFS_DIRNAME:-grub}/grub-btrfs.cfg" ] && grub2-mkconfig -o /boot/${GRUB_BTRFS_DIRNAME:-grub}/grub.cfg'
#Regenerate grub-btrfs.cfg instead of the whole grub menu
ExecStart=/etc/grub.d/41_snapshots-btrfs