Skip to content

Commit

Permalink
Add "delete" command to "rootfs-container" script
Browse files Browse the repository at this point in the history
This change extends the "rootfs-container" script to enable the deletion
of existing rootfs containers. For example, given the following system:

    $ sudo zfs list
    NAME                              USED  AVAIL     REFER  MOUNTPOINT
    rpool                            38.5G  28.9G       64K  none
    rpool/ROOT                       34.0G  28.9G       64K  none
    rpool/ROOT/delphix.739W0dx       5.56G  28.9G       64K  none
    rpool/ROOT/delphix.739W0dx/data   840K  28.9G     27.9M  legacy
    rpool/ROOT/delphix.739W0dx/home   155K  28.9G     11.0G  legacy
    rpool/ROOT/delphix.739W0dx/root  5.56G  28.9G     5.56G  /
    rpool/ROOT/delphix.BTC02Vz       5.98G  28.9G       65K  none
    rpool/ROOT/delphix.BTC02Vz/data  3.32M  28.9G     29.1M  legacy
    rpool/ROOT/delphix.BTC02Vz/home   346K  28.9G     11.0G  legacy
    rpool/ROOT/delphix.BTC02Vz/root  5.98G  28.9G     5.97G  /
    rpool/ROOT/delphix.fuzt21y       5.54G  28.9G       64K  none
    rpool/ROOT/delphix.fuzt21y/data   620K  28.9G     28.5M  legacy
    rpool/ROOT/delphix.fuzt21y/home   152K  28.9G     11.0G  legacy
    rpool/ROOT/delphix.fuzt21y/root  5.54G  28.9G     5.54G  /
    rpool/ROOT/delphix.ns2t0fx       16.9G  28.9G       64K  none
    rpool/ROOT/delphix.ns2t0fx/data  29.8M  28.9G     29.0M  legacy
    rpool/ROOT/delphix.ns2t0fx/home  11.0G  28.9G     11.0G  legacy
    rpool/ROOT/delphix.ns2t0fx/root  5.89G  28.9G     5.88G  /
    rpool/crashdump                  65.5K  17.4G     65.5K  /var/crash
    rpool/grub                       3.03M  28.9G     3.03M  legacy
    rpool/update                     4.48G  10.5G     4.48G  /var/dlpx-update

We can now use this new command to remove all but the currently booted
rootfs container:

    $ sudo /var/dlpx-update/latest/rootfs-container delete delphix.BTC02Vz
    $ sudo /var/dlpx-update/latest/rootfs-container delete delphix.739W0dx
    $ sudo /var/dlpx-update/latest/rootfs-container delete delphix.ns2t0fx

    $ zfs list -t all
    NAME                                              USED  AVAIL     REFER  MOUNTPOINT
    rpool                                            21.1G  46.3G       64K  none
    rpool/ROOT                                       16.6G  46.3G       64K  none
    rpool/ROOT/delphix.fuzt21y                       16.6G  46.3G       64K  none
    rpool/ROOT/delphix.fuzt21y/data                  30.4M  46.3G     28.9M  legacy
    rpool/ROOT/delphix.fuzt21y/[email protected]   958K      -     27.4M  -
    rpool/ROOT/delphix.fuzt21y/[email protected]   298K      -     27.4M  -
    rpool/ROOT/delphix.fuzt21y/[email protected]   336K      -     28.2M  -
    rpool/ROOT/delphix.fuzt21y/home                  11.0G  46.3G     11.0G  legacy
    rpool/ROOT/delphix.fuzt21y/[email protected]   152K      -     11.0G  -
    rpool/ROOT/delphix.fuzt21y/[email protected]   143K      -     11.0G  -
    rpool/ROOT/delphix.fuzt21y/[email protected]   143K      -     11.0G  -
    rpool/ROOT/delphix.fuzt21y/root                  5.58G  46.3G     5.58G  /
    rpool/crashdump                                  65.5K  17.4G     65.5K  /var/crash
    rpool/grub                                       3.03M  46.3G     3.03M  legacy
    rpool/update                                     4.48G  10.5G     4.48G  /var/dlpx-update

Additionally, it'll prevent the user from deleting the rootfs container
that's currently booted:

    $ zfs list /
    NAME                              USED  AVAIL     REFER  MOUNTPOINT
    rpool/ROOT/delphix.fuzt21y/root  5.57G  46.3G     5.57G  /

    $ sudo /var/dlpx-update/latest/rootfs-container delete delphix.fuzt21y
    rootfs-container: cannot delete mounted rootfs container: 'delphix.fuzt21y'
  • Loading branch information
Prakash Surya committed Jan 26, 2019
1 parent e0ce271 commit 0f2ee71
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions upgrade/upgrade-scripts/rootfs-container
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <container>"
echo "$PREFIX_SPACES set-bootfs <container>"

exit 2
Expand All @@ -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"
Expand Down

0 comments on commit 0f2ee71

Please sign in to comment.