1f38cb554SJohn Wren Kennedy#! /bin/ksh -p
2f38cb554SJohn Wren Kennedy#
3f38cb554SJohn Wren Kennedy# CDDL HEADER START
4f38cb554SJohn Wren Kennedy#
5f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
6f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
7f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
8f38cb554SJohn Wren Kennedy#
9f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
11f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
12f38cb554SJohn Wren Kennedy# and limitations under the License.
13f38cb554SJohn Wren Kennedy#
14f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
15f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
17f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
18f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
19f38cb554SJohn Wren Kennedy#
20f38cb554SJohn Wren Kennedy# CDDL HEADER END
21f38cb554SJohn Wren Kennedy#
22f38cb554SJohn Wren Kennedy
23f38cb554SJohn Wren Kennedy#
24f38cb554SJohn Wren Kennedy# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25f38cb554SJohn Wren Kennedy# Use is subject to license terms.
26f38cb554SJohn Wren Kennedy#
27f38cb554SJohn Wren Kennedy
28f38cb554SJohn Wren Kennedy#
291d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30*d3879c39SJason King# Copyright 2020 Joyent, Inc.
31f38cb554SJohn Wren Kennedy#
32f38cb554SJohn Wren Kennedy
33f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
34f38cb554SJohn Wren Kennedy. $STF_SUITE/tests/functional/zvol/zvol_common.shlib
35f38cb554SJohn Wren Kennedy. $STF_SUITE/tests/functional/zvol/zvol_swap/zvol_swap.cfg
36f38cb554SJohn Wren Kennedy
37f38cb554SJohn Wren Kennedy#
38f38cb554SJohn Wren Kennedy# DESCRIPTION:
39f38cb554SJohn Wren Kennedy# Verify that a zvol device can be used as a swap device
40f38cb554SJohn Wren Kennedy# through /etc/vfstab configuration.
41f38cb554SJohn Wren Kennedy#
42f38cb554SJohn Wren Kennedy# STRATEGY:
43f38cb554SJohn Wren Kennedy# 1. Modify /etc/vfstab to add the test zvol as swap device.
44f38cb554SJohn Wren Kennedy# 2. Use /sbin/swapadd to add zvol as swap device throuth /etc/vfstab
45f38cb554SJohn Wren Kennedy# 3. Create a file under /tmp and verify the file
46f38cb554SJohn Wren Kennedy#
47f38cb554SJohn Wren Kennedy
48f38cb554SJohn Wren Kennedyverify_runnable "global"
49f38cb554SJohn Wren Kennedy
50f38cb554SJohn Wren Kennedyfunction cleanup
51f38cb554SJohn Wren Kennedy{
521d32ba66SJohn Wren Kennedy	[[ -f /tmp/$TESTFILE ]] && log_must rm -f /tmp/$TESTFILE
531d32ba66SJohn Wren Kennedy	[[ -f $NEW_VFSTAB_FILE ]] && log_must rm -f $NEW_VFSTAB_FILE
54f38cb554SJohn Wren Kennedy	[[ -f $PREV_VFSTAB_FILE ]] && \
551d32ba66SJohn Wren Kennedy	    log_must mv $PREV_VFSTAB_FILE $VFSTAB_FILE
561d32ba66SJohn Wren Kennedy	[[ -f $PREV_VFSTAB_FILE ]] && rm -f $PREV_VFSTAB_FILE
57f38cb554SJohn Wren Kennedy
58f38cb554SJohn Wren Kennedy        if is_swap_inuse $voldev ; then
591d32ba66SJohn Wren Kennedy		log_must swap -d $voldev
60f38cb554SJohn Wren Kennedy	fi
61f38cb554SJohn Wren Kennedy
62f38cb554SJohn Wren Kennedy}
63f38cb554SJohn Wren Kennedy
64f38cb554SJohn Wren Kennedylog_assert "Verify that a zvol device can be used as a swap device" \
65f38cb554SJohn Wren Kennedy    "through /etc/vfstab configuration."
66f38cb554SJohn Wren Kennedy
67f38cb554SJohn Wren Kennedylog_onexit cleanup
68f38cb554SJohn Wren Kennedy
69f38cb554SJohn Wren Kennedyvoldev=/dev/zvol/dsk/$TESTPOOL/$TESTVOL
70f38cb554SJohn Wren KennedyVFSTAB_FILE=/etc/vfstab
71f38cb554SJohn Wren KennedyNEW_VFSTAB_FILE=/var/tmp/zvol_vfstab.$$
72f38cb554SJohn Wren KennedyPREV_VFSTAB_FILE=/var/tmp/zvol_vfstab.PREV.$$
73f38cb554SJohn Wren Kennedy
741d32ba66SJohn Wren Kennedy[[ -f $NEW_VFSTAB_FILE ]] && cp /dev/null $NEW_VFSTAB_FILE
75f38cb554SJohn Wren Kennedy
761d32ba66SJohn Wren Kennedyawk '{if ($4 != "swap") print $1}' /etc/vfstab > $NEW_VFSTAB_FILE
771d32ba66SJohn Wren Kennedyecho "$voldev\t-\t-\tswap\t-\tno\t-"  >> $NEW_VFSTAB_FILE
78f38cb554SJohn Wren Kennedy
79f38cb554SJohn Wren Kennedy# Copy off the original vfstab, and run swapadd on the newly constructed one.
801d32ba66SJohn Wren Kennedylog_must cp $VFSTAB_FILE $PREV_VFSTAB_FILE
811d32ba66SJohn Wren Kennedylog_must cp $NEW_VFSTAB_FILE $VFSTAB_FILE
821d32ba66SJohn Wren Kennedylog_must swapadd $VFSTAB_FILE
83f38cb554SJohn Wren Kennedy
841d32ba66SJohn Wren Kennedylog_must file_write -o create -f /tmp/$TESTFILE \
85f38cb554SJohn Wren Kennedy    -b $BLOCKSZ -c $NUM_WRITES -d $DATA
86f38cb554SJohn Wren Kennedy
87f38cb554SJohn Wren Kennedy[[ ! -f /tmp/$TESTFILE ]] &&
88f38cb554SJohn Wren Kennedy    log_fail "Unable to create file under /tmp"
89f38cb554SJohn Wren Kennedy
901d32ba66SJohn Wren Kennedyfilesize=`ls -l /tmp/$TESTFILE | awk '{print $5}'`
91f38cb554SJohn Wren Kennedytf_size=$((BLOCKSZ * NUM_WRITES))
92f38cb554SJohn Wren Kennedy(($tf_size != $filesize)) && \
93f38cb554SJohn Wren Kennedy    log_fail "testfile is ($filesize bytes), expected ($tf_size bytes)"
94f38cb554SJohn Wren Kennedy
95f38cb554SJohn Wren Kennedylog_pass "Successfully added a zvol to swap area through /etc/vfstab."
96