1 #!/usr/bin/bash -p
   2 #
   3 # This file and its contents are supplied under the terms of the
   4 # Common Development and Distribution License ("CDDL"), version 1.0.
   5 # You may only use this file in accordance with the terms of version
   6 # 1.0 of the CDDL.
   7 #
   8 # A full copy of the text of the CDDL should have accompanied this
   9 # source.  A copy of the CDDL is also available via the Internet at
  10 # http://www.illumos.org/license/CDDL.
  11 #
  12 
  13 #
  14 # Copyright 2018 Joyent, Inc.
  15 #
  16 
  17 . $STF_SUITE/include/libtest.shlib
  18 . $STF_SUITE/tests/functional/reservation/reservation.shlib
  19 
  20 #
  21 # DESCRIPTION:
  22 #
  23 # Cloning a volume with -o refreservation=auto creates a dense volume
  24 #
  25 # STRATEGY:
  26 # 1) Create a sparse volume.
  27 # 2) Snapshot and clone it, using clone -o refreservation=auto.
  28 # 3) Verify that the clone has refreservation that matches the size predicted by
  29 #    volsize_to_reservation().
  30 # 4) Snapshot this second volume and clone it, using clone -o
  31 #    refreservation=auto.
  32 # 5) Verify that the second clone has refreservation that matches the size
  33 #    predicted by volsize_to_reservation().
  34 #
  35 
  36 verify_runnable "global"
  37 
  38 function cleanup
  39 {
  40         if datasetexists $TESTPOOL/$TESTVOL; then
  41                 # Destroy first vol and descendants in one go.
  42                 log_must zfs destroy -Rf $TESTPOOL/$TESTVOL
  43         fi
  44 }
  45 
  46 log_onexit cleanup
  47 
  48 log_assert "Cloning a volume with -o refreservation=auto creates a dense volume"
  49 
  50 space_avail=$(get_prop available $TESTPOOL)
  51 (( vol_size = (space_avail / 4) & ~(1024 * 1024 - 1) ))
  52 
  53 vol=$TESTPOOL/$TESTVOL
  54 vol2=$TESTPOOL/$TESTVOL2
  55 vol3=$TESTPOOL/$TESTVOL2-again
  56 
  57 # Create sparse vol and verify
  58 log_must zfs create -s -V $vol_size $vol
  59 resv=$(get_prop refreservation $vol)
  60 log_must test $resv -eq 0
  61 
  62 # Clone it
  63 snap=$vol@clone
  64 log_must zfs snapshot $snap
  65 log_must zfs clone -o refreservation=auto $snap $vol2
  66 
  67 # Verify it is dense
  68 resv=$(get_prop refreservation $vol2)
  69 expected=$(volsize_to_reservation $vol2 $vol_size)
  70 log_must test $resv -eq $expected
  71 
  72 # Clone the dense volume
  73 snap=$vol2@clone
  74 log_must zfs snapshot $snap
  75 log_must zfs clone -o refreservation=auto $snap $vol3
  76 
  77 # Verify new newest clone is also dense
  78 resv=$(get_prop refreservation $vol3)
  79 expected=$(volsize_to_reservation $vol3 $vol_size)
  80 log_must test $resv -eq $expected
  81 
  82 log_pass "Cloning a dense volume results in a sparse volume, as expected"