File: grub

package info (click to toggle)
hibernate 2.0%2B15%2Bg88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (128 lines) | stat: -rw-r--r-- 3,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

GRUB_MENU_FILE="/boot/grub/grub.cfg"
GRUB_MENU_DFLT_BACKUP_FILE="/var/backups/grub.cfg.hibernate"

AddConfigHandler GrubConfigOptions
AddConfigHelp "ChangeGrubMenu <boolean>" "Change grub's config file to show system is suspended before suspending and restore after resume."
AddConfigHelp "GrubMenuFile <filename>" "Filename of grub's config file. Default is ${GRUB_MENU_FILE}."
AddConfigHelp "AlternateGrubMenuFile <filename>" "Filename of the grub config file to put in place when the machine is suspended. If this is not specified, the script will add a small header to the existing grub menu."
AddConfigHelp "BackupGrubMenuFile <filename>" "Where to keep a backup of your real grub menu file. Defaults to ${GRUB_MENU_DFLT_BACKUP_FILE}."

AddOptionHandler GrubCmdlineOptions
AddShortOption 'g'
AddLongOption 'restore-grub'
AddOptionHelp '-g, --restore-grub' 'Restores the grub menu to normal (use if a resume was not completed successfully) and exits the script. A suspend is not performed.'

GRUB_BEGIN_SENTINEL="### BEGIN HIBERNATE SENTINEL"
GRUB_END_SENTINEL="### END HIBERNATE SENTINEL"

ChangeGrubMenu() {
    if [ x"$CHANGE_GRUB_MENU" = "x1" ] ; then
	if [ -f $GRUB_MENU_FILE ] ;then
	    # given file exists so change menu

	    vecho 2 "Changing grub menu..."

	    # Make a backup, and abort if we fail to.
	    if ! cp "$GRUB_MENU_FILE" "${BACKUP_GRUB_MENU_FILE:-$GRUB_MENU_DFLT_BACKUP_FILE}" ; then
		vecho 0 "Could not make backup of grub menu. Not changing!"
		return 1 # abort if not forced
	    fi

	    # If we have an alternate file, use that. Else alter the current one
	    if [ -n "$ALT_GRUB_MENU_FILE" ] ; then
		if ! cp -f "$ALT_GRUB_MENU_FILE" "$GRUB_MENU_FILE" ; then
		    vecho 0 "Could not replace grub menu file with alternate."
		    return 1 # abort if not forced
		fi
	    else
		RestoreGrubMenu
		cat <<-EOF >> $GRUB_MENU_FILE
		$GRUB_BEGIN_SENTINEL
		
		title _____________________________________________________________________
		configfile dummy
		title WARNING: `uname -s -r` is suspended via TuxOnIce!
		configfile dummy
		$GRUB_END_SENTINEL
		EOF
	    fi
	    # Call sync to ensure it is written to disk (do we still need
	    # double/triple syncs?)
	    sync ; sync
	else
	    CHANGE_GRUB_MENU=0
	    vecho 1 "Grub config file not found. Grub scriptlet disabled."
	fi
    fi
}

RestoreGrubMenu() {
    if [ x"$CHANGE_GRUB_MENU" = "x1" ] ; then
	if [ -z "$ALT_GRUB_MENU_FILE" ]; then
	    sed -e "/^$GRUB_BEGIN_SENTINEL/,/^$GRUB_END_SENTINEL/d" \
		$GRUB_MENU_FILE > $GRUB_MENU_FILE.hibernate.new \
		&& mv $GRUB_MENU_FILE.hibernate.new $GRUB_MENU_FILE
	else
	    mv "${BACKUP_GRUB_MENU_FILE:-$GRUB_MENU_DFLT_BACKUP_FILE}" "$GRUB_MENU_FILE"
	fi
    fi
}

GrubConfigOptions() {
    case $1 in
	changegrubmenu)
	    BoolIsOn "$1" "$2" && CHANGE_GRUB_MENU=1 || return 0
	    ;;
	grubmenufile)
	    GRUB_MENU_FILE="$2"
	    return 0
	    ;;
	alternategrubmenufile)
	    if [ ! -r "$2" ] ; then
		vecho 0 "AlternateGrubMenuFile $2 is not readable."
		vecho 0 "This configuation option will be ignored."
		return 0
	    fi
	    ALT_GRUB_MENU_FILE="$2"
	    return 0
	    ;;
	backupgrubmenufile)
	    BACKUP_GRUB_MENU_FILE="$2"
	    return 0
	    ;;
	*)
	    return 1
    esac
    if [ -z "$GRUB_HOOKED" ] ; then
	AddSuspendHook 11 ChangeGrubMenu
	AddResumeHook 11 RestoreGrubMenu
	GRUB_HOOKED=1
    fi
    return 0
}

GrubCmdlineOptions() {
    case $1 in
	-g|--restore-grub)
	    AddSuspendHook 00 RestoreGrubStandalone
	    ;;
	*)
	    return 1
    esac
    return 0
}

RestoreGrubStandalone() {
    # we want to restore always
    CHANGE_GRUB_MENU=1
    RestoreGrubMenu && vecho 0 "Grub menu restored." || vecho 0 "No grub menu found to restore."

    # 3 to indicate hibernate to be silent on exiting
    return 3
}


# $Id$