1#! /usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28. $STF_SUITE/tests/zvol/zvol_common.kshlib
29
30##################################################################
31#
32# __stc_assertion_start
33#
34# ID: zvol_swap_003_pos
35#
36# DESCRIPTION:
37# Verify that a zvol device can be used as a swap device
38# through /etc/vfstab configuration.
39#
40# STRATEGY:
41# 1. Create a pool
42# 2. Create a zvol volume
43# 3. Save current swaps info and delete current swaps
44# 4. Modify /etc/vfstab to add entry for zvol as swap device
45# 5. Use /sbin/swapadd to zvol as swap device throuth /etc/vfstab
46# 6. Create a file under $TMPDIR
47# 7. Verify the file
48#
49# TESTABILITY: explicit
50#
51# TEST_AUTOMATION_LEVEL: automated
52#
53# CODING_STATUS: COMPLETED (2005-07-04)
54#
55# __stc_assertion_end
56#
57################################################################################
58
59verify_runnable "global"
60
61function cleanup
62{
63	if [[ -f $TMPDIR/$TESTFILE ]]; then
64		log_must $RM -rf $TMPDIR/$TESTFILE
65	fi
66
67	if [[ -f $TMP_VFSTAB_FILE ]]; then
68		log_must $RM -rf $TMP_VFSTAB_FILE
69	fi
70
71	if [[ -f $NEW_VFSTAB_FILE ]]; then
72		log_must $RM -f $NEW_VFSTAB_FILE
73	fi
74
75	if [[ -f $PREV_VFSTAB_FILE ]]; then
76		log_must $MV $PREV_VFSTAB_FILE $VFSTAB_FILE
77	fi
78
79	log_must $SWAPADD $VFSTAB_FILE
80
81        if is_swap_inuse $voldev ; then
82        	log_must $SWAP -d $voldev
83	fi
84
85}
86
87
88log_assert "Verify that a zvol device can be used as a swap device"\
89	  "through /etc/vfstab configuration."
90
91log_onexit cleanup
92
93test_requires SWAPADD
94
95voldev=/dev/zvol/$TESTPOOL/$TESTVOL
96VFSTAB_FILE=/etc/vfstab
97NEW_VFSTAB_FILE=$TMPDIR/zvol_vfstab.${TESTCASE_ID}
98PREV_VFSTAB_FILE=$TMPDIR/zvol_vfstab.PREV.${TESTCASE_ID}
99TMP_VFSTAB_FILE=$TMPDIR/zvol_vfstab.tmp.${TESTCASE_ID}
100
101if [[ -f $NEW_VFSTAB_FILE ]]; then
102	$RM -f $NEW_VFSTAB_FILE
103fi
104$TOUCH $NEW_VFSTAB_FILE
105$CHMOD 777 $NEW_VFSTAB_FILE
106
107#
108# Go through each line of /etc/vfstab and
109# exclude the comment line and formulate
110# a new file with an entry of zvol device
111# swap device.
112#
113
114$GREP -v "^#" $VFSTAB_FILE > $TMP_VFSTAB_FILE
115
116typeset -i fndswapline=0
117while read -r i
118do
119	line=`$ECHO "$i" | $AWK '{print $4}'`
120        if [[  $line == "swap" ]]; then
121                if [[ $fndswapline -eq 0 ]]; then
122                        $ECHO "$voldev"\
123                              "\t-\t-\tswap\t-"\
124                              "\tno\t-" \
125                                >> $NEW_VFSTAB_FILE
126                        fndswapline=1
127                        $ECHO  "Add an entry of zvol device as"\
128                                 "swap device in $VFSTAB_FILE."
129                fi
130        else
131                $ECHO "$i" >> $NEW_VFSTAB_FILE
132        fi
133
134done < $TMP_VFSTAB_FILE
135
136if [[ $fndswapline -eq 1 ]]; then
137	log_must $CP $VFSTAB_FILE $PREV_VFSTAB_FILE
138	log_must $CP $NEW_VFSTAB_FILE $VFSTAB_FILE
139else
140	log_fail  "The system has no swap device configuration in /etc/vfstab"
141fi
142
143log_note "Add zvol volume as swap space"
144log_must $SWAPADD $VFSTAB_FILE
145
146log_note "Create a file under $TMPDIR"
147log_must $FILE_WRITE -o create -f $TMPDIR/$TESTFILE \
148    -b $BLOCKSZ -c $NUM_WRITES -d $DATA
149
150[[ ! -f $TMPDIR/$TESTFILE ]] &&
151    log_fail "Unable to create file under $TMPDIR"
152
153filesize=`$LS -l $TMPDIR/$TESTFILE | $AWK '{print $5}'`
154tf_size=$(( BLOCKSZ * NUM_WRITES ))
155(( $tf_size != $filesize )) && \
156    log_fail "testfile is ($filesize bytes), expected ($tf_size bytes)"
157
158log_pass "Successfully added a zvol to swap area through /etc/vfstab."
159