| 1 | #!/bin/sh |
| 2 | |
| 3 | set -ue |
| 4 | |
| 5 | # Is expected to contain two variable definitions with device paths: |
| 6 | # - MENDER_ROOTFS_PART_A |
| 7 | # - MENDER_ROOTFS_PART_B |
| 8 | . /etc/mender/rootfs-image-v2.conf |
| 9 | |
| 10 | MENDER_ROOTFS_PART_A_NUMBER="$(echo "$MENDER_ROOTFS_PART_A" | egrep -o '[0-9]+$')" |
| 11 | MENDER_ROOTFS_PART_B_NUMBER="$(echo "$MENDER_ROOTFS_PART_B" | egrep -o '[0-9]+$')" |
| 12 | |
| 13 | if command -v grub-mender-grubenv-print > /dev/null; then |
| 14 | PRINTENV=grub-mender-grubenv-print |
| 15 | SETENV=grub-mender-grubenv-set |
| 16 | else |
| 17 | PRINTENV=fw_printenv |
| 18 | SETENV=fw_setenv |
| 19 | fi |
| 20 | |
| 21 | active_num="$(${PRINTENV} mender_boot_part)" |
| 22 | active_num="${active_num#mender_boot_part=}" |
| 23 | if test $active_num -eq $MENDER_ROOTFS_PART_A_NUMBER; then |
| 24 | active=$MENDER_ROOTFS_PART_A |
| 25 | passive=$MENDER_ROOTFS_PART_B |
| 26 | passive_num=$MENDER_ROOTFS_PART_B_NUMBER |
| 27 | else |
| 28 | active=$MENDER_ROOTFS_PART_B |
| 29 | passive=$MENDER_ROOTFS_PART_A |
| 30 | passive_num=$MENDER_ROOTFS_PART_A_NUMBER |
| 31 | fi |
| 32 | passive_num_hex=$(printf '%x' $passive_num) |
| 33 | |
| 34 | ensure_correct_root_mounted() { |
| 35 | if [ "$(stat -L -c %02t%02T $active)" != "$(stat -L -c %04D /)" ]; then |
| 36 | echo "Mounted root does not match boot loader environment!" 1>&2 |
| 37 | exit 1 |
| 38 | fi |
| 39 | } |
| 40 | |
| 41 | case "$1" in |
| 42 | Download) |
| 43 | ensure_correct_root_mounted |
| 44 | file="$(cat stream-next)" |
| 45 | cat "$file" > $passive |
| 46 | if [ "$(cat stream-next)" != "" ]; then |
| 47 | echo "More than one file in payload" 1>&2 |
| 48 | exit 1 |
| 49 | fi |
| 50 | ;; |
| 51 | |
| 52 | ArtifactInstall) |
| 53 | ensure_correct_root_mounted |
| 54 | ${SETENV} -s - <<EOF |
| 55 | mender_boot_part=$passive_num |
| 56 | mender_boot_part_hex=$passive_num_hex |
| 57 | upgrade_available=1 |
| 58 | bootcount=0 |
| 59 | EOF |
| 60 | ;; |
| 61 | |
| 62 | NeedsArtifactReboot) |
| 63 | echo "Automatic" |
| 64 | ;; |
| 65 | |
| 66 | SupportsRollback) |
| 67 | echo "Yes" |
| 68 | ;; |
| 69 | |
| 70 | ArtifactVerifyReboot) |
| 71 | ensure_correct_root_mounted |
| 72 | if test "$(${PRINTENV} upgrade_available)" != "upgrade_available=1"; then |
| 73 | exit 1 |
| 74 | fi |
| 75 | ;; |
| 76 | |
| 77 | ArtifactVerifyRollbackReboot) |
| 78 | ensure_correct_root_mounted |
| 79 | if test "$(${PRINTENV} upgrade_available)" = "upgrade_available=1"; then |
| 80 | exit 1 |
| 81 | fi |
| 82 | ;; |
| 83 | |
| 84 | ArtifactCommit) |
| 85 | ensure_correct_root_mounted |
| 86 | if test "$(${PRINTENV} upgrade_available)" = "upgrade_available=1"; then |
| 87 | ${SETENV} upgrade_available 0 |
| 88 | else |
| 89 | # If we get here, an upgrade in standalone mode failed to boot and the user is trying to commit from the old OS. |
| 90 | # This communicates to the user that the upgrade failed. |
| 91 | echo "Upgrade failed and was reverted: refusing to commit!" 1>&2 |
| 92 | exit 1 |
| 93 | fi |
| 94 | ;; |
| 95 | |
| 96 | ArtifactRollback) |
| 97 | ensure_correct_root_mounted |
| 98 | if test "$(${PRINTENV} upgrade_available)" = "upgrade_available=1"; then |
| 99 | ${SETENV} -s - <<EOF |
| 100 | mender_boot_part=$passive_num |
| 101 | mender_boot_part_hex=$passive_num_hex |
| 102 | upgrade_available=0 |
| 103 | EOF |
| 104 | fi |
| 105 | ;; |
| 106 | esac |
| 107 | exit 0 |