diff --git a/upgrade/upgrade-scripts/rootfs-container b/upgrade/upgrade-scripts/rootfs-container index d574ed92..bf38d2cd 100755 --- a/upgrade/upgrade-scripts/rootfs-container +++ b/upgrade/upgrade-scripts/rootfs-container @@ -19,6 +19,43 @@ CONTAINER= +function get_dataset_snapshots() { + zfs list -rt snapshot -Hpo name "$1" +} + +function get_snapshot_clones() { + zfs get clones -Hpo value "$1" +} + +function delete() { + zfs list "rpool/ROOT/$CONTAINER/root" &>/dev/null || + die "rootfs container '$CONTAINER' does not exist" + + MOUNTED=$(zfs get mounted -Hpo value "rpool/ROOT/$CONTAINER/root") + [[ "$MOUNTED" == "no" ]] || + die "cannot delete mounted rootfs container: '$CONTAINER'" + + # + # The "data" and "home" datasets of a rootfs container may have + # been cloned as part of an upgrade. Thus, in order to delete + # this specific rootfs container, we need to promote any clones + # that exist. Otherwise we won't be able to destroy the snapshots + # for the "data" and "home" datasets, and thus can't destroy the + # datasets themselves. + # + for snap in \ + $(get_dataset_snapshots "rpool/ROOT/$CONTAINER/data") \ + $(get_dataset_snapshots "rpool/ROOT/$CONTAINER/home"); do + for clone in $(get_snapshot_clones "$snap"); do + zfs promote "$clone" || + die "'zfs promote $clone' failed" + done + done + + zfs destroy -r "rpool/ROOT/$CONTAINER" || + die "'zfs destroy -r rpool/ROOT/$CONTAINER' failed" +} + function get_bootloader_devices() { # # When installing/updating the bootloader during upgrade, we @@ -184,6 +221,7 @@ function usage() { PREFIX_NCHARS=$(echo -n "$PREFIX_STRING" | wc -c) PREFIX_SPACES=$(printf "%.s " $(seq "$PREFIX_NCHARS")) + echo "$PREFIX_SPACES delete " echo "$PREFIX_SPACES set-bootfs " exit 2 @@ -192,6 +230,13 @@ function usage() { [[ "$EUID" -ne 0 ]] && die "must be run as root" case "$1" in +delete) + [[ $# -lt 2 ]] && usage "too few arguments specified" + [[ $# -gt 2 ]] && usage "too many arguments specified" + + CONTAINER="$2" + delete + ;; set-bootfs) [[ $# -lt 2 ]] && usage "too few arguments specified" [[ $# -gt 2 ]] && usage "too many arguments specified"