1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2018 by Datto Inc. All rights reserved.
19# Copyright 2020 Joyent, Inc.
20#
21
22. $STF_SUITE/tests/functional/rsend/rsend.kshlib
23
24#
25# DESCRIPTION:
26# Verify that zvols with dedup=on and encryption=on can be sent and received
27# with a deduplicated raw send stream.
28#
29# STRATEGY:
30# 1. Create a zvol with dedup and encryption on and put a filesystem on it
31# 2. Copy a file into the zvol a few times and take a snapshot
32# 3. Repeat step 2 a few times to create more snapshots
33# 4. Send all snapshots in a recursive, raw, deduplicated send stream
34# 5. Mount the received zvol and verify that all of the data there is correct
35#
36
37verify_runnable "both"
38
39function cleanup
40{
41	if is_linux; then
42		ismounted $recvmnt ext4 && log_must umount $recvmnt
43		ismounted $mntpnt ext4 && log_must umount $mntpnt
44	else
45		ismounted $recvmnt ufs && log_must umount $recvmnt
46		ismounted $mntpnt ufs && log_must umount $mntpnt
47	fi
48	[[ -d $recvmnt ]] && log_must rm -rf $keyfile
49	[[ -d $mntpnt ]] && log_must rm -rf $keyfile
50	destroy_dataset $TESTPOOL/recv "-r"
51	destroy_dataset $TESTPOOL/$TESTVOL "-r"
52	[[ -f $keyfile ]] && log_must rm $keyfile
53	[[ -f $sendfile ]] && log_must rm $sendfile
54}
55log_onexit cleanup
56
57log_assert "Verify zfs can receive raw, recursive, and deduplicated send streams"
58
59typeset keyfile=/$TESTPOOL/pkey
60typeset snap_count=5
61typeset zdev=$ZVOL_DEVDIR/$TESTPOOL/$TESTVOL
62typeset mntpnt=$TESTDIR/$TESTVOL
63typeset recvdev=$ZVOL_DEVDIR/$TESTPOOL/recv
64typeset recvmnt=$TESTDIR/recvmnt
65typeset sendfile=$TESTDIR/sendfile
66
67log_must eval "echo 'password' > $keyfile"
68
69log_must zfs create -o dedup=on -o encryption=on -o keyformat=passphrase \
70	-o keylocation=file://$keyfile -V 128M $TESTPOOL/$TESTVOL
71log_must block_device_wait
72
73if is_linux; then
74	log_must eval "echo 'y' | newfs -t ext4 -v $zdev"
75else
76	log_must eval "echo 'y' | newfs $zdev"
77fi
78log_must mkdir -p $mntpnt
79log_must mkdir -p $recvmnt
80log_must mount $zdev $mntpnt
81
82for ((i = 1; i <= $snap_count; i++)); do
83	log_must dd if=/dev/urandom of=$mntpnt/file bs=1M count=1
84	for ((j = 0; j < 10; j++)); do
85		log_must cp $mntpnt/file $mntpnt/file$j
86	done
87
88	log_must sync
89	log_must zfs snap $TESTPOOL/$TESTVOL@snap$i
90done
91
92log_must eval "zfs send -wDR $TESTPOOL/$TESTVOL@snap$snap_count > $sendfile"
93log_must eval "zfs recv $TESTPOOL/recv < $sendfile"
94log_must zfs load-key $TESTPOOL/recv
95log_must block_device_wait
96
97log_must mount $recvdev $recvmnt
98
99md5_1=$(cat $mntpnt/* | digest -a md5)
100md5_2=$(cat $recvmnt/* | digest -a md5)
101[[ "$md5_1" == "$md5_2" ]] || log_fail "md5 mismatch: $md5_1 != $md5_2"
102
103log_pass "zfs can receive raw, recursive, and deduplicated send streams"
104