Skip to content

Commit

Permalink
Add "rollback" command to "upgrade" script
Browse files Browse the repository at this point in the history
This change builds on the previously added "set-bootfs" command of the
"rootfs-container" script, now providing an easy way to "rollback" after
booting to an upgraded rootfs container. This allows a user to easily
revert an upgrade, if the upgrade is deemed unsuccessful.

For example, one can run the following to perform an upgrade:

    $ sudo /var/dlpx-update/latest/upgrade -v not-in-place
    $ sudo reboot

Now, after the reboot, if it's determined that the upgrade was not
successful, one can run the following to rollback that prior upgrade:

    $ sudo /var/dlpx-update/latest/upgrade rollback
    $ sudo reboot

At this point, the system will boot to the original root filesystem that
was used prior ot the upgrade.
  • Loading branch information
Prakash Surya committed Jan 29, 2019
1 parent a52beac commit c04a841
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions upgrade/upgrade-scripts/upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

. "${BASH_SOURCE%/*}/common.sh"

#
# Any changes to this value needs to be careful to properly support
# existing rootfs container datasets which may have this property
# already set. Thus, changes here may require backwards compatibility.
#
ROLLBACK_PROPERTY="com.delphix:rollback-container"

IMAGE_PATH=$(get_image_path)
[[ -n "$IMAGE_PATH" ]] || die "failed to determine image path"

Expand All @@ -42,6 +49,7 @@ function usage() {

echo "$PREFIX_STRING [-v] in-place"
echo "$PREFIX_SPACES [-v] not-in-place"
echo "$PREFIX_SPACES rollback"

exit 2
}
Expand Down Expand Up @@ -136,9 +144,23 @@ function cleanup_not_in_place_upgrade() {
return "$rc"
}

function get_mounted_rootfs_container() {
basename "$(dirname "$(zfs list -Hpo name /)")"
}

function upgrade_not_in_place() {
trap cleanup_not_in_place_upgrade EXIT

#
# We query the mounted rootfs container name here, so that if we
# can't get this information for whatever reason, we can easily
# handle the error and abort; rather than having to handle this
# error later, when it might require more work to handle it.
#
MOUNTED_CONTAINER="$(get_mounted_rootfs_container)"
[[ -n "$MOUNTED_CONTAINER" ]] ||
die "failed to determine mounted rootfs container"

CONTAINER=$("$IMAGE_PATH/upgrade-container" create not-in-place)
[[ -n "$CONTAINER" ]] || die "failed to create upgrade container"

Expand Down Expand Up @@ -178,10 +200,41 @@ function upgrade_not_in_place() {
"$IMAGE_PATH/upgrade-container" convert-to-rootfs "$CONTAINER" ||
die "failed to convert-to-rootfs '$CONTAINER'"

#
# In order for the "rollback" back command to work, we need to
# know which rootfs container to rollback to. Thus, we embed
# this information into the rootfs container using a ZFS
# property; rollback will read this information, to determine
# which rootfs container to rollback to.
#
zfs set \
"$ROLLBACK_PROPERTY=$MOUNTED_CONTAINER" \
"rpool/ROOT/$CONTAINER" ||
die "'zfs set com.delphix:rollback-container' failed"

"$IMAGE_PATH/rootfs-container" set-bootfs "$CONTAINER" ||
die "failed to set-bootfs '$CONTAINER'"
}

function rollback() {
MOUNTED_CONTAINER="$(get_mounted_rootfs_container)"
[[ -n "$MOUNTED_CONTAINER" ]] ||
die "failed to determine mounted rootfs container"

ROLLBACK_CONTAINER="$(zfs get -Hpo value \
"$ROLLBACK_PROPERTY" "rpool/ROOT/$MOUNTED_CONTAINER")"
[[ -n "$ROLLBACK_CONTAINER" && "$ROLLBACK_CONTAINER" != "-" ]] ||
die "failed to determine rollback rootfs container"

#
# The "rollback" operation is nothing more than "set-bootfs" of
# a specific rootfs container. Now that we have the specific
# rootfs container that we want to use as the next bootfs.
#
"$IMAGE_PATH/rootfs-container" set-bootfs "$ROLLBACK_CONTAINER" ||
die "failed to set-bootfs '$ROLLBACK_CONTAINER'"
}

[[ "$EUID" -ne 0 ]] && die "must be run as root"

while getopts ':v' c; do
Expand All @@ -208,6 +261,10 @@ not-in-place)
verify_upgrade_is_allowed
upgrade_not_in_place "$@"
;;
rollback)
shift 1
rollback "$@"
;;
*)
usage "invalid option -- '$1'"
;;
Expand Down

0 comments on commit c04a841

Please sign in to comment.