summaryrefslogtreecommitdiff
path: root/package/kexec-tools/files/kreboot
blob: 1a32e18cf412ce84f27496cf55c021f109dbc797 (plain)
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
#!/bin/sh

ARCH=$(uname -m)
KERNEL="/mnt/boot/kernel"
KEXEC_ARGS="-l"
if [ $ARCH = "arm" ]; then
  KEXEC_ARGS="$KEXEC_ARGS --atags"
fi
PART_A=2
PART_B=3
me=$0

usage() {
cat >&2 <<EOF
Syntax: $me [-k kernel]

 -k	give the path to the kernel eg. /boot/vmlinuz-4.7.4
        (default: $KERNEL)
EOF
  exit $1
}

while getopts "k:" ch; do
  case $ch {
  (k)
    # if default does not fit, give the actual keren via -k otion eg "-k /boot/vmlinuz-4.7.4
    KERNEL="$OPTARG"
  ;;

  (*)
    usage 1
  ;;
  }
done
shift $((OPTIND - 1))


load_kernel(){
  # get the Bootargs and replace the current Partition with
  # the one in $1 e.g. replace /dev/mmcblk0p2 with /dev/mmcblk0p3
  BOOT_ARGS="$(cat /proc/cmdline | sed s#$CURRENT_PART#$1#g)"
  # echo "kexec $KEXEC_ARG --append=\"$BOOT_ARGS\" $KERNEL"
  # set -x
  # Load the new kernel, unmount the partition and exec the new kernel
  kexec $KEXEC_ARGS --append=\""$BOOT_ARGS"\" $KERNEL
  umount /mnt
  kexec -e
}

# just to be sure
umount /mnt 2> /dev/null

# get the partiton of the current kernel
PART="/dev/$(readlink /dev/root)"
# extract the partition number
C_M_PART_NUM=$(readlink /dev/root | grep -o -e '[[:digit:]]*$')

# cut off the Partition Number
C_M_PART=$(readlink /dev/root | sed "s/$C_M_PART_NUM\$//")

CURRENT_PART="/dev/${C_M_PART}${C_M_PART_NUM}"

case $C_M_PART_NUM in
  "$PART_A")
    DEVICE="/dev/${C_M_PART}${PART_B}"
    mount -r $DEVICE /mnt
    load_kernel $DEVICE
  ;;
  "$PART_B")
    DEVICE="/dev/${C_M_PART}${PART_A}"
    mount -r $DEVICE /mnt
    load_kernel $DEVICE
  ;;
  *)
    echo "FAILURE"
  ;;
esac