2014-02-11 10:55:19 +01:00
|
|
|
#!/bin/sh
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
|
|
|
|
#
|
|
|
|
# GRUB is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# GRUB is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
grub_mkconfig="/usr/sbin/grub2-mkconfig"
|
|
|
|
grub_mkrelpath="/usr/bin/grub2-mkrelpath"
|
2014-08-30 16:03:50 +02:00
|
|
|
grub_script_check="/usr/bin/grub2-script-check"
|
2014-02-11 10:55:19 +01:00
|
|
|
grub_setting="/etc/default/grub"
|
|
|
|
grub_cfg="/boot/grub2/grub.cfg"
|
|
|
|
grub_snapshot_cfg="/boot/grub2/snapshot_submenu.cfg"
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
snapper_snapshot_path="/.snapshots"
|
|
|
|
snapshot_submenu_name="grub-snapshot.cfg"
|
|
|
|
snapper_snapshots_cfg="${snapper_snapshot_path}/${snapshot_submenu_name}"
|
|
|
|
|
|
|
|
# add hotkeys for s390. (bnc#885668)
|
|
|
|
hotkey=
|
|
|
|
incr_hotkey()
|
|
|
|
{
|
|
|
|
[ -n "$hotkey" ] || return
|
|
|
|
expr $hotkey + 1
|
|
|
|
}
|
|
|
|
print_hotkey()
|
|
|
|
{
|
|
|
|
keys="123456789abdfgijklmnoprstuvwyz"
|
|
|
|
if [ -z "$hotkey" ]||[ $hotkey -eq 0 ]||[ $hotkey -gt 30 ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
echo "--hotkey=$(expr substr $keys $hotkey 1)"
|
|
|
|
}
|
2014-02-11 10:55:19 +01:00
|
|
|
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
snapshot_submenu () {
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
s_dir="$1"
|
2014-02-11 10:55:19 +01:00
|
|
|
|
|
|
|
snapshot="${s_dir}/snapshot"
|
2014-08-30 16:03:50 +02:00
|
|
|
num="`basename $s_dir`"
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-03-04 13:14:10 +01:00
|
|
|
# bnc#864842 Important snapshots are not marked as such in grub2 menu
|
|
|
|
# the format is "important distribution version (kernel_version, timestamp, pre/post)"
|
2014-02-11 10:55:19 +01:00
|
|
|
date=`xmllint --xpath '/snapshot/date/text()' "${s_dir}/info.xml" || echo ""`
|
2014-08-30 16:03:50 +02:00
|
|
|
date=`echo $date | sed 's/\(.*\) \(.*\):.*/\1T\2/'`
|
|
|
|
important=`xmllint --xpath "/snapshot/userdata[key='important']/value/text()" "${s_dir}/info.xml" 2>/dev/null || echo ""`
|
2014-03-04 13:14:10 +01:00
|
|
|
stype=`xmllint --xpath '/snapshot/type/text()' "${s_dir}/info.xml" || echo ""`
|
2014-08-30 16:03:50 +02:00
|
|
|
kernel_ver=`readlink ${snapshot}/boot/vmlinuz | sed -e 's/^vmlinuz-//' -e 's/-default$//'`
|
|
|
|
if [ -z "$kernel_ver" -a -L ${snapshot}/boot/image ]; then
|
|
|
|
kernel_ver=`readlink ${snapshot}/boot/image | sed -e 's/^image-//' -e 's/-default$//'`
|
|
|
|
fi
|
2014-03-04 13:14:10 +01:00
|
|
|
eval `cat ${snapshot}/etc/os-release`
|
2014-08-30 16:03:50 +02:00
|
|
|
test "$important" = "yes" && important="*" || important=" "
|
2014-03-04 13:14:10 +01:00
|
|
|
test "$stype" = "single" && stype=""
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
test -z "$stype" || stype=",$stype"
|
|
|
|
desc=`xmllint --xpath '/snapshot/description/text()' "${s_dir}/info.xml" 2>/dev/null || echo ""`
|
|
|
|
#test "$desc" = "timeline" && return 1
|
|
|
|
test -z "$desc" || desc=",$desc"
|
|
|
|
title="${important}${NAME} $VERSION ($kernel_ver,$date$stype$desc)"
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
if [ -f "${snapper_snapshot_path}/$num/snapshot/boot/grub2/grub.cfg" ]; then
|
|
|
|
snapshot_found=true
|
|
|
|
saved_subvol=\$btrfs_subvol
|
|
|
|
menuentry `print_hotkey` "$title" "${snapper_snapshot_path}/$num/snapshot" "`$grub_mkrelpath ${snapper_snapshot_path}/${num}/snapshot`" {
|
|
|
|
btrfs_subvol="\$2"
|
|
|
|
extra_cmdline="rootflags=subvol=\$3"
|
|
|
|
export extra_cmdline
|
|
|
|
snapshot_num=$num
|
|
|
|
export snapshot_num
|
|
|
|
configfile "\$btrfs_subvol/boot/grub2/grub.cfg"
|
|
|
|
btrfs_subvol=\$saved_subvol
|
|
|
|
}
|
2014-03-04 13:14:10 +01:00
|
|
|
fi
|
2014-02-11 10:55:19 +01:00
|
|
|
|
|
|
|
EOF
|
2014-08-30 16:03:50 +02:00
|
|
|
hotkey=`incr_hotkey`
|
|
|
|
return 0
|
|
|
|
}
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
snapper_snapshots_cfg_refresh () {
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
if [ ! -d "$snapper_snapshot_path" ]; then
|
|
|
|
return
|
|
|
|
fi
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
for s_dir in ${snapper_snapshot_path}/*; do
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
snapshot="${s_dir}/snapshot"
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
# list only read-only snapshot (bnc#878528)
|
|
|
|
if [ -w "$snapshot" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [ -r "${s_dir}/info.xml" -a -r "${s_dir}/snapshot/boot/grub2/grub.cfg" ]; then
|
|
|
|
cs="${s_dir}\n${cs}"
|
|
|
|
fi
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
done
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
hk=""
|
|
|
|
[ -z "$hotkey" ] || hk="--hotkey=s"
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-12-05 21:03:44 +01:00
|
|
|
for c in `printf "${cs}\n" | sort -Vr`; do
|
2014-08-30 16:03:50 +02:00
|
|
|
if ! snapshot_submenu "$c" > "${c}/${snapshot_submenu_name}"; then
|
|
|
|
rm -f "${c}/${snapshot_submenu_name}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
snapshot_cfg="${snapshot_cfg}
|
|
|
|
if [ -f "$c/${snapshot_submenu_name}"; then
|
|
|
|
source "$c/${snapshot_submenu_name}"
|
|
|
|
fi"
|
|
|
|
done
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
cat <<EOF >"${snapper_snapshots_cfg}.new"
|
|
|
|
if [ -z "\$extra_cmdline" ]; then
|
|
|
|
submenu $hk "Start bootloader from a read-only snapshot" {${snapshot_cfg}
|
|
|
|
if [ x\$snapshot_found != xtrue ]; then
|
|
|
|
submenu "Not Found" { true; }
|
|
|
|
fi
|
|
|
|
}
|
2014-02-11 10:55:19 +01:00
|
|
|
fi
|
2014-08-30 16:03:50 +02:00
|
|
|
EOF
|
|
|
|
|
|
|
|
if ${grub_script_check} "${snapper_snapshots_cfg}.new"; then
|
|
|
|
mv -f "${snapper_snapshots_cfg}.new" "${snapper_snapshots_cfg}"
|
|
|
|
fi
|
2014-02-11 10:55:19 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
snapshot_submenu_clean () {
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
for s_dir in ${snapper_snapshot_path}/*; do
|
2014-02-11 10:55:19 +01:00
|
|
|
|
|
|
|
snapper_cfg="${s_dir}/${snapshot_submenu_name}"
|
|
|
|
|
|
|
|
if [ -f "$snapper_cfg" ]; then
|
|
|
|
rm -f "$snapper_cfg"
|
2014-08-30 16:03:50 +02:00
|
|
|
rmdir "$s_dir" 2>/dev/null || true
|
2014-02-11 10:55:19 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
2014-08-30 16:03:50 +02:00
|
|
|
|
|
|
|
if [ -f "${snapper_snapshot_path}/${snapshot_submenu_name}" ]; then
|
|
|
|
rm -f "${snapper_snapshot_path}/${snapshot_submenu_name}"
|
|
|
|
fi
|
|
|
|
|
2014-02-11 10:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_grub_setting () {
|
|
|
|
|
|
|
|
name=$1
|
|
|
|
val=$2
|
|
|
|
|
|
|
|
if grep -q "$name" "$grub_setting"; then
|
2014-08-30 16:03:50 +02:00
|
|
|
sed -i -e "s!.*\($name\)=.*!\1=\"$val\"!" "$grub_setting"
|
2014-02-11 10:55:19 +01:00
|
|
|
else
|
2014-08-30 16:03:50 +02:00
|
|
|
echo "$name=\"$val\"" >> "$grub_setting"
|
2014-02-11 10:55:19 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
enable_grub_settings () {
|
|
|
|
set_grub_setting SUSE_BTRFS_SNAPSHOT_BOOTING "true"
|
|
|
|
}
|
2014-02-11 10:55:19 +01:00
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
disable_grub_settings () {
|
|
|
|
set_grub_setting SUSE_BTRFS_SNAPSHOT_BOOTING "false"
|
2014-02-11 10:55:19 +01:00
|
|
|
}
|
|
|
|
|
2014-08-30 16:03:50 +02:00
|
|
|
update_grub () {
|
|
|
|
"${grub_mkconfig}" -o "${grub_cfg}"
|
|
|
|
}
|
|
|
|
|
|
|
|
machine=`uname -m`
|
|
|
|
case "$machine" in
|
|
|
|
(s390|s390x)
|
|
|
|
hotkey=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
cmdline="$0 $* hotkey='$hotkey'"
|
|
|
|
|
2014-02-11 10:55:19 +01:00
|
|
|
# Check the arguments.
|
|
|
|
while test $# -gt 0
|
|
|
|
do
|
|
|
|
option=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$option" in
|
|
|
|
-e | --enable)
|
|
|
|
opt_enable=true
|
|
|
|
;;
|
|
|
|
-d | --disable)
|
|
|
|
opt_enable=false
|
|
|
|
;;
|
|
|
|
-r | --refresh)
|
|
|
|
opt_refresh=true
|
|
|
|
;;
|
|
|
|
-c | --clean)
|
|
|
|
opt_clean=true
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "x${opt_enable}" = "xtrue" ]; then
|
2014-08-30 16:03:50 +02:00
|
|
|
#enable_grub_settings
|
|
|
|
#update_grub
|
|
|
|
snapper_snapshots_cfg_refresh
|
2014-02-11 10:55:19 +01:00
|
|
|
elif [ "x${opt_enable}" = "xfalse" ]; then
|
2014-08-30 16:03:50 +02:00
|
|
|
#disable_grub_settings
|
|
|
|
update_grub
|
2014-02-11 10:55:19 +01:00
|
|
|
snapshot_submenu_clean
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ x${opt_refresh} = "xtrue" ]; then
|
2014-08-30 16:03:50 +02:00
|
|
|
snapper_snapshots_cfg_refresh
|
2014-02-11 10:55:19 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ x${opt_clean} = "xtrue" ]; then
|
|
|
|
snapshot_submenu_clean
|
|
|
|
fi
|
2014-08-30 16:03:50 +02:00
|
|
|
|