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