Compare commits

..

8 Commits

Author SHA1 Message Date
Antynea
6f7fb178fb Merge pull request #9 from Antynea/Development
enhanced
2015-07-22 02:05:05 +02:00
Antynea
d561206775 enhanced
enhanced
2015-07-22 01:59:28 +02:00
Antynea
563151507d enhanced
enhanced
2015-07-22 01:52:48 +02:00
Antynea
fda6eb8c00 Merge pull request #8 from Antynea/Development
fix bug with underscore in subvolume name
2015-06-03 20:41:23 +02:00
Antynea
2385312345 fix bug with underscore in subvolume name 2015-06-03 11:05:05 +02:00
Antynea
5fe28d39be Merge remote-tracking branch 'origin/Development' 2015-06-02 15:59:34 +02:00
Antynea
5e8c722c6a v0.6 2015-06-02 15:58:46 +02:00
Antynea
91e72f60f8 Improve perfomance 2015-05-31 22:26:02 +02:00

View File

@@ -1,90 +1,112 @@
#! /bin/sh
#! /usr/bin/bash
#
#
#########################################################################################################################################
# Written by: Antynea #
# #
# Purpose: Include btrfs snapshots at boot options. #
# #
# What this script does: #
# - List snapshots existing on root partition (btrfs). #
# - Create corresponding menuentry in grub.cfg , which ensures a very easy rollback. #
# #
# How to use it: #
# - Add this lines to /etc/default/grub: #
# #
# * GRUB_BTRFS_SUBMENUNAME="ArchLinux Snapshots" (Name menu appearing in grub.) #
# * GRUB_BTRFS_PREFIXENTRY="Snapshot" (Add a name ahead your snapshots entries.) #
# * GRUB_BTRFS_NKERNEL=("vmlinuz-linux") (Custom Name kernel you use it.) #
# * GRUB_BTRFS_NINIT=("initramfs-linux.img" "initramfs-linux-fallback.img") (Custom Name initramfs (ramdisk) you use it.) #
# * GRUB_BTRFS_INTEL_UCODE=("intel-ucode.img") (Custom Name intel microcode you use it.) #
# #
# - Generate grub.cfg (on Archlinux use grub-mkconfig -o /boot/grub/grub.cfg ) #
# #
# - grub-btrfs automatically generates snapshots entries. #
# - You will see it appear different entries, e.g : Prefixentry name of snapshot [2013-02-11 04:00:00] #
# #
# Warning: #
# This version detect kernels,initramfs,intel microcode only in boot partition, not in snapshot. #
# If those that are present in boot partition but not in snapshot, #
# entry will be created but not fonctional, you don't boot it. #
# #
#########################################################################################################################################
set -e
. /usr/share/grub/grub-mkconfig_lib
. /etc/default/grub
### variables modifiable ###
## fr: nom du sous-menu
## en: Name of the submenu
######################################
### variables in /etc/default/grub ###
######################################
## Submenu name
submenuname=${GRUB_BTRFS_SUBMENUNAME:-"ArchLinux Snapshots"}
## fr: préfixe de l'entrée
## en: Prefix entry
## Prefix entry
prefixentry=${GRUB_BTRFS_PREFIXENTRY:-"Snapshot"}
## fr: nom(s) du ou des kernel(s)
## en: Name(s) of the kernel(s)
## Kernel(s) name(s)
nkernel=("${GRUB_BTRFS_NKERNEL[@]:-vmlinuz-linux}")
## fr: nom(s) de(s) l'init
## en: Name(s) of the init(s)
## Initramfs name(s)
ninit=("${GRUB_BTRFS_NINIT[@]:-initramfs-linux.img initramfs-linux-fallback.img}")
## fr: nom du microcode intel
## en: Name intel-ucode
## Intel-ucode name(s)
intel_ucode=("${GRUB_BTRFS_INTEL_UCODE[@]:-intel-ucode.img}")
###########################
### fr: DÉBUT DU SCRIPT ###
### en: NAME OF SCRIPT ###
### fr: NE PAS TOUCHER ###
### en: DO NOT TOUCH ###
###########################
## fr: internationalisation
## en: Internationalization
########################
### variables script ###
########################
## Internationalization (default : english)
export TEXTDOMAIN=grub-btrfs-git
export TEXTDOMAINDIR="/usr/share/locale"
## fr: paramêtre des chaînes --hint
## en: Parameter of the chains --hint (Translation unclear)
## hints string
pboot=$(${grub_probe} --target="hints_string" "/boot" 2>/dev/null)
## fr: uuid de la partition root
## en: UUID of the root partition
## UUID of the root partition
uuid=$(${grub_probe} "/" --target="fs_uuid" 2>/dev/null)
## fr: uuid de la partition boot
## en: UUID of the boot partition
## UUID of the boot partition
buuid=$(${grub_probe} --target="fs_uuid" "/boot" 2>/dev/null)
## fr: paramêtre passé au kernel
## en: Parameters passed to the kernel
## Parameters passed to the kernel
params="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT"
##############
### Script ###
##############
# fr: on affiche le menu
# en: Display the menu
typeset -A date_time
unset snapshots
# Create list of filesystem snapshots
oldIFS=$IFS
IFS=$'\n'
for snap in $($bindir/btrfs subvolume list -sa / --sort=-ogen | $bindir/awk '{gsub(/^.*<FS_TREE>\//,"",$NF);print $11" "$12"?"$NF}'); do
snap_name="${snap#*"?"}"
# Discard deleted snapshots
if [ $snap_name = "DELETED" ]; then continue; fi
snapshots+=("$snap_name")
date_time[$snap_name]="${snap%"?"*}"
done
IFS=$oldIFS
# Display the menu
echo "submenu '$submenuname' {"
# fr: on traite la variable kernel
# en: Treat the kernel variables (Translation unclear)
# Treat the kernel variables (Translation unclear)
for kernel in ${nkernel[@]}; do
# fr: on test si le(s) nom(s) du(des) kernel existe(nt), autrement on affiche une erreur et on sort
# en: Check the specified kernel(s) exist, if it/they don't, display an error and exit
# Check the specified kernel(s) exist, if it/they don't, display an error and exit
if [ ! -f /boot/$kernel ]; then gettext_printf $"Error: /boot/$kernel, kernel does not exist" >&2; exit 1; fi
# fr: on vérifie le nombre de kernel présent, si >1 on crée un menu
# en: If there are >1 kernels, create a menu
# If there are >1 kernels, create a menu
if [ ${#nkernel[*]} != 1 ]; then echo " submenu '$kernel' {
submenu '---> Kernel: $kernel <---' { echo }"; fi
# fr: On liste les snapshots présent sur le système de fichier
# en: List filesystem snapshots
for item in $($bindir/btrfs subvolume list -sa / --sort=-ogen | $bindir/awk '{gsub(/^["<FS_TREE>/"]+/,"",$NF);print $NF}'); do
# fr: On écarte les snapshots avec un status de suppression
# en: Discard deleted snapshots
if [ $item = "DELETED" ]; then continue; fi
# fr: affiche la liste des snapshots trouvés
# en: Output name of snapshot
date_time=$($bindir/btrfs subvolume list -sa / | $bindir/grep $item | $bindir/cut -d" " -f 11-12)
gettext_printf $"Found Snapshot: %s\n" "$item $date_time" >&2
# fr: on crée un menu pour chaque snapshot présent (nom + date de création)
# en: Create a menu for remaining snapshots (name + creation date)
echo " submenu '$prefixentry $item [$date_time]' {"
# fr: si plusieurs kernel on été trouvé, on affiche un titre indicatif
# en: if more than one kernel is found, create a menu
# Treat the snapshots variable
for item in ${snapshots[@]}; do
# Output name of snapshot
gettext_printf $"Found Snapshot: %s\n" "$item ${date_time[$item]}" >&2
# Create a menu for remaining snapshots (name + creation date)
echo " submenu '$prefixentry $item [${date_time[$item]}]' {"
# if more than one kernel is found, create a menu
if [ ${#nkernel[*]} != 1 ]; then echo " submenu '---> Kernel: $kernel <---' { echo }"; fi
# fr: on traite la variable de l'initramfs
# en: Treat the initramfs variables (Translation unclear)
# Treat the initramfs variable
for init in ${ninit[@]}; do
# fr: on test si le(s) nom(s) du(des) initramfs existe(nt), autrement on affiche une erreur et on sort
# en: Check the specified initramfs(es) exist, if it/they don't, display an error and exit
# Check the specified initramfs(es) exist, if it/they don't, display an error and exit
if [ ! -f /boot/$init ]; then gettext_printf $"Error: /boot/$init, initramfs does not exist" >&2; exit 1; fi
# fr: on traite chaque entrée des snapshots avec leurs kernel et initramfs respectifs
# en: Specify a kernel and initramfs for every snapshot
# Specify a kernel and initramfs for every snapshot
echo "\
menuentry '$item $init' --class arch --class gnu-linux --class gnu --class os "\$menuentry_id_option" 'gnulinux-snapshots-$uuid'{
load_video
@@ -95,14 +117,12 @@ for kernel in ${nkernel[@]}; do
search --no-floppy --fs-uuid --set=root $buuid
fi
echo 'Loading Linux snapshot ...'"
# fr: on vérifie l'emplacement de la partition /boot
# en: Check the location of the /boot partition
# Check the location of the /boot partition
if [ $uuid = $buuid ]; then
echo "\
linux /$item/boot/$kernel root=UUID=$uuid rw rootflags=subvol=$item $params
echo 'Loading initial ramdisk ...'"
# fr: on vérifie la présence du microcode intel
# en: Check intel microcode exist
# Check intel microcode exist
if [ -f /boot/$intel_ucode ]; then
echo "\
initrd /$item/boot/$intel_ucode /$item/boot/$init"
@@ -114,8 +134,7 @@ for kernel in ${nkernel[@]}; do
echo "\
linux /$kernel root=UUID=$uuid rw rootflags=subvol=$item $params
echo 'Loading initial ramdisk ...'"
# fr: on vérifie la présence du microcode intel
# en: Check intel microcode exist
# Check intel microcode exist
if [ -f /boot/$intel_ucode ]; then
echo "\
initrd /$intel_ucode /$init"
@@ -129,8 +148,7 @@ for kernel in ${nkernel[@]}; do
done
echo " }"
done
# fr: on oubli pas de fermer le menu si plusieurs kernels ont été trouvé
# en: Don't forget to close menus if more than one kernel is found
# Don't forget to close menus if more than one kernel is found
if [ ${#nkernel[*]} != 1 ]; then echo " }"; fi
done
echo "}"