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# ident	"@(#)zvol_swap_003_pos.ksh	1.3	08/05/14 SMI"
28#
29
30. $STF_SUITE/include/libtest.kshlib
31. $STF_SUITE/tests/zvol/zvol_common.kshlib
32
33##################################################################
34#
35# __stc_assertion_start
36#
37# ID: zvol_swap_003_pos
38#
39# DESCRIPTION:
40# Verify that a zvol device can be used as a swap device
41# through /etc/vfstab configuration.
42#
43# STRATEGY:
44# 1. Create a pool
45# 2. Create a zvol volume
46# 3. Save current swaps info and delete current swaps
47# 4. Modify /etc/vfstab to add entry for zvol as swap device
48# 5. Use /sbin/swapadd to zvol as swap device throuth /etc/vfstab
49# 6. Create a file under $TMPDIR
50# 7. Verify the file
51#
52# TESTABILITY: explicit
53#
54# TEST_AUTOMATION_LEVEL: automated
55#
56# CODING_STATUS: COMPLETED (2005-07-04)
57#
58# __stc_assertion_end
59#
60################################################################################
61
62verify_runnable "global"
63
64function cleanup
65{
66	if [[ -f $TMPDIR/$TESTFILE ]]; then
67		log_must $RM -rf $TMPDIR/$TESTFILE
68	fi
69
70	if [[ -f $TMP_VFSTAB_FILE ]]; then
71		log_must $RM -rf $TMP_VFSTAB_FILE
72	fi
73
74	if [[ -f $NEW_VFSTAB_FILE ]]; then
75		log_must $RM -f $NEW_VFSTAB_FILE
76	fi
77
78	if [[ -f $PREV_VFSTAB_FILE ]]; then
79		log_must $MV $PREV_VFSTAB_FILE $VFSTAB_FILE
80	fi
81
82	log_must $SWAPADD $VFSTAB_FILE
83
84        if is_swap_inuse $voldev ; then
85        	log_must $SWAP -d $voldev
86	fi
87
88}
89
90
91log_assert "Verify that a zvol device can be used as a swap device"\
92	  "through /etc/vfstab configuration."
93
94log_onexit cleanup
95
96test_requires SWAPADD
97
98voldev=/dev/zvol/$TESTPOOL/$TESTVOL
99VFSTAB_FILE=/etc/vfstab
100NEW_VFSTAB_FILE=$TMPDIR/zvol_vfstab.${TESTCASE_ID}
101PREV_VFSTAB_FILE=$TMPDIR/zvol_vfstab.PREV.${TESTCASE_ID}
102TMP_VFSTAB_FILE=$TMPDIR/zvol_vfstab.tmp.${TESTCASE_ID}
103
104if [[ -f $NEW_VFSTAB_FILE ]]; then
105	$RM -f $NEW_VFSTAB_FILE
106fi
107$TOUCH $NEW_VFSTAB_FILE
108$CHMOD 777 $NEW_VFSTAB_FILE
109
110#
111# Go through each line of /etc/vfstab and
112# exclude the comment line and formulate
113# a new file with an entry of zvol device
114# swap device.
115#
116
117$GREP -v "^#" $VFSTAB_FILE > $TMP_VFSTAB_FILE
118
119typeset -i fndswapline=0
120while read -r i
121do
122	line=`$ECHO "$i" | $AWK '{print $4}'`
123        if [[  $line == "swap" ]]; then
124                if [[ $fndswapline -eq 0 ]]; then
125                        $ECHO "$voldev"\
126                              "\t-\t-\tswap\t-"\
127                              "\tno\t-" \
128                                >> $NEW_VFSTAB_FILE
129                        fndswapline=1
130                        $ECHO  "Add an entry of zvol device as"\
131                                 "swap device in $VFSTAB_FILE."
132                fi
133        else
134                $ECHO "$i" >> $NEW_VFSTAB_FILE
135        fi
136
137done < $TMP_VFSTAB_FILE
138
139if [[ $fndswapline -eq 1 ]]; then
140	log_must $CP $VFSTAB_FILE $PREV_VFSTAB_FILE
141	log_must $CP $NEW_VFSTAB_FILE $VFSTAB_FILE
142else
143	log_fail  "The system has no swap device configuration in /etc/vfstab"
144fi
145
146log_note "Add zvol volume as swap space"
147log_must $SWAPADD $VFSTAB_FILE
148
149log_note "Create a file under $TMPDIR"
150log_must $FILE_WRITE -o create -f $TMPDIR/$TESTFILE \
151    -b $BLOCKSZ -c $NUM_WRITES -d $DATA
152
153[[ ! -f $TMPDIR/$TESTFILE ]] &&
154    log_fail "Unable to create file under $TMPDIR"
155
156filesize=`$LS -l $TMPDIR/$TESTFILE | $AWK '{print $5}'`
157tf_size=$(( BLOCKSZ * NUM_WRITES ))
158(( $tf_size != $filesize )) && \
159    log_fail "testfile is ($filesize bytes), expected ($tf_size bytes)"
160
161log_pass "Successfully added a zvol to swap area through /etc/vfstab."
162