~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to utopic/etc/init.d/kexec-load

  • Committer: Dimitri John Ledkov
  • Date: 2014-05-06 18:45:46 UTC
  • Revision ID: dimitri.ledkov@canonical.com-20140506184546-5toyx56xxrue0f0v
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
### BEGIN INIT INFO
 
3
# Provides:             kexec-load
 
4
# Required-Start:
 
5
# Required-Stop:        $local_fs $remote_fs kexec
 
6
# Should-Stop:          autofs
 
7
# Default-Start:
 
8
# Default-Stop:         6
 
9
# Short-Description: Load kernel image with kexec
 
10
# Description:
 
11
### END INIT INFO
 
12
 
 
13
PATH=/sbin:/bin:/usr/sbin:/usr/bin
 
14
NOKEXECFILE=/tmp/no-kexec-reboot
 
15
 
 
16
. /lib/lsb/init-functions
 
17
 
 
18
test -r /etc/default/kexec && . /etc/default/kexec
 
19
 
 
20
process_grub_entry() {
 
21
        initrd_image=
 
22
        while read command args; do
 
23
                if [ "$command" = "linux" ]; then
 
24
                        echo "$args" | while read kernel append; do
 
25
                        echo KERNEL_IMAGE=\"${prefix}${kernel}\"
 
26
                        echo APPEND=\"${append}\"
 
27
                        done
 
28
                elif [ "$command" = "initrd" ]; then
 
29
                        initrd_image=${prefix}${args}
 
30
                fi
 
31
        done
 
32
        echo INITRD=\"$initrd_image\"
 
33
}
 
34
 
 
35
get_grub_kernel() {
 
36
        test -f /boot/grub/grub.cfg || return
 
37
        local prefix
 
38
        mountpoint -q /boot && prefix=/boot || prefix=
 
39
        data=$(cat /boot/grub/grub.cfg)
 
40
 
 
41
        default=$(echo "$data" | awk '/^set default/ {print $2}' | cut -d'"' -f2)
 
42
        if [ "$default" = '${saved_entry}' ]; then 
 
43
                default=$(sed -ne 's/^saved_entry=//p' /boot/grub/grubenv)
 
44
        fi
 
45
        if [ -z "$default" ]; then
 
46
                default=0
 
47
        fi
 
48
        start_offset=$((default + 1))
 
49
        end_offset=$((default + 2))
 
50
 
 
51
        # grub entries start with "menuentry" commands.  Get the line 
 
52
        # numbers that surround the first entry
 
53
        offsets=$(echo "$data" | grep -n '^[[:space:]]*menuentry[[:space:]]' | cut -d: -f1)
 
54
        begin=$(echo "$offsets" | tail -n+$start_offset | head -n1)
 
55
        end=$(echo "$offsets" | tail -n+$end_offset | head -n1)
 
56
 
 
57
        # If this is the last entry, we need to read to the end of the file
 
58
        # or to the end of boot entry section
 
59
        if [ -z "$end" ]; then
 
60
                numlines=$(echo "$data" | tail --lines=+$begin | grep -n "^### END" | head -1 | cut -d: -f1)
 
61
                end=$((begin + numlines - 1))
 
62
        fi
 
63
 
 
64
        length=$((end - begin))
 
65
        entry=$(echo "$data" | tail -n+$begin | head -n$length)
 
66
        eval $(echo "$entry" | process_grub_entry)
 
67
}
 
68
 
 
69
do_stop () {
 
70
        test "$LOAD_KEXEC" = "true" || exit 0
 
71
        test -x /sbin/kexec || exit 0
 
72
        test "x`cat /sys/kernel/kexec_loaded`y" = "x1y" && exit 0
 
73
 
 
74
        if [ -f $NOKEXECFILE ]
 
75
        then
 
76
                /bin/rm -f $NOKEXECFILE
 
77
                exit 0
 
78
        fi
 
79
 
 
80
        test "$USE_GRUB_CONFIG" = "true" && get_grub_kernel
 
81
 
 
82
        REAL_APPEND="$APPEND"
 
83
 
 
84
        test -z "$REAL_APPEND" && REAL_APPEND="`cat /proc/cmdline`"
 
85
        log_action_begin_msg "Loading new kernel image into memory"
 
86
        if [ -z "$INITRD" ]
 
87
        then
 
88
                /sbin/kexec -l "$KERNEL_IMAGE" --append="$REAL_APPEND"
 
89
        else
 
90
                /sbin/kexec -l "$KERNEL_IMAGE" --initrd="$INITRD" --append="$REAL_APPEND"
 
91
        fi
 
92
        log_action_end_msg $?
 
93
}
 
94
 
 
95
case "$1" in
 
96
  start)
 
97
        # No-op
 
98
        ;;
 
99
  restart|reload|force-reload)
 
100
        echo "Error: argument '$1' not supported" >&2
 
101
        exit 3
 
102
        ;;
 
103
  stop)
 
104
        do_stop
 
105
        ;;
 
106
  *)
 
107
        echo "Usage: $0 start|stop" >&2
 
108
        exit 3
 
109
        ;;
 
110
esac
 
111
exit 0