2014-02-08 11:52:59 +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-03-31 21:51:24 +02:00
|
|
|
grub_script_check="/usr/bin/grub2-script-check"
|
2014-02-08 11:52:59 +01:00
|
|
|
grub_setting="/etc/default/grub"
|
|
|
|
grub_cfg="/boot/grub2/grub.cfg"
|
|
|
|
grub_snapshot_cfg="/boot/grub2/snapshot_submenu.cfg"
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
snapper_snapshot_path="/.snapshots"
|
2014-02-08 11:52:59 +01:00
|
|
|
snapshot_submenu_name="snapshot_submenu.cfg"
|
2014-03-31 21:51:24 +02:00
|
|
|
snapper_snapshots_cfg="${snapper_snapshot_path}/snapshot_submenu.cfg"
|
|
|
|
|
|
|
|
case x"`uname -m`" in
|
|
|
|
x"powerpc"* | x"ppc"*)
|
|
|
|
target="powerpc-ieee1275";;
|
|
|
|
x"sparc"*)
|
|
|
|
target="sparc64-ieee1275";;
|
|
|
|
x"mips"*"el")
|
|
|
|
target="mipsel-loongson";;
|
|
|
|
x"mips"*)
|
|
|
|
target="mips-arc";;
|
|
|
|
x"ia64"*)
|
|
|
|
target="ia64-efi";;
|
|
|
|
x"x86_64"* | x"amd64"*)
|
|
|
|
modprobe -q efivars 2>/dev/null || true
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
target="x86_64-efi"
|
|
|
|
else
|
|
|
|
target=i386-pc
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
x"i"?"86"*)
|
|
|
|
modprobe -q efivars 2>/dev/null || true
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
target="i386-efi"
|
|
|
|
elif [ -e /proc/device-tree ]; then
|
|
|
|
target=i386-pc
|
|
|
|
for x in /proc/device-tree/*; do
|
|
|
|
if [ -e "$x" ]; then
|
|
|
|
target="i386-ieee1275"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
target=i386-pc
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
target=""
|
|
|
|
;;
|
|
|
|
esac
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
if [ "x${target}" != "xi386-pc" ] &&
|
|
|
|
[ "x${target}" != "xx86_64-efi" ] &&
|
|
|
|
[ "x${target}" != "xi386-efi" ]; then
|
|
|
|
exit 0;
|
|
|
|
fi
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
snapshot_submenu_refresh () {
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
for s_dir in ${snapper_snapshot_path}/*; do
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
snapshot="${s_dir}/snapshot"
|
|
|
|
snapper_cfg="${s_dir}/${snapshot_submenu_name}"
|
2014-03-31 21:51:24 +02:00
|
|
|
num="`basename $s_dir`"
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
if [ ! -d "$snapshot" ]; then
|
|
|
|
rm -f "${snapper_cfg}"
|
2014-03-31 21:51:24 +02:00
|
|
|
rmdir "$s_dir" 2>/dev/null || true
|
2014-02-08 11:52:59 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2014-03-03 10:08:52 +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-08 11:52:59 +01:00
|
|
|
date=`xmllint --xpath '/snapshot/date/text()' "${s_dir}/info.xml" || echo ""`
|
2014-03-03 10:08:52 +01:00
|
|
|
date=`echo $date | sed 's/\(.*\) \(.*\):.*/\1,\2/'`
|
|
|
|
important=`xmllint --xpath "/snapshot/userdata[key='important']/value/text()" "${s_dir}/info.xml" || echo ""`
|
|
|
|
stype=`xmllint --xpath '/snapshot/type/text()' "${s_dir}/info.xml" || echo ""`
|
|
|
|
kernel_ver=`readlink /boot/vmlinuz | sed 's/vmlinuz-\([^-]*\).*/\1/'`
|
|
|
|
eval `cat ${snapshot}/etc/os-release`
|
|
|
|
test "$important" = "yes" && important="*" || important=""
|
|
|
|
test "$stype" = "single" && stype=""
|
|
|
|
|
|
|
|
if test -n "$stype"; then
|
|
|
|
title="${important}${NAME} $VERSION ($kernel_ver,$date,$stype)"
|
|
|
|
else
|
|
|
|
title="${important}${NAME} $VERSION ($kernel_ver,$date)"
|
|
|
|
fi
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
cat <<EOF > "${snapper_cfg}.new"
|
2014-03-31 21:51:24 +02:00
|
|
|
|
|
|
|
if [ -f "${snapper_snapshot_path}/$num/snapshot/boot/grub2/grub.cfg" ]; then
|
|
|
|
snapshot_found=true
|
|
|
|
saved_subvol=\$btrfs_subvol
|
|
|
|
menuentry "$title" "${snapper_snapshot_path}/$num/snapshot" "`$grub_mkrelpath ${snapper_snapshot_path}/${num}/snapshot`" {
|
|
|
|
btrfs_subvol="\$2"
|
|
|
|
extra_cmdline="rootflags=subvol=\$3"
|
|
|
|
configfile /boot/grub2/grub.cfg
|
|
|
|
btrfs_subvol=\$saved_subvol
|
|
|
|
}
|
|
|
|
fi
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
EOF
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
if ${grub_script_check} "${snapper_cfg}.new"; then
|
2014-02-08 11:52:59 +01:00
|
|
|
mv -f "${snapper_cfg}.new" "${snapper_cfg}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
snapper_snapshots_cfg_refresh () {
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
if [ ! -d "$snapper_snapshot_path" ]; then
|
|
|
|
return
|
|
|
|
fi
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
for s_dir in ${snapper_snapshot_path}/*; do
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
snapshot="${s_dir}/snapshot"
|
|
|
|
snapper_cfg="${s_dir}/${snapshot_submenu_name}"
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
if [ -f "${snapper_cfg}" ]; then
|
|
|
|
cs="${s_dir}\n${cs}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
for c in `echo -e "${cs}" | sort -V`; do
|
|
|
|
snapshot_cfg="\
|
|
|
|
if [ -f "$c/snapshot_submenu.cfg" ]; then
|
|
|
|
source "$c/snapshot_submenu.cfg"
|
|
|
|
fi
|
|
|
|
${snapshot_cfg}"
|
|
|
|
done
|
|
|
|
|
|
|
|
cat <<EOF >"${snapper_snapshots_cfg}.new"
|
2014-02-08 11:52:59 +01:00
|
|
|
submenu "Bootable snapshots" {
|
2014-03-31 21:51:24 +02:00
|
|
|
${snapshot_cfg}
|
2014-02-08 11:52:59 +01:00
|
|
|
if [ x\$snapshot_found != xtrue ]; then
|
|
|
|
submenu "Not Found" {true}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
if ${grub_script_check} "${snapper_snapshots_cfg}.new"; then
|
|
|
|
mv -f "${snapper_snapshots_cfg}.new" "${snapper_snapshots_cfg}"
|
|
|
|
fi
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
snapshot_submenu_clean () {
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
for s_dir in ${snapper_snapshot_path}/*; do
|
2014-02-08 11:52:59 +01:00
|
|
|
|
|
|
|
snapper_cfg="${s_dir}/${snapshot_submenu_name}"
|
|
|
|
|
|
|
|
if [ -f "$snapper_cfg" ]; then
|
|
|
|
rm -f "$snapper_cfg"
|
2014-03-31 21:51:24 +02:00
|
|
|
rmdir "$s_dir" 2>/dev/null || true
|
2014-02-08 11:52:59 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
2014-03-31 21:51:24 +02:00
|
|
|
|
|
|
|
if [ -f "${snapper_snapshot_path}/${snapshot_submenu_name}" ]; then
|
|
|
|
rm -f "${snapper_snapshot_path}/${snapshot_submenu_name}"
|
|
|
|
fi
|
|
|
|
|
2014-02-08 11:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_grub_setting () {
|
|
|
|
|
|
|
|
name=$1
|
|
|
|
val=$2
|
|
|
|
|
|
|
|
if grep -q "$name" "$grub_setting"; then
|
2014-03-31 21:51:24 +02:00
|
|
|
sed -i -e "s!.*\($name\)=.*!\1=\"$val\"!" "$grub_setting"
|
2014-02-08 11:52:59 +01:00
|
|
|
else
|
2014-03-31 21:51:24 +02:00
|
|
|
echo "$name=\"$val\"" >> "$grub_setting"
|
2014-02-08 11:52:59 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
enable_grub_settings () {
|
|
|
|
set_grub_setting SUSE_BTRFS_SNAPSHOT_BOOTING "true"
|
|
|
|
}
|
2014-02-08 11:52:59 +01:00
|
|
|
|
2014-03-31 21:51:24 +02:00
|
|
|
disable_grub_settings () {
|
|
|
|
set_grub_setting SUSE_BTRFS_SNAPSHOT_BOOTING "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
update_grub () {
|
|
|
|
"${grub_mkconfig}" -o "${grub_cfg}"
|
2014-02-08 11:52:59 +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-03-31 21:51:24 +02:00
|
|
|
#enable_grub_settings
|
|
|
|
#update_grub
|
2014-02-08 11:52:59 +01:00
|
|
|
snapshot_submenu_refresh
|
2014-03-31 21:51:24 +02:00
|
|
|
snapper_snapshots_cfg_refresh
|
2014-02-08 11:52:59 +01:00
|
|
|
elif [ "x${opt_enable}" = "xfalse" ]; then
|
2014-03-31 21:51:24 +02:00
|
|
|
#disable_grub_settings
|
|
|
|
update_grub
|
2014-02-08 11:52:59 +01:00
|
|
|
snapshot_submenu_clean
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ x${opt_refresh} = "xtrue" ]; then
|
|
|
|
snapshot_submenu_refresh
|
2014-03-31 21:51:24 +02:00
|
|
|
snapper_snapshots_cfg_refresh
|
2014-02-08 11:52:59 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ x${opt_clean} = "xtrue" ]; then
|
|
|
|
snapshot_submenu_clean
|
|
|
|
fi
|
2014-03-31 21:51:24 +02:00
|
|
|
|